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.ui;
18 
19 import android.content.pm.PackageManager;
20 import android.os.Bundle;
21 import android.os.Parcelable;
22 import android.support.annotation.NonNull;
23 import androidx.leanback.app.DetailsFragment;
24 import android.transition.Transition;
25 import android.transition.Transition.TransitionListener;
26 import android.util.Log;
27 import android.view.View;
28 import com.android.tv.R;
29 import com.android.tv.Starter;
30 import com.android.tv.TvSingletons;
31 import com.android.tv.dialog.PinDialogFragment;
32 import com.android.tv.dvr.DvrManager;
33 import com.android.tv.dvr.ui.browse.CurrentRecordingDetailsFragment;
34 import com.android.tv.dvr.ui.browse.RecordedProgramDetailsFragment;
35 import com.android.tv.dvr.ui.browse.ScheduledRecordingDetailsFragment;
36 import com.android.tv.dvr.ui.browse.SeriesRecordingDetailsFragment;
37 import dagger.android.ContributesAndroidInjector;
38 import dagger.android.DaggerActivity;
39 
40 /** Activity to show details view. */
41 public class DetailsActivity extends DaggerActivity
42         implements PinDialogFragment.OnPinCheckedListener {
43     private static final String TAG = "DetailsActivity";
44 
45     private static final long INVALID_RECORD_ID = -1;
46 
47     /** Name of record id added to the Intent. */
48     public static final String RECORDING_ID = "record_id";
49     /** Name of program uri added to the Intent. */
50     public static final String PROGRAM = "program";
51     /** Name of channel id added to the Intent. */
52     public static final String CHANNEL_ID = "channel_id";
53     /** Name of input id added to the Intent. */
54     public static final String INPUT_ID = "input_id";
55 
56     /**
57      * Name of flag added to the Intent to determine if details view should hide "View schedule"
58      * button.
59      */
60     public static final String HIDE_VIEW_SCHEDULE = "hide_view_schedule";
61 
62     /** Name of details view's type added to the intent. */
63     public static final String DETAILS_VIEW_TYPE = "details_view_type";
64 
65     /** Name of shared element between activities. */
66     public static final String SHARED_ELEMENT_NAME = "shared_element";
67 
68     /** CURRENT_RECORDING_VIEW refers to Current Recordings in DVR. */
69     public static final int CURRENT_RECORDING_VIEW = 1;
70 
71     /** SCHEDULED_RECORDING_VIEW refers to Scheduled Recordings in DVR. */
72     public static final int SCHEDULED_RECORDING_VIEW = 2;
73 
74     /** RECORDED_PROGRAM_VIEW refers to Recorded programs in DVR. */
75     public static final int RECORDED_PROGRAM_VIEW = 3;
76 
77     /** SERIES_RECORDING_VIEW refers to series recording in DVR. */
78     public static final int SERIES_RECORDING_VIEW = 4;
79 
80     /** SERIES_RECORDING_VIEW refers to program. */
81     public static final int PROGRAM_VIEW = 5;
82 
83     public static final int REQUEST_DELETE = 1;
84 
85     private PinDialogFragment.OnPinCheckedListener mOnPinCheckedListener;
86     private long mRecordId = INVALID_RECORD_ID;
87 
88     @Override
onCreate(Bundle savedInstanceState)89     public void onCreate(Bundle savedInstanceState) {
90         Starter.start(this);
91         super.onCreate(savedInstanceState);
92         setContentView(R.layout.activity_dvr_details);
93         long recordId = getIntent().getLongExtra(RECORDING_ID, INVALID_RECORD_ID);
94         int detailsViewType = getIntent().getIntExtra(DETAILS_VIEW_TYPE, -1);
95         boolean hideViewSchedule = getIntent().getBooleanExtra(HIDE_VIEW_SCHEDULE, false);
96         long channelId = getIntent().getLongExtra(CHANNEL_ID, -1);
97         DetailsFragment detailsFragment = null;
98         Bundle args = new Bundle();
99         if (detailsViewType != -1 && savedInstanceState == null) {
100             if (recordId != INVALID_RECORD_ID) {
101                 mRecordId = recordId;
102                 args.putLong(RECORDING_ID, mRecordId);
103                 if (detailsViewType == CURRENT_RECORDING_VIEW) {
104                     detailsFragment = new CurrentRecordingDetailsFragment();
105                 } else if (detailsViewType == SCHEDULED_RECORDING_VIEW) {
106                     args.putBoolean(HIDE_VIEW_SCHEDULE, hideViewSchedule);
107                     detailsFragment = new ScheduledRecordingDetailsFragment();
108                 } else if (detailsViewType == RECORDED_PROGRAM_VIEW) {
109                     detailsFragment = new RecordedProgramDetailsFragment();
110                 } else if (detailsViewType == SERIES_RECORDING_VIEW) {
111                     detailsFragment = new SeriesRecordingDetailsFragment();
112                 }
113             } else if (detailsViewType == PROGRAM_VIEW && channelId != -1) {
114                 Parcelable program = getIntent().getParcelableExtra(PROGRAM);
115                 if (program != null) {
116                     args.putLong(CHANNEL_ID, channelId);
117                     args.putParcelable(PROGRAM, program);
118                     args.putString(INPUT_ID, getIntent().getStringExtra(INPUT_ID));
119                     detailsFragment = new ProgramDetailsFragment();
120                 }
121             }
122             if (detailsFragment != null) {
123                 detailsFragment.setArguments(args);
124                 getFragmentManager()
125                         .beginTransaction()
126                         .replace(R.id.dvr_details_view_frame, detailsFragment)
127                         .commit();
128             }
129         }
130 
131         // This is a workaround for the focus on O device
132         addTransitionListener();
133     }
134 
135     @Override
onPinChecked(boolean checked, int type, String rating)136     public void onPinChecked(boolean checked, int type, String rating) {
137         if (mOnPinCheckedListener != null) {
138             mOnPinCheckedListener.onPinChecked(checked, type, rating);
139         }
140     }
141 
setOnPinCheckListener(PinDialogFragment.OnPinCheckedListener listener)142     public void setOnPinCheckListener(PinDialogFragment.OnPinCheckedListener listener) {
143         mOnPinCheckedListener = listener;
144     }
145 
addTransitionListener()146     private void addTransitionListener() {
147         getWindow()
148                 .getSharedElementEnterTransition()
149                 .addListener(
150                         new TransitionListener() {
151                             @Override
152                             public void onTransitionStart(Transition transition) {
153                                 // Do nothing
154                             }
155 
156                             @Override
157                             public void onTransitionEnd(Transition transition) {
158                                 View actions = findViewById(R.id.details_overview_actions);
159                                 if (actions != null) {
160                                     actions.requestFocus();
161                                 }
162                             }
163 
164                             @Override
165                             public void onTransitionCancel(Transition transition) {
166                                 // Do nothing
167 
168                             }
169 
170                             @Override
171                             public void onTransitionPause(Transition transition) {
172                                 // Do nothing
173                             }
174 
175                             @Override
176                             public void onTransitionResume(Transition transition) {
177                                 // Do nothing
178                             }
179                         });
180     }
181 
182     @Override
onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)183     public void onRequestPermissionsResult(
184             int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
185         switch (requestCode) {
186             case REQUEST_DELETE:
187                 // If request is cancelled, the result arrays are empty.
188                 if (grantResults.length > 0
189                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
190                     delete(true);
191                 } else {
192                     Log.i(
193                             TAG,
194                             "Write permission denied, Not trying to delete the file for "
195                                     + mRecordId);
196                     delete(false);
197                 }
198                 break;
199             default:
200                 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
201         }
202     }
203 
delete(boolean deleteFile)204     private void delete(boolean deleteFile) {
205         if (mRecordId != INVALID_RECORD_ID) {
206             DvrManager dvrManager = TvSingletons.getSingletons(this).getDvrManager();
207             dvrManager.removeRecordedProgram(mRecordId, deleteFile);
208         }
209         finish();
210     }
211 
212     /** Exports {@link DaggerActivity} for Dagger codegen to create the appropriate injector. */
213     @dagger.Module
214     public abstract static class Module {
215         @ContributesAndroidInjector
contributesDetailsActivityInjector()216         abstract DetailsActivity contributesDetailsActivityInjector();
217 
218         @ContributesAndroidInjector
219         abstract CurrentRecordingDetailsFragment
contributesCurrentRecordingDetailsFragmentInjector()220                 contributesCurrentRecordingDetailsFragmentInjector();
221 
222         @ContributesAndroidInjector
contributesProgramDetailsFragmentInjector()223         abstract ProgramDetailsFragment contributesProgramDetailsFragmentInjector();
224     }
225 }
226