diff options
-rw-r--r-- | service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java | 43 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java | 112 |
2 files changed, 148 insertions, 7 deletions
diff --git a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java index 05e1e6391..e97c3de93 100644 --- a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java +++ b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java @@ -36,6 +36,7 @@ import android.content.pm.PackageManager; import android.content.res.Resources; import android.graphics.drawable.Icon; import android.net.MacAddress; +import android.net.NetworkScoreManager; import android.net.wifi.ISuggestionConnectionStatusListener; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; @@ -132,6 +133,7 @@ public class WifiNetworkSuggestionsManager { private final AppOpsManager mAppOps; private final ActivityManager mActivityManager; private final NotificationManager mNotificationManager; + private final NetworkScoreManager mNetworkScoreManager; private final PackageManager mPackageManager; private final WifiPermissionsUtil mWifiPermissionsUtil; private final WifiConfigManager mWifiConfigManager; @@ -192,6 +194,24 @@ public class WifiNetworkSuggestionsManager { // else ignored. } + /** + * Returns true if this app has the necessary approvals to place network suggestions. + */ + private boolean isApproved(@Nullable String activeScorerPkg) { + return hasUserApproved || isExemptFromUserApproval(activeScorerPkg); + } + + /** + * Returns true if this app can suggest networks without user approval. + */ + private boolean isExemptFromUserApproval(@Nullable String activeScorerPkg) { + final boolean isCarrierPrivileged = carrierId != TelephonyManager.UNKNOWN_CARRIER_ID; + if (isCarrierPrivileged) { + return true; + } + return packageName.equals(activeScorerPkg); + } + // This is only needed for comparison in unit tests. @Override public boolean equals(Object other) { @@ -580,6 +600,7 @@ public class WifiNetworkSuggestionsManager { mActivityManager = context.getSystemService(ActivityManager.class); mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + mNetworkScoreManager = context.getSystemService(NetworkScoreManager.class); mPackageManager = context.getPackageManager(); mWifiInjector = wifiInjector; mFrameworkFacade = mWifiInjector.getFrameworkFacade(); @@ -817,6 +838,7 @@ public class WifiNetworkSuggestionsManager { } int carrierId = mTelephonyUtil.getCarrierIdForPackageWithCarrierPrivileges(packageName); + final String activeScorerPackage = mNetworkScoreManager.getActiveScorerPackage(); PerAppInfo perAppInfo = mActiveNetworkSuggestionsPerApp.get(packageName); if (perAppInfo == null) { perAppInfo = new PerAppInfo(uid, packageName, featureId); @@ -827,6 +849,10 @@ public class WifiNetworkSuggestionsManager { } else if (carrierId != TelephonyManager.UNKNOWN_CARRIER_ID) { Log.i(TAG, "Setting the carrier privileged app approved"); perAppInfo.carrierId = carrierId; + } else if (perAppInfo.packageName.equals(activeScorerPackage)) { + Log.i(TAG, "Exempting the active scorer app"); + // nothing more to do, user approval related checks are done at network selection + // time (which also takes care of any dynamic changes in active scorer). } else { if (isSuggestionFromForegroundApp(packageName)) { sendUserApprovalDialog(packageName, uid); @@ -1210,8 +1236,9 @@ public class WifiNetworkSuggestionsManager { */ public List<WifiConfiguration> getAllScanOptimizationSuggestionNetworks() { List<WifiConfiguration> networks = new ArrayList<>(); + final String activeScorerPackage = mNetworkScoreManager.getActiveScorerPackage(); for (PerAppInfo info : mActiveNetworkSuggestionsPerApp.values()) { - if (!info.hasUserApproved && info.carrierId == TelephonyManager.UNKNOWN_CARRIER_ID) { + if (!info.isApproved(activeScorerPackage)) { continue; } for (ExtendedWifiNetworkSuggestion ewns : info.extNetworkSuggestions) { @@ -1498,10 +1525,10 @@ public class WifiNetworkSuggestionsManager { if (extNetworkSuggestions == null) { return null; } + final String activeScorerPackage = mNetworkScoreManager.getActiveScorerPackage(); Set<ExtendedWifiNetworkSuggestion> approvedExtNetworkSuggestions = new HashSet<>(); for (ExtendedWifiNetworkSuggestion ewns : extNetworkSuggestions) { - if (!ewns.perAppInfo.hasUserApproved - && ewns.perAppInfo.carrierId == TelephonyManager.UNKNOWN_CARRIER_ID) { + if (!ewns.perAppInfo.isApproved(activeScorerPackage)) { sendUserApprovalNotificationIfNotApproved(ewns.perAppInfo.packageName, ewns.perAppInfo.uid); continue; @@ -1545,10 +1572,10 @@ public class WifiNetworkSuggestionsManager { if (extNetworkSuggestions == null) { return null; } + final String activeScorerPackage = mNetworkScoreManager.getActiveScorerPackage(); Set<ExtendedWifiNetworkSuggestion> approvedExtNetworkSuggestions = new HashSet<>(); for (ExtendedWifiNetworkSuggestion ewns : extNetworkSuggestions) { - if (!ewns.perAppInfo.hasUserApproved - && ewns.perAppInfo.carrierId == TelephonyManager.UNKNOWN_CARRIER_ID) { + if (!ewns.perAppInfo.isApproved(activeScorerPackage)) { sendUserApprovalNotificationIfNotApproved(ewns.perAppInfo.packageName, ewns.perAppInfo.uid); continue; @@ -1592,11 +1619,11 @@ public class WifiNetworkSuggestionsManager { if (extNetworkSuggestions == null || extNetworkSuggestions.isEmpty()) { return null; } + final String activeScorerPackage = mNetworkScoreManager.getActiveScorerPackage(); Set<ExtendedWifiNetworkSuggestion> approvedExtNetworkSuggestions = extNetworkSuggestions .stream() - .filter(n -> n.perAppInfo.hasUserApproved - || n.perAppInfo.carrierId != TelephonyManager.UNKNOWN_CARRIER_ID) + .filter(n -> n.perAppInfo.isApproved(activeScorerPackage)) .collect(Collectors.toSet()); if (approvedExtNetworkSuggestions.isEmpty()) { return null; @@ -2100,6 +2127,7 @@ public class WifiNetworkSuggestionsManager { public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { pw.println("Dump of WifiNetworkSuggestionsManager"); pw.println("WifiNetworkSuggestionsManager - Networks Begin ----"); + final String activeScorerPackage = mNetworkScoreManager.getActiveScorerPackage(); for (Map.Entry<String, PerAppInfo> networkSuggestionsEntry : mActiveNetworkSuggestionsPerApp.entrySet()) { pw.println("Package Name: " + networkSuggestionsEntry.getKey()); @@ -2107,6 +2135,7 @@ public class WifiNetworkSuggestionsManager { pw.println("Has user approved: " + appInfo.hasUserApproved); pw.println("Has carrier privileges: " + (appInfo.carrierId != TelephonyManager.UNKNOWN_CARRIER_ID)); + pw.println("Is active scorer: " + appInfo.packageName.equals(activeScorerPackage)); for (ExtendedWifiNetworkSuggestion extNetworkSuggestion : appInfo.extNetworkSuggestions) { pw.println("Network: " + extNetworkSuggestion); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java index e9bb940be..5bc4d3366 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -16,8 +16,11 @@ package com.android.server.wifi; +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED; import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE; +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; import static android.app.AppOpsManager.MODE_ALLOWED; import static android.app.AppOpsManager.MODE_IGNORED; import static android.app.AppOpsManager.OPSTR_CHANGE_WIFI_STATE; @@ -50,6 +53,7 @@ import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.Resources; +import android.net.NetworkScoreManager; import android.net.util.MacAddressUtils; import android.net.wifi.EAPConstants; import android.net.wifi.ISuggestionConnectionStatusListener; @@ -128,6 +132,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { private @Mock Resources mResources; private @Mock AppOpsManager mAppOpsManager; private @Mock NotificationManager mNotificationManger; + private @Mock NetworkScoreManager mNetworkScoreManager; private @Mock PackageManager mPackageManager; private @Mock WifiPermissionsUtil mWifiPermissionsUtil; private @Mock WifiInjector mWifiInjector; @@ -206,6 +211,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager); when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)) .thenReturn(mNotificationManger); + when(mContext.getSystemService(NetworkScoreManager.class)).thenReturn(mNetworkScoreManager); when(mContext.getPackageManager()).thenReturn(mPackageManager); when(mContext.getSystemService(ActivityManager.class)).thenReturn(mActivityManager); when(mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) @@ -2587,6 +2593,50 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { validateUserApprovalNotification(TEST_APP_NAME_1); } + @Test + public void testAddNetworkSuggestions_activeFgScorer_doesNotRequestForApproval() { + // Fg app + when(mActivityManager.getPackageImportance(any())).thenReturn(IMPORTANCE_FOREGROUND); + // Active scorer + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true); + List<WifiNetworkSuggestion> networkSuggestionList = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE)); + + verifyZeroInteractions(mAlertDialog); + verifyZeroInteractions(mNotificationManger); + } + + @Test + public void testAddNetworkSuggestions_activeBgScorer_doesNotRequestForApproval() { + // Bg app + when(mActivityManager.getPackageImportance(any())).thenReturn(IMPORTANCE_SERVICE); + // Active scorer + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true); + List<WifiNetworkSuggestion> networkSuggestionList = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE)); + + verifyZeroInteractions(mAlertDialog); + verifyZeroInteractions(mNotificationManger); + } + /** * Verify handling of user clicking allow on the user approval notification when first time * add suggestions. @@ -2621,6 +2671,26 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { verifyNoMoreInteractions(mNotificationManger); } + @Test + public void getNetworkSuggestionsForScanDetail_exemptsActiveScorerFromUserApproval() { + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true); + List<WifiNetworkSuggestion> networkSuggestionList = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE)); + + mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( + createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); + + verifyZeroInteractions(mNotificationManger); + verifyZeroInteractions(mAlertDialog); + } + /** * Verify handling of user clicking Disallow on the user approval notification when first time * add suggestions. @@ -2917,6 +2987,30 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { assertNull(ewns); } + @Test + public void getNetworkSuggestionsForFqdn_activeScorer_doesNotRequestForUserApproval() { + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + PasspointConfiguration passpointConfiguration = + createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); + WifiConfiguration dummyConfiguration = createDummyWifiConfigurationForPasspoint(TEST_FQDN); + dummyConfiguration.FQDN = TEST_FQDN; + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion(dummyConfiguration, + passpointConfiguration, true, false, true, true); + List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); + dummyConfiguration.creatorUid = TEST_UID_1; + when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), + anyInt(), anyString(), eq(true), eq(true))).thenReturn(true); + assertEquals(mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE), WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); + + Set<ExtendedWifiNetworkSuggestion> ewns = + mWifiNetworkSuggestionsManager.getNetworkSuggestionsForFqdn(TEST_FQDN); + + assertEquals(1, ewns.size()); + verifyZeroInteractions(mAlertDialog); + verifyZeroInteractions(mNotificationManger); + } + /** * Verify return true when allow user manually connect and user approved the app */ @@ -3622,6 +3716,24 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { assertEquals(network1.SSID, pnoNetwork.get(0).SSID); } + @Test + public void getAllScanOptimizationSuggestionNetworks_returnsActiveScorerWithoutUserApproval() { + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + WifiConfiguration network = WifiConfigurationTestUtil.createOpenNetwork(); + WifiNetworkSuggestion networkSuggestion = + new WifiNetworkSuggestion(network, null, false, false, true, true); + List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager + .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); + mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(false, TEST_PACKAGE_1); + + List<WifiConfiguration> networks = + mWifiNetworkSuggestionsManager.getAllScanOptimizationSuggestionNetworks(); + + assertEquals(1, networks.size()); + } + /** * Verify if a suggestion is mostRecently connected, flag will be persist. */ |