Loading services/core/java/com/android/server/notification/NotificationHistoryDatabase.java +23 −8 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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); } Loading Loading @@ -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); Loading services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java +22 −0 Original line number Diff line number Diff line Loading @@ -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()); } } Loading
services/core/java/com/android/server/notification/NotificationHistoryDatabase.java +23 −8 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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); } Loading Loading @@ -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); Loading
services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java +22 −0 Original line number Diff line number Diff line Loading @@ -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()); } }