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

Commit 73bc1e84 authored by Julia Reynolds's avatar Julia Reynolds Committed by Android (Google) Code Review
Browse files

Merge "Fix notification history ordering"

parents 871fe0a9 1cc5f842
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -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();
    }

+10 −7
Original line number Diff line number Diff line
@@ -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());
+1 −1
Original line number Diff line number Diff line
@@ -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);
+14 −0
Original line number Diff line number Diff line
@@ -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--) {