Loading services/core/java/com/android/server/notification/NotificationManagerService.java +48 −33 Original line number Diff line number Diff line Loading @@ -265,6 +265,7 @@ public class NotificationManagerService extends SystemService { private static final String EXTRA_KEY = "key"; private IActivityManager mAm; private ActivityManager mActivityManager; private IPackageManager mPackageManager; private PackageManager mPackageManagerClient; AudioManager mAudioManager; Loading Loading @@ -450,10 +451,13 @@ public class NotificationManagerService extends SystemService { while (parser.next() != END_DOCUMENT) { mZenModeHelper.readXml(parser, forRestore); mRankingHelper.readXml(parser, forRestore); // No non-system managed services are allowed on low ram devices if (!ActivityManager.isLowRamDeviceStatic()) { saveXml |= mListeners.readXml(parser); saveXml |= mNotificationAssistants.readXml(parser); saveXml |= mConditionProviders.readXml(parser); } } if (saveXml) { savePolicyFile(); Loading Loading @@ -1131,7 +1135,8 @@ public class NotificationManagerService extends SystemService { LightsManager lightsManager, NotificationListeners notificationListeners, NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, NotificationUsageStats usageStats, AtomicFile policyFile) { NotificationUsageStats usageStats, AtomicFile policyFile, ActivityManager activityManager) { Resources resources = getContext().getResources(); mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, Loading @@ -1145,6 +1150,7 @@ public class NotificationManagerService extends SystemService { mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); mCompanionManager = companionManager; mActivityManager = activityManager; mHandler = new WorkerHandler(looper); mRankingThread.start(); Loading Loading @@ -1330,7 +1336,8 @@ public class NotificationManagerService extends SystemService { new NotificationAssistants(AppGlobals.getPackageManager()), new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), null, snoozeHelper, new NotificationUsageStats(getContext()), new AtomicFile(new File(systemDir, "notification_policy.xml"))); new AtomicFile(new File(systemDir, "notification_policy.xml")), (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE)); publishBinderService(Context.NOTIFICATION_SERVICE, mService); publishLocalService(NotificationManagerInternal.class, mInternalService); } Loading Loading @@ -2689,17 +2696,19 @@ public class NotificationManagerService extends SystemService { public void setNotificationPolicyAccessGranted(String pkg, boolean granted) throws RemoteException { checkCallerIsSystemOrShell(); if (!mActivityManager.isLowRamDevice()) { mConditionProviders.setPackageOrComponentEnabled( pkg, getCallingUserHandle().getIdentifier(), true, granted); getContext().sendBroadcastAsUser(new Intent(NotificationManager .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(pkg) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); savePolicyFile(); } } @Override public Policy getNotificationPolicy(String pkg) { Loading Loading @@ -2779,36 +2788,42 @@ public class NotificationManagerService extends SystemService { boolean granted) throws RemoteException { Preconditions.checkNotNull(listener); enforceSystemOrSystemUI("grant notification listener access"); if (!mActivityManager.isLowRamDevice()) { mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), userId, false, granted); mListeners.setPackageOrComponentEnabled(listener.flattenToString(), userId, true, granted); getContext().sendBroadcastAsUser(new Intent(NotificationManager .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(listener.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); savePolicyFile(); } } @Override public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, int userId, boolean granted) throws RemoteException { Preconditions.checkNotNull(assistant); enforceSystemOrSystemUI("grant notification assistant access"); if (!mActivityManager.isLowRamDevice()) { mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), userId, false, granted); mNotificationAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), userId, true, granted); getContext().sendBroadcastAsUser(new Intent(NotificationManager .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(assistant.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); savePolicyFile(); } } @Override public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, Loading services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +47 −1 Original line number Diff line number Diff line Loading @@ -37,6 +37,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.app.INotificationManager; import android.app.Notification; import android.app.NotificationChannel; Loading Loading @@ -106,6 +107,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase { private NotificationUsageStats mUsageStats; @Mock private AudioManager mAudioManager; @Mock ActivityManager mActivityManager; private NotificationChannel mTestNotificationChannel = new NotificationChannel( TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT); @Mock Loading Loading @@ -172,7 +175,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase { mNotificationManagerService.init(mTestableLooper.getLooper(), mPackageManager, mPackageManagerClient, mockLightsManager, mNotificationListeners, mNotificationAssistants, mConditionProviders, mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile); mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager); } catch (SecurityException e) { if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { throw e; Loading Loading @@ -1040,4 +1043,47 @@ public class NotificationManagerServiceTest extends NotificationTestCase { verify(mNotificationListeners, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test public void testSetListenerAccess_doesNothingOnLowRam() throws Exception { when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); mBinderService.setNotificationListenerAccessGranted(c, true); verify(mNotificationListeners, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); verify(mConditionProviders, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, false, true); verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception { when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); mBinderService.setNotificationAssistantAccessGranted(c, true); verify(mNotificationListeners, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); verify(mConditionProviders, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, false, true); verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test public void testSetDndAccess_doesNothingOnLowRam() throws Exception { when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); verify(mNotificationListeners, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); verify(mConditionProviders, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, false, true); verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } } Loading
services/core/java/com/android/server/notification/NotificationManagerService.java +48 −33 Original line number Diff line number Diff line Loading @@ -265,6 +265,7 @@ public class NotificationManagerService extends SystemService { private static final String EXTRA_KEY = "key"; private IActivityManager mAm; private ActivityManager mActivityManager; private IPackageManager mPackageManager; private PackageManager mPackageManagerClient; AudioManager mAudioManager; Loading Loading @@ -450,10 +451,13 @@ public class NotificationManagerService extends SystemService { while (parser.next() != END_DOCUMENT) { mZenModeHelper.readXml(parser, forRestore); mRankingHelper.readXml(parser, forRestore); // No non-system managed services are allowed on low ram devices if (!ActivityManager.isLowRamDeviceStatic()) { saveXml |= mListeners.readXml(parser); saveXml |= mNotificationAssistants.readXml(parser); saveXml |= mConditionProviders.readXml(parser); } } if (saveXml) { savePolicyFile(); Loading Loading @@ -1131,7 +1135,8 @@ public class NotificationManagerService extends SystemService { LightsManager lightsManager, NotificationListeners notificationListeners, NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, NotificationUsageStats usageStats, AtomicFile policyFile) { NotificationUsageStats usageStats, AtomicFile policyFile, ActivityManager activityManager) { Resources resources = getContext().getResources(); mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, Loading @@ -1145,6 +1150,7 @@ public class NotificationManagerService extends SystemService { mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); mCompanionManager = companionManager; mActivityManager = activityManager; mHandler = new WorkerHandler(looper); mRankingThread.start(); Loading Loading @@ -1330,7 +1336,8 @@ public class NotificationManagerService extends SystemService { new NotificationAssistants(AppGlobals.getPackageManager()), new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), null, snoozeHelper, new NotificationUsageStats(getContext()), new AtomicFile(new File(systemDir, "notification_policy.xml"))); new AtomicFile(new File(systemDir, "notification_policy.xml")), (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE)); publishBinderService(Context.NOTIFICATION_SERVICE, mService); publishLocalService(NotificationManagerInternal.class, mInternalService); } Loading Loading @@ -2689,17 +2696,19 @@ public class NotificationManagerService extends SystemService { public void setNotificationPolicyAccessGranted(String pkg, boolean granted) throws RemoteException { checkCallerIsSystemOrShell(); if (!mActivityManager.isLowRamDevice()) { mConditionProviders.setPackageOrComponentEnabled( pkg, getCallingUserHandle().getIdentifier(), true, granted); getContext().sendBroadcastAsUser(new Intent(NotificationManager .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(pkg) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); savePolicyFile(); } } @Override public Policy getNotificationPolicy(String pkg) { Loading Loading @@ -2779,36 +2788,42 @@ public class NotificationManagerService extends SystemService { boolean granted) throws RemoteException { Preconditions.checkNotNull(listener); enforceSystemOrSystemUI("grant notification listener access"); if (!mActivityManager.isLowRamDevice()) { mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), userId, false, granted); mListeners.setPackageOrComponentEnabled(listener.flattenToString(), userId, true, granted); getContext().sendBroadcastAsUser(new Intent(NotificationManager .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(listener.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); savePolicyFile(); } } @Override public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, int userId, boolean granted) throws RemoteException { Preconditions.checkNotNull(assistant); enforceSystemOrSystemUI("grant notification assistant access"); if (!mActivityManager.isLowRamDevice()) { mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), userId, false, granted); mNotificationAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), userId, true, granted); getContext().sendBroadcastAsUser(new Intent(NotificationManager .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) getContext().sendBroadcastAsUser(new Intent( NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) .setPackage(assistant.getPackageName()) .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), getCallingUserHandle(), null); savePolicyFile(); } } @Override public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, Loading
services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +47 −1 Original line number Diff line number Diff line Loading @@ -37,6 +37,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.app.INotificationManager; import android.app.Notification; import android.app.NotificationChannel; Loading Loading @@ -106,6 +107,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase { private NotificationUsageStats mUsageStats; @Mock private AudioManager mAudioManager; @Mock ActivityManager mActivityManager; private NotificationChannel mTestNotificationChannel = new NotificationChannel( TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT); @Mock Loading Loading @@ -172,7 +175,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase { mNotificationManagerService.init(mTestableLooper.getLooper(), mPackageManager, mPackageManagerClient, mockLightsManager, mNotificationListeners, mNotificationAssistants, mConditionProviders, mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile); mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager); } catch (SecurityException e) { if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { throw e; Loading Loading @@ -1040,4 +1043,47 @@ public class NotificationManagerServiceTest extends NotificationTestCase { verify(mNotificationListeners, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test public void testSetListenerAccess_doesNothingOnLowRam() throws Exception { when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); mBinderService.setNotificationListenerAccessGranted(c, true); verify(mNotificationListeners, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); verify(mConditionProviders, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, false, true); verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception { when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); mBinderService.setNotificationAssistantAccessGranted(c, true); verify(mNotificationListeners, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); verify(mConditionProviders, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, false, true); verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @Test public void testSetDndAccess_doesNothingOnLowRam() throws Exception { when(mActivityManager.isLowRamDevice()).thenReturn(true); ComponentName c = ComponentName.unflattenFromString("package/Component"); mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); verify(mNotificationListeners, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, true, true); verify(mConditionProviders, never()).setPackageOrComponentEnabled( c.flattenToString(), 0, false, true); verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } }