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