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

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

Merge "Schedule delete for files as soon as they are written" into rvc-dev

parents 5bebe02d 32290f7e
Loading
Loading
Loading
Loading
+23 −8
Original line number Diff line number Diff line
@@ -266,11 +266,15 @@ public class NotificationHistoryDatabase {
                    mHistoryFiles.removeLast();
                } else {
                    // all remaining files are newer than the cut off; schedule jobs to delete
                    final long deletionTime = creationTime + (retentionDays * HISTORY_RETENTION_MS);
                    scheduleDeletion(currentOldestFile.getBaseFile(), deletionTime);
                    scheduleDeletion(currentOldestFile.getBaseFile(), creationTime, retentionDays);
                }
            }
        }
    }

    private void scheduleDeletion(File file, long creationTime, int retentionDays) {
        final long deletionTime = creationTime + (retentionDays * HISTORY_RETENTION_MS);
        scheduleDeletion(file, deletionTime);
    }

    private void scheduleDeletion(File file, long deletionTime) {
@@ -330,17 +334,28 @@ public class NotificationHistoryDatabase {
        }
    };

    private final class WriteBufferRunnable implements Runnable {
    final class WriteBufferRunnable implements Runnable {
        long currentTime = 0;
        AtomicFile latestNotificationsFile;

        @Override
        public void run() {
            if (DEBUG) Slog.d(TAG, "WriteBufferRunnable");
            synchronized (mLock) {
                final AtomicFile latestNotificationsFiles = new AtomicFile(
                        new File(mHistoryDir, String.valueOf(System.currentTimeMillis())));
                if (currentTime == 0) {
                    currentTime = System.currentTimeMillis();
                }
                if (latestNotificationsFile == null) {
                    latestNotificationsFile = new AtomicFile(
                            new File(mHistoryDir, String.valueOf(currentTime)));
                }
                try {
                    writeLocked(latestNotificationsFiles, mBuffer);
                    mHistoryFiles.addFirst(latestNotificationsFiles);
                    writeLocked(latestNotificationsFile, mBuffer);
                    mHistoryFiles.addFirst(latestNotificationsFile);
                    mBuffer = new NotificationHistory();

                    scheduleDeletion(latestNotificationsFile.getBaseFile(), currentTime,
                            HISTORY_RETENTION_DAYS);
                } catch (IOException e) {
                    Slog.e(TAG, "Failed to write buffer to disk. not flushing buffer", e);
                }
@@ -440,7 +455,7 @@ public class NotificationHistoryDatabase {

        @Override
        public void run() {
            if (DEBUG) Slog.d(TAG, "RemoveConversationRunnable");
            if (DEBUG) Slog.d(TAG, "RemoveConversationRunnable " + mPkg + " "  + mConversationId);
            synchronized (mLock) {
                // Remove from pending history
                mBuffer.removeConversationFromWrite(mPkg, mConversationId);
+22 −0
Original line number Diff line number Diff line
@@ -328,4 +328,26 @@ public class NotificationHistoryDatabaseTest extends UiServiceTestCase {
        verify(nh).removeConversationFromWrite("pkg", "convo");
        verify(af, never()).startWrite();
    }

    @Test
    public void testWriteBufferRunnable() throws Exception {
        NotificationHistory nh = mock(NotificationHistory.class);
        when(nh.getPooledStringsToWrite()).thenReturn(new String[]{});
        when(nh.getNotificationsToWrite()).thenReturn(new ArrayList<>());
        NotificationHistoryDatabase.WriteBufferRunnable wbr =
                mDataBase.new WriteBufferRunnable();

        mDataBase.mBuffer = nh;
        wbr.currentTime = 5;
        wbr.latestNotificationsFile = mock(AtomicFile.class);
        File file = mock(File.class);
        when(file.getName()).thenReturn("5");
        when(wbr.latestNotificationsFile.getBaseFile()).thenReturn(file);

        wbr.run();

        assertThat(mDataBase.mHistoryFiles.size()).isEqualTo(1);
        assertThat(mDataBase.mBuffer).isNotEqualTo(nh);
        verify(mAlarmManager, times(1)).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
    }
}