DvrDataManagerInMemoryImpl.java revision ba5845f23b8fbc985890f892961abc8b39886611
1/* 2 * Copyright (C) 2015 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; 18 19import android.content.Context; 20import android.support.annotation.MainThread; 21import android.support.annotation.NonNull; 22import android.support.annotation.Nullable; 23import android.support.annotation.VisibleForTesting; 24import android.util.Range; 25 26import com.android.tv.util.SoftPreconditions; 27 28import java.util.ArrayList; 29import java.util.Collections; 30import java.util.HashMap; 31import java.util.List; 32import java.util.Map; 33import java.util.concurrent.atomic.AtomicLong; 34 35/** 36 * A DVR Data manager that stores values in memory suitable for testing. 37 */ 38@VisibleForTesting // TODO(DVR): move to testing dir. 39@MainThread 40public final class DvrDataManagerInMemoryImpl extends BaseDvrDataManager { 41 private final static String TAG = "DvrDataManagerInMemory"; 42 private final AtomicLong mNextId = new AtomicLong(1); 43 private final Map<Long, Recording> mRecordings = new HashMap<>(); 44 private List<SeasonRecording> mSeasonSchedule = new ArrayList<>(); 45 46 public DvrDataManagerInMemoryImpl(Context context) { 47 super(context); 48 } 49 50 @Override 51 public boolean isInitialized() { 52 return true; 53 } 54 55 @Override 56 public List<Recording> getRecordings() { 57 return new ArrayList(mRecordings.values()); 58 } 59 60 @Override 61 public List<Recording> getFinishedRecordings() { 62 return getRecordingsWithState(Recording.STATE_RECORDING_FINISHED); 63 } 64 65 @Override 66 public List<Recording> getStartedRecordings() { 67 return getRecordingsWithState(Recording.STATE_RECORDING_IN_PROGRESS); 68 } 69 70 @Override 71 public List<Recording> getScheduledRecordings() { 72 return getRecordingsWithState(Recording.STATE_RECORDING_NOT_STARTED); 73 } 74 75 @Override 76 public List<SeasonRecording> getSeasonRecordings() { 77 return mSeasonSchedule; 78 } 79 80 @Override 81 public long getNextScheduledStartTimeAfter(long startTime) { 82 83 List<Recording> temp = getScheduledRecordings(); 84 Collections.sort(temp, Recording.START_TIME_COMPARATOR); 85 for (Recording r : temp) { 86 if (r.getStartTimeMs() > startTime) { 87 return r.getStartTimeMs(); 88 } 89 } 90 return DvrDataManager.NEXT_START_TIME_NOT_FOUND; 91 } 92 93 @Override 94 public List<Recording> getRecordingsThatOverlapWith(Range<Long> period) { 95 List<Recording> temp = getRecordings(); 96 List<Recording> result = new ArrayList<>(); 97 for (Recording r : temp) { 98 if (r.isOverLapping(period)) { 99 result.add(r); 100 } 101 } 102 return result; 103 } 104 105 /** 106 * Add a new recording. 107 */ 108 @Override 109 public void addRecording(Recording recording) { 110 addRecordingInternal(recording); 111 } 112 113 public Recording addRecordingInternal(Recording recording) { 114 SoftPreconditions.checkState(recording.getId() == Recording.ID_NOT_SET, TAG, 115 "expected id of " + Recording.ID_NOT_SET + " but was " + recording); 116 recording = Recording.buildFrom(recording).setId(mNextId.incrementAndGet()).build(); 117 mRecordings.put(recording.getId(), recording); 118 notifyRecordingAdded(recording); 119 return recording; 120 } 121 122 @Override 123 public void addSeasonRecording(SeasonRecording seasonRecording) { 124 mSeasonSchedule.add(seasonRecording); 125 } 126 127 @Override 128 public void removeRecording(Recording recording) { 129 mRecordings.remove(recording.getId()); 130 notifyRecordingRemoved(recording); 131 } 132 133 @Override 134 public void removeSeasonSchedule(SeasonRecording seasonSchedule) { 135 mSeasonSchedule.remove(seasonSchedule); 136 } 137 138 @Override 139 public void updateRecording(Recording r) { 140 long id = r.getId(); 141 if (mRecordings.containsKey(id)) { 142 mRecordings.put(id, r); 143 notifyRecordingStatusChanged(r); 144 } else { 145 throw new IllegalArgumentException("Recording not found:" + r); 146 } 147 } 148 149 @Nullable 150 @Override 151 public Recording getRecording(long id) { 152 return mRecordings.get(id); 153 } 154 155 @NonNull 156 private List<Recording> getRecordingsWithState(int state) { 157 ArrayList<Recording> result = new ArrayList<>(); 158 for (Recording r : mRecordings.values()) { 159 if(r.getState() == state){ 160 result.add(r); 161 } 162 } 163 return result; 164 } 165} 166