aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/DvrScheduleManager.java
blob: aa77c400deb39c7cd9aa6221ce7c8f9de2f0a83f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.dvr;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.tv.TvInputInfo;
import android.os.Build;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.util.ArraySet;
import android.util.Range;

import com.android.tv.ApplicationSingletons;
import com.android.tv.TvApplication;
import com.android.tv.common.SoftPreconditions;
import com.android.tv.data.Channel;
import com.android.tv.data.ChannelDataManager;
import com.android.tv.data.Program;
import com.android.tv.dvr.DvrDataManager.OnDvrScheduleLoadFinishedListener;
import com.android.tv.dvr.DvrDataManager.ScheduledRecordingListener;
import com.android.tv.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * A class to manage the schedules.
 */
@TargetApi(Build.VERSION_CODES.N)
@MainThread
public class DvrScheduleManager {
    private static final String TAG = "DvrScheduleManager";

    /**
     * The default priority of scheduled recording.
     */
    public static final long DEFAULT_PRIORITY = Long.MAX_VALUE >> 1;
    /**
     * The default priority of series recording.
     */
    public static final long DEFAULT_SERIES_PRIORITY = DEFAULT_PRIORITY >> 1;
    // The new priority will have the offset from the existing one.
    private static final long PRIORITY_OFFSET = 1024;

    private final Context mContext;
    private final DvrDataManagerImpl mDataManager;
    private final ChannelDataManager mChannelDataManager;

    private final Map<String, List<ScheduledRecording>> mInputScheduleMap = new HashMap<>();
    private final Map<String, List<ScheduledRecording>> mInputConflictMap = new HashMap<>();

    private boolean mInitialized;

    private final Set<ScheduledRecordingListener> mScheduledRecordingListeners = new ArraySet<>();
    private final Set<OnConflictStateChangeListener> mOnConflictStateChangeListeners =
            new ArraySet<>();

    public DvrScheduleManager(Context context) {
        mContext = context;
        ApplicationSingletons appSingletons = TvApplication.getSingletons(context);
        mDataManager = (DvrDataManagerImpl) appSingletons.getDvrDataManager();
        mChannelDataManager = appSingletons.getChannelDataManager();
        if (mDataManager.isDvrScheduleLoadFinished() && mChannelDataManager.isDbLoadFinished()) {
            buildData();
        } else {
            mDataManager.addDvrScheduleLoadFinishedListener(
                    new OnDvrScheduleLoadFinishedListener() {
                        @Override
                        public void onDvrScheduleLoadFinished() {
                            mDataManager.removeDvrScheduleLoadFinishedListener(this);
                            if (mChannelDataManager.isDbLoadFinished() && !mInitialized) {
                                buildData();
                            }
                        }
                    });
        }
        ScheduledRecordingListener scheduledRecordingListener = new ScheduledRecordingListener() {
            @Override
            public void onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings) {
                if (!mInitialized) {
                    return;
                }
                for (ScheduledRecording schedule : scheduledRecordings) {
                    if (!schedule.isNotStarted() && !schedule.isInProgress()) {
                        continue;
                    }
                    TvInputInfo input = Utils.getTvInputInfoForChannelId(mContext,
                            schedule.getChannelId());
                    if (input == null) {
                        // Input removed.
                        continue;
                    }
                    String inputId = input.getId();
                    List<ScheduledRecording> schedules = mInputScheduleMap.get(inputId);
                    if (schedules == null) {
                        schedules = new ArrayList<>();
                        mInputScheduleMap.put(inputId, schedules);
                    }
                    schedules.add(schedule);
                }
                onSchedulesChanged();
                notifyScheduledRecordingAdded(scheduledRecordings);
            }

            @Override
            public void onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings) {
                if (!mInitialized) {
                    return;
                }
                for (ScheduledRecording schedule : scheduledRecordings) {
                    TvInputInfo input = Utils
                            .getTvInputInfoForChannelId(mContext, schedule.getChannelId());
                    if (input == null) {
                        // Input removed.
                        continue;
                    }
                    String inputId = input.getId();
                    List<ScheduledRecording> schedules = mInputScheduleMap.get(inputId);
                    if (schedules != null) {
                        schedules.remove(schedule);
                        if (schedules.isEmpty()) {
                            mInputScheduleMap.remove(inputId);
                        }
                    }
                }
                onSchedulesChanged();
                notifyScheduledRecordingRemoved(scheduledRecordings);
            }

