Loading core/java/android/app/INotificationManager.aidl +1 −0 Original line number Original line Diff line number Diff line Loading @@ -138,6 +138,7 @@ interface INotificationManager void setNotificationPolicy(String pkg, in NotificationManager.Policy policy); void setNotificationPolicy(String pkg, in NotificationManager.Policy policy); boolean isNotificationPolicyAccessGrantedForPackage(String pkg); boolean isNotificationPolicyAccessGrantedForPackage(String pkg); void setNotificationPolicyAccessGranted(String pkg, boolean granted); void setNotificationPolicyAccessGranted(String pkg, boolean granted); void setNotificationPolicyAccessGrantedForUser(String pkg, int userId, boolean granted); AutomaticZenRule getAutomaticZenRule(String id); AutomaticZenRule getAutomaticZenRule(String id); List<ZenModeConfig.ZenRule> getZenRules(); List<ZenModeConfig.ZenRule> getZenRules(); String addAutomaticZenRule(in AutomaticZenRule automaticZenRule); String addAutomaticZenRule(in AutomaticZenRule automaticZenRule); Loading services/core/java/com/android/server/notification/NotificationManagerService.java +12 −8 Original line number Original line Diff line number Diff line Loading @@ -1226,7 +1226,6 @@ public class NotificationManagerService extends SystemService { mAccessibilityManager = am; mAccessibilityManager = am; } } // TODO: All tests should use this init instead of the one-off setters above. // TODO: All tests should use this init instead of the one-off setters above. @VisibleForTesting @VisibleForTesting void init(Looper looper, IPackageManager packageManager, void init(Looper looper, IPackageManager packageManager, Loading Loading @@ -2818,19 +2817,25 @@ public class NotificationManagerService extends SystemService { @Override @Override public void setNotificationPolicyAccessGranted(String pkg, boolean granted) public void setNotificationPolicyAccessGranted(String pkg, boolean granted) throws RemoteException { throws RemoteException { setNotificationPolicyAccessGrantedForUser( pkg, getCallingUserHandle().getIdentifier(), granted); } @Override public void setNotificationPolicyAccessGrantedForUser( String pkg, int userId, boolean granted) { checkCallerIsSystemOrShell(); checkCallerIsSystemOrShell(); final long identity = Binder.clearCallingIdentity(); final long identity = Binder.clearCallingIdentity(); try { try { if (!mActivityManager.isLowRamDevice()) { if (!mActivityManager.isLowRamDevice()) { mConditionProviders.setPackageOrComponentEnabled( mConditionProviders.setPackageOrComponentEnabled( pkg, getCallingUserHandle().getIdentifier(), true, granted); pkg, userId, true, granted); getContext().sendBroadcastAsUser(new Intent( getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(pkg) .setPackage(pkg) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); UserHandle.of(userId), null); savePolicyFile(); savePolicyFile(); } } } finally { } finally { Loading Loading @@ -2926,10 +2931,9 @@ public class NotificationManagerService extends SystemService { getContext().sendBroadcastAsUser(new Intent( getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(listener.getPackageName()) .setPackage(listener.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); UserHandle.of(userId), null); savePolicyFile(); savePolicyFile(); } } Loading @@ -2955,7 +2959,7 @@ public class NotificationManagerService extends SystemService { NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(assistant.getPackageName()) .setPackage(assistant.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); UserHandle.of(userId), null); savePolicyFile(); savePolicyFile(); } } Loading services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +72 −1 Original line number Original line Diff line number Diff line Loading @@ -24,6 +24,7 @@ import static android.content.pm.PackageManager.PERMISSION_DENIED; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static junit.framework.Assert.fail; Loading Loading @@ -72,6 +73,7 @@ import android.service.notification.NotificationStats; import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification; import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest; import android.testing.AndroidTestingRunner; import android.testing.AndroidTestingRunner; import android.testing.TestableContext; import android.testing.TestableLooper; import android.testing.TestableLooper; import android.testing.TestableLooper.RunWithLooper; import android.testing.TestableLooper.RunWithLooper; import android.util.ArrayMap; import android.util.ArrayMap; Loading Loading @@ -113,7 +115,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase { private IPackageManager mPackageManager; private IPackageManager mPackageManager; @Mock @Mock private PackageManager mPackageManagerClient; private PackageManager mPackageManagerClient; private Context mContext = getContext(); private TestableContext mContext = spy(getContext()); private final String PKG = mContext.getPackageName(); private final String PKG = mContext.getPackageName(); private TestableLooper mTestableLooper; private TestableLooper mTestableLooper; @Mock @Mock Loading Loading @@ -174,12 +176,14 @@ public class NotificationManagerServiceTest extends NotificationTestCase { mTestableLooper = TestableLooper.get(this); mTestableLooper = TestableLooper.get(this); mHandler = mService.new WorkerHandler(mTestableLooper.getLooper()); mHandler = mService.new WorkerHandler(mTestableLooper.getLooper()); // MockPackageManager - default returns ApplicationInfo with matching calling UID // MockPackageManager - default returns ApplicationInfo with matching calling UID mContext.setMockPackageManager(mPackageManagerClient); final ApplicationInfo applicationInfo = new ApplicationInfo(); final ApplicationInfo applicationInfo = new ApplicationInfo(); applicationInfo.uid = mUid; applicationInfo.uid = mUid; when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) .thenReturn(applicationInfo); .thenReturn(applicationInfo); when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) .thenReturn(applicationInfo); .thenReturn(applicationInfo); when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid); final LightsManager mockLightsManager = mock(LightsManager.class); final LightsManager mockLightsManager = mock(LightsManager.class); when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); Loading Loading @@ -227,6 +231,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase { mBinderService.createNotificationChannels( mBinderService.createNotificationChannels( PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); assertNotNull(mBinderService.getNotificationChannel(PKG, TEST_CHANNEL_ID)); } } @After @After Loading Loading @@ -1337,6 +1342,72 @@ public class NotificationManagerServiceTest extends NotificationTestCase { verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); } } @Test 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; } } verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mListeners, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), true, true); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), false, true); verify(mAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test public void testSetAssistantAccessForUser() throws Exception { UserHandle user = UserHandle.of(10); 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; } } verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mAssistants, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), true, true); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), false, true); verify(mListeners, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test 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; } } verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.getPackageName(), user.getIdentifier(), true, true); verify(mAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); verify(mListeners, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test @Test public void testSetListenerAccess() throws Exception { public void testSetListenerAccess() throws Exception { ComponentName c = ComponentName.unflattenFromString("package/Component"); ComponentName c = ComponentName.unflattenFromString("package/Component"); Loading Loading
core/java/android/app/INotificationManager.aidl +1 −0 Original line number Original line Diff line number Diff line Loading @@ -138,6 +138,7 @@ interface INotificationManager void setNotificationPolicy(String pkg, in NotificationManager.Policy policy); void setNotificationPolicy(String pkg, in NotificationManager.Policy policy); boolean isNotificationPolicyAccessGrantedForPackage(String pkg); boolean isNotificationPolicyAccessGrantedForPackage(String pkg); void setNotificationPolicyAccessGranted(String pkg, boolean granted); void setNotificationPolicyAccessGranted(String pkg, boolean granted); void setNotificationPolicyAccessGrantedForUser(String pkg, int userId, boolean granted); AutomaticZenRule getAutomaticZenRule(String id); AutomaticZenRule getAutomaticZenRule(String id); List<ZenModeConfig.ZenRule> getZenRules(); List<ZenModeConfig.ZenRule> getZenRules(); String addAutomaticZenRule(in AutomaticZenRule automaticZenRule); String addAutomaticZenRule(in AutomaticZenRule automaticZenRule); Loading
services/core/java/com/android/server/notification/NotificationManagerService.java +12 −8 Original line number Original line Diff line number Diff line Loading @@ -1226,7 +1226,6 @@ public class NotificationManagerService extends SystemService { mAccessibilityManager = am; mAccessibilityManager = am; } } // TODO: All tests should use this init instead of the one-off setters above. // TODO: All tests should use this init instead of the one-off setters above. @VisibleForTesting @VisibleForTesting void init(Looper looper, IPackageManager packageManager, void init(Looper looper, IPackageManager packageManager, Loading Loading @@ -2818,19 +2817,25 @@ public class NotificationManagerService extends SystemService { @Override @Override public void setNotificationPolicyAccessGranted(String pkg, boolean granted) public void setNotificationPolicyAccessGranted(String pkg, boolean granted) throws RemoteException { throws RemoteException { setNotificationPolicyAccessGrantedForUser( pkg, getCallingUserHandle().getIdentifier(), granted); } @Override public void setNotificationPolicyAccessGrantedForUser( String pkg, int userId, boolean granted) { checkCallerIsSystemOrShell(); checkCallerIsSystemOrShell(); final long identity = Binder.clearCallingIdentity(); final long identity = Binder.clearCallingIdentity(); try { try { if (!mActivityManager.isLowRamDevice()) { if (!mActivityManager.isLowRamDevice()) { mConditionProviders.setPackageOrComponentEnabled( mConditionProviders.setPackageOrComponentEnabled( pkg, getCallingUserHandle().getIdentifier(), true, granted); pkg, userId, true, granted); getContext().sendBroadcastAsUser(new Intent( getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(pkg) .setPackage(pkg) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); UserHandle.of(userId), null); savePolicyFile(); savePolicyFile(); } } } finally { } finally { Loading Loading @@ -2926,10 +2931,9 @@ public class NotificationManagerService extends SystemService { getContext().sendBroadcastAsUser(new Intent( getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(listener.getPackageName()) .setPackage(listener.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); UserHandle.of(userId), null); savePolicyFile(); savePolicyFile(); } } Loading @@ -2955,7 +2959,7 @@ public class NotificationManagerService extends SystemService { NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(assistant.getPackageName()) .setPackage(assistant.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); UserHandle.of(userId), null); savePolicyFile(); savePolicyFile(); } } Loading
services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +72 −1 Original line number Original line Diff line number Diff line Loading @@ -24,6 +24,7 @@ import static android.content.pm.PackageManager.PERMISSION_DENIED; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static junit.framework.Assert.fail; Loading Loading @@ -72,6 +73,7 @@ import android.service.notification.NotificationStats; import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification; import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest; import android.testing.AndroidTestingRunner; import android.testing.AndroidTestingRunner; import android.testing.TestableContext; import android.testing.TestableLooper; import android.testing.TestableLooper; import android.testing.TestableLooper.RunWithLooper; import android.testing.TestableLooper.RunWithLooper; import android.util.ArrayMap; import android.util.ArrayMap; Loading Loading @@ -113,7 +115,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase { private IPackageManager mPackageManager; private IPackageManager mPackageManager; @Mock @Mock private PackageManager mPackageManagerClient; private PackageManager mPackageManagerClient; private Context mContext = getContext(); private TestableContext mContext = spy(getContext()); private final String PKG = mContext.getPackageName(); private final String PKG = mContext.getPackageName(); private TestableLooper mTestableLooper; private TestableLooper mTestableLooper; @Mock @Mock Loading Loading @@ -174,12 +176,14 @@ public class NotificationManagerServiceTest extends NotificationTestCase { mTestableLooper = TestableLooper.get(this); mTestableLooper = TestableLooper.get(this); mHandler = mService.new WorkerHandler(mTestableLooper.getLooper()); mHandler = mService.new WorkerHandler(mTestableLooper.getLooper()); // MockPackageManager - default returns ApplicationInfo with matching calling UID // MockPackageManager - default returns ApplicationInfo with matching calling UID mContext.setMockPackageManager(mPackageManagerClient); final ApplicationInfo applicationInfo = new ApplicationInfo(); final ApplicationInfo applicationInfo = new ApplicationInfo(); applicationInfo.uid = mUid; applicationInfo.uid = mUid; when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) .thenReturn(applicationInfo); .thenReturn(applicationInfo); when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) .thenReturn(applicationInfo); .thenReturn(applicationInfo); when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid); final LightsManager mockLightsManager = mock(LightsManager.class); final LightsManager mockLightsManager = mock(LightsManager.class); when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); Loading Loading @@ -227,6 +231,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase { mBinderService.createNotificationChannels( mBinderService.createNotificationChannels( PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); assertNotNull(mBinderService.getNotificationChannel(PKG, TEST_CHANNEL_ID)); } } @After @After Loading Loading @@ -1337,6 +1342,72 @@ public class NotificationManagerServiceTest extends NotificationTestCase { verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); } } @Test 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; } } verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mListeners, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), true, true); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), false, true); verify(mAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test public void testSetAssistantAccessForUser() throws Exception { UserHandle user = UserHandle.of(10); 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; } } verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mAssistants, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), true, true); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.flattenToString(), user.getIdentifier(), false, true); verify(mListeners, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test 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; } } verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.getPackageName(), user.getIdentifier(), true, true); verify(mAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); verify(mListeners, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test @Test public void testSetListenerAccess() throws Exception { public void testSetListenerAccess() throws Exception { ComponentName c = ComponentName.unflattenFromString("package/Component"); ComponentName c = ComponentName.unflattenFromString("package/Component"); Loading