DvrDataManagerInMemoryImpl.java revision 1abddd9f6225298066094e20a6c29061b6af4590
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.Nullable;
21import android.support.annotation.VisibleForTesting;
22import android.util.Range;
23
24import java.util.ArrayList;
25import java.util.Collections;
26import java.util.HashMap;
27import java.util.List;
28import java.util.Map;
29
30/**
31 * A DVR Data manager that stores values in memory suitable for testing.
32 */
33@VisibleForTesting // TODO(DVR): move to testing dir.
34public final class DvrDataManagerInMemoryImpl extends BaseDvrDataManager {
35    private final Map<Long, Recording> mRecordings = new HashMap<>();
36    private List<SeasonRecording> mSeasonSchedule = new ArrayList<>();
37
38    DvrDataManagerInMemoryImpl(Context context) {
39        super(context);
40    }
41
42    @Override
43    public boolean isInitialized() {
44        return true;
45    }
46
47    @Override
48    public List<Recording> getRecordings() {
49        return new ArrayList(mRecordings.values());
50    }
51
52    @Override
53    public List<Recording> getFinishedRecordings() {
54        //TODO filter
55        return new ArrayList(mRecordings.values());
56    }
57
58    @Override
59    public List<Recording> getStartedRecordings() {
60        return null;
61    }
62
63    @Override
64    public List<Recording> getScheduledRecordings() {
65        //TODO filter
66        return new ArrayList(mRecordings.values());
67    }
68
69    @Override
70    public List<SeasonRecording> getSeasonRecordings() {
71        return mSeasonSchedule;
72    }
73
74    @Override
75    public long getNextScheduledStartTimeAfter(long startTime) {
76
77        List<Recording> temp = getScheduledRecordings();
78        Collections.sort(temp, Recording.START_TIME_COMPARATOR);
79        for (Recording r : temp) {
80            if (r.getStartTimeMs() > startTime) {
81                return r.getStartTimeMs();
82            }
83        }
84        return DvrDataManager.NEXT_START_TIME_NOT_FOUND;
85    }
86
87    @Override
88    public List<Recording> getRecordingsThatOverlapWith(Range<Long> period) {
89        List<Recording> temp = getRecordings();
90        List<Recording> result = new ArrayList<>();
91        for (Recording r : temp) {
92            if (r.isOverLapping(period)) {
93                result.add(r);
94            }
95        }
96        return result;
97    }
98
99    /**
100     * Add a new recording.
101     */
102    @Override
103    public void addRecording(Recording recording) {
104        mRecordings.put(recording.getId(), recording);
105        notifyRecordingAdded(recording);
106    }
107
108    @Override
109    public void addSeasonRecording(SeasonRecording seasonRecording) {
110        mSeasonSchedule.add(seasonRecording);
111    }
112
113    @Override
114    public void removeRecording(Recording recording) {
115        mRecordings.remove(recording.getId());
116        notifyRecordingRemoved(recording);
117    }
118
119    @Override
120    public void removeSeasonSchedule(SeasonRecording seasonSchedule) {
121        mSeasonSchedule.remove(seasonSchedule);
122    }
123
124    @Override
125    public void updateRecording(Recording r) {
126        long id = r.getId();
127        if (mRecordings.containsKey(id)) {
128            mRecordings.put(id, r);
129            notifyRecordingStatusChanged(r);
130        } else {
131            throw new IllegalArgumentException("Recording not found:" + r);
132        }
133    }
134
135    @Nullable
136    public Recording getRecording(long id) {
137        return mRecordings.get(id);
138    }
139}
140