Home | History | Annotate | Download | only in browse
      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 
     17 package com.android.tv.dvr.ui.browse;
     18 
     19 import android.content.Context;
     20 import android.media.tv.TvInputManager;
     21 import com.android.tv.R;
     22 import com.android.tv.TvSingletons;
     23 import com.android.tv.dvr.DvrWatchedPositionManager;
     24 import com.android.tv.dvr.DvrWatchedPositionManager.WatchedPositionChangedListener;
     25 import com.android.tv.dvr.data.RecordedProgram;
     26 import com.android.tv.util.Utils;
     27 
     28 /** Presents a {@link RecordedProgram} in the {@link DvrBrowseFragment}. */
     29 public class RecordedProgramPresenter extends DvrItemPresenter<RecordedProgram> {
     30     private final DvrWatchedPositionManager mDvrWatchedPositionManager;
     31     private String mTodayString;
     32     private String mYesterdayString;
     33     private final int mProgressBarColor;
     34     private final boolean mShowEpisodeTitle;
     35     private final boolean mExpandTitleWhenFocused;
     36 
     37     protected final class RecordedProgramViewHolder extends DvrItemViewHolder
     38             implements WatchedPositionChangedListener {
     39         private RecordedProgram mProgram;
     40         private boolean mShowProgress;
     41 
     42         public RecordedProgramViewHolder(RecordingCardView view, Integer progressColor) {
     43             super(view);
     44             if (progressColor == null) {
     45                 mShowProgress = false;
     46             } else {
     47                 mShowProgress = true;
     48                 view.setProgressBarColor(progressColor);
     49             }
     50         }
     51 
     52         private void setProgressBar(long watchedPositionMs) {
     53             ((RecordingCardView) view)
     54                     .setProgressBar(
     55                             (watchedPositionMs == TvInputManager.TIME_SHIFT_INVALID_TIME)
     56                                     ? null
     57                                     : Math.min(
     58                                             100,
     59                                             (int)
     60                                                     (100.0f
     61                                                             * watchedPositionMs
     62                                                             / mProgram.getDurationMillis())));
     63         }
     64 
     65         @Override
     66         public void onWatchedPositionChanged(long programId, long positionMs) {
     67             if (programId == mProgram.getId()) {
     68                 setProgressBar(positionMs);
     69             }
     70         }
     71 
     72         @Override
     73         protected void onBound(RecordedProgram program) {
     74             mProgram = program;
     75             if (mShowProgress) {
     76                 mDvrWatchedPositionManager.addListener(this, program.getId());
     77                 setProgressBar(mDvrWatchedPositionManager.getWatchedPosition(program.getId()));
     78             } else {
     79                 getView().setProgressBar(null);
     80             }
     81         }
     82 
     83         @Override
     84         protected void onUnbound() {
     85             if (mShowProgress) {
     86                 mDvrWatchedPositionManager.removeListener(this, mProgram.getId());
     87             }
     88             getView().reset();
     89         }
     90     }
     91 
     92     RecordedProgramPresenter(
     93             Context context, boolean showEpisodeTitle, boolean expandTitleWhenFocused) {
     94         super(context);
     95         mTodayString = mContext.getString(R.string.dvr_date_today);
     96         mYesterdayString = mContext.getString(R.string.dvr_date_yesterday);
     97         mDvrWatchedPositionManager =
     98                 TvSingletons.getSingletons(mContext).getDvrWatchedPositionManager();
     99         mProgressBarColor =
    100                 mContext.getResources().getColor(R.color.play_controls_progress_bar_watched);
    101         mShowEpisodeTitle = showEpisodeTitle;
    102         mExpandTitleWhenFocused = expandTitleWhenFocused;
    103     }
    104 
    105     public RecordedProgramPresenter(Context context) {
    106         this(context, false, false);
    107     }
    108 
    109     @Override
    110     public DvrItemViewHolder onCreateDvrItemViewHolder() {
    111         return new RecordedProgramViewHolder(
    112                 new RecordingCardView(mContext, mExpandTitleWhenFocused), mProgressBarColor);
    113     }
    114 
    115     @Override
    116     public void onBindDvrItemViewHolder(DvrItemViewHolder baseHolder, RecordedProgram program) {
    117         final RecordedProgramViewHolder viewHolder = (RecordedProgramViewHolder) baseHolder;
    118         final RecordingCardView cardView = viewHolder.getView();
    119         DetailsContent details = DetailsContent.createFromRecordedProgram(mContext, program);
    120         cardView.setTitle(
    121                 mShowEpisodeTitle ? program.getEpisodeDisplayTitle(mContext) : details.getTitle());
    122         cardView.setImageUri(details.getLogoImageUri(), details.isUsingChannelLogo());
    123         cardView.setContent(generateMajorContent(program), generateMinorContent(program));
    124         cardView.setDetailBackgroundImageUri(details.getBackgroundImageUri());
    125     }
    126 
    127     private String generateMajorContent(RecordedProgram program) {
    128         int dateDifference =
    129                 Utils.computeDateDifference(
    130                         program.getStartTimeUtcMillis(), System.currentTimeMillis());
    131         if (dateDifference == 0) {
    132             return mTodayString;
    133         } else if (dateDifference == 1) {
    134             return mYesterdayString;
    135         } else {
    136             return Utils.getDurationString(
    137                     mContext,
    138                     program.getStartTimeUtcMillis(),
    139                     program.getStartTimeUtcMillis(),
    140                     false,
    141                     true,
    142                     false,
    143                     0);
    144         }
    145     }
    146 
    147     private String generateMinorContent(RecordedProgram program) {
    148         int durationMinutes = Math.max(1, Utils.getRoundOffMinsFromMs(program.getDurationMillis()));
    149         return mContext.getResources()
    150                 .getQuantityString(
    151                         R.plurals.dvr_program_duration, durationMinutes, durationMinutes);
    152     }
    153 }
    154