            @Override
            public void onScheduledRecordingStatusChanged(
                    ScheduledRecording... scheduledRecordings) {
                if (!mInitialized) {
                    return;
                }
                for (ScheduledRecording schedule : scheduledRecordings) {
                    TvInputInfo input = Utils
                            .getTvInputInfoForChannelId(mContext, schedule.getChannelId());
                    if (input == null) {
                        // Input removed.
                        continue;
                    }
                    String inputId = input.getId();
                    List<ScheduledRecording> schedules = mInputScheduleMap.get(inputId);
                    if (schedules == null) {
                        schedules = new ArrayList<>();
                        mInputScheduleMap.put(inputId, schedules);
                    }
                    // Compare ID because ScheduledRecording.equals() doesn't work if the state
                    // is changed.
                    Iterator<ScheduledRecording> i = schedules.iterator();
                    while (i.hasNext()) {
                        if (i.next().getId() == schedule.getId()) {
                            i.remove();
                            break;
                        }
                    }
                    if (schedule.isNotStarted() || schedule.isInProgress()) {
                        schedules.add(schedule);
                    }
                    if (schedules.isEmpty()) {
                        mInputScheduleMap.remove(inputId);
                    }
                }
                onSchedulesChanged();
                notifyScheduledRecordingStatusChanged(scheduledRecordings);
            }
        };
        mDataManager.addScheduledRecordingListener(scheduledRecordingListener);
        ChannelDataManager.Listener channelDataManagerListener = new ChannelDataManager.Listener() {
            @Override
            public void onLoadFinished() {
                if (mDataManager.isDvrScheduleLoadFinished() && !mInitialized) {
                    buildData();
                }
            }

            @Override
            public void onChannelListUpdated() {
                if (mDataManager.isDvrScheduleLoadFinished()) {
                    buildData();
                }
            }

            @Override
            public void onChannelBrowsableChanged() {
            }
        };
        mChannelDataManager.addListener(channelDataManagerListener);
    }

    /**
     * Returns the started recordings for the given input.
     */
    private List<ScheduledRecording> getStartedRecordings(String inputId) {
        if (!SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet")) {
            return Collections.emptyList();
        }
        List<ScheduledRecording> result = new ArrayList<>();
        List<ScheduledRecording> schedules = mInputScheduleMap.get(inputId);
        if (schedules != null) {
            for (ScheduledRecording schedule : schedules) {
                if (schedule.getState() == ScheduledRecording.STATE_RECORDING_IN_PROGRESS) {
                    result.add(schedule);
                }
            }
        }
        return result;
    }

    private void buildData() {
        mInputScheduleMap.clear();
        for (ScheduledRecording schedule : mDataManager.getAllScheduledRecordings()) {
            if (!schedule.isNotStarted() && !schedule.isInProgress()) {
                continue;
            }
            Channel channel = mChannelDataManager.getChannel(schedule.getChannelId());
            if (channel != null) {
                String inputId = channel.getInputId();
                // Do not check whether the input is valid or not. The input might be temporarily
                // invalid.
                List<ScheduledRecording> schedules = mInputScheduleMap.get(inputId);
                if (schedules == null) {
                    schedules = new ArrayList<>();
                    mInputScheduleMap.put(inputId, schedules);
                }
                schedules.add(schedule);
            }
        }
        mInitialized = true;
        onSchedulesChanged();
    }

    private void onSchedulesChanged() {
        List<ScheduledRecording> addedConflicts = new ArrayList<>();
        List<ScheduledRecording> removedConflicts = new ArrayList<>();
        for (String inputId : mInputScheduleMap.keySet()) {
            List<ScheduledRecording> oldConflicts = mInputConflictMap.get(inputId);
            Map<Long, ScheduledRecording> oldConflictMap = new HashMap<>();
            if (oldConflicts != null) {
                for (ScheduledRecording r : oldConflicts) {
                    oldConflictMap.put(r.getId(), r);
                }
            }
            List<ScheduledRecording> conflicts = getConflictingSchedules(inputId);
            for (ScheduledRecording r : conflicts) {
                if (oldConflictMap.remove(r.getId()) == null) {
                    addedConflicts.add(r);
                }
            }
            removedConflicts.addAll(oldConflictMap.values());
            if (conflicts.isEmpty()) {
                mInputConflictMap.remove(inputId);
            } else {
                mInputConflictMap.put(inputId, conflicts);
            }
        }
        if (!removedConflicts.isEmpty()) {
            notifyConflictStateChange(false, ScheduledRecording.toArray(removedConflicts));
        }
        if (!addedConflicts.isEmpty()) {
            notifyConflictStateChange(true, ScheduledRecording.toArray(addedConflicts));
        }
    }

    /**
     * Returns {@code true} if this class has been initialized.
     */
    public boolean isInitialized() {
        return mInitialized;
    }

    /**
     * Adds a {@link ScheduledRecordingListener}.
     */
    public final void addScheduledRecordingListener(ScheduledRecordingListener listener) {
        mScheduledRecordingListeners.add(listener);
    }

    /**
     * Removes a {@link ScheduledRecordingListener}.
     */
    public final void removeScheduledRecordingListener(ScheduledRecordingListener listener) {
        mScheduledRecordingListeners.remove(listener);
    }

    /**
     * Calls {@link ScheduledRecordingListener#onScheduledRecordingAdded} for each listener.
     */
    private void notifyScheduledRecordingAdded(ScheduledRecording... scheduledRecordings) {
        for (ScheduledRecordingListener l : mScheduledRecordingListeners) {
            l.onScheduledRecordingAdded(scheduledRecordings);
        }
    }

    /**
     * Calls {@link ScheduledRecordingListener#onScheduledRecordingRemoved} for each listener.
     */
    private void notifyScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings) {
        for (ScheduledRecordingListener l : mScheduledRecordingListeners) {
            l.onScheduledRecordingRemoved(scheduledRecordings);
        }
    }

    /**
     * Calls {@link ScheduledRecordingListener#onScheduledRecordingStatusChanged} for each listener.
     */
    private void notifyScheduledRecordingStatusChanged(ScheduledRecording... scheduledRecordings) {
        for (ScheduledRecordingListener l : mScheduledRecordingListeners) {
            l.onScheduledRecordingStatusChanged(scheduledRecordings);
        }
    }

    /**
     * Adds a {@link OnConflictStateChangeListener}.
     */
    public final void addOnConflictStateChangeListener(OnConflictStateChangeListener listener) {
        mOnConflictStateChangeListeners.add(listener);
    }

    /**
     * Removes a {@link OnConflictStateChangeListener}.
     */
    public final void removeOnConflictStateChangeListener(OnConflictStateChangeListener listener) {
        mOnConflictStateChangeListeners.remove(listener);
    }

    /**
     * Calls {@link OnConflictStateChangeListener#onConflictStateChange} for each listener.
     */
    private void notifyConflictStateChange(boolean conflict,
            ScheduledRecording... scheduledRecordings) {
        for (OnConflictStateChangeListener l : mOnConflictStateChangeListeners) {
            l.onConflictStateChange(conflict, scheduledRecordings);
        }
    }

    /**
     * Returns the priority for the program if it is recorded.
     * <p>
     * The recording will have the higher priority than the existing ones.
     */
    public long suggestNewPriority() {
        if (!SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet")) {
            return DEFAULT_PRIORITY;
        }
        return suggestHighestPriority();
    }

    private long suggestHighestPriority() {
        long highestPriority = DEFAULT_PRIORITY - PRIORITY_OFFSET;
        for (ScheduledRecording schedule : mDataManager.getAllScheduledRecordings()) {
            if (schedule.getPriority() > highestPriority) {
                highestPriority = schedule.getPriority();
            }
        }
        return highestPriority + PRIORITY_OFFSET;
    }

    /**
     * Returns the priority for a series recording.
     * <p>
     * The recording will have the higher priority than the existing series.
     */
    public long suggestNewSeriesPriority() {
        if (!SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet")) {
            return DEFAULT_SERIES_PRIORITY;
        }
        return suggestHighestSeriesPriority();
    }

    /**
     * Returns the priority for a series recording by order of series recording priority.
     *
     * Higher order will have higher priority.
     */
    public static long suggestSeriesPriority(int order) {
        return DEFAULT_SERIES_PRIORITY + order * PRIORITY_OFFSET;
    }

    private long suggestHighestSeriesPriority() {
        long highestPriority = DEFAULT_SERIES_PRIORITY - PRIORITY_OFFSET;
        for (SeriesRecording schedule : mDataManager.getSeriesRecordings()) {
            if (schedule.getPriority() > highestPriority) {
                highestPriority = schedule.getPriority();
            }
        }
        return highestPriority + PRIORITY_OFFSET;
    }

    /**
     * Returns priority ordered list of all scheduled recordings that will not be recorded if
     * this program is.
     * <p>
     * Any empty list means there is no conflicts.  If there is conflict the program must be
     * scheduled to record with a priority higher than the first recording in the list returned.
     */
    public List<ScheduledRecording> getConflictingSchedules(Program program) {
        SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet");
        SoftPreconditions.checkState(Program.isValid(program), TAG,
                "Program is invalid: " + program);
        SoftPreconditions.checkState(
                program.getStartTimeUtcMillis() < program.getEndTimeUtcMillis(), TAG,
                "Program duration is empty: " + program);
        if (!mInitialized || !Program.isValid(program)
                || program.getStartTimeUtcMillis() >= program.getEndTimeUtcMillis()) {
            return Collections.emptyList();
        }
        TvInputInfo input = Utils.getTvInputInfoForProgram(mContext, program);
        if (input == null || !input.canRecord() || input.getTunerCount() <= 0) {
            return Collections.emptyList();
        }
        return getConflictingSchedules(input, Collections.singletonList(
                ScheduledRecording.builder(input.getId(), program)
                        .setPriority(suggestHighestPriority())
                        .build()));
    }

    /**
     * Returns priority ordered list of all scheduled recordings that will not be recorded if
     * this channel is.
     * <p>
     * Any empty list means there is no conflicts.  If there is conflict the channel must be
     * scheduled to record with a priority higher than the first recording in the list returned.
     */
    public List<ScheduledRecording> getConflictingSchedules(long channelId, long startTimeMs,
            long endTimeMs) {
        SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet");
        SoftPreconditions.checkState(channelId != Channel.INVALID_ID, TAG, "Invalid channel ID");
        SoftPreconditions.checkState(startTimeMs < endTimeMs, TAG, "Recording duration is empty.");
        if (!mInitialized || channelId == Channel.INVALID_ID || startTimeMs >= endTimeMs) {
            return Collections.emptyList();
        }
        TvInputInfo input = Utils.getTvInputInfoForChannelId(mContext, channelId);
        if (input == null || !input.canRecord() || input.getTunerCount() <= 0) {
            return Collections.emptyList();
        }
        return getConflictingSchedules(input, Collections.singletonList(
                ScheduledRecording.builder(input.getId(), channelId, startTimeMs, endTimeMs)
                        .setPriority(suggestHighestPriority())
                        .build()));
    }

    /**
     * Returns all the scheduled recordings that conflicts and will not be recorded or clipped for
     * the given input.
     */
    @NonNull
    private List<ScheduledRecording> getConflictingSchedules(String inputId) {
        SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet");
        TvInputInfo input = Utils.getTvInputInfoForInputId(mContext, inputId);
        SoftPreconditions.checkState(input != null, TAG, "Can't find input for : " + inputId);
        if (!mInitialized || input == null) {
            return Collections.emptyList();
        }
        List<ScheduledRecording> schedules = mInputScheduleMap.get(input.getId());
        if (schedules == null || schedules.isEmpty()) {
            return Collections.emptyList();
        }
        return getConflictingSchedules(schedules, input.getTunerCount());
    }

    /**
     * Checks if the schedule is conflicting.
     *
     * <p>Note that the {@code schedule} should be the existing one. If not, this returns
     * {@code false}.
     */
    public boolean isConflicting(ScheduledRecording schedule) {
        SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet");
        TvInputInfo input = Utils.getTvInputInfoForChannelId(mContext, schedule.getChannelId());
        SoftPreconditions.checkState(input != null, TAG, "Can't find input for channel ID : "
                + schedule.getChannelId());
        if (!mInitialized || input == null) {
            return false;
        }
        List<ScheduledRecording> conflicts = mInputConflictMap.get(input.getId());
        return conflicts != null && conflicts.contains(schedule);
    }

    /**
     * Returns priority ordered list of all scheduled recordings that will not be recorded if
     * this channel is tuned to.
     */
    public List<ScheduledRecording> getConflictingSchedulesForTune(long channelId) {
        SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet");
        SoftPreconditions.checkState(channelId != Channel.INVALID_ID, TAG, "Invalid channel ID");
        TvInputInfo input = Utils.getTvInputInfoForChannelId(mContext, channelId);
        SoftPreconditions.checkState(input != null, TAG, "Can't find input for channel ID: "
                + channelId);
        if (!mInitialized || channelId == Channel.INVALID_ID || input == null) {
            return Collections.emptyList();
        }
        return getConflictingSchedulesForTune(input.getId(), channelId, System.currentTimeMillis(),
                suggestHighestPriority(), getStartedRecordings(input.getId()),
                input.getTunerCount());
    }

    @VisibleForTesting
    public static List<ScheduledRecording> getConflictingSchedulesForTune(String inputId,
            long channelId, long currentTimeMs, long newPriority,
            List<ScheduledRecording> startedRecordings, int tunerCount) {
        boolean channelFound = false;
        for (ScheduledRecording schedule : startedRecordings) {
            if (schedule.getChannelId() == channelId) {
                channelFound = true;
                break;
            }
        }
        List<ScheduledRecording> schedules;
        if (!channelFound) {
            // The current channel is not being recorded.
            schedules = new ArrayList<>(startedRecordings);
            schedules.add(ScheduledRecording
                    .builder(inputId, channelId, currentTimeMs, currentTimeMs + 1)
                    .setPriority(newPriority)
                    .build());
        } else {
            schedules = startedRecordings;
        }
        return getConflictingSchedules(schedules, tunerCount);
    }

    /**
     * Returns priority ordered list of all scheduled recordings that will not be recorded if
     * the user keeps watching this channel.
     * <p>
     * Note that if the user keeps watching the channel, the channel can be recorded.
     */
    public List<ScheduledRecording> getConflictingSchedulesForWatching(long channelId) {
        SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet");
        SoftPreconditions.checkState(channelId != Channel.INVALID_ID, TAG, "Invalid channel ID");
        TvInputInfo input = Utils.getTvInputInfoForChannelId(mContext, channelId);
        SoftPreconditions.checkState(input != null, TAG, "Can't find input for channel ID: "
                + channelId);
        if (!mInitialized || channelId == Channel.INVALID_ID || input == null) {
            return Collections.emptyList();
        }
        List<ScheduledRecording> schedules = mInputScheduleMap.get(input.getId());
        if (schedules == null || schedules.isEmpty()) {
            return Collections.emptyList();
        }
        return getConflictingSchedulesForWatching(input.getId(), channelId,
                System.currentTimeMillis(), suggestNewPriority(), schedules, input.getTunerCount());
    }

    private List<ScheduledRecording> getConflictingSchedules(TvInputInfo input,
            List<ScheduledRecording> schedulesToAdd) {
        SoftPreconditions.checkNotNull(input);
        if (input == null || !input.canRecord() || input.getTunerCount() <= 0) {
            return Collections.emptyList();
        }
        List<ScheduledRecording> currentSchedules = mInputScheduleMap.get(input.getId());
        if (currentSchedules == null || currentSchedules.isEmpty()) {
            return Collections.emptyList();
        }
        return getConflictingSchedules(schedulesToAdd, currentSchedules, input.getTunerCount());
    }

    @VisibleForTesting
    static List<ScheduledRecording> getConflictingSchedulesForWatching(String inputId,
            long channelId, long currentTimeMs, long newPriority,
            @NonNull List<ScheduledRecording> schedules, int tunerCount) {
        List<ScheduledRecording> schedulesToCheck = new ArrayList<>(schedules);
        List<ScheduledRecording> schedulesSameChannel = new ArrayList<>();
        for (ScheduledRecording schedule : schedules) {
            if (schedule.getChannelId() == channelId) {
                schedulesSameChannel.add(schedule);
                schedulesToCheck.remove(schedule);
            }
        }
        // Assume that the user will watch the current channel forever.
        schedulesToCheck.add(ScheduledRecording
                .builder(inputId, channelId, currentTimeMs, Long.MAX_VALUE)
                .setPriority(newPriority)
                .build());
        List<ScheduledRecording> result = new ArrayList<>();
        result.addAll(getConflictingSchedules(schedulesSameChannel, 1));
        result.addAll(getConflictingSchedules(schedulesToCheck, tunerCount));
        Collections.sort(result, ScheduledRecording.PRIORITY_COMPARATOR);
        return result;
    }

    @VisibleForTesting
    static List<ScheduledRecording> getConflictingSchedules(List<ScheduledRecording> schedulesToAdd,
            List<ScheduledRecording> currentSchedules, int tunerCount) {
        List<ScheduledRecording> schedulesToCheck = new ArrayList<>(currentSchedules);
        // When the duplicate schedule is to be added, remove the current duplicate recording.
        for (Iterator<ScheduledRecording> iter = schedulesToCheck.iterator(); iter.hasNext(); ) {
            ScheduledRecording schedule = iter.next();
            for (ScheduledRecording toAdd : schedulesToAdd) {
                if (schedule.getType() == ScheduledRecording.TYPE_PROGRAM) {
                    if (toAdd.getProgramId() == schedule.getProgramId()) {
                        iter.remove();
                        break;
                    }
                } else {
                    if (toAdd.getChannelId() == schedule.getChannelId()
                            && toAdd.getStartTimeMs() == schedule.getStartTimeMs()
                            && toAdd.getEndTimeMs() == schedule.getEndTimeMs()) {
                        iter.remove();
                        break;
                    }
                }
            }
        }
        schedulesToCheck.addAll(schedulesToAdd);
        List<Range<Long>> ranges = new ArrayList<>();
        for (ScheduledRecording schedule : schedulesToAdd) {
            ranges.add(new Range<>(schedule.getStartTimeMs(), schedule.getEndTimeMs()));
        }
        return getConflictingSchedules(schedulesToCheck, tunerCount, ranges);
    }

    /**
     * Returns all conflicting scheduled recordings for the given schedules and count of tuner.
     */
    public static List<ScheduledRecording> getConflictingSchedules(
            List<ScheduledRecording> schedules, int tunerCount) {
        return getConflictingSchedules(schedules, tunerCount,
                Collections.singletonList(new Range<>(Long.MIN_VALUE, Long.MAX_VALUE)));
    }

    @VisibleForTesting
    static List<ScheduledRecording> getConflictingSchedules(List<ScheduledRecording> schedules,
            int tunerCount, List<Range<Long>> periods) {
        List<ScheduledRecording> schedulesToCheck = new ArrayList<>();
        // Filter out non-overlapping or empty duration of schedules.
        for (ScheduledRecording schedule : schedules) {
            for (Range<Long> period : periods) {
                if (schedule.isOverLapping(period)
                        && schedule.getStartTimeMs() < schedule.getEndTimeMs()) {
                    schedulesToCheck.add(schedule);
                    break;
                }
            }
        }
        // Sort by the end time.
        // If a.end <= b.end <= c.end and a overlaps with b and c, then b overlaps with c.
        // Likewise, if a1.end <= a2.end <= ... , all the schedules which overlap with a1 overlap
        // with each other.
        Collections.sort(schedulesToCheck, ScheduledRecording.END_TIME_COMPARATOR);
        Set<ScheduledRecording> conflicts = new ArraySet<>();
        List<ScheduledRecording> overlaps = new ArrayList<>();
        for (int i = 0; i < schedulesToCheck.size(); ++i) {
            ScheduledRecording r1 = schedulesToCheck.get(i);
            if (conflicts.contains(r1)) {
                // No need to check r1 because it's a conflicting schedule already.
                continue;
            }
            overlaps.clear();
            overlaps.add(r1);
            // Find schedules which overlap with r1.
            for (int j = i + 1; j < schedulesToCheck.size(); ++j) {
                ScheduledRecording r2 = schedulesToCheck.get(j);
                if (!conflicts.contains(r2) && r1.getEndTimeMs() > r2.getStartTimeMs()) {
                    overlaps.add(r2);
                }
            }
            Collections.sort(overlaps, ScheduledRecording.PRIORITY_COMPARATOR);
            // If there are more than one overlapping schedules for the same channel, only one
            // schedule will be recorded.
            HashSet<Long> channelIds = new HashSet<>();
            for (Iterator<ScheduledRecording> iter = overlaps.iterator(); iter.hasNext(); ) {
                ScheduledRecording schedule = iter.next();
                if (channelIds.contains(schedule.getChannelId())) {
                    conflicts.add(schedule);
                    iter.remove();
                } else {
                    channelIds.add(schedule.getChannelId());
                }
            }
            if (overlaps.size() > tunerCount) {
                conflicts.addAll(overlaps.subList(tunerCount, overlaps.size()));
            }
        }
        List<ScheduledRecording> result = new ArrayList<>(conflicts);
        Collections.sort(result, ScheduledRecording.PRIORITY_COMPARATOR);
        return result;
    }

    /**
     * A listener which is notified the conflict state change of the schedules.
     */
    public interface OnConflictStateChangeListener {
        /**
         * Called when the conflicting schedules change.
         *
         * @param conflict {@code true} if the {@code schedules} are the new conflicts, otherwise
         * {@code false}.
         * @param schedules the schedules
         */
        void onConflictStateChange(boolean conflict, ScheduledRecording... schedules);
    }
}