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.content.res.Resources;
21 import android.media.tv.TvInputManager;
22 
23 import androidx.leanback.widget.Action;
24 import androidx.leanback.widget.OnActionClickedListener;
25 import androidx.leanback.widget.SparseArrayObjectAdapter;
26 
27 import com.android.tv.R;
28 import com.android.tv.TvSingletons;
29 import com.android.tv.dialog.HalfSizedDialogFragment;
30 import com.android.tv.dvr.DvrDataManager;
31 import com.android.tv.dvr.DvrManager;
32 import com.android.tv.dvr.DvrWatchedPositionManager;
33 import com.android.tv.dvr.data.RecordedProgram;
34 import com.android.tv.dvr.data.ScheduledRecording;
35 import com.android.tv.dvr.ui.DvrStopRecordingFragment;
36 import com.android.tv.dvr.ui.DvrUiHelper;
37 
38 import dagger.android.AndroidInjection;
39 
40 import javax.inject.Inject;
41 
42 /** {@link RecordingDetailsFragment} for current recording in DVR. */
43 public class CurrentRecordingDetailsFragment extends RecordingDetailsFragment {
44     private static final int ACTION_STOP_RECORDING = 1;
45     private static final int ACTION_RESUME_PLAYING = 2;
46     private static final int ACTION_PLAY_FROM_BEGINNING = 3;
47 
48     private DvrDataManager mDvrDataManger;
49     private RecordedProgram mRecordedProgram;
50     @Inject DvrWatchedPositionManager mDvrWatchedPositionManager;
51     private boolean mPaused;
52     private final DvrDataManager.ScheduledRecordingListener mScheduledRecordingListener =
53             new DvrDataManager.ScheduledRecordingListener() {
54                 @Override
55                 public void onScheduledRecordingAdded(ScheduledRecording... schedules) {}
56 
57                 @Override
58                 public void onScheduledRecordingRemoved(ScheduledRecording... schedules) {
59                     for (ScheduledRecording schedule : schedules) {
60                         if (schedule.getId() == getRecording().getId()) {
61                             getActivity().finish();
62                             return;
63                         }
64                     }
65                 }
66 
67                 @Override
68                 public void onScheduledRecordingStatusChanged(ScheduledRecording... schedules) {
69                     for (ScheduledRecording schedule : schedules) {
70                         if (schedule.getId() == getRecording().getId()
71                                 && schedule.getState()
72                                         != ScheduledRecording.STATE_RECORDING_IN_PROGRESS) {
73                             getActivity().finish();
74                             return;
75                         }
76                     }
77                 }
78             };
79 
80     @Override
onAttach(Context context)81     public void onAttach(Context context) {
82         AndroidInjection.inject(this);
83         super.onAttach(context);
84         mDvrDataManger = TvSingletons.getSingletons(context).getDvrDataManager();
85         mDvrDataManger.addScheduledRecordingListener(mScheduledRecordingListener);
86     }
87 
88     @Override
onResume()89     public void onResume() {
90         super.onResume();
91         if (mPaused) {
92             updateActions();
93             mPaused = false;
94         }
95     }
96 
97     @Override
onPause()98     public void onPause() {
99         super.onPause();
100         mPaused = true;
101     }
102 
103     @Override
onCreateActionsAdapter()104     protected SparseArrayObjectAdapter onCreateActionsAdapter() {
105         Long recordedProgramId = getRecording().getRecordedProgramId();
106         if (recordedProgramId != null) {
107             mRecordedProgram = mDvrDataManger.getRecordedProgram(recordedProgramId);
108         }
109         SparseArrayObjectAdapter adapter =
110                 new SparseArrayObjectAdapter(new ActionPresenterSelector());
111         Resources res = getResources();
112         adapter.set(
113                 ACTION_STOP_RECORDING,
114                 new Action(
115                         ACTION_STOP_RECORDING,
116                         res.getString(R.string.dvr_detail_stop_recording),
117                         null,
118                         res.getDrawable(R.drawable.lb_ic_stop)));
119         if (mRecordedProgram != null && mRecordedProgram.isPartial()) {
120             if (mDvrWatchedPositionManager.getWatchedStatus(mRecordedProgram)
121                     == DvrWatchedPositionManager.DVR_WATCHED_STATUS_WATCHING) {
122                 adapter.set(
123                         ACTION_RESUME_PLAYING,
124                         new Action(
125                                 ACTION_RESUME_PLAYING,
126                                 res.getString(R.string.dvr_detail_resume_play),
127                                 null,
128                                 res.getDrawable(R.drawable.lb_ic_play)));
129                 adapter.set(
130                         ACTION_PLAY_FROM_BEGINNING,
131                         new Action(
132                                 ACTION_PLAY_FROM_BEGINNING,
133                                 res.getString(R.string.dvr_detail_play_from_beginning),
134                                 null,
135                                 res.getDrawable(R.drawable.lb_ic_replay)));
136             } else {
137                 adapter.set(
138                         ACTION_PLAY_FROM_BEGINNING,
139                         new Action(
140                                 ACTION_PLAY_FROM_BEGINNING,
141                                 res.getString(R.string.dvr_detail_watch),
142                                 null,
143                                 res.getDrawable(R.drawable.lb_ic_play)));
144             }
145         }
146         return adapter;
147     }
148 
149     @Override
onCreateOnActionClickedListener()150     protected OnActionClickedListener onCreateOnActionClickedListener() {
151         return new OnActionClickedListener() {
152             @Override
153             public void onActionClicked(Action action) {
154                 if (action.getId() == ACTION_STOP_RECORDING) {
155                     DvrUiHelper.showStopRecordingDialog(
156                             getActivity(),
157                             getRecording().getChannelId(),
158                             DvrStopRecordingFragment.REASON_USER_STOP,
159                             new HalfSizedDialogFragment.OnActionClickListener() {
160                                 @Override
161                                 public void onActionClick(long actionId) {
162                                     if (actionId == DvrStopRecordingFragment.ACTION_STOP) {
163                                         DvrManager dvrManager =
164                                                 TvSingletons.getSingletons(getContext())
165                                                         .getDvrManager();
166                                         dvrManager.stopRecording(getRecording());
167                                         getActivity().finish();
168                                     }
169                                 }
170                             });
171                 } else if (action.getId() == ACTION_RESUME_PLAYING) {
172                     startPlayback(
173                             mRecordedProgram,
174                             mDvrWatchedPositionManager.getWatchedPosition(
175                                     mRecordedProgram.getId()));
176                 } else if (action.getId() == ACTION_PLAY_FROM_BEGINNING) {
177                     startPlayback(mRecordedProgram, TvInputManager.TIME_SHIFT_INVALID_TIME);
178                 }
179             }
180         };
181     }
182 
183     @Override
184     public void onDetach() {
185         if (mDvrDataManger != null) {
186             mDvrDataManger.removeScheduledRecordingListener(mScheduledRecordingListener);
187         }
188         super.onDetach();
189     }
190 }
191