diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2018-04-26 21:27:24 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-04-26 21:27:24 +0000 |
commit | a0daf40587b185e4f65549964c465ba7489ad753 (patch) | |
tree | c8850cc59fa128f1fcb1f24b00a8e8df0c24518e | |
parent | 5d028baa92b741ffcf82363c6983c208482d3f29 (diff) | |
parent | dc30605b09e3fa6e4753a2f5388bdc3f2f31914a (diff) |
Merge "Revert "Revert "WifiServiceImpl: Fix package removal broadcast handling""" into pi-dev
-rw-r--r-- | service/java/com/android/server/wifi/WifiServiceImpl.java | 44 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java | 84 |
2 files changed, 101 insertions, 27 deletions
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index 61daadcd1..541fb28f4 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -569,7 +569,6 @@ public class WifiServiceImpl extends IWifiManager.Stub { // can result in race conditions when apps toggle wifi in the background // without active user involvement. Always receive broadcasts. registerForBroadcasts(); - registerForPackageOrUserRemoval(); mInIdleMode = mPowerManager.isDeviceIdleMode(); if (!mWifiStateMachine.syncInitialize(mWifiStateMachineChannel)) { @@ -2358,6 +2357,9 @@ public class WifiServiceImpl extends IWifiManager.Stub { String action = intent.getAction(); if (action.equals(Intent.ACTION_USER_PRESENT)) { mWifiController.sendMessage(CMD_USER_PRESENT); + } else if (action.equals(Intent.ACTION_USER_REMOVED)) { + int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); + mWifiStateMachine.removeUserConfigs(userHandle); } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.STATE_DISCONNECTED); @@ -2423,6 +2425,7 @@ public class WifiServiceImpl extends IWifiManager.Stub { private void registerForBroadcasts() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_USER_PRESENT); + intentFilter.addAction(Intent.ACTION_USER_REMOVED); intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); @@ -2433,39 +2436,26 @@ public class WifiServiceImpl extends IWifiManager.Stub { if (trackEmergencyCallState) { intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED); } - mContext.registerReceiver(mReceiver, intentFilter); - } - private void registerForPackageOrUserRemoval() { - IntentFilter intentFilter = new IntentFilter(); - intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); - intentFilter.addAction(Intent.ACTION_USER_REMOVED); - mContext.registerReceiverAsUser(new BroadcastReceiver() { + intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED); + intentFilter.addDataScheme("package"); + mContext.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - switch (intent.getAction()) { - case Intent.ACTION_PACKAGE_REMOVED: { - if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { - return; - } - int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); - Uri uri = intent.getData(); - if (uid == -1 || uri == null) { - return; - } - String pkgName = uri.getSchemeSpecificPart(); - mWifiStateMachine.removeAppConfigs(pkgName, uid); - break; - } - case Intent.ACTION_USER_REMOVED: { - int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); - mWifiStateMachine.removeUserConfigs(userHandle); - break; + 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(); + mWifiStateMachine.removeAppConfigs(pkgName, uid); } } - }, UserHandle.ALL, intentFilter, null, null); + }, intentFilter); } @Override diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index 6280ae1da..4c633af59 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -59,10 +59,12 @@ import android.app.AppOpsManager; import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; +import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.Resources; +import android.net.Uri; import android.net.wifi.ISoftApCallback; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; @@ -133,6 +135,7 @@ public class WifiServiceImplTest { private static final int TEST_PID2 = 9876; private static final int TEST_UID = 1200000; private static final int OTHER_TEST_UID = 1300000; + private static final int TEST_USER_HANDLE = 13; private static final String WIFI_IFACE_NAME = "wlan0"; private static final String TEST_COUNTRY_CODE = "US"; @@ -2618,6 +2621,87 @@ public class WifiServiceImplTest { verify(mWifiStateMachine, never()).disconnectCommand(); } + @Test + public void testPackageRemovedBroadcastHandling() { + mWifiServiceImpl.checkAndStartWifi(); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat((IntentFilter filter) -> + filter.hasAction(Intent.ACTION_PACKAGE_FULLY_REMOVED))); + + int uid = TEST_UID; + String packageName = TEST_PACKAGE_NAME; + // Send the broadcast + Intent intent = new Intent(Intent.ACTION_PACKAGE_FULLY_REMOVED); + intent.putExtra(Intent.EXTRA_UID, uid); + intent.setData(Uri.fromParts("package", packageName, "")); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); + + verify(mWifiStateMachine).removeAppConfigs(packageName, uid); + } + + @Test + public void testPackageRemovedBroadcastHandlingWithNoUid() { + mWifiServiceImpl.checkAndStartWifi(); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat((IntentFilter filter) -> + filter.hasAction(Intent.ACTION_PACKAGE_FULLY_REMOVED))); + + String packageName = TEST_PACKAGE_NAME; + // Send the broadcast + Intent intent = new Intent(Intent.ACTION_PACKAGE_FULLY_REMOVED); + intent.setData(Uri.fromParts("package", packageName, "")); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); + + verify(mWifiStateMachine, never()).removeAppConfigs(anyString(), anyInt()); + } + + @Test + public void testPackageRemovedBroadcastHandlingWithNoPackageName() { + mWifiServiceImpl.checkAndStartWifi(); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat((IntentFilter filter) -> + filter.hasAction(Intent.ACTION_PACKAGE_FULLY_REMOVED))); + + int uid = TEST_UID; + // Send the broadcast + Intent intent = new Intent(Intent.ACTION_PACKAGE_FULLY_REMOVED); + intent.putExtra(Intent.EXTRA_UID, uid); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); + + verify(mWifiStateMachine, never()).removeAppConfigs(anyString(), anyInt()); + } + + @Test + public void testUserRemovedBroadcastHandling() { + mWifiServiceImpl.checkAndStartWifi(); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat((IntentFilter filter) -> + filter.hasAction(Intent.ACTION_USER_REMOVED))); + + int userHandle = TEST_USER_HANDLE; + // Send the broadcast + Intent intent = new Intent(Intent.ACTION_USER_REMOVED); + intent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); + + verify(mWifiStateMachine).removeUserConfigs(userHandle); + } + + @Test + public void testUserRemovedBroadcastHandlingWithWrongIntentAction() { + mWifiServiceImpl.checkAndStartWifi(); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat((IntentFilter filter) -> + filter.hasAction(Intent.ACTION_USER_REMOVED))); + + int userHandle = TEST_USER_HANDLE; + // Send the broadcast with wrong action + Intent intent = new Intent(Intent.ACTION_USER_FOREGROUND); + intent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); + + verify(mWifiStateMachine, never()).removeUserConfigs(userHandle); + } private class IdleModeIntentMatcher implements ArgumentMatcher<IntentFilter> { @Override |