diff options
2 files changed, 79 insertions, 16 deletions
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 98b8af13dd33..6cebdd64d8f9 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -460,7 +460,7 @@ public class NotificationManagerService extends SystemService { mRankingHelper.readXml(parser, forRestore); } // No non-system managed services are allowed on low ram devices - if (!ActivityManager.isLowRamDeviceStatic()) { + if (canUseManagedServices()) { if (mListeners.getConfig().xmlTag.equals(parser.getName())) { mListeners.readXml(parser); migratedManagedServices = true; @@ -548,12 +548,6 @@ public class NotificationManagerService extends SystemService { out.endDocument(); } - /** Use this to check if a package can post a notification or toast. */ - private boolean checkNotificationOp(String pkg, int uid) { - return mAppOps.checkOp(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) - == AppOpsManager.MODE_ALLOWED && !isPackageSuspendedForUser(pkg, uid); - } - private static final class ToastRecord { final int pid; @@ -2827,7 +2821,7 @@ public class NotificationManagerService extends SystemService { checkCallerIsSystemOrShell(); final long identity = Binder.clearCallingIdentity(); try { - if (!mActivityManager.isLowRamDevice()) { + if (canUseManagedServices()) { mConditionProviders.setPackageOrComponentEnabled( pkg, userId, true, granted); @@ -2923,7 +2917,7 @@ public class NotificationManagerService extends SystemService { checkCallerIsSystemOrShell(); final long identity = Binder.clearCallingIdentity(); try { - if (!mActivityManager.isLowRamDevice()) { + if (canUseManagedServices()) { mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), userId, false, granted); mListeners.setPackageOrComponentEnabled(listener.flattenToString(), @@ -2949,7 +2943,7 @@ public class NotificationManagerService extends SystemService { checkCallerIsSystemOrShell(); final long identity = Binder.clearCallingIdentity(); try { - if (!mActivityManager.isLowRamDevice()) { + if (canUseManagedServices()) { mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), userId, false, granted); mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), @@ -5438,6 +5432,11 @@ public class NotificationManagerService extends SystemService { } } + private boolean canUseManagedServices() { + return !mActivityManager.isLowRamDevice() + || mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_WATCH); + } + private class TrimCache { StatusBarNotification heavy; StatusBarNotification sbnClone; diff --git a/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java index 2452b44285d0..70e8a162b1cf 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -20,6 +20,7 @@ import static android.app.NotificationManager.IMPORTANCE_HIGH; import static android.app.NotificationManager.IMPORTANCE_LOW; import static android.app.NotificationManager.IMPORTANCE_NONE; import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; +import static android.content.pm.PackageManager.FEATURE_WATCH; import static android.content.pm.PackageManager.PERMISSION_DENIED; import static junit.framework.Assert.assertEquals; @@ -187,6 +188,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase { final LightsManager mockLightsManager = mock(LightsManager.class); when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); + when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); // write to a test file; the system file isn't readable from tests mFile = new File(mContext.getCacheDir(), "test.xml"); @@ -1472,9 +1474,9 @@ public class NotificationManagerServiceTest extends NotificationTestCase { mBinderService.setNotificationListenerAccessGranted(c, true); verify(mListeners, never()).setPackageOrComponentEnabled( - c.flattenToString(), 0, true, true); + anyString(), anyInt(), anyBoolean(), anyBoolean()); verify(mConditionProviders, never()).setPackageOrComponentEnabled( - c.flattenToString(), 0, false, true); + anyString(), anyInt(), anyBoolean(), anyBoolean()); verify(mAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @@ -1485,11 +1487,10 @@ public class NotificationManagerServiceTest extends NotificationTestCase { ComponentName c = ComponentName.unflattenFromString("package/Component"); mBinderService.setNotificationAssistantAccessGranted(c, true); - verify(mListeners, never()).setPackageOrComponentEnabled( - c.flattenToString(), 0, true, true); + anyString(), anyInt(), anyBoolean(), anyBoolean()); verify(mConditionProviders, never()).setPackageOrComponentEnabled( - c.flattenToString(), 0, false, true); + anyString(), anyInt(), anyBoolean(), anyBoolean()); verify(mAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } @@ -1501,9 +1502,72 @@ public class NotificationManagerServiceTest extends NotificationTestCase { mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); verify(mListeners, never()).setPackageOrComponentEnabled( - c.flattenToString(), 0, true, true); + anyString(), anyInt(), anyBoolean(), anyBoolean()); verify(mConditionProviders, never()).setPackageOrComponentEnabled( + anyString(), anyInt(), anyBoolean(), anyBoolean()); + verify(mAssistants, never()).setPackageOrComponentEnabled( + any(), anyInt(), anyBoolean(), anyBoolean()); + } + + @Test + public void testSetListenerAccess_doesNothingOnLowRam_exceptWatch() throws Exception { + 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; + } + } + + verify(mListeners, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), 0, true, true); + verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), 0, false, true); + verify(mAssistants, never()).setPackageOrComponentEnabled( + any(), anyInt(), anyBoolean(), anyBoolean()); + } + + @Test + public void testSetAssistantAccess_doesNothingOnLowRam_exceptWatch() throws Exception { + 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; + } + } + + verify(mListeners, never()).setPackageOrComponentEnabled( + anyString(), anyInt(), anyBoolean(), anyBoolean()); + verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( c.flattenToString(), 0, false, true); + verify(mAssistants, times(1)).setPackageOrComponentEnabled( + c.flattenToString(), 0, true, true); + } + + @Test + public void testSetDndAccess_doesNothingOnLowRam_exceptWatch() throws Exception { + 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; + } + } + + verify(mListeners, never()).setPackageOrComponentEnabled( + anyString(), anyInt(), anyBoolean(), anyBoolean()); + verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( + c.getPackageName(), 0, true, true); verify(mAssistants, never()).setPackageOrComponentEnabled( any(), anyInt(), anyBoolean(), anyBoolean()); } |