aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/menu/PlayControlsRowView.java
blob: 4d766788ae076fa5a34f6d893fdf7c2a75800604 (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
/*
 * 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.menu;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.tv.MainActivity;
import com.android.tv.R;
import com.android.tv.TimeShiftManager;
import com.android.tv.TimeShiftManager.TimeShiftActionId;
import com.android.tv.TvApplication;
import com.android.tv.common.SoftPreconditions;
import com.android.tv.common.feature.CommonFeatures;
import com.android.tv.data.Channel;
import com.android.tv.data.Program;
import com.android.tv.dialog.HalfSizedDialogFragment;
import com.android.tv.dvr.DvrDataManager;
import com.android.tv.dvr.DvrDataManager.OnDvrScheduleLoadFinishedListener;
import com.android.tv.dvr.DvrDataManager.ScheduledRecordingListener;
import com.android.tv.dvr.DvrManager;
import com.android.tv.dvr.data.ScheduledRecording;
import com.android.tv.dvr.ui.DvrStopRecordingFragment;
import com.android.tv.dvr.ui.DvrUiHelper;
import com.android.tv.menu.Menu.MenuShowReason;
import com.android.tv.ui.TunableTvView;

public class PlayControlsRowView extends MenuRowView {
    private static final int NORMAL_WIDTH_MAX_BUTTON_COUNT = 5;
    // Dimensions
    private final int mTimeIndicatorLeftMargin;
    private final int mTimeTextLeftMargin;
    private final int mTimelineWidth;
    // Views
    private TextView mBackgroundView;
    private View mTimeIndicator;
    private TextView mTimeText;
    private PlaybackProgressBar mProgress;
    private PlayControlsButton mJumpPreviousButton;
    private PlayControlsButton mRewindButton;
    private PlayControlsButton mPlayPauseButton;
    private PlayControlsButton mFastForwardButton;
    private PlayControlsButton mJumpNextButton;
    private PlayControlsButton mRecordButton;
    private TextView mProgramStartTimeText;
    private TextView mProgramEndTimeText;
    private TunableTvView mTvView;
    private TimeShiftManager mTimeShiftManager;
    private final DvrDataManager mDvrDataManager;
    private final DvrManager mDvrManager;
    private final MainActivity mMainActivity;

    private final java.text.DateFormat mTimeFormat;
    private long mProgramStartTimeMs;
    private long mProgramEndTimeMs;
    private boolean mUseCompactLayout;
    private final int mNormalButtonMargin;
    private final int mCompactButtonMargin;

    private final String mUnavailableMessage;

    private final ScheduledRecordingListener mScheduledRecordingListener
            = new ScheduledRecordingListener() {
        @Override
        public void onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings) { }

        @Override
        public void onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings) { }

        @Override
        public void onScheduledRecordingStatusChanged(ScheduledRecording... scheduledRecordings) {
            Channel currentChannel = mMainActivity.getCurrentChannel();
            if (currentChannel != null && isShown()) {
                for (ScheduledRecording schedule : scheduledRecordings) {
                    if (schedule.getChannelId() == currentChannel.getId()) {
                        updateRecordButton();
                        break;
                    }
                }
            }
        }
    };

    public PlayControlsRowView(Context context) {
        this(context, null);
    }

    public PlayControlsRowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayControlsRowView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PlayControlsRowView(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        Resources res = context.getResources();
        mTimeIndicatorLeftMargin =
                - res.getDimensionPixelSize(R.dimen.play_controls_time_indicator_width) / 2;
        mTimeTextLeftMargin =
                - res.getDimensionPixelOffset(R.dimen.play_controls_time_width) / 2;
        mTimelineWidth = res.getDimensionPixelSize(R.dimen.play_controls_width);
        mTimeFormat = DateFormat.getTimeFormat(context);
        mNormalButtonMargin = res.getDimensionPixelSize(R.dimen.play_controls_button_normal_margin);
        mCompactButtonMargin =
                res.getDimensionPixelSize(R.dimen.play_controls_button_compact_margin);
        if (CommonFeatures.DVR.isEnabled(context)) {
            mDvrDataManager = TvApplication.getSingletons(context).getDvrDataManager();
            mDvrManager = TvApplication.getSingletons(context).getDvrManager();
        } else {
            mDvrDataManager = null;
            mDvrManager = null;
        }
        mMainActivity = (MainActivity) context;
        mUnavailableMessage = res.getString(R.string.play_controls_unavailable);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mDvrDataManager != null) {
            mDvrDataManager.addScheduledRecordingListener(mScheduledRecordingListener);
            if (!mDvrDataManager.isDvrScheduleLoadFinished()) {
                mDvrDataManager.addDvrScheduleLoadFinishedListener(
                        new OnDvrScheduleLoadFinishedListener() {
                            @Override
                            public void onDvrScheduleLoadFinished() {
                                mDvrDataManager.removeDvrScheduleLoadFinishedListener(this);
                                if (isShown()) {
                                    updateRecordButton();
                                }
                            }
                        });
            }

        }
    }

    @Override
    protected int getContentsViewId() {
        return R.id.play_controls;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // Clip the ViewGroup(body) to the rounded rectangle of outline.
        findViewById(R.id.body).setClipToOutline(true);
        mBackgroundView = (TextView) findViewById(R.id.background);
        mTimeIndicator = findViewById(R.id.time_indicator);
        mTimeText = (TextView) findViewById(R.id.time_text);
        mProgress = (PlaybackProgressBar) findViewById(R.id.progress);
        mJumpPreviousButton = (PlayControlsButton) findViewById(R.id.jump_previous);
        mRewindButton = (PlayControlsButton) findViewById(R.id.rewind);
        mPlayPauseButton = (PlayControlsButton) findViewById(R.id.play_pause);
        mFastForwardButton = (PlayControlsButton) findViewById(R.id.fast_forward);
        mJumpNextButton = (PlayControlsButton) findViewById(R.id.jump_next);
        mRecordButton = (PlayControlsButton) findViewById(R.id.record);
        mProgramStartTimeText = (TextView) findViewById(R.id.program_start_time);
        mProgramEndTimeText = (TextView) findViewById(R.id.program_end_time);

        initializeButton(mJumpPreviousButton, R.drawable.lb_ic_skip_previous,
                R.string.play_controls_description_skip_previous, null, new Runnable() {
            @Override
            public void run() {
                if (mTimeShiftManager.isAvailable()) {
                    mTimeShiftManager.jumpToPrevious();
                    updateControls(true);
                }
            }
        });
        initializeButton(mRewindButton, R.drawable.lb_ic_fast_rewind,
                R.string.play_controls_description_fast_rewind, null, new Runnable() {
            @Override
            public void run() {
                if (mTimeShiftManager.isAvailable()) {
                    mTimeShiftManager.rewind();
                    updateButtons();
                }
            }
        });
        initializeButton(mPlayPauseButton, R.drawable.lb_ic_play,
                R.string.play_controls_description_play_pause, null, new Runnable() {
            @Override
            public void run() {
                if (mTimeShiftManager.isAvailable()) {
                    mTimeShiftManager.togglePlayPause();
                    updateButtons();
                }
            }
        });
        initializeButton(mFastForwardButton, R.drawable.lb_ic_fast_forward,
                R.string.play_controls_description_fast_forward, null, new Runnable() {
            @Override
            public void run() {
                if (mTimeShiftManager.isAvailable()) {
                    mTimeShiftManager.fastForward();
                    updateButtons();
                }
            }
        });
        initializeButton(mJumpNextButton, R.drawable.lb_ic_skip_next,
                R.string.play_controls_description_skip_next, null, new Runnable() {
            @Override
            public void run() {
                if (mTimeShiftManager.isAvailable()) {
                    mTimeShiftManager.jumpToNext();
                    updateControls(true);
                }
            }
        });
        int color = getResources().getColor(R.color.play_controls_recording_icon_color_on_focus,
                null);
        initializeButton(mRecordButton, R.drawable.ic_record_start, R.string
                .channels_item_record_start, color, new Runnable() {
            @Override
            public void run() {
                onRecordButtonClicked();
            }
        });
    }

    private boolean isCurrentChannelRecording() {
        Channel currentChannel = mMainActivity.getCurrentChannel();
        return currentChannel != null && mDvrManager != null
                && mDvrManager.getCurrentRecording(currentChannel.getId()) != null;
    }

    private void onRecordButtonClicked() {
        boolean isRecording = isCurrentChannelRecording();
        Channel currentChannel = mMainActivity.getCurrentChannel();
        TvApplication.getSingletons(getContext()).getTracker().sendMenuClicked(isRecording ?
                R.string.channels_item_record_start : R.string.channels_item_record_stop);
        if (!isRecording) {
            if (!(mDvrManager != null && mDvrManager.isChannelRecordable(currentChannel))) {
                Toast.makeText(mMainActivity, R.string.dvr_msg_cannot_record_channel,
                        Toast.LENGTH_SHORT).show();
            } else {
                Program program = TvApplication.getSingletons(mMainActivity).getProgramDataManager()
                        .getCurrentProgram(currentChannel.getId());
                DvrUiHelper.checkStorageStatusAndShowErrorMessage(mMainActivity,
                        currentChannel.getInputId(), new Runnable() {
                            @Override
                            public void run() {
                                DvrUiHelper.requestRecordingCurrentProgram(mMainActivity,
                                        currentChannel, program, true);
                            }
                        });
            }
        } else if (currentChannel != null) {
            DvrUiHelper.showStopRecordingDialog(mMainActivity, currentChannel.getId(),
                    DvrStopRecordingFragment.REASON_USER_STOP,
                    new HalfSizedDialogFragment.OnActionClickListener() {
                        @Override
                        public void onActionClick(long actionId) {
                            if (actionId == DvrStopRecordingFragment.ACTION_STOP) {
                                ScheduledRecording currentRecording =
                                        mDvrManager.getCurrentRecording(
                                                currentChannel.getId());
                                if (currentRecording != null) {
                                    mDvrManager.stopRecording(currentRecording);
                                }
                            }
                        }
                    });
        }
    }

    private void initializeButton(PlayControlsButton button, int imageResId,
            int descriptionId, Integer focusedIconColor, Runnable clickAction) {
        button.setImageResId(imageResId);
        button.setAction(clickAction);
        if (focusedIconColor != null) {
            button.setFocusedIconColor(focusedIconColor);
        }
        button.findViewById(R.id.button)
                .setContentDescription(getResources().getString(descriptionId));
    }

    @Override
    public void onBind(MenuRow row) {
        super.onBind(row);
        PlayControlsRow playControlsRow = (PlayControlsRow) row;
        mTvView = playControlsRow.getTvView();
        mTimeShiftManager = playControlsRow.getTimeShiftManager();
        mTimeShiftManager.setListener(new TimeShiftManager.Listener() {
            @Override
            public void onAvailabilityChanged() {
                updateMenuVisibility();
                PlayControlsRowView.this.updateAll(false);
            }

            @Override
            public void onPlayStatusChanged(int status) {
                updateMenuVisibility();
                if (mTimeShiftManager.isAvailable()) {
                    updateControls(false);
                }
            }

            @Override
            public void onRecordTimeRangeChanged() {
                if (mTimeShiftManager.isAvailable()) {
                    updateControls(false);
                }
            }

            @Override
            public void onCurrentPositionChanged() {
                if (mTimeShiftManager.isAvailable()) {
                    initializeTimeline();
                    updateControls(false);
                }
            }

            @Override
            public void onProgramInfoChanged() {
                if (mTimeShiftManager.isAvailable()) {
                    initializeTimeline();
                    updateControls(false);
                }
            }

            @Override
            public void onActionEnabledChanged(@TimeShiftActionId int actionId, boolean enabled) {
                // Move focus to the play/pause button when the PREVIOUS, NEXT, REWIND or
                // FAST_FORWARD button is clicked and the button becomes disabled.
                // No need to update the UI here because the UI will be updated by other callbacks.
                if (!enabled &&
                        ((actionId == TimeShiftManager.TIME_SHIFT_ACTION_ID_JUMP_TO_PREVIOUS
                                && mJumpPreviousButton.hasFocus())
                        || (actionId == TimeShiftManager.TIME_SHIFT_ACTION_ID_REWIND
                                && mRewindButton.hasFocus())
                        || (actionId == TimeShiftManager.TIME_SHIFT_ACTION_ID_FAST_FORWARD
                                && mFastForwardButton.hasFocus())
                        || (actionId == TimeShiftManager.TIME_SHIFT_ACTION_ID_JUMP_TO_NEXT
                                && mJumpNextButton.hasFocus()))) {
                    mPlayPauseButton.requestFocus();
                }
            }
        });
        // force update to initialize everything
        updateAll(true);
    }

    private void initializeTimeline() {
        Program program = mTimeShiftManager.getProgramAt(
                mTimeShiftManager.getCurrentPositionMs());
        mProgramStartTimeMs = program.getStartTimeUtcMillis();
        mProgramEndTimeMs = program.getEndTimeUtcMillis();
        mProgress.setMax(mProgramEndTimeMs - mProgramStartTimeMs);
        updateRecTimeText();
        SoftPreconditions.checkArgument(mProgramStartTimeMs <= mProgramEndTimeMs);
    }

    private void updateMenuVisibility() {
        boolean keepMenuVisible =
                mTimeShiftManager.isAvailable() && !mTimeShiftManager.isNormalPlaying();
        getMenu().setKeepVisible(keepMenuVisible);
    }

    public void onPreselected() {
        updateControls(true);
    }

    @Override
    public void onSelected(boolean showTitle) {
        super.onSelected(showTitle);
        postHideRippleAnimation();
    }

    @Override
    public void initialize(@MenuShowReason int reason) {
        super.initialize(reason);
        switch (reason) {
            case Menu.REASON_PLAY_CONTROLS_JUMP_TO_PREVIOUS:
                if (mTimeShiftManager.isActionEnabled(
                        TimeShiftManager.TIME_SHIFT_ACTION_ID_JUMP_TO_PREVIOUS)) {
                    setInitialFocusView(mJumpPreviousButton);
                } else {
                    setInitialFocusView(mPlayPauseButton);
                }
                break;
            case Menu.REASON_PLAY_CONTROLS_REWIND:
                if (mTimeShiftManager.isActionEnabled(
                        TimeShiftManager.TIME_SHIFT_ACTION_ID_REWIND)) {
                    setInitialFocusView(mRewindButton);
                } else {
                    setInitialFocusView(mPlayPauseButton);
                }
                break;
            case Menu.REASON_PLAY_CONTROLS_FAST_FORWARD:
                if (mTimeShiftManager.isActionEnabled(
                        TimeShiftManager.TIME_SHIFT_ACTION_ID_FAST_FORWARD)) {
                    setInitialFocusView(mFastForwardButton);
                } else {
                    setInitialFocusView(mPlayPauseButton);
                }
                break;
            case Menu.REASON_PLAY_CONTROLS_JUMP_TO_NEXT:
                if (mTimeShiftManager.isActionEnabled(
                        TimeShiftManager.TIME_SHIFT_ACTION_ID_JUMP_TO_NEXT)) {
                    setInitialFocusView(mJumpNextButton);
                } else {
                    setInitialFocusView(mPlayPauseButton);
                }
                break;
            case Menu.REASON_PLAY_CONTROLS_PLAY_PAUSE:
            case Menu.REASON_PLAY_CONTROLS_PLAY:
            case Menu.REASON_PLAY_CONTROLS_PAUSE:
            default:
                setInitialFocusView(mPlayPauseButton);
                break;
        }
        postHideRippleAnimation();
    }

    private void postHideRippleAnimation() {
        // Focus may be changed in another message if requestFocus is called in this message.
        // After the focus is actually changed, hideRippleAnimation should run
        // to reflect the result of the focus change. To be sure, hideRippleAnimation is posted.
        post(new Runnable() {
            @Override
            public void run() {
                mJumpPreviousButton.hideRippleAnimation();
                mRewindButton.hideRippleAnimation();
                mPlayPauseButton.hideRippleAnimation();
                mFastForwardButton.hideRippleAnimation();
                mJumpNextButton.hideRippleAnimation();
            }
        });
    }

    @Override
    protected void onChildFocusChange(View v, boolean hasFocus) {
        super.onChildFocusChange(v, hasFocus);
        if ((v.getParent().equals(mRewindButton) || v.getParent().equals(mFastForwardButton))
                && !hasFocus) {
            if (mTimeShiftManager.getPlayStatus() == TimeShiftManager.PLAY_STATUS_PLAYING) {
                mTimeShiftManager.play();
                updateButtons();
            }
        }
    }

    /**
     * Updates the view contents. It is called from the PlayControlsRow.
     */
    public void update() {
        updateAll(false);
    }

    private void updateAll(boolean forceUpdate) {
        if (mTimeShiftManager.isAvailable() && !mTvView.isScreenBlocked()) {
            setEnabled(true);
            initializeTimeline();
            mBackgroundView.setEnabled(true);
            setTextIfNeeded(mBackgroundView, null);
        } else {
            setEnabled(false);
            mBackgroundView.setEnabled(false);
            setTextIfNeeded(mBackgroundView, mUnavailableMessage);
        }
        // force the controls be updated no matter it's visible or not.
        updateControls(forceUpdate);
    }

    private void updateControls(boolean forceUpdate) {
        if (forceUpdate || getContentsView().isShown()) {
            updateTime();
            updateProgress();
            updateButtons();
            updateRecordButton();
            updateButtonMargin();
        }
    }

    private void updateTime() {
        if (isEnabled()) {
            mTimeText.setVisibility(View.VISIBLE);
            mTimeIndicator.setVisibility(View.VISIBLE);
        } else {
            mTimeText.setVisibility(View.INVISIBLE);
            mTimeIndicator.setVisibility(View.GONE);
            return;
        }
        long currentPositionMs = mTimeShiftManager.getCurrentPositionMs();
        int currentTimePositionPixel =
                convertDurationToPixel(currentPositionMs - mProgramStartTimeMs);
        mTimeText.setTranslationX(currentTimePositionPixel + mTimeTextLeftMargin);
        setTextIfNeeded(mTimeText, getTimeString(currentPositionMs));
        mTimeIndicator.setTranslationX(currentTimePositionPixel + mTimeIndicatorLeftMargin);
    }

    private void updateProgress() {
        if (isEnabled()) {
            long progressStartTimeMs = Math.min(mProgramEndTimeMs,
                    Math.max(mProgramStartTimeMs, mTimeShiftManager.getRecordStartTimeMs()));
            long currentPlayingTimeMs = Math.min(mProgramEndTimeMs,
                    Math.max(mProgramStartTimeMs, mTimeShiftManager.getCurrentPositionMs()));
            long progressEndTimeMs = Math.min(mProgramEndTimeMs,
                    Math.max(mProgramStartTimeMs, mTimeShiftManager.getRecordEndTimeMs()));
            mProgress.setProgressRange(progressStartTimeMs - mProgramStartTimeMs,
                    progressEndTimeMs - mProgramStartTimeMs);
            mProgress.setProgress(currentPlayingTimeMs - mProgramStartTimeMs);
        } else {
            mProgress.setProgressRange(0, 0);
        }
    }

    private void updateRecTimeText() {
        if (isEnabled()) {
            mProgramStartTimeText.setVisibility(View.VISIBLE);
            setTextIfNeeded(mProgramStartTimeText, getTimeString(mProgramStartTimeMs));
            mProgramEndTimeText.setVisibility(View.VISIBLE);
            setTextIfNeeded(mProgramEndTimeText, getTimeString(mProgramEndTimeMs));
        } else {
            mProgramStartTimeText.setVisibility(View.GONE);
            mProgramEndTimeText.setVisibility(View.GONE);
        }
    }

    private void updateButtons() {
        if (isEnabled()) {
            mPlayPauseButton.setVisibility(View.VISIBLE);
            mJumpPreviousButton.setVisibility(View.VISIBLE);
            mJumpNextButton.setVisibility(View.VISIBLE);
            mRewindButton.setVisibility(View.VISIBLE);
            mFastForwardButton.setVisibility(View.VISIBLE);
        } else {
            mPlayPauseButton.setVisibility(View.GONE);
            mJumpPreviousButton.setVisibility(View.GONE);
            mJumpNextButton.setVisibility(View.GONE);
            mRewindButton.setVisibility(View.GONE);
            mFastForwardButton.setVisibility(View.GONE);
            return;
        }

        if (mTimeShiftManager.getPlayStatus() == TimeShiftManager.PLAY_STATUS_PAUSED) {
            mPlayPauseButton.setImageResId(R.drawable.lb_ic_play);
            mPlayPauseButton.setEnabled(mTimeShiftManager.isActionEnabled(
                    TimeShiftManager.TIME_SHIFT_ACTION_ID_PLAY));
        } else {
            mPlayPauseButton.setImageResId(R.drawable.lb_ic_pause);
            mPlayPauseButton.setEnabled(mTimeShiftManager.isActionEnabled(
                    TimeShiftManager.TIME_SHIFT_ACTION_ID_PAUSE));
        }
        mJumpPreviousButton.setEnabled(mTimeShiftManager.isActionEnabled(
                TimeShiftManager.TIME_SHIFT_ACTION_ID_JUMP_TO_PREVIOUS));
        mRewindButton.setEnabled(mTimeShiftManager.isActionEnabled(
                TimeShiftManager.TIME_SHIFT_ACTION_ID_REWIND));
        mFastForwardButton.setEnabled(mTimeShiftManager.isActionEnabled(
                TimeShiftManager.TIME_SHIFT_ACTION_ID_FAST_FORWARD));
        mJumpNextButton.setEnabled(mTimeShiftManager.isActionEnabled(
                TimeShiftManager.TIME_SHIFT_ACTION_ID_JUMP_TO_NEXT));
        mJumpPreviousButton.setVisibility(VISIBLE);
        mJumpNextButton.setVisibility(VISIBLE);
        updateButtonMargin();

        PlayControlsButton button;
        if (mTimeShiftManager.getPlayDirection() == TimeShiftManager.PLAY_DIRECTION_FORWARD) {
            mRewindButton.setLabel(null);
            button = mFastForwardButton;
        } else {
            mFastForwardButton.setLabel(null);
            button = mRewindButton;
        }
        if (mTimeShiftManager.getDisplayedPlaySpeed() == TimeShiftManager.PLAY_SPEED_1X) {
            button.setLabel(null);
        } else {
            button.setLabel(getResources().getString(R.string.play_controls_speed,
                    mTimeShiftManager.getDisplayedPlaySpeed()));
        }
    }

    private void updateRecordButton() {
        if (isEnabled()) {
            mRecordButton.setVisibility(VISIBLE);
        } else {
            mRecordButton.setVisibility(GONE);
            return;
        }
        if (!(mDvrManager != null
                && mDvrManager.isChannelRecordable(mMainActivity.getCurrentChannel()))) {
            mRecordButton.setVisibility(View.GONE);
            updateButtonMargin();
            return;
        }
        mRecordButton.setVisibility(View.VISIBLE);
        updateButtonMargin();
        if (isCurrentChannelRecording()) {
            mRecordButton.setImageResId(R.drawable.ic_record_stop);
        } else {
            mRecordButton.setImageResId(R.drawable.ic_record_start);
        }
    }

    private void updateButtonMargin() {
        int numOfVisibleButtons = (mJumpPreviousButton.getVisibility() == View.VISIBLE ? 1 : 0)
                + (mRewindButton.getVisibility() == View.VISIBLE ? 1 : 0)
                + (mPlayPauseButton.getVisibility() == View.VISIBLE ? 1 : 0)
                + (mFastForwardButton.getVisibility() == View.VISIBLE ? 1 : 0)
                + (mJumpNextButton.getVisibility() == View.VISIBLE ? 1 : 0)
                + (mRecordButton.getVisibility() == View.VISIBLE ? 1 : 0);
        boolean useCompactLayout = numOfVisibleButtons > NORMAL_WIDTH_MAX_BUTTON_COUNT;
        if (mUseCompactLayout == useCompactLayout) {
            return;
        }
        mUseCompactLayout = useCompactLayout;
        int margin = mUseCompactLayout ? mCompactButtonMargin : mNormalButtonMargin;
        updateButtonMargin(mJumpPreviousButton, margin);
        updateButtonMargin(mRewindButton, margin);
        updateButtonMargin(mPlayPauseButton, margin);
        updateButtonMargin(mFastForwardButton, margin);
        updateButtonMargin(mJumpNextButton, margin);
        updateButtonMargin(mRecordButton, margin);
    }

    private void updateButtonMargin(PlayControlsButton button, int margin) {
        MarginLayoutParams params = (MarginLayoutParams) button.getLayoutParams();
        params.setMargins(margin, 0, margin, 0);
        button.setLayoutParams(params);
    }

    private String getTimeString(long timeMs) {
        return mTimeFormat.format(timeMs);
    }

    private int convertDurationToPixel(long duration) {
        if (mProgramEndTimeMs <= mProgramStartTimeMs) {
            return 0;
        }
        return (int) (duration * mTimelineWidth / (mProgramEndTimeMs - mProgramStartTimeMs));
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mDvrDataManager != null) {
            mDvrDataManager.removeScheduledRecordingListener(mScheduledRecordingListener);
        }
    }

    private void setTextIfNeeded(TextView textView, String text) {
        if (!TextUtils.equals(textView.getText(), text)) {
            textView.setText(text);
        }
    }
}