From a0a2a080ee2dc91e0272a90b9471695614bd69b3 Mon Sep 17 00:00:00 2001 From: Arc Wang Date: Tue, 10 Dec 2019 19:20:14 +0800 Subject: Fix StandardNetworkDetailsTracker crash at onStart Should only update ScanResultUpdater with new ScanResult of the chosen WifiEntry. Bug: 70983952 Test: atest StandardNetworkDetailsTrackerTest Change-Id: I3bfaedf77ca1da012349636fe2b1a92b7385ebd6 --- .../wifitrackerlib/StandardNetworkDetailsTracker.java | 18 ++++++++++++------ .../StandardNetworkDetailsTrackerTest.java | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) (limited to 'libs') diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardNetworkDetailsTracker.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardNetworkDetailsTracker.java index 28e315c24..c7052a491 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardNetworkDetailsTracker.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardNetworkDetailsTracker.java @@ -73,7 +73,7 @@ class StandardNetworkDetailsTracker extends NetworkDetailsTracker { @Override protected void handleOnStart() { - mScanResultUpdater.update(mWifiManager.getScanResults()); + cacheNewScanResults(); conditionallyUpdateScanResults(true /* lastScanSucceeded */); conditionallyUpdateConfig(); } @@ -122,11 +122,7 @@ class StandardNetworkDetailsTracker extends NetworkDetailsTracker { long scanAgeWindow = mMaxScanAgeMillis; if (lastScanSucceeded) { - // Scan succeeded, cache new scans - mScanResultUpdater.update(mWifiManager.getScanResults().stream().filter( - scan -> TextUtils.equals( - scanResultToStandardWifiEntryKey(scan), mChosenEntry.getKey())) - .collect(toList())); + cacheNewScanResults(); } else { // Scan failed, increase scan age window to prevent WifiEntry list from // clearing prematurely. @@ -146,4 +142,14 @@ class StandardNetworkDetailsTracker extends NetworkDetailsTracker { .findAny(); mChosenEntry.updateConfig(optionalConfig.orElse(null)); } + + /** + * Updates ScanResultUpdater with new ScanResults matching mChosenEntry. + */ + private void cacheNewScanResults() { + mScanResultUpdater.update(mWifiManager.getScanResults().stream() + .filter(scan -> TextUtils.equals( + scanResultToStandardWifiEntryKey(scan), mChosenEntry.getKey())) + .collect(toList())); + } } diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardNetworkDetailsTrackerTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardNetworkDetailsTrackerTest.java index 740bbdcef..e4be62939 100644 --- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardNetworkDetailsTrackerTest.java +++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/StandardNetworkDetailsTrackerTest.java @@ -144,6 +144,25 @@ public class StandardNetworkDetailsTrackerTest { assertThat(tracker.getWifiEntry().getKey()).isEqualTo(key); } + /** + * Tests that SCAN_RESULTS_AVAILABLE_ACTION updates the level of the entry. + */ + @Test + public void testHandleOnStart_scanResultUpdaterUpdateCorrectly() { + final ScanResult chosen = buildScanResult("ssid", "bssid", START_MILLIS); + final String key = scanResultToStandardWifiEntryKey(chosen); + final StandardNetworkDetailsTracker tracker = createTestStandardNetworkDetailsTracker(key); + final ScanResult other = buildScanResult("ssid2", "bssid", START_MILLIS, -50 /* rssi */); + when(mMockWifiManager.getScanResults()).thenReturn(Collections.singletonList(other)); + + //tracker.onStart(); + tracker.handleOnStart(); + + final long invalidCount = tracker.mScanResultUpdater.getScanResults().stream().filter( + scanResult -> !"ssid".equals(scanResult.SSID)).count(); + assertThat(invalidCount).isEqualTo(0); + } + /** * Tests that SCAN_RESULTS_AVAILABLE_ACTION updates the level of the entry. */ -- cgit v1.2.3