Clean up WifiTetherPreferenceController

In previous code there are two main issues:
1. It listens to update from both WIFI_AP_STATE_CHANGED_ACTION and
ACTION_TETHER_STATE_CHANGED. It is unnecessary because they provides
same info(whether wifi hotspot is enabled, enabling...)

2. New API softApCallback already covers the
WIFI_AP_STATE_CHANGED_ACTION, so we don't need this broadcast anymore.

This cl fixes those two issues by cleaning up BroadcastReceiver and
update the tests.

Bug: 72702183
Test: RunSettingsRoboTests
Change-Id: I21c2818e0f0185172f34447a1716dc47ee065e23
This commit is contained in:
jackqdyulei
2018-01-31 14:26:42 -08:00
parent bf055d8095
commit dbaea5af63
3 changed files with 51 additions and 127 deletions

View File

@@ -39,13 +39,12 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop; import com.android.settingslib.core.lifecycle.events.OnStop;
import java.util.List;
public class WifiTetherPreferenceController extends AbstractPreferenceController public class WifiTetherPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop { implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {
public static final IntentFilter WIFI_TETHER_INTENT_FILTER;
private static final String WIFI_TETHER_SETTINGS = "wifi_tether"; private static final String WIFI_TETHER_SETTINGS = "wifi_tether";
private static final IntentFilter AIRPLANE_INTENT_FILTER = new IntentFilter(
Intent.ACTION_AIRPLANE_MODE_CHANGED);
private final ConnectivityManager mConnectivityManager; private final ConnectivityManager mConnectivityManager;
private final String[] mWifiRegexs; private final String[] mWifiRegexs;
@@ -58,12 +57,6 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
@VisibleForTesting @VisibleForTesting
WifiTetherSoftApManager mWifiTetherSoftApManager; WifiTetherSoftApManager mWifiTetherSoftApManager;
static {
WIFI_TETHER_INTENT_FILTER = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
WIFI_TETHER_INTENT_FILTER.addAction(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
WIFI_TETHER_INTENT_FILTER.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
}
public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) { public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) {
this(context, lifecycle, true /* initSoftApManager */); this(context, lifecycle, true /* initSoftApManager */);
} }
@@ -113,7 +106,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
@Override @Override
public void onStart() { public void onStart() {
if (mPreference != null) { if (mPreference != null) {
mContext.registerReceiver(mReceiver, WIFI_TETHER_INTENT_FILTER); mContext.registerReceiver(mReceiver, AIRPLANE_INTENT_FILTER);
clearSummaryForAirplaneMode(); clearSummaryForAirplaneMode();
if (mWifiTetherSoftApManager != null) { if (mWifiTetherSoftApManager != null) {
mWifiTetherSoftApManager.registerSoftApCallback(); mWifiTetherSoftApManager.registerSoftApCallback();
@@ -140,6 +133,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
@Override @Override
public void onStateChanged(int state, int failureReason) { public void onStateChanged(int state, int failureReason) {
mSoftApState = state; mSoftApState = state;
handleWifiApStateChanged(state, failureReason);
} }
@Override @Override
@@ -162,34 +156,21 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) { if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
int state = intent.getIntExtra(
WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED);
int reason = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_FAILURE_REASON,
WifiManager.SAP_START_FAILURE_GENERAL);
handleWifiApStateChanged(state, reason);
} else if (ConnectivityManager.ACTION_TETHER_STATE_CHANGED.equals(action)) {
List<String> active = intent.getStringArrayListExtra(
ConnectivityManager.EXTRA_ACTIVE_TETHER);
List<String> errored = intent.getStringArrayListExtra(
ConnectivityManager.EXTRA_ERRORED_TETHER);
updateTetherState(active.toArray(), errored.toArray());
} else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
clearSummaryForAirplaneMode(); clearSummaryForAirplaneMode();
} }
} }
}; };
private void handleWifiApStateChanged(int state, int reason) { @VisibleForTesting
void handleWifiApStateChanged(int state, int reason) {
switch (state) { switch (state) {
case WifiManager.WIFI_AP_STATE_ENABLING: case WifiManager.WIFI_AP_STATE_ENABLING:
mPreference.setSummary(R.string.wifi_tether_starting); mPreference.setSummary(R.string.wifi_tether_starting);
break; break;
case WifiManager.WIFI_AP_STATE_ENABLED: case WifiManager.WIFI_AP_STATE_ENABLED:
/** WifiConfiguration wifiConfig = mWifiManager.getWifiApConfiguration();
* Summary on enable is handled by tether updateConfigSummary(wifiConfig);
* broadcast notice
*/
break; break;
case WifiManager.WIFI_AP_STATE_DISABLING: case WifiManager.WIFI_AP_STATE_DISABLING:
mPreference.setSummary(R.string.wifi_tether_stopping); mPreference.setSummary(R.string.wifi_tether_stopping);
@@ -208,32 +189,6 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
} }
} }
private void updateTetherState(Object[] tethered, Object[] errored) {
boolean wifiTethered = matchRegex(tethered);
boolean wifiErrored = matchRegex(errored);
if (wifiTethered) {
WifiConfiguration wifiConfig = mWifiManager.getWifiApConfiguration();
updateConfigSummary(wifiConfig);
} else if (wifiErrored) {
mPreference.setSummary(R.string.wifi_error);
} else {
mPreference.setSummary(R.string.wifi_hotspot_off_subtext);
}
}
private boolean matchRegex(Object[] tethers) {
for (Object o : tethers) {
String s = (String) o;
for (String regex : mWifiRegexs) {
if (s.matches(regex)) {
return true;
}
}
}
return false;
}
private void updateConfigSummary(WifiConfiguration wifiConfig) { private void updateConfigSummary(WifiConfiguration wifiConfig) {
final String s = mContext.getString( final String s = mContext.getString(
com.android.internal.R.string.wifi_tether_configure_ssid_default); com.android.internal.R.string.wifi_tether_configure_ssid_default);

View File

@@ -21,6 +21,7 @@ import static android.net.ConnectivityManager.TETHERING_WIFI;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Handler; import android.os.Handler;
@@ -36,12 +37,19 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
public class WifiTetherSwitchBarController implements SwitchWidgetController.OnSwitchChangeListener, public class WifiTetherSwitchBarController implements SwitchWidgetController.OnSwitchChangeListener,
LifecycleObserver, OnStart, OnStop { LifecycleObserver, OnStart, OnStop {
private static final IntentFilter WIFI_INTENT_FILTER;
private final Context mContext; private final Context mContext;
private final SwitchWidgetController mSwitchBar; private final SwitchWidgetController mSwitchBar;
private final ConnectivityManager mConnectivityManager; private final ConnectivityManager mConnectivityManager;
private final DataSaverBackend mDataSaverBackend; private final DataSaverBackend mDataSaverBackend;
private final WifiManager mWifiManager; private final WifiManager mWifiManager;
static {
WIFI_INTENT_FILTER = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
WIFI_INTENT_FILTER.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
}
WifiTetherSwitchBarController(Context context, SwitchWidgetController switchBar) { WifiTetherSwitchBarController(Context context, SwitchWidgetController switchBar) {
mContext = context; mContext = context;
mSwitchBar = switchBar; mSwitchBar = switchBar;
@@ -56,8 +64,7 @@ public class WifiTetherSwitchBarController implements SwitchWidgetController.OnS
@Override @Override
public void onStart() { public void onStart() {
mSwitchBar.startListening(); mSwitchBar.startListening();
mContext.registerReceiver(mReceiver, mContext.registerReceiver(mReceiver, WIFI_INTENT_FILTER);
WifiTetherPreferenceController.WIFI_TETHER_INTENT_FILTER);
} }
@Override @Override

View File

@@ -73,7 +73,8 @@ import java.util.ArrayList;
}) })
public class WifiTetherPreferenceControllerTest { public class WifiTetherPreferenceControllerTest {
@Mock private static final String SSID = "Pixel";
private Context mContext; private Context mContext;
@Mock @Mock
private ConnectivityManager mConnectivityManager; private ConnectivityManager mConnectivityManager;
@@ -81,6 +82,8 @@ public class WifiTetherPreferenceControllerTest {
private WifiManager mWifiManager; private WifiManager mWifiManager;
@Mock @Mock
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
@Mock
private WifiConfiguration mWifiConfiguration;
private WifiTetherPreferenceController mController; private WifiTetherPreferenceController mController;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
@@ -90,6 +93,8 @@ public class WifiTetherPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle; mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner); mLifecycle = new Lifecycle(mLifecycleOwner);
FakeFeatureFactory.setupForTest(); FakeFeatureFactory.setupForTest();
@@ -98,10 +103,13 @@ public class WifiTetherPreferenceControllerTest {
.thenReturn(mConnectivityManager); .thenReturn(mConnectivityManager);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mScreen.findPreference(anyString())).thenReturn(mPreference); when(mScreen.findPreference(anyString())).thenReturn(mPreference);
when(mWifiManager.getWifiApConfiguration()).thenReturn(mWifiConfiguration);
mWifiConfiguration.SSID = SSID;
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"}); when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mController = new WifiTetherPreferenceController(mContext, mLifecycle,
false /* initSoftApManager */); false /* initSoftApManager */);
mController.displayPreference(mScreen);
} }
@After @After
@@ -127,7 +135,6 @@ public class WifiTetherPreferenceControllerTest {
public void startAndStop_shouldRegisterUnregisterReceiver() { public void startAndStop_shouldRegisterUnregisterReceiver() {
final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver"); final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver");
mController.displayPreference(mScreen);
mLifecycle.handleLifecycleEvent(ON_START); mLifecycle.handleLifecycleEvent(ON_START);
mLifecycle.handleLifecycleEvent(ON_STOP); mLifecycle.handleLifecycleEvent(ON_STOP);
@@ -166,48 +173,6 @@ public class WifiTetherPreferenceControllerTest {
verify(pref).setChecked(true); verify(pref).setChecked(true);
} }
@Test
public void testReceiver_apStateChangedToDisabled_shouldUpdatePreferenceSummary() {
mController.displayPreference(mScreen);
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLED);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
}
@Test
public void testReceiver_apStateChangedToDisabling_shouldUpdatePreferenceSummary() {
mController.displayPreference(mScreen);
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLING);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_tether_stopping));
}
@Test
public void testReceiver_apStateChangedToEnabling_shouldUpdatePreferenceSummary() {
mController.displayPreference(mScreen);
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_ENABLING);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_tether_starting));
}
@Test
public void testReceiver_apStateChangedToEnabled_shouldNotUpdatePreferenceSummary() {
mController.displayPreference(mScreen);
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLED);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
// When turning on the hotspot, we receive STATE_ENABLING followed by STATE_ENABLED. The
// first should change the status to wifi_tether_starting, and the second should not change
// this.
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_ENABLING);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_tether_starting));
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_ENABLED);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_tether_starting));
}
@Test @Test
public void testReceiver_goingToAirplaneMode_shouldClearPreferenceSummary() { public void testReceiver_goingToAirplaneMode_shouldClearPreferenceSummary() {
final ContentResolver cr = mock(ContentResolver.class); final ContentResolver cr = mock(ContentResolver.class);
@@ -224,22 +189,32 @@ public class WifiTetherPreferenceControllerTest {
} }
@Test @Test
public void testReceiver_tetherEnabled_shouldUpdatePreferenceSummary() { public void testHandleWifiApStateChanged_stateEnabling_showEnablingSummary() {
mController.displayPreference(mScreen); mController.handleWifiApStateChanged(WifiManager.WIFI_AP_STATE_ENABLING, 0 /* reason */);
final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver");
final Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
final ArrayList<String> activeTethers = new ArrayList<>();
activeTethers.add("1");
broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeTethers);
broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
new ArrayList<>());
final WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = "test-ap";
when(mWifiManager.getWifiApConfiguration()).thenReturn(configuration);
receiver.onReceive(RuntimeEnvironment.application, broadcast); assertThat(mPreference.getSummary()).isEqualTo("Turning hotspot on\u2026");
}
verify(mContext).getString(eq(R.string.wifi_tether_enabled_subtext), any()); @Test
public void testHandleWifiApStateChanged_stateEnabled_showEnabledSummary() {
mController.handleWifiApStateChanged(WifiManager.WIFI_AP_STATE_ENABLED, 0 /* reason */);
assertThat(mPreference.getSummary()).isEqualTo("Pixel is active");
}
@Test
public void testHandleWifiApStateChanged_stateDisabling_showDisablingSummary() {
mController.handleWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLING, 0 /* reason */);
assertThat(mPreference.getSummary()).isEqualTo("Turning off hotspot\u2026");
}
@Test
public void testHandleWifiApStateChanged_stateDisabled_showDisabledSummary() {
mController.handleWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED, 0 /* reason */);
assertThat(mPreference.getSummary()).isEqualTo(
"Not sharing internet or content with other devices");
} }
@Implements(WifiTetherSettings.class) @Implements(WifiTetherSettings.class)
@@ -285,17 +260,4 @@ public class WifiTetherPreferenceControllerTest {
onStopCalled = true; onStopCalled = true;
} }
} }
/**
* Helper to cause the controller to receive a WIFI_AP_STATE_CHANGED_ACTION with a specific
* state.
*
* @param state - the state, as specified by one of the WifiManager.WIFI_AP_STATE_* values
*/
private void receiveApStateChangedBroadcast(int state) {
final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver");
final Intent broadcast = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
broadcast.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, state);
receiver.onReceive(RuntimeEnvironment.application, broadcast);
}
} }