diff options
-rw-r--r-- | service/java/com/android/server/wifi/WifiNetworkFactory.java | 14 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java | 47 |
2 files changed, 51 insertions, 10 deletions
diff --git a/service/java/com/android/server/wifi/WifiNetworkFactory.java b/service/java/com/android/server/wifi/WifiNetworkFactory.java index 98ba5bc94..9dc89addc 100644 --- a/service/java/com/android/server/wifi/WifiNetworkFactory.java +++ b/service/java/com/android/server/wifi/WifiNetworkFactory.java @@ -146,6 +146,7 @@ public class WifiNetworkFactory extends NetworkFactory { private boolean mVerboseLoggingEnabled = false; private boolean mPeriodicScanTimerSet = false; private boolean mConnectionTimeoutSet = false; + private boolean mIsPeriodicScanEnabled = false; private boolean mIsPeriodicScanPaused = false; // We sent a new connection request and are waiting for connection success. private boolean mPendingConnectionSuccess = false; @@ -256,6 +257,7 @@ public class WifiNetworkFactory extends NetworkFactory { public void onAlarm() { // Trigger the next scan. startScan(); + mPeriodicScanTimerSet = false; } } @@ -264,6 +266,7 @@ public class WifiNetworkFactory extends NetworkFactory { public void onAlarm() { Log.e(TAG, "Timed-out connecting to network"); handleNetworkConnectionFailure(mUserSelectedNetwork); + mConnectionTimeoutSet = false; } } @@ -819,6 +822,7 @@ public class WifiNetworkFactory extends NetworkFactory { // Cancel the ongoing scans after user selection. cancelPeriodicScans(); + mIsPeriodicScanEnabled = false; // Trigger connection attempts. handleConnectToNetworkUserSelectionInternal(network); @@ -918,13 +922,13 @@ public class WifiNetworkFactory extends NetworkFactory { public void handleScreenStateChanged(boolean screenOn) { // If there is no active request or if the user has already selected a network, // ignore screen state changes. - if (mActiveSpecificNetworkRequest == null || mUserSelectedNetwork != null) return; + if (mActiveSpecificNetworkRequest == null || !mIsPeriodicScanEnabled) return; // Pause periodic scans when the screen is off & resume when the screen is on. if (screenOn) { if (mVerboseLoggingEnabled) Log.v(TAG, "Resuming scans on screen on"); - startScan(); mIsPeriodicScanPaused = false; + startScan(); } else { if (mVerboseLoggingEnabled) Log.v(TAG, "Pausing scans on screen off"); cancelPeriodicScans(); @@ -971,6 +975,7 @@ public class WifiNetworkFactory extends NetworkFactory { mActiveSpecificNetworkRequestSpecifier = null; mUserSelectedNetwork = null; mUserSelectedNetworkConnectRetryCount = 0; + mIsPeriodicScanEnabled = false; mIsPeriodicScanPaused = false; mActiveMatchedScanResults = null; mPendingConnectionSuccess = false; @@ -1075,6 +1080,7 @@ public class WifiNetworkFactory extends NetworkFactory { mScanSettings.hiddenNetworks.add(new WifiScanner.ScanSettings.HiddenNetwork( addEnclosingQuotes(wns.ssidPatternMatcher.getPath()))); } + mIsPeriodicScanEnabled = true; startScan(); } @@ -1103,6 +1109,10 @@ public class WifiNetworkFactory extends NetworkFactory { Log.e(TAG, "Scan triggered when there is no active network request. Ignoring..."); return; } + if (!mIsPeriodicScanEnabled) { + Log.e(TAG, "Scan triggered after user selected network. Ignoring..."); + return; + } if (mVerboseLoggingEnabled) { Log.v(TAG, "Starting the next scan for " + mActiveSpecificNetworkRequestSpecifier); } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java index 25aaeffd6..5f9e0333e 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java @@ -644,6 +644,35 @@ public class WifiNetworkFactoryTest extends WifiBaseTest { } /** + * Verify the periodic scan back off to find a network matching the network specifier + * is cancelled when the user selects a network. + */ + @Test + public void testPeriodicScanCancelOnUserSelectNetwork() throws Exception { + attachDefaultWifiNetworkSpecifierAndAppInfo(TEST_UID_1, false); + mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + + mWifiNetworkFactory.addCallback(mAppBinder, mNetworkRequestMatchCallback, + TEST_CALLBACK_IDENTIFIER); + verify(mNetworkRequestMatchCallback).onUserSelectionCallbackRegistration( + mNetworkRequestUserSelectionCallback.capture()); + + verifyPeriodicScans(0, + PERIODIC_SCAN_INTERVAL_MS, // 10s + PERIODIC_SCAN_INTERVAL_MS); // 10s + + // Now trigger user selection to one of the network. + mSelectedNetwork = WifiConfigurationTestUtil.createPskNetwork(); + mSelectedNetwork.SSID = "\"" + TEST_SSID_1 + "\""; + sendUserSelectionSelect(mNetworkRequestUserSelectionCallback.getValue(), mSelectedNetwork); + mLooper.dispatchAll(); + + // Cancel the alarm set for the next scan. + verify(mAlarmManager).cancel(mPeriodicScanListenerArgumentCaptor.getValue()); + } + + + /** * Verify callback registration/unregistration. */ @Test @@ -1345,7 +1374,7 @@ public class WifiNetworkFactoryTest extends WifiBaseTest { } mInOrder = inOrder(mAlarmManager, mClientModeImpl); - validateConnectionRetryAttempts(); + validateConnectionRetryAttempts(true); // Fail the request after all the retries are exhausted. verify(mNetworkRequestMatchCallback).onAbort(); @@ -1373,7 +1402,7 @@ public class WifiNetworkFactoryTest extends WifiBaseTest { mLooper.dispatchAll(); mInOrder = inOrder(mAlarmManager, mClientModeImpl); - validateConnectionRetryAttempts(); + validateConnectionRetryAttempts(false); // Fail the request after all the retries are exhausted. verify(mNetworkRequestMatchCallback).onAbort(); @@ -1407,7 +1436,7 @@ public class WifiNetworkFactoryTest extends WifiBaseTest { } mInOrder = inOrder(mAlarmManager, mClientModeImpl); - validateConnectionRetryAttempts(); + validateConnectionRetryAttempts(false); verify(mNetworkRequestMatchCallback).onAbort(); // Verify that we sent the connection failure callback. @@ -1453,7 +1482,7 @@ public class WifiNetworkFactoryTest extends WifiBaseTest { } mInOrder = inOrder(mAlarmManager, mClientModeImpl); - validateConnectionRetryAttempts(); + validateConnectionRetryAttempts(false); // Verify that we sent the connection failure callback. verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure( @@ -2783,11 +2812,13 @@ public class WifiNetworkFactoryTest extends WifiBaseTest { } } - private void validateConnectionRetryAttempts() { + private void validateConnectionRetryAttempts(boolean onTimeout) { for (int i = 0; i < WifiNetworkFactory.USER_SELECTED_NETWORK_CONNECT_RETRY_MAX; i++) { - // Cancel the existing connection timeout. - mInOrder.verify(mAlarmManager).cancel( - mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); + if (!onTimeout) { + // Cancel the existing connection timeout. + mInOrder.verify(mAlarmManager).cancel( + mConnectionTimeoutAlarmListenerArgumentCaptor.getValue()); + } // Trigger new connection. mInOrder.verify(mClientModeImpl).connect(eq(null), eq(TEST_NETWORK_ID_1), |