Put code that updates the access points lists inside a Handler.
When access point is forgot, we got callbacks from WifiManager to tell us if the forget action is successful or not. We will refresh the access point list in both cases. However, these callbacks happens on a different thread and updating preferences should only be done on the main thread. Creating a handler to handler all updates in the main thread. Change-Id: I7593befb20e46391ad69a284375693351a2cc794 Fixes: 65745404 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -22,6 +22,8 @@ import android.content.Context;
|
|||||||
import android.icu.text.Collator;
|
import android.icu.text.Collator;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -31,6 +33,7 @@ import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
|||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
import com.android.settings.search.Indexable;
|
import com.android.settings.search.Indexable;
|
||||||
|
import com.android.settings.wrapper.WifiManagerWrapper;
|
||||||
import com.android.settingslib.wifi.AccessPoint;
|
import com.android.settingslib.wifi.AccessPoint;
|
||||||
import com.android.settingslib.wifi.AccessPointPreference;
|
import com.android.settingslib.wifi.AccessPointPreference;
|
||||||
import com.android.settingslib.wifi.WifiSavedConfigUtils;
|
import com.android.settingslib.wifi.WifiSavedConfigUtils;
|
||||||
@@ -46,6 +49,8 @@ import java.util.List;
|
|||||||
public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
|
public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
|
||||||
implements Indexable, WifiDialog.WifiDialogListener {
|
implements Indexable, WifiDialog.WifiDialogListener {
|
||||||
private static final String TAG = "SavedAccessPoints";
|
private static final String TAG = "SavedAccessPoints";
|
||||||
|
@VisibleForTesting
|
||||||
|
static final int MSG_UPDATE_PREFERENCES = 1;
|
||||||
private static final Comparator<AccessPoint> SAVED_NETWORK_COMPARATOR =
|
private static final Comparator<AccessPoint> SAVED_NETWORK_COMPARATOR =
|
||||||
new Comparator<AccessPoint>() {
|
new Comparator<AccessPoint>() {
|
||||||
final Collator mCollator = Collator.getInstance();
|
final Collator mCollator = Collator.getInstance();
|
||||||
@@ -60,20 +65,31 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private final WifiManager.ActionListener mForgetListener = new WifiManager.ActionListener() {
|
@VisibleForTesting
|
||||||
|
final WifiManager.ActionListener mForgetListener = new WifiManager.ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess() {
|
public void onSuccess() {
|
||||||
initPreferences();
|
postUpdatePreference();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(int reason) {
|
public void onFailure(int reason) {
|
||||||
initPreferences();
|
postUpdatePreference();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
final Handler mHandler = new Handler() {
|
||||||
|
@Override
|
||||||
|
public void handleMessage(android.os.Message msg) {
|
||||||
|
if (msg.what == MSG_UPDATE_PREFERENCES) {
|
||||||
|
initPreferences();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private WifiDialog mDialog;
|
private WifiDialog mDialog;
|
||||||
private WifiManager mWifiManager;
|
private WifiManagerWrapper mWifiManager;
|
||||||
private AccessPoint mDlgAccessPoint;
|
private AccessPoint mDlgAccessPoint;
|
||||||
private Bundle mAccessPointSavedState;
|
private Bundle mAccessPointSavedState;
|
||||||
private AccessPoint mSelectedAccessPoint;
|
private AccessPoint mSelectedAccessPoint;
|
||||||
@@ -105,7 +121,7 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
|
mWifiManager = new WifiManagerWrapper((WifiManager) getSystemService(Context.WIFI_SERVICE));
|
||||||
|
|
||||||
if (savedInstanceState != null) {
|
if (savedInstanceState != null) {
|
||||||
if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
|
if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
|
||||||
@@ -116,12 +132,11 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initPreferences() {
|
private void initPreferences() {
|
||||||
Log.d(TAG, "Rebuilding the preferences");
|
|
||||||
PreferenceScreen preferenceScreen = getPreferenceScreen();
|
PreferenceScreen preferenceScreen = getPreferenceScreen();
|
||||||
final Context context = getPrefContext();
|
final Context context = getPrefContext();
|
||||||
|
|
||||||
final List<AccessPoint> accessPoints =
|
final List<AccessPoint> accessPoints =
|
||||||
WifiSavedConfigUtils.getAllConfigs(context, mWifiManager);
|
WifiSavedConfigUtils.getAllConfigs(context, mWifiManager.getWifiManager());
|
||||||
Collections.sort(accessPoints, SAVED_NETWORK_COMPARATOR);
|
Collections.sort(accessPoints, SAVED_NETWORK_COMPARATOR);
|
||||||
cacheRemoveAllPrefs(preferenceScreen);
|
cacheRemoveAllPrefs(preferenceScreen);
|
||||||
|
|
||||||
@@ -156,6 +171,12 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void postUpdatePreference() {
|
||||||
|
if (!mHandler.hasMessages(MSG_UPDATE_PREFERENCES)) {
|
||||||
|
mHandler.sendEmptyMessage(MSG_UPDATE_PREFERENCES);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void showWifiDialog(@Nullable LongPressAccessPointPreference accessPoint) {
|
private void showWifiDialog(@Nullable LongPressAccessPointPreference accessPoint) {
|
||||||
if (mDialog != null) {
|
if (mDialog != null) {
|
||||||
removeDialog(WifiSettings.WIFI_DIALOG_ID);
|
removeDialog(WifiSettings.WIFI_DIALOG_ID);
|
||||||
@@ -235,7 +256,7 @@ public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment
|
|||||||
Log.e(TAG, "Failed to remove Passpoint configuration for "
|
Log.e(TAG, "Failed to remove Passpoint configuration for "
|
||||||
+ mSelectedAccessPoint.getConfigName());
|
+ mSelectedAccessPoint.getConfigName());
|
||||||
}
|
}
|
||||||
initPreferences();
|
postUpdatePreference();
|
||||||
} else {
|
} else {
|
||||||
// mForgetListener will call initPreferences upon completion
|
// mForgetListener will call initPreferences upon completion
|
||||||
mWifiManager.forget(mSelectedAccessPoint.getConfig().networkId, mForgetListener);
|
mWifiManager.forget(mSelectedAccessPoint.getConfig().networkId, mForgetListener);
|
||||||
|
@@ -14,10 +14,32 @@ public class WifiManagerWrapper {
|
|||||||
mWifiManager = wifiManager;
|
mWifiManager = wifiManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the real WifiManager
|
||||||
|
* @return the real WifiManager
|
||||||
|
*/
|
||||||
|
public WifiManager getWifiManager() {
|
||||||
|
return mWifiManager;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link WifiManager#getCurrentNetworkWpsNfcConfigurationToken}
|
* {@link WifiManager#getCurrentNetworkWpsNfcConfigurationToken}
|
||||||
*/
|
*/
|
||||||
public String getCurrentNetworkWpsNfcConfigurationToken() {
|
public String getCurrentNetworkWpsNfcConfigurationToken() {
|
||||||
return mWifiManager.getCurrentNetworkWpsNfcConfigurationToken();
|
return mWifiManager.getCurrentNetworkWpsNfcConfigurationToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link WifiManager#removePasspointConfiguration}
|
||||||
|
*/
|
||||||
|
public void removePasspointConfiguration(String fqdn) {
|
||||||
|
mWifiManager.removePasspointConfiguration(fqdn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link WifiManager#removePasspointConfiguration}
|
||||||
|
*/
|
||||||
|
public void forget(int netId, WifiManager.ActionListener listener) {
|
||||||
|
mWifiManager.forget(netId, listener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.wifi;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.wrapper.WifiManagerWrapper;
|
||||||
|
import com.android.settingslib.wifi.AccessPoint;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class SavedAccessPointsWifiSettingsTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Handler mHandler;
|
||||||
|
|
||||||
|
private SavedAccessPointsWifiSettings mSettings;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mSettings = new SavedAccessPointsWifiSettings();
|
||||||
|
ReflectionHelpers.setField(mSettings, "mHandler", mHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onForget_isPasspointConfig_shouldSendMessageToHandler() {
|
||||||
|
final AccessPoint accessPoint = mock(AccessPoint.class);
|
||||||
|
when(accessPoint.isPasspointConfig()).thenReturn(true);
|
||||||
|
ReflectionHelpers.setField(mSettings, "mSelectedAccessPoint", accessPoint);
|
||||||
|
ReflectionHelpers.setField(mSettings, "mWifiManager", mock(WifiManagerWrapper.class));
|
||||||
|
|
||||||
|
mSettings.onForget(null);
|
||||||
|
|
||||||
|
verify(mHandler).sendEmptyMessage(mSettings.MSG_UPDATE_PREFERENCES);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onForget_onSuccess_shouldSendMessageToHandler() {
|
||||||
|
mSettings.mForgetListener.onSuccess();
|
||||||
|
|
||||||
|
verify(mHandler).sendEmptyMessage(mSettings.MSG_UPDATE_PREFERENCES);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onForget_onFailure_shouldSendMessageToHandler() {
|
||||||
|
mSettings.mForgetListener.onFailure(0);
|
||||||
|
|
||||||
|
verify(mHandler).sendEmptyMessage(mSettings.MSG_UPDATE_PREFERENCES);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user