From c0c03d233be05983ea86bfe03ea0e5b0e13dc96c Mon Sep 17 00:00:00 2001 From: Yuri Lin Date: Mon, 5 Apr 2021 15:38:29 -0400 Subject: [PATCH] When channels are deleted, remove notifications from recently dismissed list. This change adds a function to Archive to remove all notifications associated with a specific user & channel, and also modifies notification cancellation to not record the notifications in mArchive if the cancellation reason is that the entire channel was removed. Test: atest ArchiveTest, NotificationManagerServiceTest, and manual confirmation Fixes: 169349809 Change-Id: Id4f678534f1c26243b99740e2954b5d54591f110 --- .../NotificationManagerService.java | 23 +++++++++++++++++-- .../server/notification/ArchiveTest.java | 21 ++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 6dcf39be5cb40..2f4cbd558fb90 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -685,6 +685,21 @@ public class NotificationManagerService extends SystemService { } } } + + // Remove notifications with the specified user & channel ID. + public void removeChannelNotifications(String pkg, @UserIdInt int userId, + String channelId) { + for (int i = 0; i < mBuffer.size(); i++) { + final Pair pair = mBuffer.get(i); + if (pair.first != null + && userId == pair.first.getNormalizedUserId() + && pkg != null && pkg.equals(pair.first.getPackageName()) + && pair.first.getNotification() != null + && Objects.equals(channelId, pair.first.getNotification().getChannelId())) { + mBuffer.remove(i); + } + } + } } void loadDefaultApprovedServices(int userId) { @@ -3623,6 +3638,8 @@ public class NotificationManagerService extends SystemService { cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, callingUser, REASON_CHANNEL_REMOVED, null); mPreferencesHelper.deleteNotificationChannel(pkg, callingUid, channelId); + // Remove from both recent notification archive and notification history + mArchive.removeChannelNotifications(pkg, callingUser, channelId); mHistoryManager.deleteNotificationChannel(pkg, callingUid, channelId); mListeners.notifyNotificationChannelChanged(pkg, UserHandle.getUserHandleForUid(callingUid), @@ -8179,8 +8196,10 @@ public class NotificationManagerService extends SystemService { summaries.remove(r.getSbn().getPackageName()); } - // Save it for users of getHistoricalNotifications() - mArchive.record(r.getSbn(), reason); + // Save it for users of getHistoricalNotifications(), unless the whole channel was deleted + if (reason != REASON_CHANNEL_REMOVED) { + mArchive.record(r.getSbn(), reason); + } final long now = System.currentTimeMillis(); final LogMaker logMaker = r.getItemLogMaker() diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ArchiveTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ArchiveTest.java index c69ef8d932820..a2ad89edcf5ea 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ArchiveTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ArchiveTest.java @@ -56,7 +56,7 @@ public class ArchiveTest extends UiServiceTestCase { } private StatusBarNotification getNotification(String pkg, int id, UserHandle user) { - Notification n = new Notification.Builder(getContext(), "test") + Notification n = new Notification.Builder(getContext(), "test" + id) .setContentTitle("A") .setWhen(1205) .build(); @@ -140,4 +140,23 @@ public class ArchiveTest extends UiServiceTestCase { assertThat(expected).contains(sbn.getKey()); } } + + @Test + public void testRemoveChannelNotifications() { + List expected = new ArrayList<>(); + for (int i = 0; i < SIZE; i++) { + StatusBarNotification sbn = getNotification("pkg", i, UserHandle.of(USER_CURRENT)); + mArchive.record(sbn, REASON_CANCEL); + if (i != 3) { + // Will delete notification for this user in channel "test3". + expected.add(sbn.getKey()); + } + } + mArchive.removeChannelNotifications("pkg", USER_CURRENT, "test3"); + List actual = Arrays.asList(mArchive.getArray(SIZE, true)); + assertThat(actual).hasSize(expected.size()); + for (StatusBarNotification sbn : actual) { + assertThat(expected).contains(sbn.getKey()); + } + } }