Loading packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java +15 −2 Original line number Diff line number Diff line Loading @@ -142,6 +142,7 @@ public class WifiTracker { @VisibleForTesting Scanner mScanner; private boolean mStaleScanResults = false; public WifiTracker(Context context, WifiListener wifiListener, boolean includeSaved, boolean includeScans) { Loading Loading @@ -330,7 +331,11 @@ public class WifiTracker { * Stop tracking wifi networks and scores. * * <p>This should always be called when done with a WifiTracker (if startTracking was called) to * ensure proper cleanup and prevent any further callbacks from occuring. * ensure proper cleanup and prevent any further callbacks from occurring. * * <p>Calling this method will set the {@link #mStaleScanResults} bit, which prevents * {@link WifiListener#onAccessPointsChanged()} callbacks from being invoked (until the bit * is unset on the next SCAN_RESULTS_AVAILABLE_ACTION). */ @MainThread public void stopTracking() { Loading @@ -346,6 +351,7 @@ public class WifiTracker { mWorkHandler.removePendingMessages(); mMainHandler.removePendingMessages(); } mStaleScanResults = true; } private void unregisterAndClearScoreCache() { Loading Loading @@ -712,6 +718,11 @@ public class WifiTracker { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) { mStaleScanResults = false; } if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)); Loading Loading @@ -822,7 +833,9 @@ public class WifiTracker { switch (msg.what) { case MSG_UPDATE_ACCESS_POINTS: if (!mStaleScanResults) { updateAccessPointsLocked(); } break; case MSG_UPDATE_NETWORK_INFO: updateNetworkInfo((NetworkInfo) msg.obj); Loading packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java +33 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; Loading Loading @@ -748,4 +749,36 @@ public class WifiTrackerTest { verifyNoMoreInteractions(mockWifiListener); } @Test public void stopTrackingShouldSetStaleBitWhichPreventsCallbacksUntilNextScanResult() throws Exception { WifiTracker tracker = createMockedWifiTracker(); startTracking(tracker); tracker.stopTracking(); CountDownLatch latch1 = new CountDownLatch(1); tracker.mMainHandler.post(() -> { latch1.countDown(); }); assertTrue("Latch 1 timed out", latch1.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); startTracking(tracker); tracker.mReceiver.onReceive(mContext, new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); tracker.mReceiver.onReceive( mContext, new Intent(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION)); tracker.mReceiver.onReceive( mContext, new Intent(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION)); CountDownLatch latch2 = new CountDownLatch(1); tracker.mMainHandler.post(() -> { latch2.countDown(); }); assertTrue("Latch 2 timed out", latch2.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); verify(mockWifiListener, never()).onAccessPointsChanged(); sendScanResultsAndProcess(tracker); // verifies onAccessPointsChanged is invoked } } Loading
packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java +15 −2 Original line number Diff line number Diff line Loading @@ -142,6 +142,7 @@ public class WifiTracker { @VisibleForTesting Scanner mScanner; private boolean mStaleScanResults = false; public WifiTracker(Context context, WifiListener wifiListener, boolean includeSaved, boolean includeScans) { Loading Loading @@ -330,7 +331,11 @@ public class WifiTracker { * Stop tracking wifi networks and scores. * * <p>This should always be called when done with a WifiTracker (if startTracking was called) to * ensure proper cleanup and prevent any further callbacks from occuring. * ensure proper cleanup and prevent any further callbacks from occurring. * * <p>Calling this method will set the {@link #mStaleScanResults} bit, which prevents * {@link WifiListener#onAccessPointsChanged()} callbacks from being invoked (until the bit * is unset on the next SCAN_RESULTS_AVAILABLE_ACTION). */ @MainThread public void stopTracking() { Loading @@ -346,6 +351,7 @@ public class WifiTracker { mWorkHandler.removePendingMessages(); mMainHandler.removePendingMessages(); } mStaleScanResults = true; } private void unregisterAndClearScoreCache() { Loading Loading @@ -712,6 +718,11 @@ public class WifiTracker { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) { mStaleScanResults = false; } if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)); Loading Loading @@ -822,7 +833,9 @@ public class WifiTracker { switch (msg.what) { case MSG_UPDATE_ACCESS_POINTS: if (!mStaleScanResults) { updateAccessPointsLocked(); } break; case MSG_UPDATE_NETWORK_INFO: updateNetworkInfo((NetworkInfo) msg.obj); Loading
packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java +33 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; Loading Loading @@ -748,4 +749,36 @@ public class WifiTrackerTest { verifyNoMoreInteractions(mockWifiListener); } @Test public void stopTrackingShouldSetStaleBitWhichPreventsCallbacksUntilNextScanResult() throws Exception { WifiTracker tracker = createMockedWifiTracker(); startTracking(tracker); tracker.stopTracking(); CountDownLatch latch1 = new CountDownLatch(1); tracker.mMainHandler.post(() -> { latch1.countDown(); }); assertTrue("Latch 1 timed out", latch1.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); startTracking(tracker); tracker.mReceiver.onReceive(mContext, new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); tracker.mReceiver.onReceive( mContext, new Intent(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION)); tracker.mReceiver.onReceive( mContext, new Intent(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION)); CountDownLatch latch2 = new CountDownLatch(1); tracker.mMainHandler.post(() -> { latch2.countDown(); }); assertTrue("Latch 2 timed out", latch2.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); verify(mockWifiListener, never()).onAccessPointsChanged(); sendScanResultsAndProcess(tracker); // verifies onAccessPointsChanged is invoked } }