Loading services/core/java/com/android/server/notification/NotificationManagerService.java +5 −2 Original line number Diff line number Diff line Loading @@ -330,6 +330,9 @@ public class NotificationManagerService extends SystemService { static final int MAX_PACKAGE_NOTIFICATIONS = 50; static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; // To limit bad UX of seeing a toast many seconds after if was triggered. static final int MAX_PACKAGE_TOASTS = 5; // message codes static final int MESSAGE_DURATION_REACHED = 2; // 3: removed to a different handler Loading Loading @@ -2949,8 +2952,8 @@ public class NotificationManagerService extends SystemService { final ToastRecord r = mToastQueue.get(i); if (r.pkg.equals(pkg)) { count++; if (count >= MAX_PACKAGE_NOTIFICATIONS) { Slog.e(TAG, "Package has already posted " + count if (count >= MAX_PACKAGE_TOASTS) { Slog.e(TAG, "Package has already queued " + count + " toasts. Not showing more. Package=" + pkg); return; } Loading services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +26 −0 Original line number Diff line number Diff line Loading @@ -5155,6 +5155,32 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(1, mService.mToastQueue.size()); } @Test public void testLimitNumberOfQueuedToastsFromPackage() throws Exception { final String testPackage = "testPackageName"; assertEquals(0, mService.mToastQueue.size()); mService.isSystemUid = false; // package is not suspended when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) .thenReturn(false); INotificationManager nmService = (INotificationManager) mService.mService; // Trying to quickly enqueue more toast than allowed. for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_TOASTS + 1; i++) { nmService.enqueueTextToast( testPackage, new Binder(), "Text", /* duration */ 2000, /* displayId */ 0, /* callback */ null); } // Only allowed number enqueued, rest ignored. assertEquals(NotificationManagerService.MAX_PACKAGE_TOASTS, mService.mToastQueue.size()); } private void setAppInForegroundForToasts(int uid, boolean inForeground) { int importance = (inForeground) ? IMPORTANCE_FOREGROUND : IMPORTANCE_NONE; when(mActivityManager.getUidImportance(mUid)).thenReturn(importance); Loading Loading
services/core/java/com/android/server/notification/NotificationManagerService.java +5 −2 Original line number Diff line number Diff line Loading @@ -330,6 +330,9 @@ public class NotificationManagerService extends SystemService { static final int MAX_PACKAGE_NOTIFICATIONS = 50; static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; // To limit bad UX of seeing a toast many seconds after if was triggered. static final int MAX_PACKAGE_TOASTS = 5; // message codes static final int MESSAGE_DURATION_REACHED = 2; // 3: removed to a different handler Loading Loading @@ -2949,8 +2952,8 @@ public class NotificationManagerService extends SystemService { final ToastRecord r = mToastQueue.get(i); if (r.pkg.equals(pkg)) { count++; if (count >= MAX_PACKAGE_NOTIFICATIONS) { Slog.e(TAG, "Package has already posted " + count if (count >= MAX_PACKAGE_TOASTS) { Slog.e(TAG, "Package has already queued " + count + " toasts. Not showing more. Package=" + pkg); return; } Loading
services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +26 −0 Original line number Diff line number Diff line Loading @@ -5155,6 +5155,32 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(1, mService.mToastQueue.size()); } @Test public void testLimitNumberOfQueuedToastsFromPackage() throws Exception { final String testPackage = "testPackageName"; assertEquals(0, mService.mToastQueue.size()); mService.isSystemUid = false; // package is not suspended when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) .thenReturn(false); INotificationManager nmService = (INotificationManager) mService.mService; // Trying to quickly enqueue more toast than allowed. for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_TOASTS + 1; i++) { nmService.enqueueTextToast( testPackage, new Binder(), "Text", /* duration */ 2000, /* displayId */ 0, /* callback */ null); } // Only allowed number enqueued, rest ignored. assertEquals(NotificationManagerService.MAX_PACKAGE_TOASTS, mService.mToastQueue.size()); } private void setAppInForegroundForToasts(int uid, boolean inForeground) { int importance = (inForeground) ? IMPORTANCE_FOREGROUND : IMPORTANCE_NONE; when(mActivityManager.getUidImportance(mUid)).thenReturn(importance); Loading