Merge "Fix notification history ordering"

This commit is contained in:
Julia Reynolds
2020-01-21 18:58:20 +00:00
committed by Android (Google) Code Review
4 changed files with 40 additions and 9 deletions

View File

@@ -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();
}

View File

@@ -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());

View File

@@ -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);

View File

@@ -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--) {