Loading services/core/java/com/android/server/notification/NotificationManagerService.java +56 −14 Original line number Diff line number Diff line Loading @@ -6284,6 +6284,24 @@ public class NotificationManagerService extends SystemService { checkCallerIsSystem(); mHandler.post(() -> { synchronized (mNotificationLock) { int count = getNotificationCount(pkg, userId); boolean removeFgsNotification = false; if (count > MAX_PACKAGE_NOTIFICATIONS) { mUsageStats.registerOverCountQuota(pkg); removeFgsNotification = true; } if (removeFgsNotification) { NotificationRecord r = findNotificationLocked(pkg, null, notificationId, userId); if (r != null) { if (DBG) { Slog.d(TAG, "Remove FGS flag not allow. Cancel FGS notification"); } removeFromNotificationListsLocked(r); cancelNotificationLocked(r, false, REASON_APP_CANCEL, true, null, SystemClock.elapsedRealtime()); } } else { // strip flag from all enqueued notifications. listeners will be informed // in post runnable. List<NotificationRecord> enqueued = findNotificationsByListLocked( Loading @@ -6301,6 +6319,7 @@ public class NotificationManagerService extends SystemService { mListeners.notifyPostedLocked(r, r); } } } }); } Loading Loading @@ -6976,6 +6995,29 @@ public class NotificationManagerService extends SystemService { return mPermissionHelper.hasPermission(uid); } private int getNotificationCount(String pkg, int userId) { int count = 0; synchronized (mNotificationLock) { final int numListSize = mNotificationList.size(); for (int i = 0; i < numListSize; i++) { final NotificationRecord existing = mNotificationList.get(i); if (existing.getSbn().getPackageName().equals(pkg) && existing.getSbn().getUserId() == userId) { count++; } } final int numEnqSize = mEnqueuedNotifications.size(); for (int i = 0; i < numEnqSize; i++) { final NotificationRecord existing = mEnqueuedNotifications.get(i); if (existing.getSbn().getPackageName().equals(pkg) && existing.getSbn().getUserId() == userId) { count++; } } } return count; } protected int getNotificationCount(String pkg, int userId, int excludedId, String excludedTag) { int count = 0; Loading services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +56 −0 Original line number Diff line number Diff line Loading @@ -6038,6 +6038,62 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(0, captor.getValue().getNotification().flags); } @Test public void testCannotRemoveForegroundFlagWhenOverLimit_enqueued() { for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS; i++) { Notification n = new Notification.Builder(mContext, "").build(); StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, i, null, mUid, 0, n, UserHandle.getUserHandleForUid(mUid), null, 0); NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); mService.addEnqueuedNotification(r); } Notification n = new Notification.Builder(mContext, "").build(); n.flags |= FLAG_FOREGROUND_SERVICE; StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS, null, mUid, 0, n, UserHandle.getUserHandleForUid(mUid), null, 0); NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); mService.addEnqueuedNotification(r); mInternalService.removeForegroundServiceFlagFromNotification( PKG, r.getSbn().getId(), r.getSbn().getUserId()); waitForIdle(); assertEquals(NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS, mService.getNotificationRecordCount()); } @Test public void testCannotRemoveForegroundFlagWhenOverLimit_posted() { for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS; i++) { Notification n = new Notification.Builder(mContext, "").build(); StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, i, null, mUid, 0, n, UserHandle.getUserHandleForUid(mUid), null, 0); NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); mService.addNotification(r); } Notification n = new Notification.Builder(mContext, "").build(); n.flags |= FLAG_FOREGROUND_SERVICE; StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS, null, mUid, 0, n, UserHandle.getUserHandleForUid(mUid), null, 0); NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); mService.addNotification(r); mInternalService.removeForegroundServiceFlagFromNotification( PKG, r.getSbn().getId(), r.getSbn().getUserId()); waitForIdle(); assertEquals(NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS, mService.getNotificationRecordCount()); } @Test public void testAllowForegroundCustomToasts() throws Exception { final String testPackage = "testPackageName"; Loading Loading
services/core/java/com/android/server/notification/NotificationManagerService.java +56 −14 Original line number Diff line number Diff line Loading @@ -6284,6 +6284,24 @@ public class NotificationManagerService extends SystemService { checkCallerIsSystem(); mHandler.post(() -> { synchronized (mNotificationLock) { int count = getNotificationCount(pkg, userId); boolean removeFgsNotification = false; if (count > MAX_PACKAGE_NOTIFICATIONS) { mUsageStats.registerOverCountQuota(pkg); removeFgsNotification = true; } if (removeFgsNotification) { NotificationRecord r = findNotificationLocked(pkg, null, notificationId, userId); if (r != null) { if (DBG) { Slog.d(TAG, "Remove FGS flag not allow. Cancel FGS notification"); } removeFromNotificationListsLocked(r); cancelNotificationLocked(r, false, REASON_APP_CANCEL, true, null, SystemClock.elapsedRealtime()); } } else { // strip flag from all enqueued notifications. listeners will be informed // in post runnable. List<NotificationRecord> enqueued = findNotificationsByListLocked( Loading @@ -6301,6 +6319,7 @@ public class NotificationManagerService extends SystemService { mListeners.notifyPostedLocked(r, r); } } } }); } Loading Loading @@ -6976,6 +6995,29 @@ public class NotificationManagerService extends SystemService { return mPermissionHelper.hasPermission(uid); } private int getNotificationCount(String pkg, int userId) { int count = 0; synchronized (mNotificationLock) { final int numListSize = mNotificationList.size(); for (int i = 0; i < numListSize; i++) { final NotificationRecord existing = mNotificationList.get(i); if (existing.getSbn().getPackageName().equals(pkg) && existing.getSbn().getUserId() == userId) { count++; } } final int numEnqSize = mEnqueuedNotifications.size(); for (int i = 0; i < numEnqSize; i++) { final NotificationRecord existing = mEnqueuedNotifications.get(i); if (existing.getSbn().getPackageName().equals(pkg) && existing.getSbn().getUserId() == userId) { count++; } } } return count; } protected int getNotificationCount(String pkg, int userId, int excludedId, String excludedTag) { int count = 0; Loading
services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +56 −0 Original line number Diff line number Diff line Loading @@ -6038,6 +6038,62 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(0, captor.getValue().getNotification().flags); } @Test public void testCannotRemoveForegroundFlagWhenOverLimit_enqueued() { for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS; i++) { Notification n = new Notification.Builder(mContext, "").build(); StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, i, null, mUid, 0, n, UserHandle.getUserHandleForUid(mUid), null, 0); NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); mService.addEnqueuedNotification(r); } Notification n = new Notification.Builder(mContext, "").build(); n.flags |= FLAG_FOREGROUND_SERVICE; StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS, null, mUid, 0, n, UserHandle.getUserHandleForUid(mUid), null, 0); NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); mService.addEnqueuedNotification(r); mInternalService.removeForegroundServiceFlagFromNotification( PKG, r.getSbn().getId(), r.getSbn().getUserId()); waitForIdle(); assertEquals(NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS, mService.getNotificationRecordCount()); } @Test public void testCannotRemoveForegroundFlagWhenOverLimit_posted() { for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS; i++) { Notification n = new Notification.Builder(mContext, "").build(); StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, i, null, mUid, 0, n, UserHandle.getUserHandleForUid(mUid), null, 0); NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); mService.addNotification(r); } Notification n = new Notification.Builder(mContext, "").build(); n.flags |= FLAG_FOREGROUND_SERVICE; StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS, null, mUid, 0, n, UserHandle.getUserHandleForUid(mUid), null, 0); NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); mService.addNotification(r); mInternalService.removeForegroundServiceFlagFromNotification( PKG, r.getSbn().getId(), r.getSbn().getUserId()); waitForIdle(); assertEquals(NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS, mService.getNotificationRecordCount()); } @Test public void testAllowForegroundCustomToasts() throws Exception { final String testPackage = "testPackageName"; Loading