1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.dvr; 18 19 import android.support.annotation.MainThread; 20 import android.support.annotation.VisibleForTesting; 21 22 import com.android.tv.util.Clock; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 import java.util.concurrent.TimeUnit; 27 28 /** 29 * Deletes {@link ScheduledRecording} older than {@value @DAYS} days. 30 */ 31 class ScheduledProgramReaper implements Runnable { 32 33 @VisibleForTesting 34 static final int DAYS = 2; 35 private final WritableDvrDataManager mDvrDataManager; 36 private final Clock mClock; 37 ScheduledProgramReaper(WritableDvrDataManager dvrDataManager, Clock clock)38 ScheduledProgramReaper(WritableDvrDataManager dvrDataManager, Clock clock) { 39 mDvrDataManager = dvrDataManager; 40 mClock = clock; 41 } 42 43 @Override 44 @MainThread run()45 public void run() { 46 long cutoff = mClock.currentTimeMillis() - TimeUnit.DAYS.toMillis(DAYS); 47 List<ScheduledRecording> toRemove = new ArrayList<>(); 48 for (ScheduledRecording r : mDvrDataManager.getAllScheduledRecordings()) { 49 // Do not remove the schedules if it belongs to the series recording and was finished 50 // successfully. The schedule is necessary for checking the scheduled episode of the 51 // series recording. 52 if (r.getEndTimeMs() < cutoff 53 && (r.getSeriesRecordingId() == SeriesRecording.ID_NOT_SET 54 || r.getState() != ScheduledRecording.STATE_RECORDING_FINISHED)) { 55 toRemove.add(r); 56 } 57 } 58 for (ScheduledRecording r : mDvrDataManager.getDeletedSchedules()) { 59 if (r.getEndTimeMs() < cutoff) { 60 toRemove.add(r); 61 } 62 } 63 if (!toRemove.isEmpty()) { 64 mDvrDataManager.removeScheduledRecording(ScheduledRecording.toArray(toRemove)); 65 } 66 } 67 } 68