1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.wifi.tether; 18 19 import android.content.Context; 20 import android.net.wifi.WifiConfiguration; 21 import android.support.annotation.VisibleForTesting; 22 import android.support.v7.preference.EditTextPreference; 23 import android.support.v7.preference.Preference; 24 import android.util.Log; 25 26 import com.android.settings.widget.ValidatedEditTextPreference; 27 import com.android.settings.wifi.WifiUtils; 28 29 public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreferenceController 30 implements ValidatedEditTextPreference.Validator { 31 32 private static final String TAG = "WifiTetherSsidPref"; 33 private static final String PREF_KEY = "wifi_tether_network_name"; 34 @VisibleForTesting 35 static final String DEFAULT_SSID = "AndroidAP"; 36 37 private String mSSID; 38 private WifiDeviceNameTextValidator mWifiDeviceNameTextValidator; 39 WifiTetherSSIDPreferenceController(Context context, OnTetherConfigUpdateListener listener)40 public WifiTetherSSIDPreferenceController(Context context, 41 OnTetherConfigUpdateListener listener) { 42 super(context, listener); 43 mWifiDeviceNameTextValidator = new WifiDeviceNameTextValidator(); 44 } 45 46 @Override getPreferenceKey()47 public String getPreferenceKey() { 48 return PREF_KEY; 49 } 50 51 @Override updateDisplay()52 public void updateDisplay() { 53 final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); 54 if (config != null) { 55 mSSID = config.SSID; 56 Log.d(TAG, "Updating SSID in Preference, " + mSSID); 57 } else { 58 mSSID = DEFAULT_SSID; 59 Log.d(TAG, "Updating to default SSID in Preference, " + mSSID); 60 } 61 ((ValidatedEditTextPreference) mPreference).setValidator(this); 62 updateSsidDisplay((EditTextPreference) mPreference); 63 } 64 65 @Override onPreferenceChange(Preference preference, Object newValue)66 public boolean onPreferenceChange(Preference preference, Object newValue) { 67 mSSID = (String) newValue; 68 updateSsidDisplay((EditTextPreference) preference); 69 mListener.onTetherConfigUpdated(); 70 return true; 71 } 72 73 @Override isTextValid(String value)74 public boolean isTextValid(String value) { 75 return mWifiDeviceNameTextValidator.isTextValid(value); 76 } 77 getSSID()78 public String getSSID() { 79 return mSSID; 80 } 81 updateSsidDisplay(EditTextPreference preference)82 private void updateSsidDisplay(EditTextPreference preference) { 83 preference.setText(mSSID); 84 preference.setSummary(mSSID); 85 } 86 } 87