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 17package com.android.tv.dvr.recorder; 18 19import android.support.annotation.MainThread; 20import android.support.annotation.VisibleForTesting; 21 22import com.android.tv.dvr.WritableDvrDataManager; 23import com.android.tv.dvr.data.ScheduledRecording; 24import com.android.tv.dvr.data.SeriesRecording; 25import com.android.tv.util.Clock; 26 27import java.util.ArrayList; 28import java.util.List; 29import java.util.concurrent.TimeUnit; 30 31/** 32 * Deletes {@link ScheduledRecording} older than {@value @DAYS} days. 33 */ 34class ScheduledProgramReaper implements Runnable { 35 36 @VisibleForTesting 37 static final int DAYS = 2; 38 private final WritableDvrDataManager mDvrDataManager; 39 private final Clock mClock; 40 41 ScheduledProgramReaper(WritableDvrDataManager dvrDataManager, Clock clock) { 42 mDvrDataManager = dvrDataManager; 43 mClock = clock; 44 } 45 46 @Override 47 @MainThread 48 public void run() { 49 long cutoff = mClock.currentTimeMillis() - TimeUnit.DAYS.toMillis(DAYS); 50 List<ScheduledRecording> toRemove = new ArrayList<>(); 51 for (ScheduledRecording r : mDvrDataManager.getAllScheduledRecordings()) { 52 // Do not remove the schedules if it belongs to the series recording and was finished 53 // successfully. The schedule is necessary for checking the scheduled episode of the 54 // series recording. 55 if (r.getEndTimeMs() < cutoff 56 && (r.getSeriesRecordingId() == SeriesRecording.ID_NOT_SET 57 || r.getState() != ScheduledRecording.STATE_RECORDING_FINISHED)) { 58 toRemove.add(r); 59 } 60 } 61 for (ScheduledRecording r : mDvrDataManager.getDeletedSchedules()) { 62 if (r.getEndTimeMs() < cutoff) { 63 toRemove.add(r); 64 } 65 } 66 if (!toRemove.isEmpty()) { 67 mDvrDataManager.removeScheduledRecording(ScheduledRecording.toArray(toRemove)); 68 } 69 } 70} 71