Merge "Do not evict scan results in cold start." into oc-dr1-dev
am: d68b6dc56b
Change-Id: If442689c6a05ad261a846a5252d9a0256520508e
This commit is contained in:
@@ -120,6 +120,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";
|
||||
@@ -474,6 +475,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();
|
||||
|
||||
@@ -145,7 +145,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) {
|
||||
@@ -239,9 +241,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) {
|
||||
@@ -255,6 +255,7 @@ public class WifiTracker {
|
||||
}
|
||||
|
||||
List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
|
||||
mInternalAccessPoints.clear();
|
||||
updateAccessPointsLocked(newScanResults, configs);
|
||||
|
||||
// Synchronously copy access points
|
||||
@@ -355,7 +356,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).
|
||||
*/
|
||||
@@ -373,8 +374,8 @@ public class WifiTracker {
|
||||
|
||||
mWorkHandler.removePendingMessages();
|
||||
mMainHandler.removePendingMessages();
|
||||
sStaleScanResults = true;
|
||||
}
|
||||
mStaleScanResults = true;
|
||||
}
|
||||
|
||||
private void unregisterScoreCache() {
|
||||
@@ -479,7 +480,7 @@ 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();
|
||||
@@ -489,7 +490,7 @@ public class WifiTracker {
|
||||
}
|
||||
|
||||
synchronized (mLock) {
|
||||
if(!mStaleScanResults) {
|
||||
if(!sStaleScanResults) {
|
||||
updateAccessPointsLocked(newScanResults, configs);
|
||||
}
|
||||
}
|
||||
@@ -499,7 +500,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,
|
||||
@@ -789,9 +790,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());
|
||||
|
||||
mMainHandler.sendEmptyMessage(MainHandler.MSG_CONNECTED_CHANGED);
|
||||
if(mConnected.get() != info.isConnected()) {
|
||||
mConnected.set(info.isConnected());
|
||||
mMainHandler.sendEmptyMessage(MainHandler.MSG_CONNECTED_CHANGED);
|
||||
}
|
||||
|
||||
mWorkHandler.obtainMessage(WorkHandler.MSG_UPDATE_NETWORK_INFO, info)
|
||||
.sendToTarget();
|
||||
@@ -844,7 +847,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*/);
|
||||
@@ -899,7 +902,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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user