Merge "Create a staleScanResults bit." into oc-dev
am: d1094717a1
Change-Id: I1a4ea5a52093fa2cd9fe1bc321786c349d4c4f11
This commit is contained in:
@@ -144,6 +144,7 @@ public class WifiTracker {
|
||||
|
||||
@VisibleForTesting
|
||||
Scanner mScanner;
|
||||
private boolean mStaleScanResults = false;
|
||||
|
||||
public WifiTracker(Context context, WifiListener wifiListener,
|
||||
boolean includeSaved, boolean includeScans) {
|
||||
@@ -348,7 +349,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() {
|
||||
@@ -365,6 +370,7 @@ public class WifiTracker {
|
||||
mWorkHandler.removePendingMessages();
|
||||
mMainHandler.removePendingMessages();
|
||||
}
|
||||
mStaleScanResults = true;
|
||||
}
|
||||
|
||||
private void unregisterAndClearScoreCache() {
|
||||
@@ -730,6 +736,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));
|
||||
@@ -840,7 +851,9 @@ public class WifiTracker {
|
||||
|
||||
switch (msg.what) {
|
||||
case MSG_UPDATE_ACCESS_POINTS:
|
||||
updateAccessPointsLocked();
|
||||
if (!mStaleScanResults) {
|
||||
updateAccessPointsLocked();
|
||||
}
|
||||
break;
|
||||
case MSG_UPDATE_NETWORK_INFO:
|
||||
updateNetworkInfo((NetworkInfo) msg.obj);
|
||||
|
||||
@@ -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;
|
||||
@@ -747,4 +748,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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user