From 4afe264811d47023e26a3c9bce2d2d266ad83069 Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Wed, 1 May 2019 08:42:24 -0400 Subject: [PATCH] Mirror primary noti assistant to work profile They should stay in sync because the work profile one cannot be changed in Settings (and because work profile notifications are displayed along side primary profile notifications and should be treated the same way). Test: atest, manual confirmation of bugreport Fixes: 131246841 Change-Id: I74ec250ad4feea2c455c568654e5037118dbcab7 --- .../NotificationManagerService.java | 61 +++-- .../NotificationManagerServiceTest.java | 223 ++++++++++-------- 2 files changed, 162 insertions(+), 122 deletions(-) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 21a862a5ec6c1..4bbb6a65850ba 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -26,6 +26,7 @@ import static android.app.Notification.FLAG_ONGOING_EVENT; import static android.app.NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED; import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED; import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED; +import static android.app.NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED; import static android.app.NotificationManager.IMPORTANCE_LOW; import static android.app.NotificationManager.IMPORTANCE_MIN; import static android.app.NotificationManager.IMPORTANCE_NONE; @@ -3753,7 +3754,7 @@ public class NotificationManagerService extends SystemService { pkg, userId, true, granted); getContext().sendBroadcastAsUser(new Intent( - NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) + ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(pkg) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), UserHandle.of(userId), null); @@ -3913,7 +3914,7 @@ public class NotificationManagerService extends SystemService { userId, true, granted); getContext().sendBroadcastAsUser(new Intent( - NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) + ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(listener.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.of(userId), null); @@ -3929,7 +3930,9 @@ public class NotificationManagerService extends SystemService { public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, int userId, boolean granted) { checkCallerIsSystemOrSystemUiOrShell(); - mAssistants.setUserSet(userId, true); + for (UserInfo ui : mUm.getEnabledProfiles(userId)) { + mAssistants.setUserSet(ui.id, true); + } final long identity = Binder.clearCallingIdentity(); try { setNotificationAssistantAccessGrantedForUserInternal(assistant, userId, granted); @@ -4143,30 +4146,36 @@ public class NotificationManagerService extends SystemService { @VisibleForTesting protected void setNotificationAssistantAccessGrantedForUserInternal( - ComponentName assistant, int userId, boolean granted) { - if (assistant == null) { - ComponentName allowedAssistant = CollectionUtils.firstOrNull( - mAssistants.getAllowedComponents(userId)); - if (allowedAssistant != null) { - setNotificationAssistantAccessGrantedForUserInternal( - allowedAssistant, userId, false); + ComponentName assistant, int baseUserId, boolean granted) { + List users = mUm.getEnabledProfiles(baseUserId); + if (users != null) { + for (UserInfo user : users) { + int userId = user.id; + if (assistant == null) { + ComponentName allowedAssistant = CollectionUtils.firstOrNull( + mAssistants.getAllowedComponents(userId)); + if (allowedAssistant != null) { + setNotificationAssistantAccessGrantedForUserInternal( + allowedAssistant, userId, false); + } + continue; + } + if (!granted || mAllowedManagedServicePackages.test(assistant.getPackageName(), + userId, mAssistants.getRequiredPermission())) { + mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), + userId, false, granted); + mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), + userId, true, granted); + + getContext().sendBroadcastAsUser( + new Intent(ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) + .setPackage(assistant.getPackageName()) + .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), + UserHandle.of(userId), null); + + handleSavePolicyFile(); + } } - return; - } - if (!granted || mAllowedManagedServicePackages.test(assistant.getPackageName(), userId, - mAssistants.getRequiredPermission())) { - mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), - userId, false, granted); - mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), - userId, true, granted); - - getContext().sendBroadcastAsUser(new Intent( - NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) - .setPackage(assistant.getPackageName()) - .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), - UserHandle.of(userId), null); - - handleSavePolicyFile(); } } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index cbca087872d96..3ce1317829f7e 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -62,6 +62,7 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.reset; @@ -96,6 +97,7 @@ import android.content.pm.ApplicationInfo; import android.content.pm.IPackageManager; import android.content.pm.PackageManager; import android.content.pm.ParceledListSlice; +import android.content.pm.UserInfo; import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.Icon; @@ -327,6 +329,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { LocalServices.removeServiceForTest(WindowManagerInternal.class); LocalServices.addService(WindowManagerInternal.class, mWindowManagerInternal); + doNothing().when(mContext).sendBroadcastAsUser(any(), any(), any()); + mService = new TestableNotificationManagerService(mContext); // Use this testable looper. @@ -375,20 +379,16 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { when(mAssistants.isAdjustmentAllowed(anyString())).thenReturn(true); - try { - mService.init(mTestableLooper.getLooper(), - mPackageManager, mPackageManagerClient, mockLightsManager, - mListeners, mAssistants, mConditionProviders, - mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, - mGroupHelper, mAm, mAppUsageStats, - mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal, - mAppOpsManager, mUm); - mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + + mService.init(mTestableLooper.getLooper(), + mPackageManager, mPackageManagerClient, mockLightsManager, + mListeners, mAssistants, mConditionProviders, + mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, + mGroupHelper, mAm, mAppUsageStats, + mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal, + mAppOpsManager, mUm); + mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); + mService.setAudioManager(mAudioManager); // Tests call directly into the Binder. @@ -2080,14 +2080,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { public void testSetListenerAccessForUser() throws Exception { UserHandle user = UserHandle.of(10); ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationListenerAccessGrantedForUser( - c, user.getIdentifier(), true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + mBinderService.setNotificationListenerAccessGrantedForUser(c, user.getIdentifier(), true); + verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mListeners, times(1)).setPackageOrComponentEnabled( @@ -2101,15 +2095,14 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testSetAssistantAccessForUser() throws Exception { UserHandle user = UserHandle.of(10); + List uis = new ArrayList<>(); + UserInfo ui = new UserInfo(); + ui.id = 10; + uis.add(ui); ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationAssistantAccessGrantedForUser( - c, user.getIdentifier(), true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + when(mUm.getEnabledProfiles(10)).thenReturn(uis); + + mBinderService.setNotificationAssistantAccessGrantedForUser(c, user.getIdentifier(), true); verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mAssistants, times(1)).setPackageOrComponentEnabled( @@ -2150,14 +2143,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { public void testSetDndAccessForUser() throws Exception { UserHandle user = UserHandle.of(10); ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationPolicyAccessGrantedForUser( - c.getPackageName(), user.getIdentifier(), true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + mBinderService.setNotificationPolicyAccessGrantedForUser( + c.getPackageName(), user.getIdentifier(), true); verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( @@ -2171,13 +2158,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testSetListenerAccess() throws Exception { ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationListenerAccessGranted(c, true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + mBinderService.setNotificationListenerAccessGranted(c, true); verify(mListeners, times(1)).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); @@ -2189,14 +2170,14 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testSetAssistantAccess() throws Exception { + List uis = new ArrayList<>(); + UserInfo ui = new UserInfo(); + ui.id = 0; + uis.add(ui); + when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationAssistantAccessGranted(c, true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + + mBinderService.setNotificationAssistantAccessGranted(c, true); verify(mAssistants, times(1)).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); @@ -2206,20 +2187,45 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { any(), anyInt(), anyBoolean(), anyBoolean()); } + @Test + public void testSetAssistantAccess_multiProfile() throws Exception { + List uis = new ArrayList<>(); + UserInfo ui = new UserInfo(); + ui.id = 0; + uis.add(ui); + UserInfo ui10 = new UserInfo(); + ui10.id = 10; + uis.add(ui10); + when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); + ComponentName c = ComponentName.unflattenFromString("package/Component"); + + mBinderService.setNotificationAssistantAccessGranted(c, true); + + verify(mAssistants, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), 0, true, true); + verify(mAssistants, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), 10, true, true); + verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), 0, false, true); + verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), 10, false, true); + verify(mListeners, never()).setPackageOrComponentEnabled( + any(), anyInt(), anyBoolean(), anyBoolean()); + } + @Test public void testSetAssistantAccess_nullWithAllowedAssistant() throws Exception { ArrayList componentList = new ArrayList<>(); ComponentName c = ComponentName.unflattenFromString("package/Component"); componentList.add(c); when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); + List uis = new ArrayList<>(); + UserInfo ui = new UserInfo(); + ui.id = 0; + uis.add(ui); + when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); - try { - mBinderService.setNotificationAssistantAccessGranted(null, true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + mBinderService.setNotificationAssistantAccessGranted(null, true); verify(mAssistants, times(1)).setPackageOrComponentEnabled( c.flattenToString(), 0, true, false); @@ -2231,39 +2237,68 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testSetAssistantAccessForUser_nullWithAllowedAssistant() throws Exception { - UserHandle user = UserHandle.of(10); + List uis = new ArrayList<>(); + UserInfo ui = new UserInfo(); + ui.id = 10; + uis.add(ui); + UserHandle user = ui.getUserHandle(); ArrayList componentList = new ArrayList<>(); ComponentName c = ComponentName.unflattenFromString("package/Component"); componentList.add(c); when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); + when(mUm.getEnabledProfiles(10)).thenReturn(uis); - try { - mBinderService.setNotificationAssistantAccessGrantedForUser( - null, user.getIdentifier(), true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + mBinderService.setNotificationAssistantAccessGrantedForUser( + null, user.getIdentifier(), true); verify(mAssistants, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), true, false); + verify(mAssistants).setUserSet(10, true); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), false, false); verify(mListeners, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } + @Test + public void testSetAssistantAccessForUser_workProfile_nullWithAllowedAssistant() + throws Exception { + List uis = new ArrayList<>(); + UserInfo ui = new UserInfo(); + ui.id = 0; + uis.add(ui); + UserInfo ui10 = new UserInfo(); + ui10.id = 10; + uis.add(ui10); + UserHandle user = ui.getUserHandle(); + ArrayList componentList = new ArrayList<>(); + ComponentName c = ComponentName.unflattenFromString("package/Component"); + componentList.add(c); + when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); + when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); + + mBinderService.setNotificationAssistantAccessGrantedForUser( + null, user.getIdentifier(), true); + + verify(mAssistants, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), user.getIdentifier(), true, false); + verify(mAssistants, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), ui10.id, true, false); + verify(mAssistants).setUserSet(0, true); + verify(mAssistants).setUserSet(10, true); + verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), user.getIdentifier(), false, false); + verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), ui10.id, false, false); + verify(mListeners, never()).setPackageOrComponentEnabled( + any(), anyInt(), anyBoolean(), anyBoolean()); + } + @Test public void testSetDndAccess() throws Exception { ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + + mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.getPackageName(), 0, true, true); @@ -2291,6 +2326,12 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception { when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); + List uis = new ArrayList<>(); + UserInfo ui = new UserInfo(); + ui.id = 0; + uis.add(ui); + when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); + mBinderService.setNotificationAssistantAccessGranted(c, true); verify(mListeners, never()).setPackageOrComponentEnabled( @@ -2320,13 +2361,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationListenerAccessGranted(c, true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + + mBinderService.setNotificationListenerAccessGranted(c, true); verify(mListeners, times(1)).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); @@ -2341,13 +2377,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationAssistantAccessGranted(c, true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + List uis = new ArrayList<>(); + UserInfo ui = new UserInfo(); + ui.id = 0; + uis.add(ui); + when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); + + mBinderService.setNotificationAssistantAccessGranted(c, true); verify(mListeners, never()).setPackageOrComponentEnabled( anyString(), anyInt(), anyBoolean(), anyBoolean()); @@ -2362,13 +2398,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); - try { - mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); - } catch (SecurityException e) { - if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { - throw e; - } - } + + mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); verify(mListeners, never()).setPackageOrComponentEnabled( anyString(), anyInt(), anyBoolean(), anyBoolean());