Skip to content

Commit 24f74b3

Browse files
Julia Reynoldsofficialputuid
authored andcommitted
Limit the number of concurrently snoozed notifications
Test: atest FrameworksUiServicesTests Bug: 234441463 Change-Id: I005b43979d1c708fd505c8b33ae0c8cb03ddbb35 Merged-In: I005b43979d1c708fd505c8b33ae0c8cb03ddbb35 (cherry picked from commit 7c38394) (cherry picked from commit 455a525) Merged-In: I005b43979d1c708fd505c8b33ae0c8cb03ddbb35
1 parent 0605925 commit 24f74b3

4 files changed

Lines changed: 116 additions & 14 deletions

File tree

services/core/java/com/android/server/notification/NotificationManagerService.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6343,6 +6343,7 @@ public void run() {
63436343

63446344
@GuardedBy("mNotificationLock")
63456345
void snoozeLocked(NotificationRecord r) {
6346+
final List<NotificationRecord> recordsToSnooze = new ArrayList<>();
63466347
if (r.getSbn().isGroup()) {
63476348
final List<NotificationRecord> groupNotifications =
63486349
findCurrentAndSnoozedGroupNotificationsLocked(
@@ -6351,8 +6352,8 @@ void snoozeLocked(NotificationRecord r) {
63516352
if (r.getNotification().isGroupSummary()) {
63526353
// snooze all children
63536354
for (int i = 0; i < groupNotifications.size(); i++) {
6354-
if (mKey != groupNotifications.get(i).getKey()) {
6355-
snoozeNotificationLocked(groupNotifications.get(i));
6355+
if (!mKey.equals(groupNotifications.get(i).getKey())) {
6356+
recordsToSnooze.add(groupNotifications.get(i));
63566357
}
63576358
}
63586359
} else {
@@ -6362,16 +6363,24 @@ void snoozeLocked(NotificationRecord r) {
63626363
if (groupNotifications.size() == 2) {
63636364
// snooze summary and the one child
63646365
for (int i = 0; i < groupNotifications.size(); i++) {
6365-
if (mKey != groupNotifications.get(i).getKey()) {
6366-
snoozeNotificationLocked(groupNotifications.get(i));
6366+
if (!mKey.equals(groupNotifications.get(i).getKey())) {
6367+
recordsToSnooze.add(groupNotifications.get(i));
63676368
}
63686369
}
63696370
}
63706371
}
63716372
}
63726373
}
63736374
// snooze the notification
6374-
snoozeNotificationLocked(r);
6375+
recordsToSnooze.add(r);
6376+
6377+
if (mSnoozeHelper.canSnooze(recordsToSnooze.size())) {
6378+
for (int i = 0; i < recordsToSnooze.size(); i++) {
6379+
snoozeNotificationLocked(recordsToSnooze.get(i));
6380+
}
6381+
} else {
6382+
Log.w(TAG, "Cannot snooze " + r.getKey() + ": too many snoozed notifications");
6383+
}
63756384

63766385
}
63776386

services/core/java/com/android/server/notification/SnoozeHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
public class SnoozeHelper {
6161
public static final String XML_SNOOZED_NOTIFICATION_VERSION = "1";
6262

63+
static final int CONCURRENT_SNOOZE_LIMIT = 500;
64+
6365
protected static final String XML_TAG_NAME = "snoozed-notifications";
6466

6567
private static final String XML_SNOOZED_NOTIFICATION = "notification";
@@ -132,6 +134,15 @@ void cleanupPersistedContext(String key){
132134
}
133135
}
134136

137+
protected boolean canSnooze(int numberToSnooze) {
138+
synchronized (mLock) {
139+
if ((mPackages.size() + numberToSnooze) > CONCURRENT_SNOOZE_LIMIT) {
140+
return false;
141+
}
142+
}
143+
return true;
144+
}
145+
135146
@NonNull
136147
protected Long getSnoozeTimeForUnpostedNotification(int userId, String pkg, String key) {
137148
Long time = null;

services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,40 +2795,99 @@ public void testHasCompanionDevice_noService() {
27952795
assertFalse(mService.hasCompanionDevice(mListener));
27962796
}
27972797

2798+
@Test
2799+
public void testSnoozeRunnable_tooManySnoozed_singleNotification() {
2800+
final NotificationRecord notification = generateNotificationRecord(
2801+
mTestNotificationChannel, 1, null, true);
2802+
mService.addNotification(notification);
2803+
2804+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
2805+
when(mSnoozeHelper.canSnooze(1)).thenReturn(false);
2806+
2807+
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
2808+
mService.new SnoozeNotificationRunnable(
2809+
notification.getKey(), 100, null);
2810+
snoozeNotificationRunnable.run();
2811+
2812+
verify(mSnoozeHelper, never()).snooze(any(NotificationRecord.class), anyLong());
2813+
assertEquals(1, mService.getNotificationRecordCount());
2814+
}
2815+
2816+
@Test
2817+
public void testSnoozeRunnable_tooManySnoozed_singleGroupChildNotification() {
2818+
final NotificationRecord notification = generateNotificationRecord(
2819+
mTestNotificationChannel, 1, "group", true);
2820+
final NotificationRecord notificationChild = generateNotificationRecord(
2821+
mTestNotificationChannel, 1, "group", false);
2822+
mService.addNotification(notification);
2823+
mService.addNotification(notificationChild);
2824+
2825+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
2826+
when(mSnoozeHelper.canSnooze(2)).thenReturn(false);
2827+
2828+
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
2829+
mService.new SnoozeNotificationRunnable(
2830+
notificationChild.getKey(), 100, null);
2831+
snoozeNotificationRunnable.run();
2832+
2833+
verify(mSnoozeHelper, never()).snooze(any(NotificationRecord.class), anyLong());
2834+
assertEquals(2, mService.getNotificationRecordCount());
2835+
}
2836+
2837+
@Test
2838+
public void testSnoozeRunnable_tooManySnoozed_summaryNotification() {
2839+
final NotificationRecord notification = generateNotificationRecord(
2840+
mTestNotificationChannel, 1, "group", true);
2841+
final NotificationRecord notificationChild = generateNotificationRecord(
2842+
mTestNotificationChannel, 12, "group", false);
2843+
final NotificationRecord notificationChild2 = generateNotificationRecord(
2844+
mTestNotificationChannel, 13, "group", false);
2845+
mService.addNotification(notification);
2846+
mService.addNotification(notificationChild);
2847+
mService.addNotification(notificationChild2);
2848+
2849+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
2850+
when(mSnoozeHelper.canSnooze(3)).thenReturn(false);
2851+
2852+
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
2853+
mService.new SnoozeNotificationRunnable(
2854+
notification.getKey(), 100, null);
2855+
snoozeNotificationRunnable.run();
2856+
2857+
verify(mSnoozeHelper, never()).snooze(any(NotificationRecord.class), anyLong());
2858+
assertEquals(3, mService.getNotificationRecordCount());
2859+
}
2860+
27982861
@Test
27992862
public void testSnoozeRunnable_reSnoozeASingleSnoozedNotification() throws Exception {
28002863
final NotificationRecord notification = generateNotificationRecord(
28012864
mTestNotificationChannel, 1, null, true);
28022865
mService.addNotification(notification);
28032866
when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
2867+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
28042868

28052869
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
28062870
mService.new SnoozeNotificationRunnable(
28072871
notification.getKey(), 100, null);
28082872
snoozeNotificationRunnable.run();
2809-
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
2810-
mService.new SnoozeNotificationRunnable(
2811-
notification.getKey(), 100, null);
28122873
snoozeNotificationRunnable.run();
28132874

28142875
// snooze twice
28152876
verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong());
28162877
}
28172878

28182879
@Test
2819-
public void testSnoozeRunnable_reSnoozeASnoozedNotificationWithGroupKey() throws Exception {
2880+
public void testSnoozeRunnable_reSnoozeASnoozedNotificationWithGroupKey() {
28202881
final NotificationRecord notification = generateNotificationRecord(
28212882
mTestNotificationChannel, 1, "group", true);
28222883
mService.addNotification(notification);
28232884
when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
2885+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
28242886

28252887
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
28262888
mService.new SnoozeNotificationRunnable(
28272889
notification.getKey(), 100, null);
28282890
snoozeNotificationRunnable.run();
2829-
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
2830-
mService.new SnoozeNotificationRunnable(
2831-
notification.getKey(), 100, null);
28322891
snoozeNotificationRunnable.run();
28332892

28342893
// snooze twice
@@ -2846,6 +2905,7 @@ public void testSnoozeRunnable_reSnoozeMultipleNotificationsWithGroupKey() throw
28462905
when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
28472906
when(mSnoozeHelper.getNotifications(
28482907
anyString(), anyString(), anyInt())).thenReturn(new ArrayList<>());
2908+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
28492909

28502910
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
28512911
mService.new SnoozeNotificationRunnable(
@@ -2855,8 +2915,8 @@ mService.new SnoozeNotificationRunnable(
28552915
.thenReturn(new ArrayList<>(Arrays.asList(notification, notification2)));
28562916
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
28572917
mService.new SnoozeNotificationRunnable(
2858-
notification.getKey(), 100, null);
2859-
snoozeNotificationRunnable.run();
2918+
notification2.getKey(), 100, null);
2919+
snoozeNotificationRunnable2.run();
28602920

28612921
// snooze twice
28622922
verify(mSnoozeHelper, times(4)).snooze(any(NotificationRecord.class), anyLong());
@@ -2870,6 +2930,7 @@ public void testSnoozeRunnable_snoozeNonGrouped() throws Exception {
28702930
mTestNotificationChannel, 2, "group", false);
28712931
mService.addNotification(grouped);
28722932
mService.addNotification(nonGrouped);
2933+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
28732934

28742935
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
28752936
mService.new SnoozeNotificationRunnable(
@@ -2899,6 +2960,7 @@ public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception {
28992960
mService.addNotification(parent);
29002961
mService.addNotification(child);
29012962
mService.addNotification(child2);
2963+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
29022964

29032965
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
29042966
mService.new SnoozeNotificationRunnable(
@@ -2920,6 +2982,7 @@ public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exceptio
29202982
mService.addNotification(parent);
29212983
mService.addNotification(child);
29222984
mService.addNotification(child2);
2985+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
29232986

29242987
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
29252988
mService.new SnoozeNotificationRunnable(
@@ -2945,6 +3008,7 @@ public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exce
29453008
mTestNotificationChannel, 2, "group", false);
29463009
mService.addNotification(parent);
29473010
mService.addNotification(child);
3011+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
29483012

29493013
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
29503014
mService.new SnoozeNotificationRunnable(
@@ -2972,6 +3036,7 @@ public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Excepti
29723036
final NotificationRecord child = generateNotificationRecord(
29733037
mTestNotificationChannel, 2, "group", false);
29743038
mService.addNotification(child);
3039+
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
29753040

29763041
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
29773042
mService.new SnoozeNotificationRunnable(

services/tests/uiservicestests/src/com/android/server/notification/SnoozeHelperTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.android.server.notification;
1717

18+
import static com.android.server.notification.SnoozeHelper.CONCURRENT_SNOOZE_LIMIT;
1819
import static com.android.server.notification.SnoozeHelper.EXTRA_KEY;
1920

2021
import static junit.framework.Assert.assertEquals;
@@ -278,6 +279,22 @@ public void testSnooze() throws Exception {
278279
UserHandle.USER_SYSTEM, r.getSbn().getPackageName(), r.getKey()));
279280
}
280281

282+
@Test
283+
public void testSnoozeLimit() {
284+
for (int i = 0; i < CONCURRENT_SNOOZE_LIMIT; i++ ) {
285+
NotificationRecord r = getNotificationRecord("pkg", i, i+"", UserHandle.SYSTEM);
286+
287+
assertTrue("cannot snooze record " + i, mSnoozeHelper.canSnooze(1));
288+
289+
if (i % 2 == 0) {
290+
mSnoozeHelper.snooze(r, null);
291+
} else {
292+
mSnoozeHelper.snooze(r, 9000);
293+
}
294+
}
295+
assertFalse(mSnoozeHelper.canSnooze(1));
296+
}
297+
281298
@Test
282299
public void testCancelByApp() throws Exception {
283300
NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);

0 commit comments

Comments
 (0)