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