aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/ui/list/ScheduleRowPresenter.java
blob: dc4e3c4175bb14333c8be82b5d60397a637398f9 (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
/*
 * Copyright (C) 2016 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.ui.list;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.support.annotation.IntDef;
import android.support.v17.leanback.widget.RowPresenter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.tv.R;
import com.android.tv.TvApplication;
import com.android.tv.common.SoftPreconditions;
import com.android.tv.data.Channel;
import com.android.tv.dialog.HalfSizedDialogFragment;
import com.android.tv.dvr.DvrManager;
import com.android.tv.dvr.DvrScheduleManager;
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.util.ToastUtils;
import com.android.tv.util.Utils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

/**
 * A RowPresenter for {@link ScheduleRow}.
 */
@TargetApi(Build.VERSION_CODES.N)
class ScheduleRowPresenter extends RowPresenter {
    private static final String TAG = "ScheduleRowPresenter";

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({ACTION_START_RECORDING, ACTION_STOP_RECORDING, ACTION_CREATE_SCHEDULE,
            ACTION_REMOVE_SCHEDULE})
    public @interface ScheduleRowAction {}
    /** An action to start recording. */
    public static final int ACTION_START_RECORDING = 1;
    /** An action to stop recording. */
    public static final int ACTION_STOP_RECORDING = 2;
    /** An action to create schedule for the row. */
    public static final int ACTION_CREATE_SCHEDULE = 3;
    /** An action to remove the schedule. */
    public static final int ACTION_REMOVE_SCHEDULE = 4;

    private final Context mContext;
    private final DvrManager mDvrManager;
    private final DvrScheduleManager mDvrScheduleManager;

    private final String mTunerConflictWillNotBeRecordedInfo;
    private final String mTunerConflictWillBePartiallyRecordedInfo;
    private final int mAnimationDuration;

    private int mLastFocusedViewId;

    /**
     * A ViewHolder for {@link ScheduleRow}
     */
    public static class ScheduleRowViewHolder extends RowPresenter.ViewHolder {
        private ScheduleRowPresenter mPresenter;
        @ScheduleRowAction private int[] mActions;
        private boolean mLtr;
        private LinearLayout mInfoContainer;
        // The first action is on the right of the second action.
        private RelativeLayout mSecondActionContainer;
        private RelativeLayout mFirstActionContainer;
        private View mSelectorView;
        private TextView mTimeView;
        private TextView mProgramTitleView;
        private TextView mInfoSeparatorView;
        private TextView mChannelNameView;
        private TextView mConflictInfoView;
        private ImageView mSecondActionView;
        private ImageView mFirstActionView;

        private Runnable mPendingAnimationRunnable;

        private final int mSelectorTranslationDelta;
        private final int mSelectorWidthDelta;
        private final int mInfoContainerTargetWidthWithNoAction;
        private final int mInfoContainerTargetWidthWithOneAction;
        private final int mInfoContainerTargetWidthWithTwoAction;
        private final int mRoundRectRadius;

        private final OnFocusChangeListener mOnFocusChangeListener =
                new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View view, boolean focused) {
                        view.post(new Runnable() {
                            @Override
                            public void run() {
                                if (view.isFocused()) {
                                    mPresenter.mLastFocusedViewId = view.getId();
                                }
                                updateSelector();
                            }
                        });
                    }
                };

        public ScheduleRowViewHolder(View view, ScheduleRowPresenter presenter) {
            super(view);
            mPresenter = presenter;
            mLtr = view.getContext().getResources().getConfiguration().getLayoutDirection()
                    == View.LAYOUT_DIRECTION_LTR;
            mInfoContainer = (LinearLayout) view.findViewById(R.id.info_container);
            mSecondActionContainer = (RelativeLayout) view.findViewById(
                    R.id.action_second_container);
            mSecondActionView = (ImageView) view.findViewById(R.id.action_second);
            mFirstActionContainer = (RelativeLayout) view.findViewById(
                    R.id.action_first_container);
            mFirstActionView = (ImageView) view.findViewById(R.id.action_first);
            mSelectorView = view.findViewById(R.id.selector);
            mTimeView = (TextView) view.findViewById(R.id.time);
            mProgramTitleView = (TextView) view.findViewById(R.id.program_title);
            mInfoSeparatorView = (TextView) view.findViewById(R.id.info_separator);
            mChannelNameView = (TextView) view.findViewById(R.id.channel_name);
            mConflictInfoView = (TextView) view.findViewById(R.id.conflict_info);
            Resources res = view.getResources();
            mSelectorTranslationDelta =
                    res.getDimensionPixelSize(R.dimen.dvr_schedules_item_section_margin)
                    - res.getDimensionPixelSize(R.dimen.dvr_schedules_item_focus_translation_delta);
            mSelectorWidthDelta = res.getDimensionPixelSize(
                    R.dimen.dvr_schedules_item_focus_width_delta);
            mRoundRectRadius = res.getDimensionPixelSize(R.dimen.dvr_schedules_selector_radius);
            int fullWidth = res.getDimensionPixelSize(
                    R.dimen.dvr_schedules_item_width)
                    - 2 * res.getDimensionPixelSize(R.dimen.dvr_schedules_layout_padding);
            mInfoContainerTargetWidthWithNoAction = fullWidth + 2 * mRoundRectRadius;
            mInfoContainerTargetWidthWithOneAction = fullWidth
                    - res.getDimensionPixelSize(R.dimen.dvr_schedules_item_section_margin)
                    - res.getDimensionPixelSize(R.dimen.dvr_schedules_item_delete_width)
                    + mRoundRectRadius + mSelectorWidthDelta;
            mInfoContainerTargetWidthWithTwoAction = mInfoContainerTargetWidthWithOneAction
                    - res.getDimensionPixelSize(R.dimen.dvr_schedules_item_section_margin)
                    - res.getDimensionPixelSize(R.dimen.dvr_schedules_item_icon_size);

            mInfoContainer.setOnFocusChangeListener(mOnFocusChangeListener);
            mFirstActionContainer.setOnFocusChangeListener(mOnFocusChangeListener);
            mSecondActionContainer.setOnFocusChangeListener(mOnFocusChangeListener);
        }

        /**
         * Returns time view.
         */
        public TextView getTimeView() {
            return mTimeView;
        }

        /**
         * Returns title view.
         */
        public TextView getProgramTitleView() {
            return mProgramTitleView;
        }

        private void updateSelector() {
            int animationDuration = mSelectorView.getResources().getInteger(
                    android.R.integer.config_shortAnimTime);
            DecelerateInterpolator interpolator = new DecelerateInterpolator();

            if (mInfoContainer.isFocused() || mSecondActionContainer.isFocused()
                    || mFirstActionContainer.isFocused()) {
                final ViewGroup.LayoutParams lp = mSelectorView.getLayoutParams();
                final int targetWidth;
                if (mInfoContainer.isFocused()) {
                    // Use actions to check the visibility of the actions instead of calling
                    // View.getVisibility() because the view could be on the hiding animation.
                    if (mActions == null || mActions.length == 0) {
                        targetWidth = mInfoContainerTargetWidthWithNoAction;
                    } else if (mActions.length == 1) {
                        targetWidth = mInfoContainerTargetWidthWithOneAction;
                    } else {
                        targetWidth = mInfoContainerTargetWidthWithTwoAction;
                    }
                } else if (mSecondActionContainer.isFocused()) {
                    targetWidth = Math.max(mSecondActionContainer.getWidth(), 2 * mRoundRectRadius);
                } else {
                    targetWidth = mFirstActionContainer.getWidth() + mRoundRectRadius
                            + mSelectorTranslationDelta;
                }

                float targetTranslationX;
                if (mInfoContainer.isFocused()) {
                    targetTranslationX = mLtr ? mInfoContainer.getLeft() - mRoundRectRadius
                            - mSelectorView.getLeft() :
                            mInfoContainer.getRight() + mRoundRectRadius - mSelectorView.getRight();
                } else if (mSecondActionContainer.isFocused()) {
                    if (mSecondActionContainer.getWidth() > 2 * mRoundRectRadius) {
                        targetTranslationX = mLtr ? mSecondActionContainer.getLeft() -
                                mSelectorView.getLeft()
                                : mSecondActionContainer.getRight() - mSelectorView.getRight();
                    } else {
                        targetTranslationX = mLtr ? mSecondActionContainer.getLeft() -
                                (mRoundRectRadius - mSecondActionContainer.getWidth() / 2) -
                                mSelectorView.getLeft()
                                : mSecondActionContainer.getRight() +
                                (mRoundRectRadius - mSecondActionContainer.getWidth() / 2) -
                                mSelectorView.getRight();
                    }
                } else {
                    targetTranslationX = mLtr ? mFirstActionContainer.getLeft()
                            - mSelectorTranslationDelta - mSelectorView.getLeft()
                            : mFirstActionContainer.getRight() + mSelectorTranslationDelta
                            - mSelectorView.getRight();
                }

                if (mSelectorView.getAlpha() == 0) {
                    mSelectorView.setTranslationX(targetTranslationX);
                    lp.width = targetWidth;
                    mSelectorView.requestLayout();
                }

                // animate the selector in and to the proper width and translation X.
                final float deltaWidth = lp.width - targetWidth;
                mSelectorView.animate().cancel();
                mSelectorView.animate().translationX(targetTranslationX).alpha(1f)
                        .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                // Set width to the proper width for this animation step.
                                lp.width = targetWidth + Math.round(
                                        deltaWidth * (1f - animation.getAnimatedFraction()));
                                mSelectorView.requestLayout();
                            }
                        }).setDuration(animationDuration).setInterpolator(interpolator).start();
                if (mPendingAnimationRunnable != null) {
                    mPendingAnimationRunnable.run();
                    mPendingAnimationRunnable = null;
                }
            } else {
                mSelectorView.animate().cancel();
                mSelectorView.animate().alpha(0f).setDuration(animationDuration)
                        .setInterpolator(interpolator).setUpdateListener(null).start();
            }
        }

        /**
         * Grey out the information body.
         */
        public void greyOutInfo() {
            mTimeView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info_grey, null));
            mProgramTitleView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info_grey, null));
            mInfoSeparatorView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info_grey, null));
            mChannelNameView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info_grey, null));
            mConflictInfoView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info_grey, null));
        }

        /**
         * Reverse grey out operation.
         */
        public void whiteBackInfo() {
            mTimeView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info, null));
            mProgramTitleView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_main, null));
            mInfoSeparatorView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info, null));
            mChannelNameView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info, null));
            mConflictInfoView.setTextColor(mInfoContainer.getResources().getColor(R.color
                    .dvr_schedules_item_info, null));
        }
    }

    public ScheduleRowPresenter(Context context) {
        setHeaderPresenter(null);
        setSelectEffectEnabled(false);
        mContext = context;
        mDvrManager = TvApplication.getSingletons(context).getDvrManager();
        mDvrScheduleManager = TvApplication.getSingletons(context).getDvrScheduleManager();
        mTunerConflictWillNotBeRecordedInfo = mContext.getString(
                R.string.dvr_schedules_tuner_conflict_will_not_be_recorded_info);
        mTunerConflictWillBePartiallyRecordedInfo = mContext.getString(
                R.string.dvr_schedules_tuner_conflict_will_be_partially_recorded);
        mAnimationDuration = mContext.getResources().getInteger(
                android.R.integer.config_shortAnimTime);
    }

    @Override
    public ViewHolder createRowViewHolder(ViewGroup parent) {
        return onGetScheduleRowViewHolder(LayoutInflater.from(mContext)
                .inflate(R.layout.dvr_schedules_item, parent, false));
    }

    /**
     * Returns context.
     */
    protected Context getContext() {
        return mContext;
    }

    /**
     * Returns DVR manager.
     */
    protected DvrManager getDvrManager() {
        return mDvrManager;
    }

    @Override
    protected void onBindRowViewHolder(RowPresenter.ViewHolder vh, Object item) {
        super.onBindRowViewHolder(vh, item);
        ScheduleRowViewHolder viewHolder = (ScheduleRowViewHolder) vh;
        ScheduleRow row = (ScheduleRow) item;
        @ScheduleRowAction int[] actions = getAvailableActions(row);
        viewHolder.mActions = actions;
        viewHolder.mInfoContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isInfoClickable(row)) {
                    onInfoClicked(row);
                }
            }
        });

        viewHolder.mFirstActionContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onActionClicked(actions[0], row);
            }
        });

        viewHolder.mSecondActionContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onActionClicked(actions[1], row);
            }
        });

        viewHolder.mTimeView.setText(onGetRecordingTimeText(row));
        String programInfoText = onGetProgramInfoText(row);
        if (TextUtils.isEmpty(programInfoText)) {
            int durationMins = Math.max(1, Utils.getRoundOffMinsFromMs(row.getDuration()));
            programInfoText = mContext.getResources().getQuantityString(
                    R.plurals.dvr_schedules_recording_duration, durationMins, durationMins);
        }
        String channelName = getChannelNameText(row);
        viewHolder.mProgramTitleView.setText(programInfoText);
        viewHolder.mInfoSeparatorView.setVisibility((!TextUtils.isEmpty(programInfoText)
                && !TextUtils.isEmpty(channelName)) ? View.VISIBLE : View.GONE);
        viewHolder.mChannelNameView.setText(channelName);
        if (actions != null) {
            switch (actions.length) {
                case 2:
                    viewHolder.mSecondActionView.setImageResource(getImageForAction(actions[1]));
                    // pass through
                case 1:
                    viewHolder.mFirstActionView.setImageResource(getImageForAction(actions[0]));
                    break;
            }
        }
        if (mDvrManager.isConflicting(row.getSchedule())) {
            String conflictInfo;
            if (mDvrScheduleManager.isPartiallyConflicting(row.getSchedule())) {
                conflictInfo = mTunerConflictWillBePartiallyRecordedInfo;
            } else {
                conflictInfo = mTunerConflictWillNotBeRecordedInfo;
            }
            viewHolder.mConflictInfoView.setText(conflictInfo);
            viewHolder.mConflictInfoView.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mConflictInfoView.setVisibility(View.GONE);
        }
        if (shouldBeGrayedOut(row)) {
            viewHolder.greyOutInfo();
        } else {
            viewHolder.whiteBackInfo();
        }
        viewHolder.mInfoContainer.setFocusable(isInfoClickable(row));
        updateActionContainer(viewHolder, viewHolder.isSelected());
    }

    private int getImageForAction(@ScheduleRowAction int action) {
        switch (action) {
            case ACTION_START_RECORDING:
                return R.drawable.ic_record_start;
            case ACTION_STOP_RECORDING:
                return R.drawable.ic_record_stop;
            case ACTION_CREATE_SCHEDULE:
                return R.drawable.ic_scheduled_recording;
            case ACTION_REMOVE_SCHEDULE:
                return R.drawable.ic_dvr_cancel;
            default:
                return 0;
        }
    }

    /**
     * Returns view holder for schedule row.
     */
    protected ScheduleRowViewHolder onGetScheduleRowViewHolder(View view) {
        return new ScheduleRowViewHolder(view, this);
    }

    /**
     * Returns time text for time view from scheduled recording.
     */
    protected String onGetRecordingTimeText(ScheduleRow row) {
        return Utils.getDurationString(mContext, row.getStartTimeMs(), row.getEndTimeMs(), true,
                false, true, 0);
    }

    /**
     * Returns program info text for program title view.
     */
    protected String onGetProgramInfoText(ScheduleRow row) {
        return row.getProgramTitleWithEpisodeNumber(mContext);
    }

    private String getChannelNameText(ScheduleRow row) {
        Channel channel = TvApplication.getSingletons(mContext).getChannelDataManager()
                .getChannel(row.getChannelId());
        return channel == null ? null :
                TextUtils.isEmpty(channel.getDisplayName()) ? channel.getDisplayNumber() :
                        channel.getDisplayName().trim() + " " + channel.getDisplayNumber();
    }

    /**
     * Called when user click Info in {@link ScheduleRow}.
     */
    protected void onInfoClicked(ScheduleRow row) {
        DvrUiHelper.startDetailsActivity((Activity) mContext, row.getSchedule(), null, true);
    }

    private boolean isInfoClickable(ScheduleRow row) {
        return row.getSchedule() != null
                && (row.getSchedule().isNotStarted() || row.getSchedule().isInProgress());
    }

    /**
     * Called when the button in a row is clicked.
     */
    protected void onActionClicked(@ScheduleRowAction final int action, ScheduleRow row) {
        switch (action) {
            case ACTION_START_RECORDING:
                onStartRecording(row);
                break;
            case ACTION_STOP_RECORDING:
                onStopRecording(row);
                break;
            case ACTION_CREATE_SCHEDULE:
                onCreateSchedule(row);
                break;
            case ACTION_REMOVE_SCHEDULE:
                onRemoveSchedule(row);
                break;
        }
    }

    /**
     * Action handler for {@link #ACTION_START_RECORDING}.
     */
    protected void onStartRecording(ScheduleRow row) {
        ScheduledRecording schedule = row.getSchedule();
        if (schedule == null) {
            // This row has been deleted.
            return;
        }
        // Checks if there are current recordings that will be stopped by schedule this program.
        // If so, shows confirmation dialog to users.
        List<ScheduledRecording> conflictSchedules = mDvrScheduleManager.getConflictingSchedules(
                schedule.getChannelId(), System.currentTimeMillis(), schedule.getEndTimeMs());
        for (int i = conflictSchedules.size() - 1; i >= 0; i--) {
            ScheduledRecording conflictSchedule = conflictSchedules.get(i);
            if (conflictSchedule.isInProgress()) {
                DvrUiHelper.showStopRecordingDialog((Activity) mContext,
                        conflictSchedule.getChannelId(),
                        DvrStopRecordingFragment.REASON_ON_CONFLICT,
                        new HalfSizedDialogFragment.OnActionClickListener() {
                            @Override
                            public void onActionClick(long actionId) {
                                if (actionId == DvrStopRecordingFragment.ACTION_STOP) {
                                    onStartRecordingInternal(row);
                                }
                            }
                        });
                return;
            }
        }
        onStartRecordingInternal(row);
    }

    private void onStartRecordingInternal(ScheduleRow row) {
        if (row.isOnAir() && !row.isRecordingInProgress() && !row.isStartRecordingRequested()) {
            row.setStartRecordingRequested(true);
            if (row.isRecordingNotStarted()) {
                mDvrManager.setHighestPriority(row.getSchedule());
            } else if (row.isRecordingFinished()) {
                mDvrManager.addSchedule(ScheduledRecording.buildFrom(row.getSchedule())
                        .setId(ScheduledRecording.ID_NOT_SET)
                        .setState(ScheduledRecording.STATE_RECORDING_NOT_STARTED)
                        .setPriority(mDvrManager.suggestHighestPriority(row.getSchedule()))
                        .build());
            } else {
                SoftPreconditions.checkState(false, TAG, "Invalid row state to start recording: "
                        + row);
                return;
            }
            String msg = mContext.getString(R.string.dvr_msg_current_program_scheduled,
                    row.getSchedule().getProgramTitle(),
                    Utils.toTimeString(row.getEndTimeMs(), false));
            ToastUtils.show(mContext, msg, Toast.LENGTH_SHORT);
        }
    }

    /**
     * Action handler for {@link #ACTION_STOP_RECORDING}.
     */
    protected void onStopRecording(ScheduleRow row) {
        if (row.getSchedule() == null) {
            // This row has been deleted.
            return;
        }
        if (row.isRecordingInProgress() && !row.isStopRecordingRequested()) {
            row.setStopRecordingRequested(true);
            mDvrManager.stopRecording(row.getSchedule());
            CharSequence deletedInfo = onGetProgramInfoText(row);
            if (TextUtils.isEmpty(deletedInfo)) {
                deletedInfo = getChannelNameText(row);
            }
            ToastUtils.show(mContext, mContext.getResources()
                    .getString(R.string.dvr_schedules_deletion_info, deletedInfo),
                    Toast.LENGTH_SHORT);
        }
    }

    /**
     * Action handler for {@link #ACTION_CREATE_SCHEDULE}.
     */
    protected void onCreateSchedule(ScheduleRow row) {
        if (row.getSchedule() == null) {
            // This row has been deleted.
            return;
        }
        if (!row.isOnAir()) {
            if (row.isScheduleCanceled()) {
                mDvrManager.updateScheduledRecording(ScheduledRecording.buildFrom(row.getSchedule())
                        .setState(ScheduledRecording.STATE_RECORDING_NOT_STARTED)
                        .setPriority(mDvrManager.suggestHighestPriority(row.getSchedule()))
                        .build());
                String msg = mContext.getString(R.string.dvr_msg_program_scheduled,
                        row.getSchedule().getProgramTitle());
                ToastUtils.show(mContext, msg, Toast.LENGTH_SHORT);
            } else if (mDvrManager.isConflicting(row.getSchedule())) {
                mDvrManager.setHighestPriority(row.getSchedule());
            }
        }
    }

    /**
     * Action handler for {@link #ACTION_REMOVE_SCHEDULE}.
     */
    protected void onRemoveSchedule(ScheduleRow row) {
        if (row.getSchedule() == null) {
            // This row has been deleted.
            return;
        }
        CharSequence deletedInfo = null;
        if (row.isOnAir()) {
            if (row.isRecordingNotStarted()) {
                deletedInfo = getDeletedInfo(row);
                mDvrManager.removeScheduledRecording(row.getSchedule());
            }
        } else {
            if (mDvrManager.isConflicting(row.getSchedule())
                    && !shouldKeepScheduleAfterRemoving()) {
                deletedInfo = getDeletedInfo(row);
                mDvrManager.removeScheduledRecording(row.getSchedule());
            } else if (row.isRecordingNotStarted()) {
                deletedInfo = getDeletedInfo(row);
                mDvrManager.updateScheduledRecording(ScheduledRecording.buildFrom(row.getSchedule())
                        .setState(ScheduledRecording.STATE_RECORDING_CANCELED)
                        .build());
            }
        }
        if (deletedInfo != null) {
            ToastUtils.show(mContext, mContext.getResources()
                            .getString(R.string.dvr_schedules_deletion_info, deletedInfo),
                    Toast.LENGTH_SHORT);
        }
    }

    private CharSequence getDeletedInfo(ScheduleRow row) {
        CharSequence deletedInfo = onGetProgramInfoText(row);
        if (TextUtils.isEmpty(deletedInfo)) {
            return getChannelNameText(row);
        }
        return deletedInfo;
    }

    @Override
    protected void onRowViewSelected(ViewHolder vh, boolean selected) {
        super.onRowViewSelected(vh, selected);
        updateActionContainer(vh, selected);
    }

    /**
     * Internal method for onRowViewSelected, can be customized by subclass.
     */
    private void updateActionContainer(ViewHolder vh, boolean selected) {
        ScheduleRowViewHolder viewHolder = (ScheduleRowViewHolder) vh;
        viewHolder.mSecondActionContainer.animate().setListener(null).cancel();
        viewHolder.mFirstActionContainer.animate().setListener(null).cancel();
        if (selected && viewHolder.mActions != null) {
            switch (viewHolder.mActions.length) {
                case 2:
                    prepareShowActionView(viewHolder.mSecondActionContainer);
                    prepareShowActionView(viewHolder.mFirstActionContainer);
                    viewHolder.mPendingAnimationRunnable = new Runnable() {
                        @Override
                        public void run() {
                            showActionView(viewHolder.mSecondActionContainer);
                            showActionView(viewHolder.mFirstActionContainer);
                        }
                    };
                    break;
                case 1:
                    prepareShowActionView(viewHolder.mFirstActionContainer);
                    viewHolder.mPendingAnimationRunnable = new Runnable() {
                        @Override
                        public void run() {
                            hideActionView(viewHolder.mSecondActionContainer, View.GONE);
                            showActionView(viewHolder.mFirstActionContainer);
                        }
                    };
                    if (mLastFocusedViewId == R.id.action_second_container) {
                        mLastFocusedViewId = R.id.info_container;
                    }
                    break;
                case 0:
                default:
                    viewHolder.mPendingAnimationRunnable = new Runnable() {
                        @Override
                        public void run() {
                            hideActionView(viewHolder.mSecondActionContainer, View.GONE);
                            hideActionView(viewHolder.mFirstActionContainer, View.GONE);
                        }
                    };
                    mLastFocusedViewId = R.id.info_container;
                    SoftPreconditions.checkState(viewHolder.mInfoContainer.isFocusable(), TAG,
                            "No focusable view in this row: " + viewHolder);
                    break;
            }
            View view = viewHolder.view.findViewById(mLastFocusedViewId);
            if (view != null && view.getVisibility() == View.VISIBLE) {
                // When the row is selected, information container gets the initial focus.
                // To give the focus to the same control as the previous row, we need to call
                // requestFocus() explicitly.
                if (view.hasFocus()) {
                    viewHolder.mPendingAnimationRunnable.run();
                } else if (view.isFocusable()){
                    view.requestFocus();
                } else {
                    viewHolder.view.requestFocus();
                }
            }
        } else {
            viewHolder.mPendingAnimationRunnable = null;
            hideActionView(viewHolder.mFirstActionContainer, View.GONE);
            hideActionView(viewHolder.mSecondActionContainer, View.GONE);
        }
    }

    private void prepareShowActionView(View view) {
        if (view.getVisibility() != View.VISIBLE) {
            view.setAlpha(0.0f);
        }
        view.setVisibility(View.VISIBLE);
    }

    /**
     * Add animation when view is visible.
     */
    private void showActionView(View view) {
        view.animate().alpha(1.0f).setInterpolator(new DecelerateInterpolator())
                .setDuration(mAnimationDuration).start();
    }

    /**
     * Add animation when view change to invisible.
     */
    private void hideActionView(View view, int visibility) {
        if (view.getVisibility() != View.VISIBLE) {
            if (view.getVisibility() != visibility) {
                view.setVisibility(visibility);
            }
            return;
        }
        view.animate().alpha(0.0f).setInterpolator(new DecelerateInterpolator())
                .setDuration(mAnimationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        view.setVisibility(visibility);
                        view.animate().setListener(null);
                    }
                }).start();
    }

    /**
     * Returns the available actions according to the row's state. It should be the reverse order
     * with that in the screen.
     */
    @ScheduleRowAction
    protected int[] getAvailableActions(ScheduleRow row) {
        if (row.getSchedule() != null) {
            if (row.isRecordingInProgress()) {
                return new int[]{ACTION_STOP_RECORDING};
            } else if (row.isOnAir()) {
                if (row.isRecordingNotStarted()) {
                    if (canResolveConflict()) {
                        // The "START" action can change the conflict states.
                        return new int[] {ACTION_REMOVE_SCHEDULE, ACTION_START_RECORDING};
                    } else {
                        return new int[] {ACTION_REMOVE_SCHEDULE};
                    }
                } else if (row.isRecordingFinished()) {
                    return new int[] {ACTION_START_RECORDING};
                } else {
                    SoftPreconditions.checkState(false, TAG, "Invalid row state in checking the"
                            + " available actions(on air): " + row);
                }
            } else {
                if (row.isScheduleCanceled()) {
                    return new int[] {ACTION_CREATE_SCHEDULE};
                } else if (mDvrManager.isConflicting(row.getSchedule()) && canResolveConflict()) {
                    return new int[] {ACTION_REMOVE_SCHEDULE, ACTION_CREATE_SCHEDULE};
                } else if (row.isRecordingNotStarted()) {
                    return new int[] {ACTION_REMOVE_SCHEDULE};
                } else {
                    SoftPreconditions.checkState(false, TAG, "Invalid row state in checking the"
                            + " available actions(future schedule): " + row);
                }
            }
        }
        return null;
    }

    /**
     * Check if the conflict can be resolved in this screen.
     */
    protected boolean canResolveConflict() {
        return true;
    }

    /**
     * Check if the schedule should be kept after removing it.
     */
    protected boolean shouldKeepScheduleAfterRemoving() {
        return false;
    }

    /**
     * Checks if the row should be grayed out.
     */
    protected boolean shouldBeGrayedOut(ScheduleRow row) {
        return row.getSchedule() == null
                || (row.isOnAir() && !row.isRecordingInProgress())
                || mDvrManager.isConflicting(row.getSchedule())
                || row.isScheduleCanceled();
    }
}