diff --git a/core/java/android/app/NotificationHistory.java b/core/java/android/app/NotificationHistory.java index 8ba39a883555a..909a476f3c941 100644 --- a/core/java/android/app/NotificationHistory.java +++ b/core/java/android/app/NotificationHistory.java @@ -24,6 +24,8 @@ import android.os.Parcelable; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Objects; @@ -311,11 +313,23 @@ public final class NotificationHistory implements Parcelable { mHistoryCount++; } + /** + * Used when populating a history from disk; adds an historical notification. + */ + public void addNewNotificationToWrite(@NonNull HistoricalNotification notification) { + if (notification == null) { + return; + } + mNotificationsToWrite.add(0, notification); + mHistoryCount++; + } + public void addNotificationsToWrite(@NonNull NotificationHistory notificationHistory) { for (HistoricalNotification hn : notificationHistory.getNotificationsToWrite()) { - // TODO: consider merging by date addNotificationToWrite(hn); } + Collections.sort(mNotificationsToWrite, + (o1, o2) -> -1 * Long.compare(o1.getPostedTimeMs(), o2.getPostedTimeMs())); poolStringsFromNotifications(); } diff --git a/core/tests/coretests/src/android/app/NotificationHistoryTest.java b/core/tests/coretests/src/android/app/NotificationHistoryTest.java index f9a6a5c2af4d7..0a21875fd77de 100644 --- a/core/tests/coretests/src/android/app/NotificationHistoryTest.java +++ b/core/tests/coretests/src/android/app/NotificationHistoryTest.java @@ -116,25 +116,28 @@ public class NotificationHistoryTest { @Test public void testAddNotificationsToWrite() { NotificationHistory history = new NotificationHistory(); - HistoricalNotification n = getHistoricalNotification(0); + HistoricalNotification n = getHistoricalNotification(3); HistoricalNotification n2 = getHistoricalNotification(1); + HistoricalNotification n5 = getHistoricalNotification(0); history.addNotificationToWrite(n2); history.addNotificationToWrite(n); + history.addNotificationToWrite(n5); NotificationHistory secondHistory = new NotificationHistory(); - HistoricalNotification n3 = getHistoricalNotification(2); - HistoricalNotification n4 = getHistoricalNotification(3); + HistoricalNotification n3 = getHistoricalNotification(4); + HistoricalNotification n4 = getHistoricalNotification(2); secondHistory.addNotificationToWrite(n4); secondHistory.addNotificationToWrite(n3); history.addNotificationsToWrite(secondHistory); - assertThat(history.getNotificationsToWrite().size()).isEqualTo(4); - assertThat(history.getNotificationsToWrite().get(0)).isSameAs(n2); + assertThat(history.getNotificationsToWrite().size()).isEqualTo(5); + assertThat(history.getNotificationsToWrite().get(0)).isSameAs(n3); assertThat(history.getNotificationsToWrite().get(1)).isSameAs(n); assertThat(history.getNotificationsToWrite().get(2)).isSameAs(n4); - assertThat(history.getNotificationsToWrite().get(3)).isSameAs(n3); - assertThat(history.getHistoryCount()).isEqualTo(4); + assertThat(history.getNotificationsToWrite().get(3)).isSameAs(n2); + assertThat(history.getNotificationsToWrite().get(4)).isSameAs(n5); + assertThat(history.getHistoryCount()).isEqualTo(5); assertThat(history.getPooledStringsToWrite()).asList().contains(n2.getChannelName()); assertThat(history.getPooledStringsToWrite()).asList().contains(n4.getPackage()); diff --git a/services/core/java/com/android/server/notification/NotificationHistoryDatabase.java b/services/core/java/com/android/server/notification/NotificationHistoryDatabase.java index e1ebdf17ef7c0..e8cb1632bdc32 100644 --- a/services/core/java/com/android/server/notification/NotificationHistoryDatabase.java +++ b/services/core/java/com/android/server/notification/NotificationHistoryDatabase.java @@ -172,7 +172,7 @@ public class NotificationHistoryDatabase { public void addNotification(final HistoricalNotification notification) { synchronized (mLock) { - mBuffer.addNotificationToWrite(notification); + mBuffer.addNewNotificationToWrite(notification); // Each time we have new history to write to disk, schedule a write in [interval] ms if (mBuffer.getHistoryCount() == 1) { mFileWriteHandler.postDelayed(mWriteBufferRunnable, WRITE_BUFFER_INTERVAL_MS); diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java index 9ad6986f2f90e..5b5ad877081cf 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java @@ -185,6 +185,20 @@ public class NotificationHistoryDatabaseTest extends UiServiceTestCase { verify(mFileWriteHandler, times(1)).postDelayed(any(), anyLong()); } + @Test + public void testAddNotification_newestFirst() { + HistoricalNotification n = getHistoricalNotification(1); + HistoricalNotification n2 = getHistoricalNotification(2); + + mDataBase.addNotification(n); + + // second add should not trigger another write + mDataBase.addNotification(n2); + + assertThat(mDataBase.mBuffer.getNotificationsToWrite().get(0)).isEqualTo(n2); + assertThat(mDataBase.mBuffer.getNotificationsToWrite().get(1)).isEqualTo(n); + } + @Test public void testReadNotificationHistory_readsAllFiles() throws Exception { for (long i = 10; i >= 5; i--) {