diff options
author | Nate(Qiang) Jiang <qiangjiang@google.com> | 2020-02-26 09:43:51 -0800 |
---|---|---|
committer | Nate(Qiang) Jiang <qiangjiang@google.com> | 2020-02-26 18:07:18 -0800 |
commit | f771687761010c14f5187ea516d1c3f159eedead (patch) | |
tree | e6b0a82cffd9f7f6684208c8195dd658196a5f08 | |
parent | 861798ec8b5c8770bb883a101ceed513ef7954c4 (diff) |
Consider app disabled same as app is removed
When app is disabled, remove all related saved network, suggestion and
passpoint.
Bug: 150247605
Test: atest com.android.server.wifi
Change-Id: I274c148597cd27c7ea72ee60083b98519ca4236d
-rw-r--r-- | service/java/com/android/server/wifi/WifiServiceImpl.java | 65 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java | 75 |
2 files changed, 111 insertions, 29 deletions
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index acb060dfc..96a60f19b 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -40,6 +40,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ParceledListSlice; import android.content.pm.ResolveInfo; @@ -3051,36 +3052,50 @@ public class WifiServiceImpl extends BaseWifiService { private void registerForBroadcasts() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED); + intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); + intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); intentFilter.addDataScheme("package"); mContext.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - if (action.equals(Intent.ACTION_PACKAGE_FULLY_REMOVED)) { - int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); - Uri uri = intent.getData(); - if (uid == -1 || uri == null) { - return; - } - String pkgName = uri.getSchemeSpecificPart(); - - // Call the method in the main Wifi thread. - mWifiThreadRunner.post(() -> { - ApplicationInfo ai = new ApplicationInfo(); - ai.packageName = pkgName; - ai.uid = uid; - mWifiConfigManager.removeNetworksForApp(ai); - mScanRequestProxy.clearScanRequestTimestampsForApp(pkgName, uid); - - // Remove all suggestions from the package. - mWifiNetworkSuggestionsManager.removeApp(pkgName); - mClientModeImpl.removeNetworkRequestUserApprovedAccessPointsForApp(pkgName); - - // Remove all Passpoint profiles from package. - mWifiInjector.getPasspointManager().removePasspointProviderWithPackage( - pkgName); - }); + int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); + Uri uri = intent.getData(); + if (uid == -1 || uri == null) { + Log.e(TAG, "Uid or Uri is missing for action:" + intent.getAction()); + return; + } + String pkgName = uri.getSchemeSpecificPart(); + PackageManager pm = context.getPackageManager(); + PackageInfo packageInfo = null; + try { + packageInfo = pm.getPackageInfo(pkgName, 0); + } catch (PackageManager.NameNotFoundException e) { + Log.e(TAG, "Couldn't get PackageInfo for package:" + pkgName); + return; + } + // If package is not removed or disabled, just ignore. + if (packageInfo != null + && packageInfo.applicationInfo != null + && packageInfo.applicationInfo.enabled) { + return; } + Log.d(TAG, "Remove settings for package:" + pkgName); + // Call the method in the main Wifi thread. + mWifiThreadRunner.post(() -> { + ApplicationInfo ai = new ApplicationInfo(); + ai.packageName = pkgName; + ai.uid = uid; + mWifiConfigManager.removeNetworksForApp(ai); + mScanRequestProxy.clearScanRequestTimestampsForApp(pkgName, uid); + + // Remove all suggestions from the package. + mWifiNetworkSuggestionsManager.removeApp(pkgName); + mClientModeImpl.removeNetworkRequestUserApprovedAccessPointsForApp(pkgName); + + // Remove all Passpoint profiles from package. + mWifiInjector.getPasspointManager().removePasspointProviderWithPackage( + pkgName); + }); } }, intentFilter); } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index f1e515b9c..689efb289 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -88,6 +88,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ParceledListSlice; import android.content.res.Resources; @@ -308,6 +309,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Mock IWifiConnectedNetworkScorer mWifiConnectedNetworkScorer; @Mock WifiSettingsConfigStore mWifiSettingsConfigStore; @Mock WifiScanAlwaysAvailableSettingsCompatibility mScanAlwaysAvailableSettingsCompatibility; + @Mock PackageInfo mPackageInfo; WifiLog mLog = new LogcatLog(TAG); @@ -338,6 +340,7 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mContext.getPackageManager()).thenReturn(mPackageManager); when(mPackageManager.getApplicationInfoAsUser(any(), anyInt(), any())) .thenReturn(mApplicationInfo); + when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo); when(mWifiInjector.getWifiApConfigStore()).thenReturn(mWifiApConfigStore); doNothing().when(mFrameworkFacade).registerContentObserver(eq(mContext), any(), anyBoolean(), any()); @@ -3624,15 +3627,17 @@ public class WifiServiceImplTest extends WifiBaseTest { } @Test - public void testPackageRemovedBroadcastHandling() { + public void testPackageFullyRemovedBroadcastHandling() throws Exception { mWifiServiceImpl.checkAndStartWifi(); mLooper.dispatchAll(); verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), argThat((IntentFilter filter) -> - filter.hasAction(Intent.ACTION_PACKAGE_FULLY_REMOVED))); - + filter.hasAction(Intent.ACTION_PACKAGE_FULLY_REMOVED) + && filter.hasAction(Intent.ACTION_PACKAGE_REMOVED) + && filter.hasAction(Intent.ACTION_PACKAGE_CHANGED))); int uid = TEST_UID; String packageName = TEST_PACKAGE_NAME; + when(mPackageManager.getApplicationInfo(TEST_PACKAGE_NAME, 0)).thenReturn(null); // Send the broadcast Intent intent = new Intent(Intent.ACTION_PACKAGE_FULLY_REMOVED); intent.putExtra(Intent.EXTRA_UID, uid); @@ -3653,6 +3658,68 @@ public class WifiServiceImplTest extends WifiBaseTest { } @Test + public void testPackageRemovedBroadcastHandling() throws Exception { + mWifiServiceImpl.checkAndStartWifi(); + mLooper.dispatchAll(); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + argThat((IntentFilter filter) -> + filter.hasAction(Intent.ACTION_PACKAGE_FULLY_REMOVED) + && filter.hasAction(Intent.ACTION_PACKAGE_REMOVED) + && filter.hasAction(Intent.ACTION_PACKAGE_CHANGED))); + int uid = TEST_UID; + String packageName = TEST_PACKAGE_NAME; + // Send the broadcast + Intent intent = new Intent(Intent.ACTION_PACKAGE_REMOVED); + intent.putExtra(Intent.EXTRA_UID, uid); + intent.setData(Uri.fromParts("package", packageName, "")); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); + mLooper.dispatchAll(); + + ArgumentCaptor<ApplicationInfo> aiCaptor = ArgumentCaptor.forClass(ApplicationInfo.class); + verify(mWifiConfigManager).removeNetworksForApp(aiCaptor.capture()); + assertNotNull(aiCaptor.getValue()); + assertEquals(uid, aiCaptor.getValue().uid); + assertEquals(packageName, aiCaptor.getValue().packageName); + + verify(mScanRequestProxy).clearScanRequestTimestampsForApp(packageName, uid); + verify(mWifiNetworkSuggestionsManager).removeApp(packageName); + verify(mClientModeImpl).removeNetworkRequestUserApprovedAccessPointsForApp(packageName); + verify(mPasspointManager).removePasspointProviderWithPackage(packageName); + } + + @Test + public void testPackageDisableBroadcastHandling() throws Exception { + mWifiServiceImpl.checkAndStartWifi(); + mLooper.dispatchAll(); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + argThat((IntentFilter filter) -> + filter.hasAction(Intent.ACTION_PACKAGE_FULLY_REMOVED) + && filter.hasAction(Intent.ACTION_PACKAGE_REMOVED) + && filter.hasAction(Intent.ACTION_PACKAGE_CHANGED))); + int uid = TEST_UID; + String packageName = TEST_PACKAGE_NAME; + mPackageInfo.applicationInfo = mApplicationInfo; + mApplicationInfo.enabled = false; + // Send the broadcast + Intent intent = new Intent(Intent.ACTION_PACKAGE_CHANGED); + intent.putExtra(Intent.EXTRA_UID, uid); + intent.setData(Uri.fromParts("package", packageName, "")); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); + mLooper.dispatchAll(); + + ArgumentCaptor<ApplicationInfo> aiCaptor = ArgumentCaptor.forClass(ApplicationInfo.class); + verify(mWifiConfigManager).removeNetworksForApp(aiCaptor.capture()); + assertNotNull(aiCaptor.getValue()); + assertEquals(uid, aiCaptor.getValue().uid); + assertEquals(packageName, aiCaptor.getValue().packageName); + + verify(mScanRequestProxy).clearScanRequestTimestampsForApp(packageName, uid); + verify(mWifiNetworkSuggestionsManager).removeApp(packageName); + verify(mClientModeImpl).removeNetworkRequestUserApprovedAccessPointsForApp(packageName); + verify(mPasspointManager).removePasspointProviderWithPackage(packageName); + } + + @Test public void testPackageRemovedBroadcastHandlingWithNoUid() { mWifiServiceImpl.checkAndStartWifi(); mLooper.dispatchAll(); @@ -3662,7 +3729,7 @@ public class WifiServiceImplTest extends WifiBaseTest { String packageName = TEST_PACKAGE_NAME; // Send the broadcast - Intent intent = new Intent(Intent.ACTION_PACKAGE_FULLY_REMOVED); + Intent intent = new Intent(Intent.ACTION_PACKAGE_REMOVED); intent.setData(Uri.fromParts("package", packageName, "")); mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); |