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.NonNull;
25 import android.text.format.DateUtils;
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.common.flags.DvrFlags;
33 import com.android.tv.data.api.Program;
34 import com.android.tv.dvr.DvrManager;
35 import com.android.tv.dvr.data.ScheduledRecording;
36 
37 import java.util.List;
38 
39 import javax.inject.Inject;
40 import dagger.android.AndroidInjection;
41 
42 /**
43  * A fragment which notifies the user that the same episode has already been scheduled.
44  *
45  * <p>Note that the schedule has not been created yet.
46  */
47 @TargetApi(Build.VERSION_CODES.N)
48 public class DvrAlreadyScheduledFragment extends DvrGuidedStepFragment {
49     private static final int ACTION_RECORD_ANYWAY = 1;
50     private static final int ACTION_RECORD_INSTEAD = 2;
51     private static final int ACTION_CANCEL = 3;
52 
53     private Program mProgram;
54     private ScheduledRecording mDuplicate;
55     @Inject DvrFlags mDvrFlags;
56 
57     @Override
onAttach(Context context)58     public void onAttach(Context context) {
59         AndroidInjection.inject(this);
60         super.onAttach(context);
61         mProgram = getArguments().getParcelable(DvrHalfSizedDialogFragment.KEY_PROGRAM);
62         DvrManager dvrManager = TvSingletons.getSingletons(context).getDvrManager();
63         mDuplicate =
64                 dvrManager.getScheduledRecording(
65                         mProgram.getTitle(),
66                         mProgram.getSeasonNumber(),
67                         mProgram.getEpisodeNumber());
68         if (mDuplicate == null) {
69             if (mDvrFlags.startEarlyEndLateEnabled()) {
70                 DvrUiHelper.startRecordingSettingsActivity(getContext(), mProgram);
71             } else {
72                 dvrManager.addSchedule(mProgram);
73                 DvrUiHelper.showAddScheduleToast(
74                         context,
75                         mProgram.getTitle(),
76                         mProgram.getStartTimeUtcMillis(),
77                         mProgram.getEndTimeUtcMillis());
78                 dismissDialog();
79             }
80         }
81     }
82 
83     @NonNull
84     @Override
onCreateGuidance(Bundle savedInstanceState)85     public Guidance onCreateGuidance(Bundle savedInstanceState) {
86         String title = getString(R.string.dvr_already_scheduled_dialog_title);
87         String description =
88                 getString(
89                         R.string.dvr_already_scheduled_dialog_description,
90                         DateUtils.formatDateTime(
91                                 getContext(),
92                                 mDuplicate.getStartTimeMs(),
93                                 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE));
94         Drawable image = getResources().getDrawable(R.drawable.quantum_ic_warning_white_96, null);
95         return new Guidance(title, description, null, image);
96     }
97 
98     @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)99     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
100         Context context = getContext();
101         actions.add(
102                 new GuidedAction.Builder(context)
103                         .id(ACTION_RECORD_ANYWAY)
104                         .title(R.string.dvr_action_record_anyway)
105                         .build());
106         actions.add(
107                 new GuidedAction.Builder(context)
108                         .id(ACTION_RECORD_INSTEAD)
109                         .title(R.string.dvr_action_record_instead)
110                         .build());
111         actions.add(
112                 new GuidedAction.Builder(context)
113                         .id(ACTION_CANCEL)
114                         .title(R.string.dvr_action_record_cancel)
115                         .build());
116     }
117 
118     @Override
onTrackedGuidedActionClicked(GuidedAction action)119     public void onTrackedGuidedActionClicked(GuidedAction action) {
120         if (action.getId() == ACTION_RECORD_ANYWAY) {
121             if (mDvrFlags.startEarlyEndLateEnabled()) {
122                 DvrUiHelper.startRecordingSettingsActivity(getContext(), mProgram);
123             } else {
124                 getDvrManager().addSchedule(mProgram);
125             }
126         } else if (action.getId() == ACTION_RECORD_INSTEAD) {
127             getDvrManager().removeScheduledRecording(mDuplicate);
128             if (mDvrFlags.startEarlyEndLateEnabled()) {
129                 DvrUiHelper.startRecordingSettingsActivity(getContext(), mProgram);
130             } else {
131                 getDvrManager().addSchedule(mProgram);
132             }
133         }
134         dismissDialog();
135     }
136 
137     @Override
getTrackerPrefix()138     public String getTrackerPrefix() {
139         return "DvrAlreadyScheduledFragment";
140     }
141 
142     @Override
getTrackerLabelForGuidedAction(GuidedAction action)143     public String getTrackerLabelForGuidedAction(GuidedAction action) {
144         long actionId = action.getId();
145         if (actionId == ACTION_RECORD_ANYWAY) {
146             return "record-anyway";
147         } else if (actionId == ACTION_RECORD_INSTEAD) {
148             return "record-instead";
149         } else if (actionId == ACTION_CANCEL) {
150             return "cancel-recording";
151         } else {
152             return super.getTrackerLabelForGuidedAction(action);
153         }
154     }
155 }
156