Merge "Revert "Limit the number of concurrently snoozed notifications"" into tm-dev am: 9af30793f0

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/19340631

Change-Id: I774c66c5aa1e91192304aad2651841391f51a8e3
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index 88f5432..d1e0b04 100755
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -7030,7 +7030,6 @@
 
         @GuardedBy("mNotificationLock")
         void snoozeLocked(NotificationRecord r) {
-            final List<NotificationRecord> recordsToSnooze = new ArrayList<>();
             if (r.getSbn().isGroup()) {
                 final List<NotificationRecord> groupNotifications =
                         findCurrentAndSnoozedGroupNotificationsLocked(
@@ -7039,8 +7038,8 @@
                 if (r.getNotification().isGroupSummary()) {
                     // snooze all children
                     for (int i = 0; i < groupNotifications.size(); i++) {
-                        if (!mKey.equals(groupNotifications.get(i).getKey())) {
-                            recordsToSnooze.add(groupNotifications.get(i));
+                        if (mKey != groupNotifications.get(i).getKey()) {
+                            snoozeNotificationLocked(groupNotifications.get(i));
                         }
                     }
                 } else {
@@ -7050,8 +7049,8 @@
                         if (groupNotifications.size() == 2) {
                             // snooze summary and the one child
                             for (int i = 0; i < groupNotifications.size(); i++) {
-                                if (!mKey.equals(groupNotifications.get(i).getKey())) {
-                                    recordsToSnooze.add(groupNotifications.get(i));
+                                if (mKey != groupNotifications.get(i).getKey()) {
+                                    snoozeNotificationLocked(groupNotifications.get(i));
                                 }
                             }
                         }
@@ -7059,15 +7058,7 @@
                 }
             }
             // snooze the notification
-            recordsToSnooze.add(r);
-
-            if (mSnoozeHelper.canSnooze(recordsToSnooze.size())) {
-                for (int i = 0; i < recordsToSnooze.size(); i++) {
-                    snoozeNotificationLocked(recordsToSnooze.get(i));
-                }
-            } else {
-                Log.w(TAG, "Cannot snooze " + r.getKey() + ": too many snoozed notifications");
-            }
+            snoozeNotificationLocked(r);
 
         }
 
diff --git a/services/core/java/com/android/server/notification/SnoozeHelper.java b/services/core/java/com/android/server/notification/SnoozeHelper.java
index 15d7c1e..7f265df 100644
--- a/services/core/java/com/android/server/notification/SnoozeHelper.java
+++ b/services/core/java/com/android/server/notification/SnoozeHelper.java
@@ -62,8 +62,6 @@
 public class SnoozeHelper {
     public static final int XML_SNOOZED_NOTIFICATION_VERSION = 1;
 
-    static final int CONCURRENT_SNOOZE_LIMIT = 500;
-
     protected static final String XML_TAG_NAME = "snoozed-notifications";
 
     private static final String XML_SNOOZED_NOTIFICATION = "notification";
@@ -137,15 +135,6 @@
         }
     }
 
-    protected boolean canSnooze(int numberToSnooze) {
-        synchronized (mLock) {
-            if ((mPackages.size() + numberToSnooze) > CONCURRENT_SNOOZE_LIMIT) {
-                return false;
-            }
-        }
-        return true;
-    }
-
     @NonNull
     protected Long getSnoozeTimeForUnpostedNotification(int userId, String pkg, String key) {
         Long time = null;
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
index b1b323b..22721a1 100755
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
@@ -3380,80 +3380,19 @@
     }
 
     @Test
-    public void testSnoozeRunnable_tooManySnoozed_singleNotification() {
-        final NotificationRecord notification = generateNotificationRecord(
-                mTestNotificationChannel, 1, null, true);
-        mService.addNotification(notification);
-
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
-        when(mSnoozeHelper.canSnooze(1)).thenReturn(false);
-
-        NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
-                mService.new SnoozeNotificationRunnable(
-                        notification.getKey(), 100, null);
-        snoozeNotificationRunnable.run();
-
-        verify(mSnoozeHelper, never()).snooze(any(NotificationRecord.class), anyLong());
-        assertThat(mService.getNotificationRecordCount()).isEqualTo(1);
-    }
-
-    @Test
-    public void testSnoozeRunnable_tooManySnoozed_singleGroupChildNotification() {
-        final NotificationRecord notification = generateNotificationRecord(
-                mTestNotificationChannel, 1, "group", true);
-        final NotificationRecord notificationChild = generateNotificationRecord(
-                mTestNotificationChannel, 1, "group", false);
-        mService.addNotification(notification);
-        mService.addNotification(notificationChild);
-
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
-        when(mSnoozeHelper.canSnooze(2)).thenReturn(false);
-
-        NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
-                mService.new SnoozeNotificationRunnable(
-                        notificationChild.getKey(), 100, null);
-        snoozeNotificationRunnable.run();
-
-        verify(mSnoozeHelper, never()).snooze(any(NotificationRecord.class), anyLong());
-        assertThat(mService.getNotificationRecordCount()).isEqualTo(2);
-    }
-
-    @Test
-    public void testSnoozeRunnable_tooManySnoozed_summaryNotification() {
-        final NotificationRecord notification = generateNotificationRecord(
-                mTestNotificationChannel, 1, "group", true);
-        final NotificationRecord notificationChild = generateNotificationRecord(
-                mTestNotificationChannel, 12, "group", false);
-        final NotificationRecord notificationChild2 = generateNotificationRecord(
-                mTestNotificationChannel, 13, "group", false);
-        mService.addNotification(notification);
-        mService.addNotification(notificationChild);
-        mService.addNotification(notificationChild2);
-
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
-        when(mSnoozeHelper.canSnooze(3)).thenReturn(false);
-
-        NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
-                mService.new SnoozeNotificationRunnable(
-                        notification.getKey(), 100, null);
-        snoozeNotificationRunnable.run();
-
-        verify(mSnoozeHelper, never()).snooze(any(NotificationRecord.class), anyLong());
-        assertThat(mService.getNotificationRecordCount()).isEqualTo(3);
-    }
-
-    @Test
-    public void testSnoozeRunnable_reSnoozeASingleSnoozedNotification() {
+    public void testSnoozeRunnable_reSnoozeASingleSnoozedNotification() throws Exception {
         final NotificationRecord notification = generateNotificationRecord(
                 mTestNotificationChannel, 1, null, true);
         mService.addNotification(notification);
         when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
 
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
                 mService.new SnoozeNotificationRunnable(
                 notification.getKey(), 100, null);
         snoozeNotificationRunnable.run();
+        NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
+                mService.new SnoozeNotificationRunnable(
+                notification.getKey(), 100, null);
         snoozeNotificationRunnable.run();
 
         // snooze twice
@@ -3461,17 +3400,19 @@
     }
 
     @Test
-    public void testSnoozeRunnable_reSnoozeASnoozedNotificationWithGroupKey() {
+    public void testSnoozeRunnable_reSnoozeASnoozedNotificationWithGroupKey() throws Exception {
         final NotificationRecord notification = generateNotificationRecord(
                 mTestNotificationChannel, 1, "group", true);
         mService.addNotification(notification);
         when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
 
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
                 mService.new SnoozeNotificationRunnable(
                 notification.getKey(), 100, null);
         snoozeNotificationRunnable.run();
+        NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
+                mService.new SnoozeNotificationRunnable(
+                notification.getKey(), 100, null);
         snoozeNotificationRunnable.run();
 
         // snooze twice
@@ -3489,7 +3430,6 @@
         when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
         when(mSnoozeHelper.getNotifications(
                 anyString(), anyString(), anyInt())).thenReturn(new ArrayList<>());
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
 
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
                 mService.new SnoozeNotificationRunnable(
@@ -3499,8 +3439,8 @@
                 .thenReturn(new ArrayList<>(Arrays.asList(notification, notification2)));
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
                 mService.new SnoozeNotificationRunnable(
-                        notification2.getKey(), 100, null);
-        snoozeNotificationRunnable2.run();
+                        notification.getKey(), 100, null);
+        snoozeNotificationRunnable.run();
 
         // snooze twice
         verify(mSnoozeHelper, times(4)).snooze(any(NotificationRecord.class), anyLong());
@@ -3514,7 +3454,6 @@
                 mTestNotificationChannel, 2, "group", false);
         mService.addNotification(grouped);
         mService.addNotification(nonGrouped);
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
 
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
                 mService.new SnoozeNotificationRunnable(
@@ -3544,7 +3483,6 @@
         mService.addNotification(parent);
         mService.addNotification(child);
         mService.addNotification(child2);
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
 
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
                 mService.new SnoozeNotificationRunnable(
@@ -3566,7 +3504,6 @@
         mService.addNotification(parent);
         mService.addNotification(child);
         mService.addNotification(child2);
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
 
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
                 mService.new SnoozeNotificationRunnable(
@@ -3592,7 +3529,6 @@
                 mTestNotificationChannel, 2, "group", false);
         mService.addNotification(parent);
         mService.addNotification(child);
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
 
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
                 mService.new SnoozeNotificationRunnable(
@@ -3620,7 +3556,6 @@
         final NotificationRecord child = generateNotificationRecord(
                 mTestNotificationChannel, 2, "group", false);
         mService.addNotification(child);
-        when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
 
         NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
                 mService.new SnoozeNotificationRunnable(
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/SnoozeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/SnoozeHelperTest.java
index 8bead57..2ae2ef7 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/SnoozeHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/SnoozeHelperTest.java
@@ -15,7 +15,6 @@
  */
 package com.android.server.notification;
 
-import static com.android.server.notification.SnoozeHelper.CONCURRENT_SNOOZE_LIMIT;
 import static com.android.server.notification.SnoozeHelper.EXTRA_KEY;
 
 import static junit.framework.Assert.assertEquals;
@@ -282,22 +281,6 @@
     }
 
     @Test
-    public void testSnoozeLimit() {
-        for (int i = 0; i < CONCURRENT_SNOOZE_LIMIT; i++ ) {
-            NotificationRecord r = getNotificationRecord("pkg", i, i+"", UserHandle.SYSTEM);
-
-            assertTrue("cannot snooze record " + i, mSnoozeHelper.canSnooze(1));
-
-            if (i % 2 == 0) {
-                mSnoozeHelper.snooze(r, null);
-            } else {
-                mSnoozeHelper.snooze(r, 9000);
-            }
-        }
-        assertFalse(mSnoozeHelper.canSnooze(1));
-    }
-
-    @Test
     public void testCancelByApp() throws Exception {
         NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
         NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM);