From 7347b91f448800656c79c0eb0cca39b83861e575 Mon Sep 17 00:00:00 2001 From: Nate Jiang Date: Thu, 2 Apr 2020 14:36:49 -0700 Subject: Duplicate ScanResult should return only one matched suggestion Bug: 153010858 Test: atest com.android.server.wifi Change-Id: Ia4254fd407e8ff12cdf9d45d07a280ea7b5ffcdf --- .../android/server/wifi/WifiNetworkSuggestionsManager.java | 6 +++++- .../server/wifi/WifiNetworkSuggestionsManagerTest.java | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java index 19c9da156..5539f8131 100644 --- a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java +++ b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java @@ -1615,6 +1615,8 @@ public class WifiNetworkSuggestionsManager { */ public @NonNull List getWifiConfigForMatchedNetworkSuggestionsSharedWithUser( List scanResults) { + // Create a HashSet to avoid return multiple result for duplicate ScanResult. + Set networkKeys = new HashSet<>(); List sharedWifiConfigs = new ArrayList<>(); for (ScanResult scanResult : scanResults) { ScanResultMatchInfo scanResultMatchInfo = @@ -1648,7 +1650,9 @@ public class WifiNetworkSuggestionsManager { if (existingConfig == null || !existingConfig.fromWifiNetworkSuggestion) { continue; } - sharedWifiConfigs.add(existingConfig); + if (networkKeys.add(existingConfig.getKey())) { + sharedWifiConfigs.add(existingConfig); + } } return sharedWifiConfigs; } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java index bb090cae5..7fd9e6906 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -2982,6 +2982,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { * - app without user approval will not be returned. * - open network will not be returned. * - suggestion doesn't allow user manually connect will not be return. + * - Multiple duplicate ScanResults will only return single matched config. */ @Test public void testGetWifiConfigForMatchedNetworkSuggestionsSharedWithUser() { @@ -2996,8 +2997,14 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { createScanDetailForNetwork(networkSuggestion1.wifiConfiguration).getScanResult()); scanResults.add( createScanDetailForNetwork(networkSuggestion2.wifiConfiguration).getScanResult()); - scanResults.add( - createScanDetailForNetwork(networkSuggestion3.wifiConfiguration).getScanResult()); + + // Create two same ScanResult for networkSuggestion3 + ScanResult scanResult1 = createScanDetailForNetwork(networkSuggestion3.wifiConfiguration) + .getScanResult(); + ScanResult scanResult2 = new ScanResult(scanResult1); + scanResults.add(scanResult1); + scanResults.add(scanResult2); + List networkSuggestionList = new ArrayList() {{ add(networkSuggestion1); @@ -3018,6 +3025,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_PACKAGE_1); wifiConfigurationList = mWifiNetworkSuggestionsManager .getWifiConfigForMatchedNetworkSuggestionsSharedWithUser(scanResults); + assertEquals(1, wifiConfigurationList.size()); assertEquals(networkSuggestion3.wifiConfiguration, wifiConfigurationList.get(0)); } -- cgit v1.2.3