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.annotation.TargetApi;
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 import android.os.Build;
23 import android.os.Bundle;
24 import android.support.annotation.IntDef;
25 import android.support.annotation.NonNull;
26 
27 import androidx.leanback.widget.GuidanceStylist.Guidance;
28 import androidx.leanback.widget.GuidedAction;
29 
30 import com.android.tv.R;
31 import com.android.tv.TvSingletons;
32 import com.android.tv.data.ProgramImpl;
33 import com.android.tv.dvr.DvrDataManager;
34 import com.android.tv.dvr.DvrDataManager.ScheduledRecordingListener;
35 import com.android.tv.dvr.data.ScheduledRecording;
36 
37 import java.lang.annotation.Retention;
38 import java.lang.annotation.RetentionPolicy;
39 import java.util.List;
40 
41 /**
42  * A fragment which asks the user to make a recording schedule for the program.
43  *
44  * <p>If the program belongs to a series and the series recording is not created yet, we will show
45  * the option to record all the episodes of the series.
46  */
47 @TargetApi(Build.VERSION_CODES.N)
48 public class DvrStopRecordingFragment extends DvrGuidedStepFragment {
49     /** The action ID for the stop action. */
50     public static final int ACTION_STOP = 1;
51     /** Key for the program. Type: {@link ProgramImpl}. */
52     public static final String KEY_REASON = "DvrStopRecordingFragment.type";
53 
54     @Retention(RetentionPolicy.SOURCE)
55     @IntDef({REASON_USER_STOP, REASON_ON_CONFLICT})
56     public @interface ReasonType {}
57     /** The dialog is shown because users want to stop some currently recording program. */
58     public static final int REASON_USER_STOP = 1;
59     /**
60      * The dialog is shown because users want to record some program that is conflict to the current
61      * recording program.
62      */
63     public static final int REASON_ON_CONFLICT = 2;
64 
65     private ScheduledRecording mSchedule;
66     private DvrDataManager mDvrDataManager;
67     private @ReasonType int mStopReason;
68 
69     private final ScheduledRecordingListener mScheduledRecordingListener =
70             new ScheduledRecordingListener() {
71                 @Override
72                 public void onScheduledRecordingAdded(ScheduledRecording... schedules) {}
73 
74                 @Override
75                 public void onScheduledRecordingRemoved(ScheduledRecording... schedules) {
76                     for (ScheduledRecording schedule : schedules) {
77                         if (schedule.getId() == mSchedule.getId()) {
78                             dismissDialog();
79                             return;
80                         }
81                     }
82                 }
83 
84                 @Override
85                 public void onScheduledRecordingStatusChanged(ScheduledRecording... schedules) {
86                     for (ScheduledRecording schedule : schedules) {
87                         if (schedule.getId() == mSchedule.getId()
88                                 && schedule.getState()
89                                         != ScheduledRecording.STATE_RECORDING_IN_PROGRESS) {
90                             dismissDialog();
91                             return;
92                         }
93                     }
94                 }
95             };
96 
97     @Override
onAttach(Context context)98     public void onAttach(Context context) {
99         super.onAttach(context);
100         Bundle args = getArguments();
101         long channelId = args.getLong(DvrHalfSizedDialogFragment.KEY_CHANNEL_ID);
102         mSchedule = getDvrManager().getCurrentRecording(channelId);
103         if (mSchedule == null) {
104             dismissDialog();
105             return;
106         }
107         mDvrDataManager = TvSingletons.getSingletons(context).getDvrDataManager();
108         mDvrDataManager.addScheduledRecordingListener(mScheduledRecordingListener);
109         mStopReason = args.getInt(KEY_REASON);
110     }
111 
112     @Override
onDetach()113     public void onDetach() {
114         if (mDvrDataManager != null) {
115             mDvrDataManager.removeScheduledRecordingListener(mScheduledRecordingListener);
116         }
117         super.onDetach();
118     }
119 
120     @NonNull
121     @Override
onCreateGuidance(Bundle savedInstanceState)122     public Guidance onCreateGuidance(Bundle savedInstanceState) {
123         String title = getString(R.string.dvr_stop_recording_dialog_title);
124         String description;
125         if (mStopReason == REASON_ON_CONFLICT) {
126             description =
127                     getString(
128                             R.string.dvr_stop_recording_dialog_description_on_conflict,
129                             mSchedule.getProgramDisplayTitle(getContext()));
130         } else {
131             description = getString(R.string.dvr_stop_recording_dialog_description);
132         }
133         Drawable image = getResources().getDrawable(R.drawable.quantum_ic_warning_white_96, null);
134         return new Guidance(title, description, null, image);
135     }
136 
137     @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)138     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
139         Context context = getContext();
140         actions.add(
141                 new GuidedAction.Builder(context)
142                         .id(ACTION_STOP)
143                         .title(R.string.dvr_action_stop)
144                         .build());
145         actions.add(
146                 new GuidedAction.Builder(context)
147                         .clickAction(GuidedAction.ACTION_ID_CANCEL)
148                         .build());
149     }
150 
151     @Override
getTrackerPrefix()152     public String getTrackerPrefix() {
153         return "DvrStopRecordingFragment";
154     }
155 
156     @Override
getTrackerLabelForGuidedAction(GuidedAction action)157     public String getTrackerLabelForGuidedAction(GuidedAction action) {
158         long actionId = action.getId();
159         if (actionId == ACTION_STOP) {
160             return "stop";
161         } else {
162             return super.getTrackerLabelForGuidedAction(action);
163         }
164     }
165 }
166