Merge "When channels are deleted, remove notifications from recently dismissed list." into sc-dev

This commit is contained in:
Yuri Lin
2021-04-07 14:24:15 +00:00
committed by Android (Google) Code Review
2 changed files with 41 additions and 3 deletions

View File

@@ -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<StatusBarNotification, Integer> 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()

View File

@@ -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<String> 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<StatusBarNotification> actual = Arrays.asList(mArchive.getArray(SIZE, true));
assertThat(actual).hasSize(expected.size());
for (StatusBarNotification sbn : actual) {
assertThat(expected).contains(sbn.getKey());
}
}
}