Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0c03d23 authored by Yuri Lin's avatar Yuri Lin
Browse files

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
parent 7972c833
Loading
Loading
Loading
Loading
+21 −2
Original line number Original line Diff line number Diff line
@@ -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) {
    void loadDefaultApprovedServices(int userId) {
@@ -3623,6 +3638,8 @@ public class NotificationManagerService extends SystemService {
            cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true,
            cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true,
                    callingUser, REASON_CHANNEL_REMOVED, null);
                    callingUser, REASON_CHANNEL_REMOVED, null);
            mPreferencesHelper.deleteNotificationChannel(pkg, callingUid, channelId);
            mPreferencesHelper.deleteNotificationChannel(pkg, callingUid, channelId);
            // Remove from both recent notification archive and notification history
            mArchive.removeChannelNotifications(pkg, callingUser, channelId);
            mHistoryManager.deleteNotificationChannel(pkg, callingUid, channelId);
            mHistoryManager.deleteNotificationChannel(pkg, callingUid, channelId);
            mListeners.notifyNotificationChannelChanged(pkg,
            mListeners.notifyNotificationChannelChanged(pkg,
                    UserHandle.getUserHandleForUid(callingUid),
                    UserHandle.getUserHandleForUid(callingUid),
@@ -8179,8 +8196,10 @@ public class NotificationManagerService extends SystemService {
            summaries.remove(r.getSbn().getPackageName());
            summaries.remove(r.getSbn().getPackageName());
        }
        }


        // Save it for users of getHistoricalNotifications()
        // Save it for users of getHistoricalNotifications(), unless the whole channel was deleted
        if (reason != REASON_CHANNEL_REMOVED) {
            mArchive.record(r.getSbn(), reason);
            mArchive.record(r.getSbn(), reason);
        }


        final long now = System.currentTimeMillis();
        final long now = System.currentTimeMillis();
        final LogMaker logMaker = r.getItemLogMaker()
        final LogMaker logMaker = r.getItemLogMaker()
+20 −1
Original line number Original line Diff line number Diff line
@@ -56,7 +56,7 @@ public class ArchiveTest extends UiServiceTestCase {
    }
    }


    private StatusBarNotification getNotification(String pkg, int id, UserHandle user) {
    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")
                .setContentTitle("A")
                .setWhen(1205)
                .setWhen(1205)
                .build();
                .build();
@@ -140,4 +140,23 @@ public class ArchiveTest extends UiServiceTestCase {
            assertThat(expected).contains(sbn.getKey());
            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());
        }
    }
}
}