Revert "Revert "Limit the number of concurrently snoozed notifications""
This reverts commit 705019b5fc.
Reason for revert: adding merged in this time
Merged-In: I005b43979d1c708fd505c8b33ae0c8cb03ddbb35
Bug: 234441463
Change-Id: I5ecfdf77e56d9a50ca5cc8031ce83313b8d4ee5d
This commit is contained in:
@@ -7030,6 +7030,7 @@ public class NotificationManagerService extends SystemService {
|
|||||||
|
|
||||||
@GuardedBy("mNotificationLock")
|
@GuardedBy("mNotificationLock")
|
||||||
void snoozeLocked(NotificationRecord r) {
|
void snoozeLocked(NotificationRecord r) {
|
||||||
|
final List<NotificationRecord> recordsToSnooze = new ArrayList<>();
|
||||||
if (r.getSbn().isGroup()) {
|
if (r.getSbn().isGroup()) {
|
||||||
final List<NotificationRecord> groupNotifications =
|
final List<NotificationRecord> groupNotifications =
|
||||||
findCurrentAndSnoozedGroupNotificationsLocked(
|
findCurrentAndSnoozedGroupNotificationsLocked(
|
||||||
@@ -7038,8 +7039,8 @@ public class NotificationManagerService extends SystemService {
|
|||||||
if (r.getNotification().isGroupSummary()) {
|
if (r.getNotification().isGroupSummary()) {
|
||||||
// snooze all children
|
// snooze all children
|
||||||
for (int i = 0; i < groupNotifications.size(); i++) {
|
for (int i = 0; i < groupNotifications.size(); i++) {
|
||||||
if (mKey != groupNotifications.get(i).getKey()) {
|
if (!mKey.equals(groupNotifications.get(i).getKey())) {
|
||||||
snoozeNotificationLocked(groupNotifications.get(i));
|
recordsToSnooze.add(groupNotifications.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -7049,8 +7050,8 @@ public class NotificationManagerService extends SystemService {
|
|||||||
if (groupNotifications.size() == 2) {
|
if (groupNotifications.size() == 2) {
|
||||||
// snooze summary and the one child
|
// snooze summary and the one child
|
||||||
for (int i = 0; i < groupNotifications.size(); i++) {
|
for (int i = 0; i < groupNotifications.size(); i++) {
|
||||||
if (mKey != groupNotifications.get(i).getKey()) {
|
if (!mKey.equals(groupNotifications.get(i).getKey())) {
|
||||||
snoozeNotificationLocked(groupNotifications.get(i));
|
recordsToSnooze.add(groupNotifications.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7058,7 +7059,15 @@ public class NotificationManagerService extends SystemService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// snooze the notification
|
// snooze the notification
|
||||||
snoozeNotificationLocked(r);
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,8 @@ import java.util.Set;
|
|||||||
public class SnoozeHelper {
|
public class SnoozeHelper {
|
||||||
public static final int XML_SNOOZED_NOTIFICATION_VERSION = 1;
|
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";
|
protected static final String XML_TAG_NAME = "snoozed-notifications";
|
||||||
|
|
||||||
private static final String XML_SNOOZED_NOTIFICATION = "notification";
|
private static final String XML_SNOOZED_NOTIFICATION = "notification";
|
||||||
@@ -135,6 +137,15 @@ public class SnoozeHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean canSnooze(int numberToSnooze) {
|
||||||
|
synchronized (mLock) {
|
||||||
|
if ((mPackages.size() + numberToSnooze) > CONCURRENT_SNOOZE_LIMIT) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
protected Long getSnoozeTimeForUnpostedNotification(int userId, String pkg, String key) {
|
protected Long getSnoozeTimeForUnpostedNotification(int userId, String pkg, String key) {
|
||||||
Long time = null;
|
Long time = null;
|
||||||
|
|||||||
@@ -3380,39 +3380,98 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSnoozeRunnable_reSnoozeASingleSnoozedNotification() throws Exception {
|
public void testSnoozeRunnable_tooManySnoozed_singleNotification() {
|
||||||
final NotificationRecord notification = generateNotificationRecord(
|
final NotificationRecord notification = generateNotificationRecord(
|
||||||
mTestNotificationChannel, 1, null, true);
|
mTestNotificationChannel, 1, null, true);
|
||||||
mService.addNotification(notification);
|
mService.addNotification(notification);
|
||||||
when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
|
|
||||||
|
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
|
||||||
|
when(mSnoozeHelper.canSnooze(1)).thenReturn(false);
|
||||||
|
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
notification.getKey(), 100, null);
|
notification.getKey(), 100, null);
|
||||||
snoozeNotificationRunnable.run();
|
snoozeNotificationRunnable.run();
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
|
|
||||||
|
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(
|
mService.new SnoozeNotificationRunnable(
|
||||||
notification.getKey(), 100, null);
|
notification.getKey(), 100, null);
|
||||||
snoozeNotificationRunnable.run();
|
snoozeNotificationRunnable.run();
|
||||||
|
|
||||||
|
verify(mSnoozeHelper, never()).snooze(any(NotificationRecord.class), anyLong());
|
||||||
|
assertThat(mService.getNotificationRecordCount()).isEqualTo(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSnoozeRunnable_reSnoozeASingleSnoozedNotification() {
|
||||||
|
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();
|
||||||
|
snoozeNotificationRunnable.run();
|
||||||
|
|
||||||
// snooze twice
|
// snooze twice
|
||||||
verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong());
|
verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSnoozeRunnable_reSnoozeASnoozedNotificationWithGroupKey() throws Exception {
|
public void testSnoozeRunnable_reSnoozeASnoozedNotificationWithGroupKey() {
|
||||||
final NotificationRecord notification = generateNotificationRecord(
|
final NotificationRecord notification = generateNotificationRecord(
|
||||||
mTestNotificationChannel, 1, "group", true);
|
mTestNotificationChannel, 1, "group", true);
|
||||||
mService.addNotification(notification);
|
mService.addNotification(notification);
|
||||||
when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
|
when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
|
||||||
|
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
|
||||||
|
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
notification.getKey(), 100, null);
|
notification.getKey(), 100, null);
|
||||||
snoozeNotificationRunnable.run();
|
snoozeNotificationRunnable.run();
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
|
|
||||||
mService.new SnoozeNotificationRunnable(
|
|
||||||
notification.getKey(), 100, null);
|
|
||||||
snoozeNotificationRunnable.run();
|
snoozeNotificationRunnable.run();
|
||||||
|
|
||||||
// snooze twice
|
// snooze twice
|
||||||
@@ -3430,6 +3489,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
|||||||
when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
|
when(mSnoozeHelper.getNotification(any())).thenReturn(notification);
|
||||||
when(mSnoozeHelper.getNotifications(
|
when(mSnoozeHelper.getNotifications(
|
||||||
anyString(), anyString(), anyInt())).thenReturn(new ArrayList<>());
|
anyString(), anyString(), anyInt())).thenReturn(new ArrayList<>());
|
||||||
|
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
|
||||||
|
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
@@ -3439,8 +3499,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
|||||||
.thenReturn(new ArrayList<>(Arrays.asList(notification, notification2)));
|
.thenReturn(new ArrayList<>(Arrays.asList(notification, notification2)));
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
notification.getKey(), 100, null);
|
notification2.getKey(), 100, null);
|
||||||
snoozeNotificationRunnable.run();
|
snoozeNotificationRunnable2.run();
|
||||||
|
|
||||||
// snooze twice
|
// snooze twice
|
||||||
verify(mSnoozeHelper, times(4)).snooze(any(NotificationRecord.class), anyLong());
|
verify(mSnoozeHelper, times(4)).snooze(any(NotificationRecord.class), anyLong());
|
||||||
@@ -3454,6 +3514,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
|||||||
mTestNotificationChannel, 2, "group", false);
|
mTestNotificationChannel, 2, "group", false);
|
||||||
mService.addNotification(grouped);
|
mService.addNotification(grouped);
|
||||||
mService.addNotification(nonGrouped);
|
mService.addNotification(nonGrouped);
|
||||||
|
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
|
||||||
|
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
@@ -3483,6 +3544,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
|||||||
mService.addNotification(parent);
|
mService.addNotification(parent);
|
||||||
mService.addNotification(child);
|
mService.addNotification(child);
|
||||||
mService.addNotification(child2);
|
mService.addNotification(child2);
|
||||||
|
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
|
||||||
|
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
@@ -3504,6 +3566,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
|||||||
mService.addNotification(parent);
|
mService.addNotification(parent);
|
||||||
mService.addNotification(child);
|
mService.addNotification(child);
|
||||||
mService.addNotification(child2);
|
mService.addNotification(child2);
|
||||||
|
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
|
||||||
|
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
@@ -3529,6 +3592,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
|||||||
mTestNotificationChannel, 2, "group", false);
|
mTestNotificationChannel, 2, "group", false);
|
||||||
mService.addNotification(parent);
|
mService.addNotification(parent);
|
||||||
mService.addNotification(child);
|
mService.addNotification(child);
|
||||||
|
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
|
||||||
|
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
@@ -3556,6 +3620,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
|||||||
final NotificationRecord child = generateNotificationRecord(
|
final NotificationRecord child = generateNotificationRecord(
|
||||||
mTestNotificationChannel, 2, "group", false);
|
mTestNotificationChannel, 2, "group", false);
|
||||||
mService.addNotification(child);
|
mService.addNotification(child);
|
||||||
|
when(mSnoozeHelper.canSnooze(anyInt())).thenReturn(true);
|
||||||
|
|
||||||
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
|
||||||
mService.new SnoozeNotificationRunnable(
|
mService.new SnoozeNotificationRunnable(
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.server.notification;
|
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 com.android.server.notification.SnoozeHelper.EXTRA_KEY;
|
||||||
|
|
||||||
import static junit.framework.Assert.assertEquals;
|
import static junit.framework.Assert.assertEquals;
|
||||||
@@ -280,6 +281,22 @@ public class SnoozeHelperTest extends UiServiceTestCase {
|
|||||||
UserHandle.USER_SYSTEM, r.getSbn().getPackageName(), r.getKey()));
|
UserHandle.USER_SYSTEM, r.getSbn().getPackageName(), r.getKey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Test
|
||||||
public void testCancelByApp() throws Exception {
|
public void testCancelByApp() throws Exception {
|
||||||
NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
|
NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
|
||||||
|
|||||||
Reference in New Issue
Block a user