aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/guide/ProgramManager.java
blob: 3f20a837192e322ffd4ad3aff51868da76ad5094 (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
/*
 * 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.guide;

import android.support.annotation.MainThread;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.util.ArraySet;
import android.util.Log;
import com.android.tv.data.ChannelDataManager;
import com.android.tv.data.GenreItems;
import com.android.tv.data.Program;
import com.android.tv.data.ProgramDataManager;
import com.android.tv.data.api.Channel;
import com.android.tv.dvr.DvrDataManager;
import com.android.tv.dvr.DvrScheduleManager;
import com.android.tv.dvr.DvrScheduleManager.OnConflictStateChangeListener;
import com.android.tv.dvr.data.ScheduledRecording;
import com.android.tv.util.TvInputManagerHelper;
import com.android.tv.util.Utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/** Manages the channels and programs for the program guide. */
@MainThread
public class ProgramManager {
    private static final String TAG = "ProgramManager";
    private static final boolean DEBUG = false;

    /**
     * If the first entry's visible duration is shorter than this value, we clip the entry out.
     * Note: If this value is larger than 1 min, it could cause mismatches between the entry's
     * position and detailed view's time range.
     */
    static final long FIRST_ENTRY_MIN_DURATION = TimeUnit.MINUTES.toMillis(1);

    private static final long INVALID_ID = -1;

    private final TvInputManagerHelper mTvInputManagerHelper;
    private final ChannelDataManager mChannelDataManager;
    private final ProgramDataManager mProgramDataManager;
    private final DvrDataManager mDvrDataManager; // Only set if DVR is enabled
    private final DvrScheduleManager mDvrScheduleManager;

    private long mStartUtcMillis;
    private long mEndUtcMillis;
    private long mFromUtcMillis;
    private long mToUtcMillis;

    private List<Channel> mChannels = new ArrayList<>();
    private final Map<Long, List<TableEntry>> mChannelIdEntriesMap = new HashMap<>();
    private final List<List<Channel>> mGenreChannelList = new ArrayList<>();
    private final List<Integer> mFilteredGenreIds = new ArrayList<>();

    // Position of selected genre to filter channel list.
    private int mSelectedGenreId = GenreItems.ID_ALL_CHANNELS;
    // Channel list after applying genre filter.
    // Should be matched with mSelectedGenreId always.
    private List<Channel> mFilteredChannels = mChannels;
    private boolean mChannelDataLoaded;

    private final Set<Listener> mListeners = new ArraySet<>();
    private final Set<TableEntriesUpdatedListener> mTableEntriesUpdatedListeners = new ArraySet<>();

    private final Set<TableEntryChangedListener> mTableEntryChangedListeners = new ArraySet<>();

    private final DvrDataManager.OnDvrScheduleLoadFinishedListener mDvrLoadedListener =
            new DvrDataManager.OnDvrScheduleLoadFinishedListener() {
                @Override
                public void onDvrScheduleLoadFinished() {
                    if (mChannelDataLoaded) {
                        for (ScheduledRecording r : mDvrDataManager.getAllScheduledRecordings()) {
                            mScheduledRecordingListener.onScheduledRecordingAdded(r);
                        }
                    }
                    mDvrDataManager.removeDvrScheduleLoadFinishedListener(this);
                }
            };

    private final ChannelDataManager.Listener mChannelDataManagerListener =
            new ChannelDataManager.Listener() {
                @Override
                public void onLoadFinished() {
                    mChannelDataLoaded = true;
                    updateChannels(false);
                }

                @Override
                public void onChannelListUpdated() {
                    updateChannels(false);
                }

                @Override
                public void onChannelBrowsableChanged() {
                    updateChannels(false);
                }
            };

    private final ProgramDataManager.Listener mProgramDataManagerListener =
            new ProgramDataManager.Listener() {
                @Override
                public void onProgramUpdated() {
                    updateTableEntries(true);
                }
            };

    private final DvrDataManager.ScheduledRecordingListener mScheduledRecordingListener =
            new DvrDataManager.ScheduledRecordingListener() {
                @Override
                public void onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings) {
                    for (ScheduledRecording schedule : scheduledRecordings) {
                        TableEntry oldEntry = getTableEntry(schedule);
                        if (oldEntry != null) {
                            TableEntry newEntry =
                                    new TableEntry(
                                            oldEntry.channelId,
                                            oldEntry.program,
                                            schedule,
                                            oldEntry.entryStartUtcMillis,
                                            oldEntry.entryEndUtcMillis,
                                            oldEntry.isBlocked());
                            updateEntry(oldEntry, newEntry);
                        }
                    }
                }

                @Override
                public void onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings) {
                    for (ScheduledRecording schedule : scheduledRecordings) {
                        TableEntry oldEntry = getTableEntry(schedule);
                        if (oldEntry != null) {
                            TableEntry newEntry =
                                    new TableEntry(
                                            oldEntry.channelId,
                                            oldEntry.program,
                                            null,
                                            oldEntry.entryStartUtcMillis,
                                            oldEntry.entryEndUtcMillis,
                                            oldEntry.isBlocked());
                            updateEntry(oldEntry, newEntry);
                        }
                    }
                }

                @Override
                public void onScheduledRecordingStatusChanged(
                        ScheduledRecording... scheduledRecordings) {
                    for (ScheduledRecording schedule : scheduledRecordings) {
                        TableEntry oldEntry = getTableEntry(schedule);
                        if (oldEntry != null) {
                            TableEntry newEntry =
                                    new TableEntry(
                                            oldEntry.channelId,
                                            oldEntry.program,
                                            schedule,
                                            oldEntry.entryStartUtcMillis,
                                            oldEntry.entryEndUtcMillis,
                                            oldEntry.isBlocked());
                            updateEntry(oldEntry, newEntry);
                        }
                    }
                }
            };

    private final OnConflictStateChangeListener mOnConflictStateChangeListener =
            new OnConflictStateChangeListener() {
                @Override
                public void onConflictStateChange(
                        boolean conflict, ScheduledRecording... schedules) {
                    for (ScheduledRecording schedule : schedules) {
                        TableEntry entry = getTableEntry(schedule);
                        if (entry != null) {
                            notifyTableEntryUpdated(entry);
                        }
                    }
                }
            };

    public ProgramManager(
            TvInputManagerHelper tvInputManagerHelper,
            ChannelDataManager channelDataManager,
            ProgramDataManager programDataManager,
            @Nullable DvrDataManager dvrDataManager,
            @Nullable DvrScheduleManager dvrScheduleManager) {
        mTvInputManagerHelper = tvInputManagerHelper;
        mChannelDataManager = channelDataManager;
        mProgramDataManager = programDataManager;
        mDvrDataManager = dvrDataManager;
        mDvrScheduleManager = dvrScheduleManager;
    }

    void programGuideVisibilityChanged(boolean visible) {
        mProgramDataManager.setPauseProgramUpdate(visible);
        if (visible) {
            mChannelDataManager.addListener(mChannelDataManagerListener);
            mProgramDataManager.addListener(mProgramDataManagerListener);
            if (mDvrDataManager != null) {
                if (!mDvrDataManager.isDvrScheduleLoadFinished()) {
                    mDvrDataManager.addDvrScheduleLoadFinishedListener(mDvrLoadedListener);
                }
                mDvrDataManager.addScheduledRecordingListener(mScheduledRecordingListener);
            }
            if (mDvrScheduleManager != null) {
                mDvrScheduleManager.addOnConflictStateChangeListener(
                        mOnConflictStateChangeListener);
            }
        } else {
            mChannelDataManager.removeListener(mChannelDataManagerListener);
            mProgramDataManager.removeListener(mProgramDataManagerListener);
            if (mDvrDataManager != null) {
                mDvrDataManager.removeDvrScheduleLoadFinishedListener(mDvrLoadedListener);
                mDvrDataManager.removeScheduledRecordingListener(mScheduledRecordingListener);
            }
            if (mDvrScheduleManager != null) {
                mDvrScheduleManager.removeOnConflictStateChangeListener(
                        mOnConflictStateChangeListener);
            }
        }
    }

    /** Adds a {@link Listener}. */
    void addListener(Listener listener) {
        mListeners.add(listener);
    }

    /** Registers a listener to be invoked when table entries are updated. */
    void addTableEntriesUpdatedListener(TableEntriesUpdatedListener listener) {
        mTableEntriesUpdatedListeners.add(listener);
    }

    /** Registers a listener to be invoked when a table entry is changed. */
    void addTableEntryChangedListener(TableEntryChangedListener listener) {
        mTableEntryChangedListeners.add(listener);
    }

    /** Removes a {@link Listener}. */
    void removeListener(Listener listener) {
        mListeners.remove(listener);
    }

    /** Removes a previously installed table entries update listener. */
    void removeTableEntriesUpdatedListener(TableEntriesUpdatedListener listener) {
        mTableEntriesUpdatedListeners.remove(listener);
    }

    /** Removes a previously installed table entry changed listener. */
    void removeTableEntryChangedListener(TableEntryChangedListener listener) {
        mTableEntryChangedListeners.remove(listener);
    }

    /**
     * Resets channel list with given genre. Caller should call {@link #buildGenreFilters()} prior
     * to call this API to make This notifies channel updates to listeners.
     */
    void resetChannelListWithGenre(int genreId) {
        if (genreId == mSelectedGenreId) {
            return;
        }
        mFilteredChannels = mGenreChannelList.get(genreId);
        mSelectedGenreId = genreId;
        if (DEBUG) {
            Log.d(
                    TAG,
                    "resetChannelListWithGenre: "
                            + GenreItems.getCanonicalGenre(genreId)
                            + " has "
                            + mFilteredChannels.size()
                            + " channels out of "
                            + mChannels.size());
        }
        if (mGenreChannelList.get(mSelectedGenreId) == null) {
            throw new IllegalStateException("Genre filter isn't ready.");
        }
        notifyChannelsUpdated();
    }

    /** Update the initial time range to manage. It updates program entries and genre as well. */
    void updateInitialTimeRange(long startUtcMillis, long endUtcMillis) {
        mStartUtcMillis = startUtcMillis;
        if (endUtcMillis > mEndUtcMillis) {
            mEndUtcMillis = endUtcMillis;
        }

        mProgramDataManager.setPrefetchTimeRange(mStartUtcMillis);
        updateChannels(true);
        setTimeRange(startUtcMillis, endUtcMillis);
    }

    /** Shifts the time range by the given time. Also makes ProgramGuide scroll the views. */
    void shiftTime(long timeMillisToScroll) {
        long fromUtcMillis = mFromUtcMillis + timeMillisToScroll;
        long toUtcMillis = mToUtcMillis + timeMillisToScroll;
        if (fromUtcMillis < mStartUtcMillis) {
            fromUtcMillis = mStartUtcMillis;
            toUtcMillis += mStartUtcMillis - fromUtcMillis;
        }
        if (toUtcMillis > mEndUtcMillis) {
            fromUtcMillis -= toUtcMillis - mEndUtcMillis;
            toUtcMillis = mEndUtcMillis;
        }
        setTimeRange(fromUtcMillis, toUtcMillis);
    }

    /** Returned the scrolled(shifted) time in milliseconds. */
    long getShiftedTime() {
        return mFromUtcMillis - mStartUtcMillis;
    }

    /** Returns the start time set by {@link #updateInitialTimeRange}. */
    long getStartTime() {
        return mStartUtcMillis;
    }

    /** Returns the program index of the program with {@code entryId} or -1 if not found. */
    int getProgramIdIndex(long channelId, long entryId) {
        List<TableEntry> entries = mChannelIdEntriesMap.get(channelId);
        if (entries != null) {
            for (int i = 0; i < entries.size(); i++) {
                if (entries.get(i).getId() == entryId) {
                    return i;
                }
            }
        }
        return -1;
    }

    /** Returns the program index of the program at {@code time} or -1 if not found. */
    int getProgramIndexAtTime(long channelId, long time) {
        List<TableEntry> entries = mChannelIdEntriesMap.get(channelId);
        for (int i = 0; i < entries.size(); ++i) {
            TableEntry entry = entries.get(i);
            if (entry.entryStartUtcMillis <= time && time < entry.entryEndUtcMillis) {
                return i;
            }
        }
        return -1;
    }

    /** Returns the start time of currently managed time range, in UTC millisecond. */
    long getFromUtcMillis() {
        return mFromUtcMillis;
    }

    /** Returns the end time of currently managed time range, in UTC millisecond. */
    long getToUtcMillis() {
        return mToUtcMillis;
    }

    /** Returns the number of the currently managed channels. */
    int getChannelCount() {
        return mFilteredChannels.size();
    }

    /**
     * Returns a {@link Channel} at a given {@code channelIndex} of the currently managed channels.
     * Returns {@code null} if such a channel is not found.
     */
    Channel getChannel(int channelIndex) {
        if (channelIndex < 0 || channelIndex >= getChannelCount()) {
            return null;
        }
        return mFilteredChannels.get(channelIndex);
    }

    /**
     * Returns the index of provided {@link Channel} within the currently managed channels. Returns
     * -1 if such a channel is not found.
     */
    int getChannelIndex(Channel channel) {
        return mFilteredChannels.indexOf(channel);
    }

    /**
     * Returns the index of channel with {@code channelId} within the currently managed channels.
     * Returns -1 if such a channel is not found.
     */
    int getChannelIndex(long channelId) {
        return getChannelIndex(mChannelDataManager.getChannel(channelId));
    }

    /**
     * Returns the number of "entries", which lies within the currently managed time range, for a
     * given {@code channelId}.
     */
    int getTableEntryCount(long channelId) {
        return mChannelIdEntriesMap.get(channelId).size();
    }

    /**
     * Returns an entry as {@link Program} for a given {@code channelId} and {@code index} of
     * entries within the currently managed time range. Returned {@link Program} can be a dummy one
     * (e.g., whose channelId is INVALID_ID), when it corresponds to a gap between programs.
     */
    TableEntry getTableEntry(long channelId, int index) {
        return mChannelIdEntriesMap.get(channelId).get(index);
    }

    /** Returns list genre ID's which has a channel. */
    List<Integer> getFilteredGenreIds() {
        return mFilteredGenreIds;
    }

    int getSelectedGenreId() {
        return mSelectedGenreId;
    }

    // Note that This can be happens only if program guide isn't shown
    // because an user has to select channels as browsable through UI.
    private void updateChannels(boolean clearPreviousTableEntries) {
        if (DEBUG) Log.d(TAG, "updateChannels");
        mChannels = mChannelDataManager.getBrowsableChannelList();
        mSelectedGenreId = GenreItems.ID_ALL_CHANNELS;
        mFilteredChannels = mChannels;
        updateTableEntriesWithoutNotification(clearPreviousTableEntries);
        // Channel update notification should be called after updating table entries, so that
        // the listener can get the entries.
        notifyChannelsUpdated();
        notifyTableEntriesUpdated();
        buildGenreFilters();
    }

    private void updateTableEntries(boolean clear) {
        updateTableEntriesWithoutNotification(clear);
        notifyTableEntriesUpdated();
        buildGenreFilters();
    }

    /** Updates the table entries without notifying the change. */
    private void updateTableEntriesWithoutNotification(boolean clear) {
        if (clear) {
            mChannelIdEntriesMap.clear();
        }
        boolean parentalControlsEnabled =
                mTvInputManagerHelper.getParentalControlSettings().isParentalControlsEnabled();
        for (Channel channel : mChannels) {
            long channelId = channel.getId();
            // Inline the updating of the mChannelIdEntriesMap here so we can only call
            // getParentalControlSettings once.
            List<TableEntry> entries = createProgramEntries(channelId, parentalControlsEnabled);
            mChannelIdEntriesMap.put(channelId, entries);

            int size = entries.size();
            if (DEBUG) {
                Log.d(
                        TAG,
                        "Programs are loaded for channel "
                                + channel.getId()
                                + ", loaded size = "
                                + size);
            }
            if (size == 0) {
                continue;
            }
            TableEntry lastEntry = entries.get(size - 1);
            if (mEndUtcMillis < lastEntry.entryEndUtcMillis
                    && lastEntry.entryEndUtcMillis != Long.MAX_VALUE) {
                mEndUtcMillis = lastEntry.entryEndUtcMillis;
            }
        }
        if (mEndUtcMillis > mStartUtcMillis) {
            for (Channel channel : mChannels) {
                long channelId = channel.getId();
                List<TableEntry> entries = mChannelIdEntriesMap.get(channelId);
                if (entries.isEmpty()) {
                    entries.add(new TableEntry(channelId, mStartUtcMillis, mEndUtcMillis));
                } else {
                    TableEntry lastEntry = entries.get(entries.size() - 1);
                    if (mEndUtcMillis > lastEntry.entryEndUtcMillis) {
                        entries.add(
                                new TableEntry(
                                        channelId, lastEntry.entryEndUtcMillis, mEndUtcMillis));
                    } else if (lastEntry.entryEndUtcMillis == Long.MAX_VALUE) {
                        entries.remove(entries.size() - 1);
                        entries.add(
                                new TableEntry(
                                        lastEntry.channelId,
                                        lastEntry.program,
                                        lastEntry.scheduledRecording,
                                        lastEntry.entryStartUtcMillis,
                                        mEndUtcMillis,
                                        lastEntry.mIsBlocked));
                    }
                }
            }
        }
    }

    /**
     * Build genre filters based on the current programs. This categories channels by its current
     * program's canonical genres and subsequent @{link resetChannelListWithGenre(int)} calls will
     * reset channel list with built channel list. This is expected to be called whenever program
     * guide is shown.
     */
    private void buildGenreFilters() {
        if (DEBUG) Log.d(TAG, "buildGenreFilters");

        mGenreChannelList.clear();
        for (int i = 0; i < GenreItems.getGenreCount(); i++) {
            mGenreChannelList.add(new ArrayList<>());
        }
        for (Channel channel : mChannels) {
            Program currentProgram = mProgramDataManager.getCurrentProgram(channel.getId());
            if (currentProgram != null && currentProgram.getCanonicalGenres() != null) {
                for (String genre : currentProgram.getCanonicalGenres()) {
                    mGenreChannelList.get(GenreItems.getId(genre)).add(channel);
                }
            }
        }
        mGenreChannelList.set(GenreItems.ID_ALL_CHANNELS, mChannels);
        mFilteredGenreIds.clear();
        mFilteredGenreIds.add(0);
        for (int i = 1; i < GenreItems.getGenreCount(); i++) {
            if (mGenreChannelList.get(i).size() > 0) {
                mFilteredGenreIds.add(i);
            }
        }
        mSelectedGenreId = GenreItems.ID_ALL_CHANNELS;
        mFilteredChannels = mChannels;
        notifyGenresUpdated();
    }

    @Nullable
    private TableEntry getTableEntry(ScheduledRecording scheduledRecording) {
        return getTableEntry(scheduledRecording.getChannelId(), scheduledRecording.getProgramId());
    }

    @Nullable
    private TableEntry getTableEntry(long channelId, long entryId) {
        List<TableEntry> entries = mChannelIdEntriesMap.get(channelId);
        if (entries != null) {
            for (TableEntry entry : entries) {
                if (entry.getId() == entryId) {
                    return entry;
                }
            }
        }
        return null;
    }

    private void updateEntry(TableEntry old, TableEntry newEntry) {
        List<TableEntry> entries = mChannelIdEntriesMap.get(old.channelId);
        int index = entries.indexOf(old);
        entries.set(index, newEntry);
        notifyTableEntryUpdated(newEntry);
    }

    private void setTimeRange(long fromUtcMillis, long toUtcMillis) {
        if (DEBUG) {
            Log.d(
                    TAG,
                    "setTimeRange. {FromTime="
                            + Utils.toTimeString(fromUtcMillis)
                            + ", ToTime="
                            + Utils.toTimeString(toUtcMillis)
                            + "}");
        }
        if (mFromUtcMillis != fromUtcMillis || mToUtcMillis != toUtcMillis) {
            mFromUtcMillis = fromUtcMillis;
            mToUtcMillis = toUtcMillis;
            notifyTimeRangeUpdated();
        }
    }

    private List<TableEntry> createProgramEntries(long channelId, boolean parentalControlsEnabled) {
        List<TableEntry> entries = new ArrayList<>();
        boolean channelLocked =
                parentalControlsEnabled && mChannelDataManager.getChannel(channelId).isLocked();
        if (channelLocked) {
            entries.add(new TableEntry(channelId, mStartUtcMillis, Long.MAX_VALUE, true));
        } else {
            long lastProgramEndTime = mStartUtcMillis;
            List<Program> programs = mProgramDataManager.getPrograms(channelId, mStartUtcMillis);
            for (Program program : programs) {
                if (program.getChannelId() == INVALID_ID) {
                    // Dummy program.
                    continue;
                }
                long programStartTime = Math.max(program.getStartTimeUtcMillis(), mStartUtcMillis);
                long programEndTime = program.getEndTimeUtcMillis();
                if (programStartTime > lastProgramEndTime) {
                    // Gap since the last program.
                    entries.add(new TableEntry(channelId, lastProgramEndTime, programStartTime));
                    lastProgramEndTime = programStartTime;
                }
                if (programEndTime > lastProgramEndTime) {
                    ScheduledRecording scheduledRecording =
                            mDvrDataManager == null
                                    ? null
                                    : mDvrDataManager.getScheduledRecordingForProgramId(
                                            program.getId());
                    entries.add(
                            new TableEntry(
                                    channelId,
                                    program,
                                    scheduledRecording,
                                    lastProgramEndTime,
                                    programEndTime,
                                    false));
                    lastProgramEndTime = programEndTime;
                }
            }
        }

        if (entries.size() > 1) {
            TableEntry secondEntry = entries.get(1);
            if (secondEntry.entryStartUtcMillis < mStartUtcMillis + FIRST_ENTRY_MIN_DURATION) {
                // If the first entry's width doesn't have enough width, it is not good to show
                // the first entry from UI perspective. So we clip it out.
                entries.remove(0);
                entries.set(
                        0,
                        new TableEntry(
                                secondEntry.channelId,
                                secondEntry.program,
                                secondEntry.scheduledRecording,
                                mStartUtcMillis,
                                secondEntry.entryEndUtcMillis,
                                secondEntry.mIsBlocked));
            }
        }
        return entries;
    }

    private void notifyGenresUpdated() {
        for (Listener listener : mListeners) {
            listener.onGenresUpdated();
        }
    }

    private void notifyChannelsUpdated() {
        for (Listener listener : mListeners) {
            listener.onChannelsUpdated();
        }
    }

    private void notifyTimeRangeUpdated() {
        for (Listener listener : mListeners) {
            listener.onTimeRangeUpdated();
        }
    }

    private void notifyTableEntriesUpdated() {
        for (TableEntriesUpdatedListener listener : mTableEntriesUpdatedListeners) {
            listener.onTableEntriesUpdated();
        }
    }

    private void notifyTableEntryUpdated(TableEntry entry) {
        for (TableEntryChangedListener listener : mTableEntryChangedListeners) {
            listener.onTableEntryChanged(entry);
        }
    }

    /**
     * Entry for program guide table. An "entry" can be either an actual program or a gap between
     * programs. This is needed for {@link ProgramListAdapter} because {@link
     * android.support.v17.leanback.widget.HorizontalGridView} ignores margins between items.
     */
    static class TableEntry {
        /** Channel ID which this entry is included. */
        final long channelId;

        /** Program corresponding to the entry. {@code null} means that this entry is a gap. */
        final Program program;

        final ScheduledRecording scheduledRecording;

        /** Start time of entry in UTC milliseconds. */
        final long entryStartUtcMillis;

        /** End time of entry in UTC milliseconds */
        final long entryEndUtcMillis;

        private final boolean mIsBlocked;

        private TableEntry(long channelId, long startUtcMillis, long endUtcMillis) {
            this(channelId, null, startUtcMillis, endUtcMillis, false);
        }

        private TableEntry(
                long channelId, long startUtcMillis, long endUtcMillis, boolean blocked) {
            this(channelId, null, null, startUtcMillis, endUtcMillis, blocked);
        }

        private TableEntry(
                long channelId,
                Program program,
                long entryStartUtcMillis,
                long entryEndUtcMillis,
                boolean isBlocked) {
            this(channelId, program, null, entryStartUtcMillis, entryEndUtcMillis, isBlocked);
        }

        private TableEntry(
                long channelId,
                Program program,
                ScheduledRecording scheduledRecording,
                long entryStartUtcMillis,
                long entryEndUtcMillis,
                boolean isBlocked) {
            this.channelId = channelId;
            this.program = program;
            this.scheduledRecording = scheduledRecording;
            this.entryStartUtcMillis = entryStartUtcMillis;
            this.entryEndUtcMillis = entryEndUtcMillis;
            mIsBlocked = isBlocked;
        }

        /** A stable id useful for {@link android.support.v7.widget.RecyclerView.Adapter}. */
        long getId() {
            // using a negative entryEndUtcMillis keeps it from conflicting with program Id
            return program != null ? program.getId() : -entryEndUtcMillis;
        }

        /** Returns true if this is a gap. */
        boolean isGap() {
            return !Program.isProgramValid(program);
        }

        /** Returns true if this channel is blocked. */
        boolean isBlocked() {
            return mIsBlocked;
        }

        /** Returns true if this program is on the air. */
        boolean isCurrentProgram() {
            long current = System.currentTimeMillis();
            return entryStartUtcMillis <= current && entryEndUtcMillis > current;
        }

        /** Returns if this program has the genre. */
        boolean hasGenre(int genreId) {
            return !isGap() && program.hasGenre(genreId);
        }

        /** Returns the width of table entry, in pixels. */
        int getWidth() {
            return GuideUtils.convertMillisToPixel(entryStartUtcMillis, entryEndUtcMillis);
        }

        @Override
        public String toString() {
            return "TableEntry{"
                    + "hashCode="
                    + hashCode()
                    + ", channelId="
                    + channelId
                    + ", program="
                    + program
                    + ", startTime="
                    + Utils.toTimeString(entryStartUtcMillis)
                    + ", endTimeTime="
                    + Utils.toTimeString(entryEndUtcMillis)
                    + "}";
        }
    }

    @VisibleForTesting
    public static TableEntry createTableEntryForTest(
            long channelId,
            Program program,
            ScheduledRecording scheduledRecording,
            long entryStartUtcMillis,
            long entryEndUtcMillis,
            boolean isBlocked) {
        return new TableEntry(
                channelId,
                program,
                scheduledRecording,
                entryStartUtcMillis,
                entryEndUtcMillis,
                isBlocked);
    }

    interface Listener {
        void onGenresUpdated();

        void onChannelsUpdated();

        void onTimeRangeUpdated();
    }

    interface TableEntriesUpdatedListener {
        void onTableEntriesUpdated();
    }

    interface TableEntryChangedListener {
        void onTableEntryChanged(TableEntry entry);
    }

    static class ListenerAdapter implements Listener {
        @Override
        public void onGenresUpdated() {}

        @Override
        public void onChannelsUpdated() {}

        @Override
        public void onTimeRangeUpdated() {}
    }
}