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