Merge "Fix unexpected Wi-Fi hotspot shutdown" into tm-dev

This commit is contained in:
TreeHugger Robot
2022-04-09 08:59:05 +00:00
committed by Android (Google) Code Review
3 changed files with 109 additions and 48 deletions

View File

@@ -72,7 +72,7 @@ public class WifiUtils extends com.android.settingslib.wifi.WifiUtils {
} }
} }
configBuilder.setPassphrase(password, securityType); configBuilder.setPassphrase(password, securityType);
} catch (IllegalArgumentException e) { } catch (Exception e) {
return false; return false;
} }
return true; return true;

View File

@@ -17,6 +17,11 @@
package com.android.settings.wifi.tether; package com.android.settings.wifi.tether;
import static android.net.ConnectivityManager.TETHERING_WIFI; import static android.net.ConnectivityManager.TETHERING_WIFI;
import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLING;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@@ -51,7 +56,7 @@ public class WifiTetherSwitchBarController implements
private final WifiManager mWifiManager; private final WifiManager mWifiManager;
@VisibleForTesting @VisibleForTesting
final DataSaverBackend mDataSaverBackend; DataSaverBackend mDataSaverBackend;
@VisibleForTesting @VisibleForTesting
final ConnectivityManager.OnStartTetheringCallback mOnStartTetheringCallback = final ConnectivityManager.OnStartTetheringCallback mOnStartTetheringCallback =
new ConnectivityManager.OnStartTetheringCallback() { new ConnectivityManager.OnStartTetheringCallback() {
@@ -75,7 +80,7 @@ public class WifiTetherSwitchBarController implements
mConnectivityManager = mConnectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mSwitchBar.setChecked(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED); mSwitchBar.setChecked(mWifiManager.getWifiApState() == WIFI_AP_STATE_ENABLED);
updateWifiSwitch(); updateWifiSwitch();
} }
@@ -95,6 +100,9 @@ public class WifiTetherSwitchBarController implements
@Override @Override
public void onSwitchChanged(Switch switchView, boolean isChecked) { public void onSwitchChanged(Switch switchView, boolean isChecked) {
// Filter out unnecessary callbacks when switch is disabled.
if (!switchView.isEnabled()) return;
if (isChecked) { if (isChecked) {
startTether(); startTether();
} else { } else {
@@ -118,39 +126,21 @@ public class WifiTetherSwitchBarController implements
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 (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) {
final int state = intent.getIntExtra( final int state = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_FAILED);
WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED);
handleWifiApStateChanged(state); handleWifiApStateChanged(state);
} }
} }
}; };
private void handleWifiApStateChanged(int state) { @VisibleForTesting
switch (state) { void handleWifiApStateChanged(int state) {
case WifiManager.WIFI_AP_STATE_ENABLING: if (state == WIFI_AP_STATE_ENABLING || state == WIFI_AP_STATE_DISABLING) return;
mSwitchBar.setEnabled(false);
break; final boolean shouldBeChecked = (state == WIFI_AP_STATE_ENABLED);
case WifiManager.WIFI_AP_STATE_ENABLED: if (mSwitch.isChecked() != shouldBeChecked) {
if (!mSwitch.isChecked()) { mSwitch.setChecked(shouldBeChecked);
mSwitch.setChecked(true);
}
updateWifiSwitch();
break;
case WifiManager.WIFI_AP_STATE_DISABLING:
if (mSwitch.isChecked()) {
mSwitch.setChecked(false);
}
mSwitchBar.setEnabled(false);
break;
case WifiManager.WIFI_AP_STATE_DISABLED:
mSwitch.setChecked(false);
updateWifiSwitch();
break;
default:
mSwitch.setChecked(false);
updateWifiSwitch();
break;
} }
updateWifiSwitch();
} }
private void updateWifiSwitch() { private void updateWifiSwitch() {

View File

@@ -16,66 +16,76 @@
package com.android.settings.wifi.tether; package com.android.settings.wifi.tether;
import static android.net.ConnectivityManager.TETHERING_WIFI; import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLING;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkPolicyManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.widget.Switch; import android.widget.Switch;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.datausage.DataSaverBackend;
import com.android.settings.widget.SettingsMainSwitchBar; import com.android.settings.widget.SettingsMainSwitchBar;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class WifiTetherSwitchBarControllerTest { public class WifiTetherSwitchBarControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Spy
Context mContext = ApplicationProvider.getApplicationContext();
@Mock @Mock
private WifiManager mWifiManager; private WifiManager mWifiManager;
@Mock @Mock
private ConnectivityManager mConnectivityManager; private ConnectivityManager mConnectivityManager;
@Mock @Mock
private NetworkPolicyManager mNetworkPolicyManager; private DataSaverBackend mDataSaverBackend;
@Mock @Mock
private Switch mSwitch; private Switch mSwitch;
private Context mContext;
private SettingsMainSwitchBar mSwitchBar; private SettingsMainSwitchBar mSwitchBar;
private WifiTetherSwitchBarController mController; private WifiTetherSwitchBarController mController;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mSwitchBar = new SettingsMainSwitchBar(mContext); mSwitchBar = new SettingsMainSwitchBar(mContext);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn( when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager); mConnectivityManager);
when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE)).thenReturn( when(mDataSaverBackend.isDataSaverEnabled()).thenReturn(false);
mNetworkPolicyManager); when(mSwitch.isEnabled()).thenReturn(true);
mController = new WifiTetherSwitchBarController(mContext, mSwitchBar); mController = new WifiTetherSwitchBarController(mContext, mSwitchBar);
mController.mDataSaverBackend = mDataSaverBackend;
} }
@Test @Test
public void startTether_fail_resetSwitchBar() { public void startTether_fail_resetSwitchBar() {
when(mNetworkPolicyManager.getRestrictBackground()).thenReturn(false); when(mDataSaverBackend.isDataSaverEnabled()).thenReturn(false);
mController.startTether(); mController.startTether();
mController.mOnStartTetheringCallback.onTetheringFailed(); mController.mOnStartTetheringCallback.onTetheringFailed();
@@ -89,23 +99,33 @@ public class WifiTetherSwitchBarControllerTest {
assertThat(mSwitchBar.isEnabled()).isTrue(); assertThat(mSwitchBar.isEnabled()).isTrue();
// try to turn data saver on // try to turn data saver on
when(mNetworkPolicyManager.getRestrictBackground()).thenReturn(true); when(mDataSaverBackend.isDataSaverEnabled()).thenReturn(true);
mController.onDataSaverChanged(true); mController.onDataSaverChanged(true);
assertThat(mSwitchBar.isEnabled()).isFalse(); assertThat(mSwitchBar.isEnabled()).isFalse();
// lets turn data saver off again // lets turn data saver off again
when(mNetworkPolicyManager.getRestrictBackground()).thenReturn(false); when(mDataSaverBackend.isDataSaverEnabled()).thenReturn(false);
mController.onDataSaverChanged(false); mController.onDataSaverChanged(false);
assertThat(mSwitchBar.isEnabled()).isTrue(); assertThat(mSwitchBar.isEnabled()).isTrue();
} }
@Test
public void onSwitchChanged_switchNotEnabled_doNothingForTethering() {
when(mSwitch.isEnabled()).thenReturn(false);
mController.onSwitchChanged(mSwitch, mSwitch.isChecked());
verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any());
verify(mConnectivityManager, never()).stopTethering(anyInt());
}
@Test @Test
public void onSwitchChanged_isChecked_startTethering() { public void onSwitchChanged_isChecked_startTethering() {
when(mSwitch.isChecked()).thenReturn(true); when(mSwitch.isChecked()).thenReturn(true);
mController.onSwitchChanged(mSwitch, mSwitch.isChecked()); mController.onSwitchChanged(mSwitch, mSwitch.isChecked());
verify(mConnectivityManager).startTethering(eq(TETHERING_WIFI), anyBoolean(), any(), any()); verify(mConnectivityManager).startTethering(anyInt(), anyBoolean(), any(), any());
} }
@Test @Test
@@ -114,6 +134,57 @@ public class WifiTetherSwitchBarControllerTest {
mController.onSwitchChanged(mSwitch, mSwitch.isChecked()); mController.onSwitchChanged(mSwitch, mSwitch.isChecked());
verify(mConnectivityManager).stopTethering(TETHERING_WIFI); verify(mConnectivityManager).stopTethering(anyInt());
}
@Test
public void handleWifiApStateChanged_stateIsEnabling_notEnabledSwitchBar() {
mSwitchBar.setEnabled(false);
mController.handleWifiApStateChanged(WIFI_AP_STATE_ENABLING);
assertThat(mSwitchBar.isEnabled()).isFalse();
}
@Test
public void handleWifiApStateChanged_stateIsDisabling_notEnabledSwitchBar() {
mSwitchBar.setEnabled(false);
mController.handleWifiApStateChanged(WIFI_AP_STATE_DISABLING);
assertThat(mSwitchBar.isEnabled()).isFalse();
}
@Test
public void handleWifiApStateChanged_stateIsEnabled_enabledAndCheckedSwitchBar() {
mSwitchBar.setEnabled(false);
mSwitchBar.setChecked(false);
mController.handleWifiApStateChanged(WIFI_AP_STATE_ENABLED);
assertThat(mSwitchBar.isEnabled()).isTrue();
assertThat(mSwitchBar.isChecked()).isTrue();
}
@Test
public void handleWifiApStateChanged_stateIsDisabled_enabledAndUncheckedSwitchBar() {
mSwitchBar.setEnabled(false);
mSwitchBar.setChecked(true);
mController.handleWifiApStateChanged(WIFI_AP_STATE_DISABLED);
assertThat(mSwitchBar.isEnabled()).isTrue();
assertThat(mSwitchBar.isChecked()).isFalse();
}
@Test
public void handleWifiApStateChanged_stateIsFailed_enabledAndUncheckedSwitchBar() {
mSwitchBar.setEnabled(false);
mSwitchBar.setChecked(true);
mController.handleWifiApStateChanged(WIFI_AP_STATE_FAILED);
assertThat(mSwitchBar.isEnabled()).isTrue();
assertThat(mSwitchBar.isChecked()).isFalse();
} }
} }