1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tv.dvr.ui.browse;
18
19import android.content.Context;
20import android.media.tv.TvInputManager;
21
22import com.android.tv.R;
23import com.android.tv.TvApplication;
24import com.android.tv.dvr.DvrWatchedPositionManager;
25import com.android.tv.dvr.DvrWatchedPositionManager.WatchedPositionChangedListener;
26import com.android.tv.dvr.data.RecordedProgram;
27import com.android.tv.util.Utils;
28
29/**
30 * Presents a {@link RecordedProgram} in the {@link DvrBrowseFragment}.
31 */
32public class RecordedProgramPresenter extends DvrItemPresenter<RecordedProgram> {
33    private final DvrWatchedPositionManager mDvrWatchedPositionManager;
34    private String mTodayString;
35    private String mYesterdayString;
36    private final int mProgressBarColor;
37    private final boolean mShowEpisodeTitle;
38    private final boolean mExpandTitleWhenFocused;
39
40    protected final class RecordedProgramViewHolder extends DvrItemViewHolder
41            implements WatchedPositionChangedListener {
42        private RecordedProgram mProgram;
43        private boolean mShowProgress;
44
45        public RecordedProgramViewHolder(RecordingCardView view, Integer progressColor) {
46            super(view);
47            if (progressColor == null) {
48                mShowProgress = false;
49            } else {
50                mShowProgress = true;
51                view.setProgressBarColor(progressColor);
52            }
53        }
54
55        private void setProgressBar(long watchedPositionMs) {
56            ((RecordingCardView) view).setProgressBar(
57                    (watchedPositionMs == TvInputManager.TIME_SHIFT_INVALID_TIME) ? null
58                            : Math.min(100, (int) (100.0f * watchedPositionMs
59                                    / mProgram.getDurationMillis())));
60        }
61
62        @Override
63        public void onWatchedPositionChanged(long programId, long positionMs) {
64            if (programId == mProgram.getId()) {
65                setProgressBar(positionMs);
66            }
67        }
68
69        @Override
70        protected void onBound(RecordedProgram program) {
71            mProgram = program;
72            if (mShowProgress) {
73                mDvrWatchedPositionManager.addListener(this, program.getId());
74                setProgressBar(mDvrWatchedPositionManager.getWatchedPosition(program.getId()));
75            } else {
76                getView().setProgressBar(null);
77            }
78        }
79
80        @Override
81        protected void onUnbound() {
82            if (mShowProgress) {
83                mDvrWatchedPositionManager.removeListener(this, mProgram.getId());
84            }
85            getView().reset();
86        }
87    }
88
89    RecordedProgramPresenter(Context context, boolean showEpisodeTitle,
90            boolean expandTitleWhenFocused) {
91        super(context);
92        mTodayString = mContext.getString(R.string.dvr_date_today);
93        mYesterdayString = mContext.getString(R.string.dvr_date_yesterday);
94        mDvrWatchedPositionManager =
95                TvApplication.getSingletons(mContext).getDvrWatchedPositionManager();
96        mProgressBarColor = mContext.getResources()
97                .getColor(R.color.play_controls_progress_bar_watched);
98        mShowEpisodeTitle = showEpisodeTitle;
99        mExpandTitleWhenFocused = expandTitleWhenFocused;
100    }
101
102    public RecordedProgramPresenter(Context context) {
103        this(context, false, false);
104    }
105
106    @Override
107    public DvrItemViewHolder onCreateDvrItemViewHolder() {
108        return new RecordedProgramViewHolder(
109                new RecordingCardView(mContext, mExpandTitleWhenFocused), mProgressBarColor);
110    }
111
112    @Override
113    public void onBindDvrItemViewHolder(DvrItemViewHolder baseHolder, RecordedProgram program) {
114        final RecordedProgramViewHolder viewHolder = (RecordedProgramViewHolder) baseHolder;
115        final RecordingCardView cardView = viewHolder.getView();
116        DetailsContent details = DetailsContent.createFromRecordedProgram(mContext, program);
117        cardView.setTitle(mShowEpisodeTitle ?
118                program.getEpisodeDisplayTitle(mContext) : details.getTitle());
119        cardView.setImageUri(details.getLogoImageUri(), details.isUsingChannelLogo());
120        cardView.setContent(generateMajorContent(program), generateMinorContent(program));
121        cardView.setDetailBackgroundImageUri(details.getBackgroundImageUri());
122    }
123
124    private String generateMajorContent(RecordedProgram program) {
125        int dateDifference = Utils.computeDateDifference(program.getStartTimeUtcMillis(),
126                System.currentTimeMillis());
127        if (dateDifference == 0) {
128            return mTodayString;
129        } else if (dateDifference == 1) {
130            return mYesterdayString;
131        } else {
132            return Utils.getDurationString(mContext, program.getStartTimeUtcMillis(),
133                    program.getStartTimeUtcMillis(), false, true, false, 0);
134        }
135    }
136
137    private String generateMinorContent(RecordedProgram program) {
138        int durationMinutes = Math.max(1, Utils.getRoundOffMinsFromMs(program.getDurationMillis()));
139        return mContext.getResources().getQuantityString(
140                R.plurals.dvr_program_duration, durationMinutes, durationMinutes);
141    }
142}
143