diff --git a/services/core/java/com/android/server/notification/NotificationHistoryDatabase.java b/services/core/java/com/android/server/notification/NotificationHistoryDatabase.java index 377e731fbeeb4..2e8b76ee75fad 100644 --- a/services/core/java/com/android/server/notification/NotificationHistoryDatabase.java +++ b/services/core/java/com/android/server/notification/NotificationHistoryDatabase.java @@ -266,13 +266,17 @@ 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) { if (DEBUG) { Slog.d(TAG, "Scheduling deletion for " + file.getName() + " at " + 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); 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 b7bcbfb055312..7e5469090d82e 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java @@ -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()); + } }