From dc30605b09e3fa6e4753a2f5388bdc3f2f31914a Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Wed, 18 Apr 2018 11:40:16 -0700 Subject: Revert "Revert "WifiServiceImpl: Fix package removal broadcast handling"" Reland of ag/3849997 Bug: 77588735 Bug: 77924407 Test: Unit tests This reverts commit 4b41c8af276b307a10bb5751596fda11079070bb. Change-Id: I7363c937a6d364917c13159004bb8b0b14c09803 --- .../com/android/server/wifi/WifiServiceImpl.java | 44 +++++------- .../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 1a5e0e27d..cb3628b98 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)) { @@ -2354,6 +2353,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); @@ -2419,6 +2421,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); @@ -2429,39 +2432,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 136d029d3..597db6965 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"; @@ -2594,6 +2597,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 { @Override -- cgit v1.2.3