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