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

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

Merge "Handle malnamed files in history dir" into rvc-dev

parents 12a5e353 281e02a7
Loading
Loading
Loading
Loading
+23 −13
Original line number Diff line number Diff line
@@ -250,24 +250,34 @@ public class NotificationHistoryDatabase {

            for (int i = mHistoryFiles.size() - 1; i >= 0; i--) {
                final AtomicFile currentOldestFile = mHistoryFiles.get(i);
                final long creationTime = Long.parseLong(currentOldestFile.getBaseFile().getName());
                try {
                    final long creationTime = Long.parseLong(
                            currentOldestFile.getBaseFile().getName());
                    if (DEBUG) {
                        Slog.d(TAG, "File " + currentOldestFile.getBaseFile().getName()
                                + " created on " + creationTime);
                    }
                    if (creationTime <= retentionBoundary.getTimeInMillis()) {
                    if (DEBUG) {
                        Slog.d(TAG, "Removed " + currentOldestFile.getBaseFile().getName());
                    }
                    currentOldestFile.delete();
                    // TODO: delete all relevant bitmaps, once they exist
                    mHistoryFiles.removeLast();
                        deleteFile(currentOldestFile);
                    } else {
                        // all remaining files are newer than the cut off; schedule jobs to delete
                    scheduleDeletion(currentOldestFile.getBaseFile(), creationTime, retentionDays);
                        scheduleDeletion(
                                currentOldestFile.getBaseFile(), creationTime, retentionDays);
                    }
                } catch (NumberFormatException e) {
                    deleteFile(currentOldestFile);
                }
            }
        }
    }

    private void deleteFile(AtomicFile file) {
        if (DEBUG) {
            Slog.d(TAG, "Removed " + file.getBaseFile().getName());
        }
        file.delete();
        // TODO: delete all relevant bitmaps, once they exist
        mHistoryFiles.removeLast();
    }

    private void scheduleDeletion(File file, long creationTime, int retentionDays) {
+22 −0
Original line number Diff line number Diff line
@@ -146,7 +146,29 @@ public class NotificationHistoryDatabaseTest extends UiServiceTestCase {
        assertThat(mDataBase.mHistoryFiles).containsExactlyElementsIn(expectedFiles);

        verify(mAlarmManager, times(6)).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
    }

    @Test
    public void testPrune_badFileName() {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTimeInMillis(10);
        int retainDays = 1;

        List<AtomicFile> expectedFiles = new ArrayList<>();

        // add 5 files with a creation date of "today", but the file names are bad
        for (long i = cal.getTimeInMillis(); i >= 5; i--) {
            File file = mock(File.class);
            when(file.getName()).thenReturn(i + ".txt");
            AtomicFile af = new AtomicFile(file);
            mDataBase.mHistoryFiles.addLast(af);
        }

        // trim everything a day+ old
        cal.add(Calendar.DATE, 1 * retainDays);
        mDataBase.prune(retainDays, cal.getTimeInMillis());

        assertThat(mDataBase.mHistoryFiles).containsExactlyElementsIn(expectedFiles);
    }

    @Test