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