From 915a3782e33684562dff8a0897d1446227301b75 Mon Sep 17 00:00:00 2001 From: Randy Pan Date: Mon, 13 Mar 2017 13:21:48 -0700 Subject: Schedule a scan immediately when disconnected This gives the device a chance to reconnect as soon as possible. Bug: 35188856 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Change-Id: I8eb019ca11b2b63ab17edcc2fa60fb8795141951 --- .../server/wifi/WifiConnectivityManager.java | 5 +- .../server/wifi/WifiConnectivityManagerTest.java | 77 ++++++++++++++++++---- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java index 179da0f14..4ae3a96a2 100644 --- a/service/java/com/android/server/wifi/WifiConnectivityManager.java +++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java @@ -948,9 +948,10 @@ public class WifiConnectivityManager { if (mWifiState == WIFI_STATE_DISCONNECTED) { mLastConnectionAttemptBssid = null; scheduleWatchdogTimer(); + startConnectivityScan(SCAN_IMMEDIATELY); + } else { + startConnectivityScan(SCAN_ON_SCHEDULE); } - - startConnectivityScan(SCAN_ON_SCHEDULE); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 56af3fbb9..39fa247bf 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -703,14 +703,14 @@ public class WifiConnectivityManagerTest { } /** - * When screen on trigger two connection state change events back to back to - * verify that the minium scan interval is enforced. + * When screen on trigger a disconnected state change event then a connected state + * change event back to back to verify that the minium scan interval is enforced. * * Expected behavior: WifiConnectivityManager start the second periodic single * scan PERIODIC_SCAN_INTERVAL_MS after the first one. */ @Test - public void checkMinimumPeriodicScanIntervalWhenScreenOn() { + public void checkMinimumPeriodicScanIntervalWhenScreenOnAndConnected() { long currentTimeStamp = CURRENT_SYSTEM_TIME_MS; when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); @@ -720,30 +720,79 @@ public class WifiConnectivityManagerTest { // Wait for MAX_PERIODIC_SCAN_INTERVAL_MS so that any impact triggered // by screen state change can settle currentTimeStamp += WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS; - long firstScanTimeStamp = currentTimeStamp; + long scanForDisconnectedTimeStamp = currentTimeStamp; + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); + + // Set WiFi to disconnected state which triggers a scan immediately + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_DISCONNECTED); + verify(mWifiScanner, times(1)).startScan(anyObject(), anyObject(), anyObject()); + + // Set up time stamp for when entering CONNECTED state + currentTimeStamp += 2000; + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); + + // Set WiFi to connected state to trigger its periodic scan + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_CONNECTED); + + // The very first scan triggered for connected state is actually via the alarm timer + // and it obeys the minimum scan interval + long firstScanForConnectedTimeStamp = mAlarmManager + .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG); + + // Verify that the first scan for connected state is scheduled PERIODIC_SCAN_INTERVAL_MS + // after the scan for disconnected state + assertEquals(firstScanForConnectedTimeStamp, scanForDisconnectedTimeStamp + + WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS); + } + + /** + * When screen on trigger a connected state change event then a disconnected state + * change event back to back to verify that a scan is fired immediately for the + * disconnected state change event. + * + * Expected behavior: WifiConnectivityManager directly starts the periodic immediately + * for the disconnected state change event. The second scan for disconnected state is + * via alarm timer. + */ + @Test + public void scanImmediatelyWhenScreenOnAndDisconnected() { + long currentTimeStamp = CURRENT_SYSTEM_TIME_MS; + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); + + // Set screen to ON + mWifiConnectivityManager.handleScreenStateChanged(true); + + // Wait for MAX_PERIODIC_SCAN_INTERVAL_MS so that any impact triggered + // by screen state change can settle + currentTimeStamp += WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS; + long scanForConnectedTimeStamp = currentTimeStamp; when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); // Set WiFi to connected state to trigger the periodic scan mWifiConnectivityManager.handleConnectionStateChanged( WifiConnectivityManager.WIFI_STATE_CONNECTED); + verify(mWifiScanner, times(1)).startScan(anyObject(), anyObject(), anyObject()); - // Set the second scan attempt time stamp. + // Set up the time stamp for when entering DISCONNECTED state currentTimeStamp += 2000; + long enteringDisconnectedStateTimeStamp = currentTimeStamp; when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); - // Set WiFi to disconnected state to trigger another periodic scan + // Set WiFi to disconnected state to trigger its periodic scan mWifiConnectivityManager.handleConnectionStateChanged( WifiConnectivityManager.WIFI_STATE_DISCONNECTED); - // Get the second periodic scan actual time stamp - long secondScanTimeStamp = mAlarmManager - .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG); - - // Verify that the second scan is scheduled PERIODIC_SCAN_INTERVAL_MS after the - // very first scan. - assertEquals(secondScanTimeStamp, firstScanTimeStamp - + WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS); + // Verify the very first scan for DISCONNECTED state is fired immediately + verify(mWifiScanner, times(2)).startScan(anyObject(), anyObject(), anyObject()); + long secondScanForDisconnectedTimeStamp = mAlarmManager + .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG); + // Verify that the second scan is scheduled PERIODIC_SCAN_INTERVAL_MS after + // entering DISCONNECTED state. + assertEquals(secondScanForDisconnectedTimeStamp, enteringDisconnectedStateTimeStamp + + WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS); } /** -- cgit v1.2.3