Refresh Wifi AP Config display when config changes

Change-Id: I6fe284355223cbcedd82f95d6415c4d6b660f39f
Fixes: 64757839
Test: robotests
Test: rerun ACTS WifiTetheringTest:test_change_wifi_hotspot_ssid_when_hotspot_enabled
This commit is contained in:
Fan Zhang
2017-09-01 11:10:37 -07:00
parent fe18f8e876
commit 5a8b70d3da
8 changed files with 102 additions and 30 deletions

View File

@@ -23,12 +23,13 @@ import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.util.Log;
import com.android.settings.R;
public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferenceController {
private static final String TAG = "WifiTetherApBandPref";
private static final String PREF_KEY = "wifi_tether_network_ap_band";
private static final String[] BAND_VALUES =
{String.valueOf(AP_BAND_2GHZ), String.valueOf(AP_BAND_5GHZ)};
@@ -40,21 +41,23 @@ public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferen
OnTetherConfigUpdateListener listener) {
super(context, listener);
mBandEntries = mContext.getResources().getStringArray(R.array.wifi_ap_band_config_full);
}
@Override
public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
if (config == null) {
mBandIndex = 0;
Log.d(TAG, "Updating band index to 0 because no config");
} else if (is5GhzBandSupported()) {
mBandIndex = config.apBand;
Log.d(TAG, "Updating band index to " + mBandIndex);
} else {
config.apBand = 0;
mWifiManager.setWifiApConfiguration(config);
mBandIndex = config.apBand;
Log.d(TAG, "5Ghz not supported, updating band index to " + mBandIndex);
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
ListPreference preference = (ListPreference) mPreference;
if (!is5GhzBandSupported()) {
preference.setEnabled(false);

View File

@@ -57,5 +57,8 @@ public abstract class WifiTetherBasePreferenceController extends AbstractPrefere
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
updateDisplay();
}
public abstract void updateDisplay();
}

View File

@@ -20,7 +20,7 @@ import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.util.Log;
import com.android.settings.widget.ValidatedEditTextPreference;
import com.android.settings.wifi.WifiUtils;
@@ -28,6 +28,7 @@ import com.android.settings.wifi.WifiUtils;
public class WifiTetherPasswordPreferenceController extends WifiTetherBasePreferenceController
implements ValidatedEditTextPreference.Validator {
private static final String TAG = "WifiTetherPswdPref";
private static final String PREF_KEY = "wifi_tether_network_password";
private String mPassword;
@@ -35,10 +36,6 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
public WifiTetherPasswordPreferenceController(Context context,
OnTetherConfigUpdateListener listener) {
super(context, listener);
final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
if (config != null) {
mPassword = config.preSharedKey;
}
}
@Override
@@ -47,8 +44,12 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
if (config != null) {
mPassword = config.preSharedKey;
Log.d(TAG, "Updating password in Preference, " + mPassword);
}
((ValidatedEditTextPreference) mPreference).setValidator(this);
updatePasswordDisplay((EditTextPreference) mPreference);
}

View File

