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

Commit 591e96c1 authored by Sundeep Ghuman's avatar Sundeep Ghuman Committed by android-build-merger
Browse files

Merge "Refactor WifiTracker sStaleScanResults." into oc-dr1-dev

am: e14d59a6

Change-Id: I2bcf3d52e970190d900005988cc0e562c8ab7b59
parents 4bb0e041 e14d59a6
Loading
Loading
Loading
Loading
+14 −16
Original line number Diff line number Diff line
@@ -223,6 +223,8 @@ public class AccessPoint implements Comparable<AccessPoint> {
            mProviderFriendlyName = savedState.getString(KEY_PROVIDER_FRIENDLY_NAME);
        }
        update(mConfig, mInfo, mNetworkInfo);

        // Do not evict old scan results on initial creation
        updateRssi();
        updateSeen();
        mId = sLastId.incrementAndGet();
@@ -442,10 +444,6 @@ 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();
@@ -509,12 +507,8 @@ public class AccessPoint implements Comparable<AccessPoint> {
     * results, returning the best RSSI for all matching AccessPoints averaged with the previous
     * value. If the access point is not connected and there are no scan results, the rssi will be
     * set to {@link #UNREACHABLE_RSSI}.
     *
     * <p>Old scan results will be evicted from the cache when this method is invoked.
     */
    private void updateRssi() {
        evictOldScanResults();

        if (this.isActive()) {
            return;
        }
@@ -533,14 +527,8 @@ public class AccessPoint implements Comparable<AccessPoint> {
        }
    }

    /**
     * Updates {@link #mSeen} based on the scan result cache.
     *
     * <p>Old scan results will be evicted from the cache when this method is invoked.
     */
    /** Updates {@link #mSeen} based on the scan result cache. */
    private void updateSeen() {
        evictOldScanResults();

        // TODO(sghuman): Set to now if connected

        long seen = 0;
@@ -1030,12 +1018,22 @@ public class AccessPoint implements Comparable<AccessPoint> {
        mAccessPointListener = listener;
    }

    boolean update(ScanResult result) {
    /**
     * Update the AP with the given scan result.
     *
     * @param result the ScanResult to add to the AccessPoint scan cache
     * @param evictOldScanResults whether stale scan results should be removed
     *         from the cache during this update process
     * @return true if the scan result update caused a change in state which would impact ranking
     *     or AccessPoint rendering (e.g. wifi level, security)
     */
    boolean update(ScanResult result, boolean evictOldScanResults) {
        if (matches(result)) {
            int oldLevel = getLevel();

            /* Add or update the scan result for the BSSID */
            mScanResultCache.put(result.BSSID, result);
            if (evictOldScanResults) evictOldScanResults();
            updateSeen();
            updateRssi();
            int newLevel = getLevel();
+12 −10
Original line number Diff line number Diff line
@@ -145,7 +145,7 @@ public class WifiTracker {
    Scanner mScanner;

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

    public WifiTracker(Context context, WifiListener wifiListener,
            boolean includeSaved, boolean includeScans) {
@@ -354,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 #sStaleScanResults} bit, which prevents
     * <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).
     */
@@ -372,7 +372,7 @@ public class WifiTracker {

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

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

        synchronized (mLock) {
            if(!sStaleScanResults) {
            if(!mStaleScanResults) {
                updateAccessPointsLocked(newScanResults, configs);
            }
        }
@@ -495,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 #sStaleScanResults}.
     * respects {@link #mStaleScanResults}.
     */
    @GuardedBy("mLock")
    private void updateAccessPointsLocked(final List<ScanResult> newScanResults,
@@ -569,7 +569,8 @@ public class WifiTracker {

                boolean found = false;
                for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
                    if (accessPoint.update(result)) {
                    // We want to evict old scan results if are current results are not stale
                    if (accessPoint.update(result, !mStaleScanResults)) {
                        found = true;
                        break;
                    }
@@ -642,7 +643,8 @@ public class WifiTracker {
        for (int i = 0; i < N; i++) {
            if (cache.get(i).matches(result)) {
                AccessPoint ret = cache.remove(i);
                ret.update(result);
                // evict old scan results only if we have fresh results
                ret.update(result, !mStaleScanResults);
                return ret;
            }
        }
@@ -842,7 +844,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 (sStaleScanResults) {
                    if (mStaleScanResults) {
                        copyAndNotifyListeners(false /*notifyListeners*/);
                    } else {
                        copyAndNotifyListeners(true /*notifyListeners*/);
@@ -897,7 +899,7 @@ public class WifiTracker {
            switch (msg.what) {
                case MSG_UPDATE_ACCESS_POINTS:
                    if (msg.arg1 == CLEAR_STALE_SCAN_RESULTS) {
                        sStaleScanResults = false;
                        mStaleScanResults = false;
                    }
                    updateAccessPoints();
                    break;
+1 −1
Original line number Diff line number Diff line
@@ -247,7 +247,7 @@ public class AccessPointTest {
        scanResult.BSSID = "bssid";
        scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
        scanResult.capabilities = "";
        assertThat(ap.update(scanResult)).isTrue();
        assertThat(ap.update(scanResult, true /* evict old scan results */)).isTrue();

        assertThat(ap.getRssi()).isEqualTo(expectedRssi);
    }
+0 −4
Original line number Diff line number Diff line
@@ -137,7 +137,6 @@ public class WifiTrackerTest {
    private Looper mMainLooper;

    private int mOriginalScoringUiSettingValue;
    private boolean mOriginalStaleScanResultsValue;

    @Before
    public void setUp() {
@@ -213,7 +212,6 @@ public class WifiTrackerTest {
                Settings.Global.NETWORK_SCORING_UI_ENABLED,
                1 /* enabled */);

        mOriginalStaleScanResultsValue = WifiTracker.sStaleScanResults;
    }

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

        WifiTracker.sStaleScanResults = mOriginalStaleScanResultsValue;
    }

    private static ScanResult buildScanResult1() {