Loading services/core/java/com/android/server/notification/NotificationManagerService.java +29 −19 Original line number Diff line number Diff line Loading @@ -3867,14 +3867,18 @@ public class NotificationManagerService extends SystemService { private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason) { final String canceledKey = r.getKey(); // Remove from either list boolean wasPosted; if (mNotificationList.remove(r)) { mNotificationsByKey.remove(r.sbn.getKey()); // Remove from both lists, either list could have a separate Record for what is effectively // the same notification. boolean wasPosted = false; NotificationRecord recordInList = null; if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) != null) { mNotificationList.remove(recordInList); mNotificationsByKey.remove(recordInList.sbn.getKey()); wasPosted = true; } else { mEnqueuedNotifications.remove(r); wasPosted = false; } if ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) != null) { mEnqueuedNotifications.remove(recordInList); } // Record caller. Loading Loading @@ -4298,17 +4302,12 @@ public class NotificationManagerService extends SystemService { // TODO: need to combine a bunch of these getters with slightly different behavior. // TODO: Should enqueuing just add to mNotificationsByKey instead? private NotificationRecord findNotificationByKeyLocked(String key) { final int N = mNotificationList.size(); for (int i = 0; i < N; i++) { if (key.equals(mNotificationList.get(i).getKey())) { return mNotificationList.get(i); } } final int M = mEnqueuedNotifications.size(); for (int i = 0; i < M; i++) { if (key.equals(mEnqueuedNotifications.get(i).getKey())) { return mEnqueuedNotifications.get(i); NotificationRecord r; if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { return r; } if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { return r; } return null; } Loading @@ -4326,8 +4325,7 @@ public class NotificationManagerService extends SystemService { } private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, String pkg, String tag, int id, int userId) { String pkg, String tag, int id, int userId) { final int len = list.size(); for (int i = 0; i < len; i++) { NotificationRecord r = list.get(i); Loading @@ -4339,6 +4337,18 @@ public class NotificationManagerService extends SystemService { return null; } private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, String key) { final int N = list.size(); for (int i = 0; i < N; i++) { if (key.equals(list.get(i).getKey())) { return list.get(i); } } return null; } // lock on mNotificationList int indexOfNotificationLocked(String key) { final int N = mNotificationList.size(); Loading services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +15 −0 Original line number Diff line number Diff line Loading @@ -278,6 +278,21 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, generateNotificationRecord(null).getNotification(), new int[1], 0); waitForIdle(); mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, generateNotificationRecord(null).getNotification(), new int[1], 0); mBinderService.cancelNotificationWithTag(mContext.getPackageName(), "tag", 0, 0); waitForIdle(); StatusBarNotification[] notifs = mBinderService.getActiveNotifications(mContext.getPackageName()); assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { Loading Loading
services/core/java/com/android/server/notification/NotificationManagerService.java +29 −19 Original line number Diff line number Diff line Loading @@ -3867,14 +3867,18 @@ public class NotificationManagerService extends SystemService { private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason) { final String canceledKey = r.getKey(); // Remove from either list boolean wasPosted; if (mNotificationList.remove(r)) { mNotificationsByKey.remove(r.sbn.getKey()); // Remove from both lists, either list could have a separate Record for what is effectively // the same notification. boolean wasPosted = false; NotificationRecord recordInList = null; if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) != null) { mNotificationList.remove(recordInList); mNotificationsByKey.remove(recordInList.sbn.getKey()); wasPosted = true; } else { mEnqueuedNotifications.remove(r); wasPosted = false; } if ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) != null) { mEnqueuedNotifications.remove(recordInList); } // Record caller. Loading Loading @@ -4298,17 +4302,12 @@ public class NotificationManagerService extends SystemService { // TODO: need to combine a bunch of these getters with slightly different behavior. // TODO: Should enqueuing just add to mNotificationsByKey instead? private NotificationRecord findNotificationByKeyLocked(String key) { final int N = mNotificationList.size(); for (int i = 0; i < N; i++) { if (key.equals(mNotificationList.get(i).getKey())) { return mNotificationList.get(i); } } final int M = mEnqueuedNotifications.size(); for (int i = 0; i < M; i++) { if (key.equals(mEnqueuedNotifications.get(i).getKey())) { return mEnqueuedNotifications.get(i); NotificationRecord r; if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { return r; } if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { return r; } return null; } Loading @@ -4326,8 +4325,7 @@ public class NotificationManagerService extends SystemService { } private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, String pkg, String tag, int id, int userId) { String pkg, String tag, int id, int userId) { final int len = list.size(); for (int i = 0; i < len; i++) { NotificationRecord r = list.get(i); Loading @@ -4339,6 +4337,18 @@ public class NotificationManagerService extends SystemService { return null; } private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, String key) { final int N = list.size(); for (int i = 0; i < N; i++) { if (key.equals(list.get(i).getKey())) { return list.get(i); } } return null; } // lock on mNotificationList int indexOfNotificationLocked(String key) { final int N = mNotificationList.size(); Loading
services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +15 −0 Original line number Diff line number Diff line Loading @@ -278,6 +278,21 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, generateNotificationRecord(null).getNotification(), new int[1], 0); waitForIdle(); mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, generateNotificationRecord(null).getNotification(), new int[1], 0); mBinderService.cancelNotificationWithTag(mContext.getPackageName(), "tag", 0, 0); waitForIdle(); StatusBarNotification[] notifs = mBinderService.getActiveNotifications(mContext.getPackageName()); assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { Loading