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.res.Resources;
     20 import android.media.tv.TvInputManager;
     21 import android.os.Bundle;
     22 import android.support.v17.leanback.widget.Action;
     23 import android.support.v17.leanback.widget.OnActionClickedListener;
     24 import android.support.v17.leanback.widget.SparseArrayObjectAdapter;
     25 import com.android.tv.R;
     26 import com.android.tv.TvSingletons;
     27 import com.android.tv.dvr.DvrDataManager;
     28 import com.android.tv.dvr.DvrManager;
     29 import com.android.tv.dvr.DvrWatchedPositionManager;
     30 import com.android.tv.dvr.data.RecordedProgram;
     31 
     32 /** {@link android.support.v17.leanback.app.DetailsFragment} for recorded program in DVR. */
     33 public class RecordedProgramDetailsFragment extends DvrDetailsFragment
     34         implements DvrDataManager.RecordedProgramListener {
     35     private static final int ACTION_RESUME_PLAYING = 1;
     36     private static final int ACTION_PLAY_FROM_BEGINNING = 2;
     37     private static final int ACTION_DELETE_RECORDING = 3;
     38 
     39     private DvrWatchedPositionManager mDvrWatchedPositionManager;
     40 
     41     private RecordedProgram mRecordedProgram;
     42     private boolean mPaused;
     43     private DvrDataManager mDvrDataManager;
     44 
     45     @Override
     46     public void onCreate(Bundle savedInstanceState) {
     47         mDvrDataManager = TvSingletons.getSingletons(getContext()).getDvrDataManager();
     48         mDvrDataManager.addRecordedProgramListener(this);
     49         super.onCreate(savedInstanceState);
     50     }
     51 
     52     @Override
     53     public void onCreateInternal() {
     54         mDvrWatchedPositionManager =
     55                 TvSingletons.getSingletons(getActivity()).getDvrWatchedPositionManager();
     56         setDetailsOverviewRow(
     57                 DetailsContent.createFromRecordedProgram(getContext(), mRecordedProgram));
     58     }
     59 
     60     @Override
     61     public void onResume() {
     62         super.onResume();
     63         if (mPaused) {
     64             updateActions();
     65             mPaused = false;
     66         }
     67     }
     68 
     69     @Override
     70     public void onPause() {
     71         super.onPause();
     72         mPaused = true;
     73     }
     74 
     75     @Override
     76     public void onDestroy() {
     77         mDvrDataManager.removeRecordedProgramListener(this);
     78         super.onDestroy();
     79     }
     80 
     81     @Override
     82     protected boolean onLoadRecordingDetails(Bundle args) {
     83         long recordedProgramId = args.getLong(DvrDetailsActivity.RECORDING_ID);
     84         mRecordedProgram = mDvrDataManager.getRecordedProgram(recordedProgramId);
     85         return mRecordedProgram != null;
     86     }
     87 
     88     @Override
     89     protected SparseArrayObjectAdapter onCreateActionsAdapter() {
     90         SparseArrayObjectAdapter adapter =
     91                 new SparseArrayObjectAdapter(new ActionPresenterSelector());
     92         Resources res = getResources();
     93         if (mDvrWatchedPositionManager.getWatchedStatus(mRecordedProgram)
     94                 == DvrWatchedPositionManager.DVR_WATCHED_STATUS_WATCHING) {
     95             adapter.set(
     96                     ACTION_RESUME_PLAYING,
     97                     new Action(
     98                             ACTION_RESUME_PLAYING,
     99                             res.getString(R.string.dvr_detail_resume_play),
    100                             null,
    101                             res.getDrawable(R.drawable.lb_ic_play)));
    102             adapter.set(
    103                     ACTION_PLAY_FROM_BEGINNING,
    104                     new Action(
    105                             ACTION_PLAY_FROM_BEGINNING,
    106                             res.getString(R.string.dvr_detail_play_from_beginning),
    107                             null,
    108                             res.getDrawable(R.drawable.lb_ic_replay)));
    109         } else {
    110             adapter.set(
    111                     ACTION_PLAY_FROM_BEGINNING,
    112                     new Action(
    113                             ACTION_PLAY_FROM_BEGINNING,
    114                             res.getString(R.string.dvr_detail_watch),
    115                             null,
    116                             res.getDrawable(R.drawable.lb_ic_play)));
    117         }
    118         adapter.set(
    119                 ACTION_DELETE_RECORDING,
    120                 new Action(
    121                         ACTION_DELETE_RECORDING,
    122                         res.getString(R.string.dvr_detail_delete),
    123                         null,
    124                         res.getDrawable(R.drawable.ic_delete_32dp)));
    125         return adapter;
    126     }
    127 
    128     @Override
    129     protected OnActionClickedListener onCreateOnActionClickedListener() {
    130         return new OnActionClickedListener() {
    131             @Override
    132             public void onActionClicked(Action action) {
    133                 if (action.getId() == ACTION_PLAY_FROM_BEGINNING) {
    134                     startPlayback(mRecordedProgram, TvInputManager.TIME_SHIFT_INVALID_TIME);
    135                 } else if (action.getId() == ACTION_RESUME_PLAYING) {
    136                     startPlayback(
    137                             mRecordedProgram,
    138                             mDvrWatchedPositionManager.getWatchedPosition(
    139                                     mRecordedProgram.getId()));
    140                 } else if (action.getId() == ACTION_DELETE_RECORDING) {
    141                     DvrManager dvrManager =
    142                             TvSingletons.getSingletons(getActivity()).getDvrManager();
    143                     dvrManager.removeRecordedProgram(mRecordedProgram);
    144                     getActivity().finish();
    145                 }
    146             }
    147         };
    148     }
    149 
    150     @Override
    151     public void onRecordedProgramsAdded(RecordedProgram... recordedPrograms) {}
    152 
    153     @Override
    154     public void onRecordedProgramsChanged(RecordedProgram... recordedPrograms) {}
    155 
    156     @Override
    157     public void onRecordedProgramsRemoved(RecordedProgram... recordedPrograms) {
    158         for (RecordedProgram recordedProgram : recordedPrograms) {
    159             if (recordedProgram.getId() == mRecordedProgram.getId()) {
    160                 getActivity().finish();
    161             }
    162         }
    163     }
    164 }
    165