/** public for testing purposes */
  public synchronized void removeOldBackups() {

    SortedMap<Long, File> filesMap = getBackupFiles();

    if (filesMap.size() > 0) {

      Calendar rangeStart = TaskActivityUtil.getCalendar();
      rangeStart.setTimeInMillis(filesMap.lastKey());
      TaskActivityUtil.snapStartOfHour(rangeStart);
      int startHour = rangeStart.get(Calendar.HOUR_OF_DAY);
      Calendar rangeEnd = TaskActivityUtil.getCalendar();
      rangeEnd.setTimeInMillis(rangeStart.getTimeInMillis());
      rangeEnd.add(Calendar.HOUR_OF_DAY, 1);
      // Keep one backup for last 8 hours of today
      for (int x = 0; x <= startHour && x < 9; x++) {
        SortedMap<Long, File> subMap =
            filesMap.subMap(rangeStart.getTimeInMillis(), rangeEnd.getTimeInMillis());
        if (subMap.size() > 1) {
          while (subMap.size() > 1) {
            File toDelete = subMap.remove(subMap.firstKey());
            toDelete.delete();
          }
        }
        rangeStart.add(Calendar.HOUR_OF_DAY, -1);
        rangeEnd.add(Calendar.HOUR_OF_DAY, -1);
      }

      // Keep one backup a day for the past 12 days
      TaskActivityUtil.snapStartOfDay(rangeEnd);
      rangeStart.add(Calendar.DAY_OF_YEAR, -1);
      for (int x = 1; x <= 12; x++) {
        SortedMap<Long, File> subMap =
            filesMap.subMap(rangeStart.getTimeInMillis(), rangeEnd.getTimeInMillis());
        if (subMap.size() > 1) {
          while (subMap.size() > 1) {
            File toDelete = subMap.remove(subMap.firstKey());
            toDelete.delete();
          }
        }
        rangeStart.add(Calendar.DAY_OF_YEAR, -1);
        rangeEnd.add(Calendar.DAY_OF_YEAR, -1);
      }

      // Remove all older backups
      SortedMap<Long, File> subMap = filesMap.subMap(0l, rangeStart.getTimeInMillis());
      if (subMap.size() > 0) {
        while (subMap.size() > 0) {
          File toDelete = subMap.remove(subMap.firstKey());
          toDelete.delete();
        }
      }
    }
  }
예제 #2
0
  public void testScheduledTaskSorting() {
    final TaskListInterestSorter sorter = new TaskListInterestSorter();
    MockTask task1 = new MockTask("local", "MYLN:1", "1");
    MockTask task2 = new MockTask("local", "MYLN:2", "2");

    Calendar start1 = TaskActivityUtil.getCalendar();
    start1.add(Calendar.WEEK_OF_YEAR, -1);
    TaskActivityUtil.snapStartOfWorkWeek(start1);
    Calendar end1 = TaskActivityUtil.getCalendar();
    end1.setTimeInMillis(start1.getTimeInMillis());
    TaskActivityUtil.snapEndOfWeek(end1);
    WeekDateRange range1 = new WeekDateRange(start1, end1);
    TasksUiPlugin.getTaskActivityManager().setScheduledFor(task1, range1);

    Calendar start2 = TaskActivityUtil.getCalendar();
    start2.add(Calendar.HOUR_OF_DAY, -1);
    Calendar end2 = TaskActivityUtil.getCalendar();
    DateRange range2 = new DateRange(start2, end2);
    TasksUiPlugin.getTaskActivityManager().setScheduledFor(task2, range2);

    assertTrue(sorter.compare(new EmptyViewer(), task1, task2) < 0);
  }