diff options
-rw-r--r-- | service/java/com/android/server/wifi/WifiStateMachine.java | 30 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java | 69 |
2 files changed, 95 insertions, 4 deletions
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java index b247083fb..eec0996c1 100644 --- a/service/java/com/android/server/wifi/WifiStateMachine.java +++ b/service/java/com/android/server/wifi/WifiStateMachine.java @@ -769,6 +769,14 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss private static final int SUSPEND_DUE_TO_HIGH_PERF = 1 << 1; private static final int SUSPEND_DUE_TO_SCREEN = 1 << 2; + /** + * Time window in milliseconds for which we send + * {@link NetworkAgent#explicitlySelected(boolean)} + * after connecting to the network which the user last selected. + */ + @VisibleForTesting + public static final int LAST_SELECTED_NETWORK_EXPIRATION_AGE_MILLIS = 30 * 1000; + /* Tracks if user has enabled suspend optimizations through settings */ private AtomicBoolean mUserWantsSuspendOpt = new AtomicBoolean(true); @@ -6077,13 +6085,29 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss } } + /** + * Helper function to check if we need to invoke + * {@link NetworkAgent#explicitlySelected(boolean)} to indicate that we connected to a network + * which the user just chose + * (i.e less than {@link #LAST_SELECTED_NETWORK_EXPIRATION_AGE_MILLIS) before). + */ + @VisibleForTesting + public boolean shouldEvaluateWhetherToSendExplicitlySelected(WifiConfiguration currentConfig) { + if (currentConfig == null) { + Log.wtf(TAG, "Current WifiConfiguration is null, but IP provisioning just succeeded"); + return false; + } + long currentTimeMillis = mClock.getElapsedSinceBootMillis(); + return (mWifiConfigManager.getLastSelectedNetwork() == currentConfig.networkId + && currentTimeMillis - mWifiConfigManager.getLastSelectedTimeStamp() + < LAST_SELECTED_NETWORK_EXPIRATION_AGE_MILLIS); + } + private void sendConnectedState() { // If this network was explicitly selected by the user, evaluate whether to call // explicitlySelected() so the system can treat it appropriately. WifiConfiguration config = getCurrentWifiConfiguration(); - if (config == null) { - Log.wtf(TAG, "Current WifiConfiguration is null, but IP provisioning just succeeded"); - } else if (mWifiConfigManager.getLastSelectedNetwork() == config.networkId) { + if (shouldEvaluateWhetherToSendExplicitlySelected(config)) { boolean prompt = mWifiPermissionsUtil.checkConfigOverridePermission(config.lastConnectUid); if (mVerboseLoggingEnabled) { diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java index eb1bae018..8d578b3d9 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java @@ -346,6 +346,7 @@ public class WifiStateMachineTest { @Mock IpManager mIpManager; @Mock TelephonyManager mTelephonyManager; @Mock WrongPasswordNotifier mWrongPasswordNotifier; + @Mock Clock mClock; public WifiStateMachineTest() throws Exception { } @@ -387,6 +388,7 @@ public class WifiStateMachineTest { when(mWifiInjector.getWifiNative()).thenReturn(mWifiNative); when(mWifiInjector.getSelfRecovery()).thenReturn(mSelfRecovery); when(mWifiInjector.makeTelephonyManager()).thenReturn(mTelephonyManager); + when(mWifiInjector.getClock()).thenReturn(mClock); when(mWifiNative.setupForClientMode()) .thenReturn(Pair.create(WifiNative.SETUP_SUCCESS, mClientInterface)); @@ -2028,7 +2030,7 @@ public class WifiStateMachineTest { verify(mWifiNative, never()).resetTxPowerLimit(); } - /* + /** * Verifies that a network disconnection event will result in WifiStateMachine invoking * {@link WifiConfigManager#removeAllEphemeralOrPasspointConfiguredNetworks()} to remove * any ephemeral or passpoint networks from it's internal database. @@ -2075,4 +2077,69 @@ public class WifiStateMachineTest { verify(mWifiConfigManager).clearRecentFailureReason(eq(0)); verify(mWifiConfigManager, never()).setRecentFailureAssociationStatus(anyInt(), anyInt()); } + + /** + * Test that the helper method + * {@link WifiStateMachine#shouldEvaluateWhetherToSendExplicitlySelected(WifiConfiguration)} + * returns true when we connect to the last selected network before expiration of + * {@link WifiStateMachine#LAST_SELECTED_NETWORK_EXPIRATION_AGE_MILLIS}. + */ + @Test + public void testShouldEvaluateWhetherToSendExplicitlySelected_SameNetworkNotExpired() { + long lastSelectedTimestamp = 45666743454L; + int lastSelectedNetworkId = 5; + + when(mClock.getElapsedSinceBootMillis()).thenReturn( + lastSelectedTimestamp + + WifiStateMachine.LAST_SELECTED_NETWORK_EXPIRATION_AGE_MILLIS - 1); + when(mWifiConfigManager.getLastSelectedTimeStamp()).thenReturn(lastSelectedTimestamp); + when(mWifiConfigManager.getLastSelectedNetwork()).thenReturn(lastSelectedNetworkId); + + WifiConfiguration currentConfig = new WifiConfiguration(); + currentConfig.networkId = lastSelectedNetworkId; + assertTrue(mWsm.shouldEvaluateWhetherToSendExplicitlySelected(currentConfig)); + } + + /** + * Test that the helper method + * {@link WifiStateMachine#shouldEvaluateWhetherToSendExplicitlySelected(WifiConfiguration)} + * returns false when we connect to the last selected network after expiration of + * {@link WifiStateMachine#LAST_SELECTED_NETWORK_EXPIRATION_AGE_MILLIS}. + */ + @Test + public void testShouldEvaluateWhetherToSendExplicitlySelected_SameNetworkExpired() { + long lastSelectedTimestamp = 45666743454L; + int lastSelectedNetworkId = 5; + + when(mClock.getElapsedSinceBootMillis()).thenReturn( + lastSelectedTimestamp + + WifiStateMachine.LAST_SELECTED_NETWORK_EXPIRATION_AGE_MILLIS + 1); + when(mWifiConfigManager.getLastSelectedTimeStamp()).thenReturn(lastSelectedTimestamp); + when(mWifiConfigManager.getLastSelectedNetwork()).thenReturn(lastSelectedNetworkId); + + WifiConfiguration currentConfig = new WifiConfiguration(); + currentConfig.networkId = lastSelectedNetworkId; + assertFalse(mWsm.shouldEvaluateWhetherToSendExplicitlySelected(currentConfig)); + } + + /** + * Test that the helper method + * {@link WifiStateMachine#shouldEvaluateWhetherToSendExplicitlySelected(WifiConfiguration)} + * returns false when we connect to a different network to the last selected network. + */ + @Test + public void testShouldEvaluateWhetherToSendExplicitlySelected_DifferentNetwork() { + long lastSelectedTimestamp = 45666743454L; + int lastSelectedNetworkId = 5; + + when(mClock.getElapsedSinceBootMillis()).thenReturn( + lastSelectedTimestamp + + WifiStateMachine.LAST_SELECTED_NETWORK_EXPIRATION_AGE_MILLIS - 1); + when(mWifiConfigManager.getLastSelectedTimeStamp()).thenReturn(lastSelectedTimestamp); + when(mWifiConfigManager.getLastSelectedNetwork()).thenReturn(lastSelectedNetworkId); + + WifiConfiguration currentConfig = new WifiConfiguration(); + currentConfig.networkId = lastSelectedNetworkId - 1; + assertFalse(mWsm.shouldEvaluateWhetherToSendExplicitlySelected(currentConfig)); + } } |