diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java index 8f6210042fe8e..306f9acee406e 100644 --- a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java +++ b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java @@ -223,6 +223,8 @@ public class AccessPoint implements Comparable { 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 { } 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 iter = mScanResultCache.values().iterator(); iter.hasNext(); ) { ScanResult result = iter.next(); @@ -509,12 +507,8 @@ public class AccessPoint implements Comparable { * 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}. - * - *

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 { } } - /** - * Updates {@link #mSeen} based on the scan result cache. - * - *

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 { 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(); diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java index b299961ffac2a..f8e2f8bbb1468 100644 --- a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java +++ b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java @@ -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 { *

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. * - *

Calling this method will set the {@link #sStaleScanResults} bit, which prevents + *

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. * - *

Will not perform the update if {@link #sStaleScanResults} is true + *

Will not perform the update if {@link #mStaleScanResults} is true */ private void updateAccessPoints() { List configs = mWifiManager.getConfiguredNetworks(); final List 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. * *

Do not called directly (except for forceUpdate), use {@link #updateAccessPoints()} which - * respects {@link #sStaleScanResults}. + * respects {@link #mStaleScanResults}. */ @GuardedBy("mLock") private void updateAccessPointsLocked(final List 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; diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java index 2f02b9b984552..88d3a320e7818 100644 --- a/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java @@ -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); } diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java index 76d9823c6ab6a..df6587e5042f8 100644 --- a/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java @@ -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() {