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

Commit d68b6dc5 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Do not evict scan results in cold start." into oc-dr1-dev

parents bad473d1 4205874e
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -118,6 +118,7 @@ public class AccessPoint implements Comparable<AccessPoint> {
     */
    private final ConcurrentHashMap<String, ScanResult> mScanResultCache =
            new ConcurrentHashMap<String, ScanResult>(32);
    /** Maximum age of scan results to hold onto while actively scanning. **/
    private static final long MAX_SCAN_RESULT_AGE_MS = 15000;

    static final String KEY_NETWORKINFO = "key_networkinfo";
@@ -441,6 +442,10 @@ public class AccessPoint implements Comparable<AccessPoint> {
    }

    private void evictOldScanResults() {
        if (WifiTracker.sStaleScanResults) {
            // Do not evict old scan results unless we are scanning and have fresh results.
            return;
        }
        long nowMs = SystemClock.elapsedRealtime();
        for (Iterator<ScanResult> iter = mScanResultCache.values().iterator(); iter.hasNext(); ) {
            ScanResult result = iter.next();
+16 −13
Original line number Diff line number Diff line
@@ -143,7 +143,9 @@ public class WifiTracker {

    @VisibleForTesting
    Scanner mScanner;
    private boolean mStaleScanResults = true;

    @GuardedBy("mLock")
    static boolean sStaleScanResults = true;

    public WifiTracker(Context context, WifiListener wifiListener,
            boolean includeSaved, boolean includeScans) {
@@ -237,9 +239,7 @@ public class WifiTracker {
        };
    }

    /**
     * Synchronously update the list of access points with the latest information.
     */
    /** Synchronously update the list of access points with the latest information. */
    @MainThread
    public void forceUpdate() {
        synchronized (mLock) {
@@ -249,6 +249,7 @@ public class WifiTracker {

            final List<ScanResult> newScanResults = mWifiManager.getScanResults();
            List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
            mInternalAccessPoints.clear();
            updateAccessPointsLocked(newScanResults, configs);

            if (DBG) {
@@ -353,7 +354,7 @@ public class WifiTracker {
     * <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 occurring.
     *
     * <p>Calling this method will set the {@link #mStaleScanResults} bit, which prevents
     * <p>Calling this method will set the {@link #sStaleScanResults} bit, which prevents
     * {@link WifiListener#onAccessPointsChanged()} callbacks from being invoked (until the bit
     * is unset on the next SCAN_RESULTS_AVAILABLE_ACTION).
     */
@@ -371,8 +372,8 @@ public class WifiTracker {

            mWorkHandler.removePendingMessages();
            mMainHandler.removePendingMessages();
            sStaleScanResults = true;
        }
        mStaleScanResults = true;
    }

    private void unregisterScoreCache() {
@@ -477,14 +478,14 @@ public class WifiTracker {
    /**
     * Safely modify {@link #mInternalAccessPoints} by acquiring {@link #mLock} first.
     *
     * <p>Will not perform the update if {@link #mStaleScanResults} is true
     * <p>Will not perform the update if {@link #sStaleScanResults} is true
     */
    private void updateAccessPoints() {
        List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
        final List<ScanResult> newScanResults = mWifiManager.getScanResults();

        synchronized (mLock) {
            if(!mStaleScanResults) {
            if(!sStaleScanResults) {
                updateAccessPointsLocked(newScanResults, configs);
            }
        }
@@ -494,7 +495,7 @@ public class WifiTracker {
     * Update the internal list of access points.
     *
     * <p>Do not called directly (except for forceUpdate), use {@link #updateAccessPoints()} which
     * respects {@link #mStaleScanResults}.
     * respects {@link #sStaleScanResults}.
     */
    @GuardedBy("mLock")
    private void updateAccessPointsLocked(final List<ScanResult> newScanResults,
@@ -784,9 +785,11 @@ public class WifiTracker {
                mWorkHandler.sendEmptyMessage(WorkHandler.MSG_UPDATE_ACCESS_POINTS);
            } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
                NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                mConnected.set(info.isConnected());

                if(mConnected.get() != info.isConnected()) {
                    mConnected.set(info.isConnected());
                    mMainHandler.sendEmptyMessage(MainHandler.MSG_CONNECTED_CHANGED);
                }

                mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info)
                        .sendToTarget();
@@ -839,7 +842,7 @@ public class WifiTracker {
                    // Only notify listeners of changes if we have fresh scan results, otherwise the
                    // UI will be updated with stale results. We want to copy the APs regardless,
                    // for instances where forceUpdate was invoked by the caller.
                    if (mStaleScanResults) {
                    if (sStaleScanResults) {
                        copyAndNotifyListeners(false /*notifyListeners*/);
                    } else {
                        copyAndNotifyListeners(true /*notifyListeners*/);
@@ -894,7 +897,7 @@ public class WifiTracker {
            switch (msg.what) {
                case MSG_UPDATE_ACCESS_POINTS:
                    if (msg.arg1 == CLEAR_STALE_SCAN_RESULTS) {
                        mStaleScanResults = false;
                        sStaleScanResults = false;
                    }
                    updateAccessPoints();
                    break;
+39 −0
Original line number Diff line number Diff line
@@ -135,7 +135,9 @@ public class WifiTrackerTest {
    private HandlerThread mWorkerThread;
    private Looper mWorkerLooper;
    private Looper mMainLooper;

    private int mOriginalScoringUiSettingValue;
    private boolean mOriginalStaleScanResultsValue;

    @Before
    public void setUp() {
@@ -210,6 +212,8 @@ public class WifiTrackerTest {
                InstrumentationRegistry.getTargetContext().getContentResolver(),
                Settings.Global.NETWORK_SCORING_UI_ENABLED,
                1 /* enabled */);

        mOriginalStaleScanResultsValue = WifiTracker.sStaleScanResults;
    }

    @After
@@ -218,6 +222,8 @@ public class WifiTrackerTest {
                InstrumentationRegistry.getTargetContext().getContentResolver(),
                Settings.Global.NETWORK_SCORING_UI_ENABLED,
                mOriginalScoringUiSettingValue);

        WifiTracker.sStaleScanResults = mOriginalStaleScanResultsValue;
    }

    private static ScanResult buildScanResult1() {
@@ -840,4 +846,37 @@ public class WifiTrackerTest {

        assertThat(tracker.getAccessPoints()).isEmpty();
    }

    @Test
    public void onConnectedChangedCallback_shouldNotBeInvokedWhenNoStateChange() throws Exception {
        WifiTracker tracker = createTrackerWithScanResultsAndAccessPoint1Connected();
        verify(mockWifiListener, times(1)).onConnectedChanged();

        NetworkInfo networkInfo = new NetworkInfo(
                ConnectivityManager.TYPE_WIFI, 0, "Type Wifi", "subtype");
        networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "connected", "test");

        Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo);
        tracker.mReceiver.onReceive(mContext, intent);

        verify(mockWifiListener, times(1)).onConnectedChanged();
    }

    @Test
    public void onConnectedChangedCallback_shouldNBeInvokedWhenStateChanges() throws Exception {
        WifiTracker tracker = createTrackerWithScanResultsAndAccessPoint1Connected();
        verify(mockWifiListener, times(1)).onConnectedChanged();

        NetworkInfo networkInfo = new NetworkInfo(
                ConnectivityManager.TYPE_WIFI, 0, "Type Wifi", "subtype");
        networkInfo.setDetailedState(
                NetworkInfo.DetailedState.DISCONNECTED, "dicconnected", "test");

        Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo);
        tracker.mReceiver.onReceive(mContext, intent);

        verify(mockWifiListener, times(2)).onConnectedChanged();
    }
}