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.graphics.drawable.Drawable;
21 import android.os.Bundle;
22 import android.support.annotation.NonNull;
23 import androidx.leanback.widget.GuidanceStylist;
24 import androidx.leanback.widget.GuidedAction;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import com.android.tv.R;
29 import com.android.tv.TvSingletons;
30 import com.android.tv.dvr.DvrDataManager;
31 import com.android.tv.dvr.DvrManager;
32 import com.android.tv.dvr.data.ScheduledRecording;
33 import com.android.tv.dvr.data.SeriesRecording;
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 /** A fragment which asks the user to stop series recording. */
38 public class DvrStopSeriesRecordingFragment extends DvrGuidedStepFragment {
39     /** Key for the series recording to be stopped. */
40     public static final String KEY_SERIES_RECORDING = "key_series_recoridng";
41 
42     private static final int ACTION_STOP_SERIES_RECORDING = 1;
43 
44     private SeriesRecording mSeriesRecording;
45 
46     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)47     public View onCreateView(
48             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
49         mSeriesRecording = getArguments().getParcelable(KEY_SERIES_RECORDING);
50         return super.onCreateView(inflater, container, savedInstanceState);
51     }
52 
53     @NonNull
54     @Override
onCreateGuidance(Bundle savedInstanceState)55     public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
56         String title = getString(R.string.dvr_series_schedules_stop_dialog_title);
57         String description = getString(R.string.dvr_series_schedules_stop_dialog_description);
58         Drawable icon = getContext().getDrawable(R.drawable.ic_dvr_delete);
59         return new GuidanceStylist.Guidance(title, description, null, icon);
60     }
61 
62     @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)63     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
64         Activity activity = getActivity();
65         actions.add(
66                 new GuidedAction.Builder(activity)
67                         .id(ACTION_STOP_SERIES_RECORDING)
68                         .title(R.string.dvr_series_schedules_stop_dialog_action_stop)
69                         .build());
70         actions.add(
71                 new GuidedAction.Builder(activity)
72                         .clickAction(GuidedAction.ACTION_ID_CANCEL)
73                         .build());
74     }
75 
76     @Override
onTrackedGuidedActionClicked(GuidedAction action)77     public void onTrackedGuidedActionClicked(GuidedAction action) {
78         if (action.getId() == ACTION_STOP_SERIES_RECORDING) {
79             TvSingletons singletons = TvSingletons.getSingletons(getContext());
80             DvrManager dvrManager = singletons.getDvrManager();
81             DvrDataManager dataManager = singletons.getDvrDataManager();
82             List<ScheduledRecording> toDelete = new ArrayList<>();
83             for (ScheduledRecording r : dataManager.getAvailableScheduledRecordings()) {
84                 if (r.getSeriesRecordingId() == mSeriesRecording.getId()) {
85                     if (r.getState() == ScheduledRecording.STATE_RECORDING_NOT_STARTED) {
86                         toDelete.add(r);
87                     } else {
88                         dvrManager.stopRecording(r);
89                     }
90                 }
91             }
92             if (!toDelete.isEmpty()) {
93                 dvrManager.forceRemoveScheduledRecording(ScheduledRecording.toArray(toDelete));
94             }
95             dvrManager.updateSeriesRecording(
96                     SeriesRecording.buildFrom(mSeriesRecording)
97                             .setState(SeriesRecording.STATE_SERIES_STOPPED)
98                             .build());
99         }
100         dismissDialog();
101     }
102 
103     @Override
getTrackerPrefix()104     public String getTrackerPrefix() {
105         return "DvrStopSeriesRecordingFragment";
106     }
107 
108     @Override
getTrackerLabelForGuidedAction(GuidedAction action)109     public String getTrackerLabelForGuidedAction(GuidedAction action) {
110         if (action.getId() == ACTION_STOP_SERIES_RECORDING) {
111             return "stop";
112         } else {
113             return super.getTrackerLabelForGuidedAction(action);
114         }
115     }
116 }
117