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