@@ -21,7 +21,7 @@ import android.net.wifi.WifiConfiguration;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.util.Log;
import com.android.settings.widget.ValidatedEditTextPreference;
import com.android.settings.wifi.WifiUtils;
@@ -29,6 +29,7 @@ import com.android.settings.wifi.WifiUtils;
public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreferenceController
implements ValidatedEditTextPreference.Validator {
private static final String TAG = "WifiTetherSsidPref";
private static final String PREF_KEY = "wifi_tether_network_name";
@VisibleForTesting
static final String DEFAULT_SSID = "AndroidAP";
@@ -46,13 +47,14 @@ public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreference
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
if (config != null) {
mSSID = config.SSID;
Log.d(TAG, "Updating SSID in Preference, " + mSSID);
} else {
mSSID = DEFAULT_SSID;
Log.d(TAG, "Updating to default SSID in Preference, " + mSSID);
}
((ValidatedEditTextPreference) mPreference).setValidator(this);
updateSsidDisplay((EditTextPreference) mPreference);

View File

@@ -44,6 +44,7 @@ import java.util.List;
public class WifiTetherSettings extends RestrictedDashboardFragment
implements WifiTetherBasePreferenceController.OnTetherConfigUpdateListener {
private static final String TAG = "WifiTetherSettings";
private static final IntentFilter TETHER_STATE_CHANGE_FILTER;
private WifiTetherSwitchBarController mSwitchBarController;
@@ -161,27 +162,37 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
return config;
}
private void startTether() {
mRestartWifiApAfterConfigChange = false;
mSwitchBarController.startTether();
}
private void updateDisplayWithNewConfig() {
getPreferenceController(WifiTetherSSIDPreferenceController.class)
.updateDisplay();
getPreferenceController(WifiTetherPasswordPreferenceController.class)
.updateDisplay();
getPreferenceController(WifiTetherApBandPreferenceController.class)
.updateDisplay();
}
@VisibleForTesting
class TetherChangeReceiver extends BroadcastReceiver {
private static final String TAG = "TetherChangeReceiver";
@Override
public void onReceive(Context content, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "updating display config due to receiving broadcast action " + action);
updateDisplayWithNewConfig();
if (action.equals(ACTION_TETHER_STATE_CHANGED)) {
if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED
&& mRestartWifiApAfterConfigChange) {
mRestartWifiApAfterConfigChange = false;
Log.d(TAG, "Restarting WifiAp due to prior config change.");
mSwitchBarController.startTether();
startTether();
}
} else if (action.equals(WIFI_AP_STATE_CHANGED_ACTION)) {
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_STATE, 0);
if (state == WifiManager.WIFI_AP_STATE_DISABLED
&& mRestartWifiApAfterConfigChange) {
mRestartWifiApAfterConfigChange = false;
Log.d(TAG, "Restarting WifiAp due to prior config change.");
mSwitchBarController.startTether();
startTether();
}
}
}

View File

@@ -121,4 +121,20 @@ public class WifiTetherApBandPreferenceControllerTest {
verify(mListener, times(2)).onTetherConfigUpdated();
}
@Test
public void updateDisplay_shouldUpdateValue() {
// Set controller band index to 1 and verify is set.
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mListPreference, "1");
assertThat(mController.getBandIndex()).isEqualTo(1);
// Disable 5Ghz band
when(mWifiManager.is5GHzBandSupported()).thenReturn(false);
// Call updateDisplay and verify it's changed.
mController.updateDisplay();
assertThat(mController.getBandIndex()).isEqualTo(0);
}
}

View File

@@ -99,4 +99,22 @@ public class WifiTetherPasswordPreferenceControllerTest {
verify(mListener, times(2)).onTetherConfigUpdated();
}
@Test
public void updateDisplay_shouldUpdateValue() {
// Set controller password to anything and verify is set.
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, "1");
assertThat(mController.getPassword()).isEqualTo("1");
// Create a new config using different password
final WifiConfiguration config = new WifiConfiguration();
config.preSharedKey = "test_1234";
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
// Call updateDisplay and verify it's changed.
mController.updateDisplay();
assertThat(mController.getPassword()).isEqualTo(config.preSharedKey);
assertThat(mPreference.getSummary()).isEqualTo(config.preSharedKey);
}
}

View File

@@ -16,6 +16,12 @@
package com.android.settings.wifi.tether;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
@@ -35,12 +41,6 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WifiTetherSSIDPreferenceControllerTest {
@@ -104,4 +104,22 @@ public class WifiTetherSSIDPreferenceControllerTest {
verify(mListener, times(2)).onTetherConfigUpdated();
}
@Test
public void updateDisplay_shouldUpdateValue() {
// Set controller ssid to anything and verify is set.
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, "1");
assertThat(mController.getSSID()).isEqualTo("1");
// Create a new config using different SSID
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "test_1234";
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
// Call updateDisplay and verify it's changed.
mController.updateDisplay();
assertThat(mController.getSSID()).isEqualTo(config.SSID);
assertThat(mPreference.getSummary()).isEqualTo(config.SSID);
}
}