aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/DvrScheduleManager.java
blob: b72117aa52269c6b3063ab667d2a6812e50178a4 (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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
/*
 * 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.dvr.recorder.InputTaskScheduler;
import com.android.tv.util.CompositeComparator;
import com.android.tv.dvr.data.ScheduledRecording;
import com.android.tv.dvr.data.SeriesRecording;
import com.android.tv.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 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 static final Comparator<ScheduledRecording> RESULT_COMPARATOR =
            new CompositeComparator<>(
                    ScheduledRecording.PRIORITY_COMPARATOR.reversed(),
                    ScheduledRecording.START_TIME_COMPARATOR,
                    ScheduledRecording.ID_COMPARATOR.reversed());

    // The candidate comparator should be the consistent with
    // InputTaskScheduler#CANDIDATE_COMPARATOR.
    private static final Comparator<ScheduledRecording> CANDIDATE_COMPARATOR =
            new CompositeComparator<>(
                    ScheduledRecording.PRIORITY_COMPARATOR,
                    ScheduledRecording.END_TIME_COMPARATOR,
                    ScheduledRecording.ID_COMPARATOR);

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

    private final Map<String, List<ScheduledRecording>> mInputScheduleMap = new HashMap<>();
    // The inner map is a hash map from scheduled recording to its conflicting status, i.e.,
    // the boolean value true denotes the schedule is just partially conflicting, which means
    // although there's conflict, it might still be recorded partially.
    private final Map<String, Map<Long, ConflictInfo>> mInputConflictInfoMap = new HashMap<>();

    private boolean mInitialized;

    private final Set<OnInitializeListener> mOnInitializeListeners = new CopyOnWriteArraySet<>();
    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
                            .getTvInputInfoForInputId(mContext, schedule.getInputId());
                    if (!SoftPreconditions.checkArgument(input != null, TAG,
                            "Input was removed for : " + schedule)) {
                        // Input removed.
                        mInputScheduleMap.remove(schedule.getInputId());
                        mInputConflictInfoMap.remove(schedule.getInputId());
                        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
                            .getTvInputInfoForInputId(mContext, schedule.getInputId());
                    if (input == null) {
                        // Input removed.
                        mInputScheduleMap.remove(schedule.getInputId());
                        mInputConflictInfoMap.remove(schedule.getInputId());
                        continue;
                    }
                    String inputId = input.getId();
                    List<ScheduledRecording> schedules = mInputScheduleMap.get(inputId);
                    if (schedules != null) {
                        schedules.remove(schedule);
                        if (schedules.isEmpty()) {
                            mInputScheduleMap.remove(inputId);
                        }
                    }
                    Map<Long, ConflictInfo> conflictInfo = mInputConflictInfoMap.get(inputId);
                    if (conflictInfo != null) {
                        conflictInfo.remove(schedule.getId());
                        if (conflictInfo.isEmpty()) {
                            mInputConflictInfoMap.remove(inputId);
                        }
                    }
                }
                onSchedulesChanged();
                notifyScheduledRecordingRemoved(scheduledRecordings);
            }

            @Override
            public void onScheduledRecordingStatusChanged(
                    ScheduledRecording... scheduledRecordings) {
                if (!mInitialized) {
                    return;
                }
                for (ScheduledRecording schedule : scheduledRecordings) {
                    TvInputInfo input = Utils
                            .getTvInputInfoForInputId(mContext, schedule.getInputId());
                    if (!SoftPreconditions.checkArgument(input != null, TAG,
                            "Input was removed for : " + schedule)) {
                        // Input removed.
                        mInputScheduleMap.remove(schedule.getInputId());
                        mInputConflictInfoMap.remove(schedule.getInputId());
                        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.
                    for (Iterator<ScheduledRecording> i = schedules.iterator(); 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);
                    }
                    // Update conflict list as well
                    Map<Long, ConflictInfo> conflictInfo = mInputConflictInfoMap.get(inputId);
                    if (conflictInfo != null) {
                        ConflictInfo oldConflictInfo = conflictInfo.get(schedule.getId());
                        if (oldConflictInfo != null) {
                            oldConflictInfo.schedule = schedule;
                        }
                    }
                }
                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);
            }
        }
        if (!mInitialized) {
            mInitialized = true;
            notifyInitialize();
        }
        onSchedulesChanged();
    }

    private void onSchedulesChanged() {
        // TODO: notify conflict state change when some conflicting recording becomes partially
        //       conflicting, vice versa.
        List<ScheduledRecording> addedConflicts = new ArrayList<>();
        List<ScheduledRecording> removedConflicts = new ArrayList<>();
        for (String inputId : mInputScheduleMap.keySet()) {
            Map<Long, ConflictInfo> oldConflictInfo = mInputConflictInfoMap.get(inputId);
            Map<Long, ScheduledRecording> oldConflictMap = new HashMap<>();
            if (oldConflictInfo != null) {
                for (ConflictInfo conflictInfo : oldConflictInfo.values()) {
                    oldConflictMap.put(conflictInfo.schedule.getId(), conflictInfo.schedule);
                }
            }
            List<ConflictInfo> conflicts = getConflictingSchedulesInfo(inputId);
            if (conflicts.isEmpty()) {
                mInputConflictInfoMap.remove(inputId);
            } else {
                Map<Long, ConflictInfo> conflictInfos = new HashMap<>();
                for (ConflictInfo conflictInfo : conflicts) {
                    conflictInfos.put(conflictInfo.schedule.getId(), conflictInfo);
                    if (oldConflictMap.remove(conflictInfo.schedule.getId()) == null) {
                        addedConflicts.add(conflictInfo.schedule);
                    }
                }
                mInputConflictInfoMap.put(inputId, conflictInfos);
            }
            removedConflicts.addAll(oldConflictMap.values());
        }
        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 OnInitializeListener}.
     */
    public final void addOnInitializeListener(OnInitializeListener listener) {
        mOnInitializeListeners.add(listener);
    }

    /**
     * Removes a {@link OnInitializeListener}.
     */
    public final void removeOnInitializeListener(OnInitializeListener listener) {
        mOnInitializeListeners.remove(listener);
    }

    /**
     * Calls {@link OnInitializeListener#onInitialize} for each listener.
     */
    private void notifyInitialize() {
        for (OnInitializeListener l : mOnInitializeListeners) {
            l.onInitialize();
        }
    }

    /**
     * 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;
    }

    /**
     * Suggests the higher priority than the schedules which overlap with {@code schedule}.
     */
    public long suggestHighestPriority(ScheduledRecording schedule) {
        List<ScheduledRecording> schedules = mInputScheduleMap.get(schedule.getInputId());
        if (schedules == null) {
            return DEFAULT_PRIORITY;
        }
        long highestPriority = Long.MIN_VALUE;
        for (ScheduledRecording r : schedules) {
            if (!r.equals(schedule) && r.isOverLapping(schedule)
                    && r.getPriority() > highestPriority) {
                highestPriority = r.getPriority();
            }
        }
        if (highestPriority == Long.MIN_VALUE || highestPriority < schedule.getPriority()) {
            return schedule.getPriority();
        }
        return highestPriority + PRIORITY_OFFSET;
    }

    /**
     * Suggests the higher priority than the schedules which overlap with {@code schedule}.
     */
    public long suggestHighestPriority(String inputId, Range<Long> peroid, long basePriority) {
        List<ScheduledRecording> schedules = mInputScheduleMap.get(inputId);
        if (schedules == null) {
            return DEFAULT_PRIORITY;
        }
        long highestPriority = Long.MIN_VALUE;
        for (ScheduledRecording r : schedules) {
            if (r.isOverLapping(peroid) && r.getPriority() > highestPriority) {
                highestPriority = r.getPriority();
            }
        }
        if (highestPriority == Long.MIN_VALUE || highestPriority < basePriority) {
            return basePriority;
        }
        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 a sorted list of all scheduled recordings that will not be recorded if
     * this program is going to be recorded, with their priorities in decending order.
     * <p>
     * An empty list means there is no conflicts. If there is conflict, a priority higher than
     * the first recording in the returned list should be assigned to the new schedule of this
     * program to guarantee the program would be completely recorded.
     */
    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 list of all conflicting scheduled recordings for the given {@code seriesRecording}
     * recording.
     * <p>
     * Any empty list means there is no conflicts.
     */
    public List<ScheduledRecording> getConflictingSchedules(SeriesRecording seriesRecording) {
        SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet");
        SoftPreconditions.checkState(seriesRecording != null, TAG, "series recording is null");
        if (!mInitialized || seriesRecording == null) {
            return Collections.emptyList();
        }
        TvInputInfo input = Utils.getTvInputInfoForInputId(mContext, seriesRecording.getInputId());
        if (input == null || !input.canRecord() || input.getTunerCount() <= 0) {
            return Collections.emptyList();
        }
        List<ScheduledRecording> scheduledRecordingForSeries = mDataManager.getScheduledRecordings(
                seriesRecording.getId());
        List<ScheduledRecording> availableScheduledRecordingForSeries = new ArrayList<>();
        for (ScheduledRecording scheduledRecording : scheduledRecordingForSeries) {
            if (scheduledRecording.isNotStarted() || scheduledRecording.isInProgress()) {
                availableScheduledRecordingForSeries.add(scheduledRecording);
            }
        }
        if (availableScheduledRecordingForSeries.isEmpty()) {
            return Collections.emptyList();
        }
        return getConflictingSchedules(input, availableScheduledRecordingForSeries);
    }

    /**
     * Returns a sorted list of all scheduled recordings that will not be recorded if
     * this channel is going to be recorded, with their priority in decending order.
     * <p>
     * An empty list means there is no conflicts. If there is conflict, a priority higher than
     * the first recording in the returned list should be assigned to the new schedule of this
     * channel to guarantee the channel would be completely recorded in the designated time range.
     */
    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<ConflictInfo> getConflictingSchedulesInfo(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 getConflictingSchedulesInfo(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.getTvInputInfoForInputId(mContext, schedule.getInputId());
        SoftPreconditions.checkState(input != null, TAG, "Can't find input for channel ID : "
                + schedule.getChannelId());
        if (!mInitialized || input == null) {
            return false;
        }
        Map<Long, ConflictInfo> conflicts = mInputConflictInfoMap.get(input.getId());
        return conflicts != null && conflicts.containsKey(schedule.getId());
    }

    /**
     * Checks if the schedule is partially conflicting, i.e., part of the scheduled program might be
     * recorded even if the priority of the schedule is not raised.
     * <p>
     * If the given schedule is not conflicting or is totally conflicting, i.e., cannot be recorded
     * at all, this method returns {@code false} in both cases.
     */
    public boolean isPartiallyConflicting(@NonNull ScheduledRecording schedule) {
        SoftPreconditions.checkState(mInitialized, TAG, "Not initialized yet");
        TvInputInfo input = Utils.getTvInputInfoForInputId(mContext, schedule.getInputId());
        SoftPreconditions.checkState(input != null, TAG, "Can't find input for channel ID : "
                + schedule.getChannelId());
        if (!mInitialized || input == null) {
            return false;
        }
        Map<Long, ConflictInfo> conflicts = mInputConflictInfoMap.get(input.getId());
        if (conflicts != null) {
            ConflictInfo conflictInfo = conflicts.get(schedule.getId());
            return conflictInfo != null && conflictInfo.partialConflict;
        }
        return false;
    }

    /**
     * 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, RESULT_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, null);
    }

    @VisibleForTesting
    static List<ScheduledRecording> getConflictingSchedules(
            List<ScheduledRecording> schedules, int tunerCount, List<Range<Long>> periods) {
        List<ScheduledRecording> result = new ArrayList<>();
        for (ConflictInfo conflictInfo :
                getConflictingSchedulesInfo(schedules, tunerCount, periods)) {
            result.add(conflictInfo.schedule);
        }
        return result;
    }

    @VisibleForTesting
    static List<ConflictInfo> getConflictingSchedulesInfo(List<ScheduledRecording> schedules,
            int tunerCount) {
        return getConflictingSchedulesInfo(schedules, tunerCount, null);
    }

    /**
     * This is the core method to calculate all the conflicting schedules (in given periods).
     * <p>
     * Note that this method will ignore duplicated schedules with a same hash code. (Please refer
     * to {@link ScheduledRecording#hashCode}.)
     *
     * @return A {@link HashMap} from {@link ScheduledRecording} to {@link Boolean}. The boolean
     *         value denotes if the scheduled recording is partially conflicting, i.e., is possible
     *         to be partially recorded under the given schedules and tuner count {@code true},
     *         or not {@code false}.
     */
    private static List<ConflictInfo> getConflictingSchedulesInfo(
            List<ScheduledRecording> schedules, int tunerCount, List<Range<Long>> periods) {
        List<ScheduledRecording> schedulesToCheck = new ArrayList<>(schedules);
        // Sort by the same order as that in InputTaskScheduler.
        Collections.sort(schedulesToCheck, InputTaskScheduler.getRecordingOrderComparator());
        List<ScheduledRecording> recordings = new ArrayList<>();
        Map<ScheduledRecording, ConflictInfo> conflicts = new HashMap<>();
        Map<ScheduledRecording, ScheduledRecording> modified2OriginalSchedules = new HashMap<>();
        // Simulate InputTaskScheduler.
        while (!schedulesToCheck.isEmpty()) {
            ScheduledRecording schedule = schedulesToCheck.remove(0);
            removeFinishedRecordings(recordings, schedule.getStartTimeMs());
            if (recordings.size() < tunerCount) {
                recordings.add(schedule);
                if (modified2OriginalSchedules.containsKey(schedule)) {
                    // Schedule has been modified, which means it's already conflicted.
                    // Modify its state to partially conflicted.
                    ScheduledRecording originalSchedule = modified2OriginalSchedules.get(schedule);
                    conflicts.put(originalSchedule, new ConflictInfo(originalSchedule, true));
                }
            } else {
                ScheduledRecording candidate = findReplaceableRecording(recordings, schedule);
                if (candidate != null) {
                    if (!modified2OriginalSchedules.containsKey(candidate)) {
                        conflicts.put(candidate, new ConflictInfo(candidate, true));
                    }
                    recordings.remove(candidate);
                    recordings.add(schedule);
                    if (modified2OriginalSchedules.containsKey(schedule)) {
                        // Schedule has been modified, which means it's already conflicted.
                        // Modify its state to partially conflicted.
                        ScheduledRecording originalSchedule =
                                modified2OriginalSchedules.get(schedule);
                        conflicts.put(originalSchedule, new ConflictInfo(originalSchedule, true));
                    }
                } else {
                    if (!modified2OriginalSchedules.containsKey(schedule)) {
                        // if schedule has been modified, it's already conflicted.
                        // No need to add it again.
                        conflicts.put(schedule, new ConflictInfo(schedule, false));
                    }
                    long earliestEndTime = getEarliestEndTime(recordings);
                    if (earliestEndTime < schedule.getEndTimeMs()) {
                        // The schedule can starts when other recording ends even though it's
                        // clipped.
                        ScheduledRecording modifiedSchedule = ScheduledRecording.buildFrom(schedule)
                                .setStartTimeMs(earliestEndTime).build();
                        ScheduledRecording originalSchedule =
                                modified2OriginalSchedules.getOrDefault(schedule, schedule);
                        modified2OriginalSchedules.put(modifiedSchedule, originalSchedule);
                        int insertPosition = Collections.binarySearch(schedulesToCheck,
                                modifiedSchedule,
                                ScheduledRecording.START_TIME_THEN_PRIORITY_THEN_ID_COMPARATOR);
                        if (insertPosition >= 0) {
                            schedulesToCheck.add(insertPosition, modifiedSchedule);
                        } else {
                            schedulesToCheck.add(-insertPosition - 1, modifiedSchedule);
                        }
                    }
                }
            }
        }
        // Returns only the schedules with the given range.
        if (periods != null && !periods.isEmpty()) {
            for (Iterator<ScheduledRecording> iter = conflicts.keySet().iterator();
                    iter.hasNext(); ) {
                boolean overlapping = false;
                ScheduledRecording schedule = iter.next();
                for (Range<Long> period : periods) {
                    if (schedule.isOverLapping(period)) {
                        overlapping = true;
                        break;
                    }
                }
                if (!overlapping) {
                    iter.remove();
                }
            }
        }
        List<ConflictInfo> result = new ArrayList<>(conflicts.values());
        Collections.sort(result, new Comparator<ConflictInfo>() {
            @Override
            public int compare(ConflictInfo lhs, ConflictInfo rhs) {
                return RESULT_COMPARATOR.compare(lhs.schedule, rhs.schedule);
            }
        });
        return result;
    }

    private static void removeFinishedRecordings(List<ScheduledRecording> recordings,
            long currentTimeMs) {
        for (Iterator<ScheduledRecording> iter = recordings.iterator(); iter.hasNext(); ) {
            if (iter.next().getEndTimeMs() <= currentTimeMs) {
                iter.remove();
            }
        }
    }

    /**
     * @see InputTaskScheduler#getReplacableTask
     */
    private static ScheduledRecording findReplaceableRecording(List<ScheduledRecording> recordings,
            ScheduledRecording schedule) {
        // Returns the recording with the following priority.
        // 1. The recording with the lowest priority is returned.
        // 2. If the priorities are the same, the recording which finishes early is returned.
        // 3. If 1) and 2) are the same, the early created schedule is returned.
        ScheduledRecording candidate = null;
        for (ScheduledRecording recording : recordings) {
            if (schedule.getPriority() > recording.getPriority()) {
                if (candidate == null || CANDIDATE_COMPARATOR.compare(candidate, recording) > 0) {
                    candidate = recording;
                }
            }
        }
        return candidate;
    }

    private static long getEarliestEndTime(List<ScheduledRecording> recordings) {
        long earliest = Long.MAX_VALUE;
        for (ScheduledRecording recording : recordings) {
            if (earliest > recording.getEndTimeMs()) {
                earliest = recording.getEndTimeMs();
            }
        }
        return earliest;
    }

    @VisibleForTesting
    static class ConflictInfo {
        public ScheduledRecording schedule;
        public boolean partialConflict;

        ConflictInfo(ScheduledRecording schedule, boolean partialConflict) {
            this.schedule = schedule;
            this.partialConflict = partialConflict;
        }
    }

    /**
     * A listener which is notified the initialization of schedule manager.
     */
    public interface OnInitializeListener {
        /**
         * Called when the schedule manager has been initialized.
         */
        void onInitialize();
    }

    /**
     * A listener which is notified the conflict state change of the schedules.
     */
    public interface OnConflictStateChangeListener {
        /**
         * Called when the conflicting schedules change.
         * <p>
         * Note that this can be called before
         * {@link ScheduledRecordingListener#onScheduledRecordingAdded} is called.
         *
         * @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);
    }
}