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; 18 19 import android.app.Activity; 20 import android.content.pm.PackageManager; 21 import android.os.Bundle; 22 import android.support.annotation.NonNull; 23 import android.support.v17.leanback.app.GuidedStepFragment; 24 import android.util.Log; 25 import android.widget.Toast; 26 27 import com.android.tv.R; 28 import com.android.tv.Starter; 29 import com.android.tv.TvSingletons; 30 import com.android.tv.dvr.DvrManager; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 /** Activity to show details view in DVR. */ 36 public class DvrSeriesDeletionActivity extends Activity { 37 private static final String TAG = "DvrSeriesDeletionActivity"; 38 39 /** Name of series id added to the Intent. */ 40 public static final String SERIES_RECORDING_ID = "series_recording_id"; 41 42 public static final int REQUEST_DELETE = 1; 43 public static final long INVALID_SERIES_RECORDING_ID = -1; 44 45 private long mSeriesRecordingId = INVALID_SERIES_RECORDING_ID; 46 private final List<Long> mIdsToDelete = new ArrayList<>(); 47 48 @Override onCreate(Bundle savedInstanceState)49 public void onCreate(Bundle savedInstanceState) { 50 Starter.start(this); 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.activity_dvr_series_settings); 53 // Check savedInstanceState to prevent that activity is being showed with animation. 54 if (savedInstanceState == null) { 55 mSeriesRecordingId = 56 getIntent().getLongExtra(SERIES_RECORDING_ID, INVALID_SERIES_RECORDING_ID); 57 DvrSeriesDeletionFragment deletionFragment = new DvrSeriesDeletionFragment(); 58 deletionFragment.setArguments(getIntent().getExtras()); 59 GuidedStepFragment.addAsRoot(this, deletionFragment, R.id.dvr_settings_view_frame); 60 } 61 } 62 63 @Override onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)64 public void onRequestPermissionsResult( 65 int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 66 switch (requestCode) { 67 case REQUEST_DELETE: 68 // If request is cancelled, the result arrays are empty. 69 if (grantResults.length > 0 70 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 71 deleteSelectedIds(true); 72 } else { 73 // NOTE: If Live TV ever supports both embedded and separate DVR inputs 74 // then we should try to do the delete regardless. 75 Log.i( 76 TAG, 77 "Write permission denied, Not trying to delete the files for series " 78 + mSeriesRecordingId); 79 deleteSelectedIds(false); 80 } 81 break; 82 default: 83 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 84 } 85 } 86 deleteSelectedIds(boolean deleteFiles)87 private void deleteSelectedIds(boolean deleteFiles) { 88 TvSingletons singletons = TvSingletons.getSingletons(this); 89 int recordingSize = 90 singletons.getDvrDataManager().getRecordedPrograms(mSeriesRecordingId).size(); 91 if (!mIdsToDelete.isEmpty()) { 92 DvrManager dvrManager = singletons.getDvrManager(); 93 dvrManager.removeRecordedPrograms(mIdsToDelete, deleteFiles); 94 } 95 Toast.makeText( 96 this, 97 getResources() 98 .getQuantityString( 99 R.plurals.dvr_msg_episodes_deleted, 100 mIdsToDelete.size(), 101 mIdsToDelete.size(), 102 recordingSize), 103 Toast.LENGTH_LONG) 104 .show(); 105 finish(); 106 } 107 setIdsToDelete(List<Long> ids)108 void setIdsToDelete(List<Long> ids) { 109 mIdsToDelete.clear(); 110 mIdsToDelete.addAll(ids); 111 } 112 } 113