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.recorder;
18
19import android.annotation.TargetApi;
20import android.content.ContentUris;
21import android.media.tv.TvContract;
22import android.net.Uri;
23import android.os.Build;
24import android.os.Message;
25import android.support.annotation.MainThread;
26import android.support.annotation.NonNull;
27import android.support.annotation.Nullable;
28import android.util.ArraySet;
29import android.util.Log;
30
31import com.android.tv.ApplicationSingletons;
32import com.android.tv.InputSessionManager;
33import com.android.tv.InputSessionManager.OnTvViewChannelChangeListener;
34import com.android.tv.MainActivity;
35import com.android.tv.TvApplication;
36import com.android.tv.common.WeakHandler;
37import com.android.tv.data.Channel;
38import com.android.tv.data.ChannelDataManager;
39import com.android.tv.dvr.DvrDataManager.ScheduledRecordingListener;
40import com.android.tv.dvr.DvrScheduleManager;
41import com.android.tv.dvr.data.ScheduledRecording;
42import com.android.tv.dvr.ui.DvrUiHelper;
43
44import java.util.ArrayList;
45import java.util.HashMap;
46import java.util.List;
47import java.util.Map;
48import java.util.Set;
49import java.util.concurrent.TimeUnit;
50
51/**
52 * Checking the runtime conflict of DVR recording.
53 * <p>
54 * This class runs only while the {@link MainActivity} is resumed and holds the upcoming conflicts.
55 */
56@TargetApi(Build.VERSION_CODES.N)
57@MainThread
58public class ConflictChecker {
59    private static final String TAG = "ConflictChecker";
60    private static final boolean DEBUG = false;
61
62    private static final int MSG_CHECK_CONFLICT = 1;
63
64    private static final long CHECK_RETRY_PERIOD_MS = TimeUnit.SECONDS.toMillis(30);
65
66    /**
67     * To show watch conflict dialog, the start time of the earliest conflicting schedule should be
68     * less than or equal to this time.
69     */
70    private static final long MAX_WATCH_CONFLICT_CHECK_TIME_MS = TimeUnit.MINUTES.toMillis(5);
71    /**
72     * To show watch conflict dialog, the start time of the earliest conflicting schedule should be
73     * greater than or equal to this time.
74     */
75    private static final long MIN_WATCH_CONFLICT_CHECK_TIME_MS = TimeUnit.SECONDS.toMillis(30);
76
77    private final MainActivity mMainActivity;
78    private final ChannelDataManager mChannelDataManager;
79    private final DvrScheduleManager mScheduleManager;
80    private final InputSessionManager mSessionManager;
81    private final ConflictCheckerHandler mHandler = new ConflictCheckerHandler(this);
82
83    private final List<ScheduledRecording> mUpcomingConflicts = new ArrayList<>();
84    private final Set<OnUpcomingConflictChangeListener> mOnUpcomingConflictChangeListeners =
85            new ArraySet<>();
86    private final Map<Long, List<ScheduledRecording>> mCheckedConflictsMap = new HashMap<>();
87
88    private final ScheduledRecordingListener mScheduledRecordingListener =
89            new ScheduledRecordingListener() {
90        @Override
91        public void onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings) {
92            if (DEBUG) Log.d(TAG, "onScheduledRecordingAdded: " + scheduledRecordings);
93            mHandler.sendEmptyMessage(MSG_CHECK_CONFLICT);
94        }
95
96        @Override
97        public void onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings) {
98            if (DEBUG) Log.d(TAG, "onScheduledRecordingRemoved: " + scheduledRecordings);
99            mHandler.sendEmptyMessage(MSG_CHECK_CONFLICT);
100        }
101
102        @Override
103        public void onScheduledRecordingStatusChanged(ScheduledRecording... scheduledRecordings) {
104            if (DEBUG) Log.d(TAG, "onScheduledRecordingStatusChanged: " + scheduledRecordings);
105            mHandler.sendEmptyMessage(MSG_CHECK_CONFLICT);
106        }
107    };
108
109    private final OnTvViewChannelChangeListener mOnTvViewChannelChangeListener =
110            new OnTvViewChannelChangeListener() {
111                @Override
112                public void onTvViewChannelChange(@Nullable Uri channelUri) {
113                    mHandler.sendEmptyMessage(MSG_CHECK_CONFLICT);
114                }
115            };
116
117    private boolean mStarted;
118
119    public ConflictChecker(MainActivity mainActivity) {
120        mMainActivity = mainActivity;
121        ApplicationSingletons appSingletons = TvApplication.getSingletons(mainActivity);
122        mChannelDataManager = appSingletons.getChannelDataManager();
123        mScheduleManager = appSingletons.getDvrScheduleManager();
124        mSessionManager = appSingletons.getInputSessionManager();
125    }
126
127    /**
128     * Starts checking the conflict.
129     */
130    public void start() {
131        if (mStarted) {
132            return;
133        }
134        mStarted = true;
135        mHandler.sendEmptyMessage(MSG_CHECK_CONFLICT);
136        mScheduleManager.addScheduledRecordingListener(mScheduledRecordingListener);
137        mSessionManager.addOnTvViewChannelChangeListener(mOnTvViewChannelChangeListener);
138    }
139
140    /**
141     * Stops checking the conflict.
142     */
143    public void stop() {
144        if (!mStarted) {
145            return;
146        }
147        mStarted = false;
148        mSessionManager.removeOnTvViewChannelChangeListener(mOnTvViewChannelChangeListener);
149        mScheduleManager.removeScheduledRecordingListener(mScheduledRecordingListener);
150        mHandler.removeCallbacksAndMessages(null);
151    }
152
153    /**
154     * Returns the upcoming conflicts.
155     */
156    public List<ScheduledRecording> getUpcomingConflicts() {
157        return new ArrayList<>(mUpcomingConflicts);
158    }
159
160    /**
161     * Adds a {@link OnUpcomingConflictChangeListener}.
162     */
163    public void addOnUpcomingConflictChangeListener(OnUpcomingConflictChangeListener listener) {
164        mOnUpcomingConflictChangeListeners.add(listener);
165    }
166
167    /**
168     * Removes the {@link OnUpcomingConflictChangeListener}.
169     */
170    public void removeOnUpcomingConflictChangeListener(OnUpcomingConflictChangeListener listener) {
171        mOnUpcomingConflictChangeListeners.remove(listener);
172    }
173
174    private void notifyUpcomingConflictChanged() {
175        for (OnUpcomingConflictChangeListener l : mOnUpcomingConflictChangeListeners) {
176            l.onUpcomingConflictChange();
177        }
178    }
179
180    /**
181     * Remembers the user's decision to record while watching the channel.
182     */
183    public void setCheckedConflictsForChannel(long mChannelId, List<ScheduledRecording> conflicts) {
184        mCheckedConflictsMap.put(mChannelId, new ArrayList<>(conflicts));
185    }
186
187    void onCheckConflict() {
188        // Checks the conflicting schedules and setup the next re-check time.
189        // If there are upcoming conflicts soon, it opens the conflict dialog.
190        if (DEBUG) Log.d(TAG, "Handling MSG_CHECK_CONFLICT");
191        mHandler.removeMessages(MSG_CHECK_CONFLICT);
192        mUpcomingConflicts.clear();
193        if (!mScheduleManager.isInitialized()
194                || !mChannelDataManager.isDbLoadFinished()) {
195            mHandler.sendEmptyMessageDelayed(MSG_CHECK_CONFLICT, CHECK_RETRY_PERIOD_MS);
196            notifyUpcomingConflictChanged();
197            return;
198        }
199        if (mSessionManager.getCurrentTvViewChannelUri() == null) {
200            // As MainActivity is not using a tuner, no need to check the conflict.
201            notifyUpcomingConflictChanged();
202            return;
203        }
204        Uri channelUri = mSessionManager.getCurrentTvViewChannelUri();
205        if (TvContract.isChannelUriForPassthroughInput(channelUri)) {
206            notifyUpcomingConflictChanged();
207            return;
208        }
209        long channelId = ContentUris.parseId(channelUri);
210        Channel channel = mChannelDataManager.getChannel(channelId);
211        // The conflicts caused by watching the channel.
212        List<ScheduledRecording> conflicts = mScheduleManager
213                .getConflictingSchedulesForWatching(channel.getId());
214        long earliestToCheck = Long.MAX_VALUE;
215        long currentTimeMs = System.currentTimeMillis();
216        for (ScheduledRecording schedule : conflicts) {
217            long startTimeMs = schedule.getStartTimeMs();
218            if (startTimeMs < currentTimeMs + MIN_WATCH_CONFLICT_CHECK_TIME_MS) {
219                // The start time of the upcoming conflict remains less than the minimum
220                // check time.
221                continue;
222            }
223            if (startTimeMs > currentTimeMs + MAX_WATCH_CONFLICT_CHECK_TIME_MS) {
224                // The start time of the upcoming conflict remains greater than the
225                // maximum check time. Setup the next re-check time.
226                long nextCheckTimeMs = startTimeMs - MAX_WATCH_CONFLICT_CHECK_TIME_MS;
227                if (earliestToCheck > nextCheckTimeMs) {
228                    earliestToCheck = nextCheckTimeMs;
229                }
230            } else {
231                // Found upcoming conflicts which will start soon.
232                mUpcomingConflicts.add(schedule);
233                // The schedule will be removed from the "upcoming conflict" when the
234                // recording is almost started.
235                long nextCheckTimeMs = startTimeMs - MIN_WATCH_CONFLICT_CHECK_TIME_MS;
236                if (earliestToCheck > nextCheckTimeMs) {
237                    earliestToCheck = nextCheckTimeMs;
238                }
239            }
240        }
241        if (earliestToCheck != Long.MAX_VALUE) {
242            mHandler.sendEmptyMessageDelayed(MSG_CHECK_CONFLICT,
243                    earliestToCheck - currentTimeMs);
244        }
245        if (DEBUG) Log.d(TAG, "upcoming conflicts: " + mUpcomingConflicts);
246        notifyUpcomingConflictChanged();
247        if (!mUpcomingConflicts.isEmpty()
248                && !DvrUiHelper.isChannelWatchConflictDialogShown(mMainActivity)) {
249            // Don't show the conflict dialog if the user already knows.
250            List<ScheduledRecording> checkedConflicts = mCheckedConflictsMap.get(
251                    channel.getId());
252            if (checkedConflicts == null
253                    || !checkedConflicts.containsAll(mUpcomingConflicts)) {
254                DvrUiHelper.showChannelWatchConflictDialog(mMainActivity, channel);
255            }
256        }
257    }
258
259    private static class ConflictCheckerHandler extends WeakHandler<ConflictChecker> {
260        ConflictCheckerHandler(ConflictChecker conflictChecker) {
261            super(conflictChecker);
262        }
263
264        @Override
265        protected void handleMessage(Message msg, @NonNull ConflictChecker conflictChecker) {
266            switch (msg.what) {
267                case MSG_CHECK_CONFLICT:
268                    conflictChecker.onCheckConflict();
269                    break;
270            }
271        }
272    }
273
274    /**
275     * A listener for the change of upcoming conflicts.
276     */
277    public interface OnUpcomingConflictChangeListener {
278        void onUpcomingConflictChange();
279    }
280}
281