Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7dd26353 authored by Sundeep Ghuman's avatar Sundeep Ghuman Committed by android-build-merger
Browse files

Merge "Create a staleScanResults bit." into oc-dev

am: d1094717

Change-Id: Iab2e12d6c307dd57f14ff4830a5827d3c687fa04
parents 4372af30 d1094717
Loading
Loading
Loading
Loading
+15 −2
Original line number Original line Diff line number Diff line
@@ -142,6 +142,7 @@ public class WifiTracker {


    @VisibleForTesting
    @VisibleForTesting
    Scanner mScanner;
    Scanner mScanner;
    private boolean mStaleScanResults = false;


    public WifiTracker(Context context, WifiListener wifiListener,
    public WifiTracker(Context context, WifiListener wifiListener,
            boolean includeSaved, boolean includeScans) {
            boolean includeSaved, boolean includeScans) {
@@ -330,7 +331,11 @@ public class WifiTracker {
     * Stop tracking wifi networks and scores.
     * Stop tracking wifi networks and scores.
     *
     *
     * <p>This should always be called when done with a WifiTracker (if startTracking was called) to
     * <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
    @MainThread
    public void stopTracking() {
    public void stopTracking() {
@@ -346,6 +351,7 @@ public class WifiTracker {
            mWorkHandler.removePendingMessages();
            mWorkHandler.removePendingMessages();
            mMainHandler.removePendingMessages();
            mMainHandler.removePendingMessages();
        }
        }
        mStaleScanResults = true;
    }
    }


    private void unregisterAndClearScoreCache() {
    private void unregisterAndClearScoreCache() {
@@ -712,6 +718,11 @@ public class WifiTracker {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String action = intent.getAction();

            if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
                mStaleScanResults = false;
            }

            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
                updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                        WifiManager.WIFI_STATE_UNKNOWN));
                        WifiManager.WIFI_STATE_UNKNOWN));
@@ -822,7 +833,9 @@ public class WifiTracker {


            switch (msg.what) {
            switch (msg.what) {
                case MSG_UPDATE_ACCESS_POINTS:
                case MSG_UPDATE_ACCESS_POINTS:
                    if (!mStaleScanResults) {
                        updateAccessPointsLocked();
                        updateAccessPointsLocked();
                    }
                    break;
                    break;
                case MSG_UPDATE_NETWORK_INFO:
                case MSG_UPDATE_NETWORK_INFO:
                    updateNetworkInfo((NetworkInfo) msg.obj);
                    updateNetworkInfo((NetworkInfo) msg.obj);
+33 −0
Original line number Original line Diff line number Diff line
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@@ -748,4 +749,36 @@ public class WifiTrackerTest {


        verifyNoMoreInteractions(mockWifiListener);
        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
    }
}
}