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.graphics.drawable.Drawable;
20 import android.os.Bundle;
21 import androidx.leanback.app.GuidedStepFragment;
22 import androidx.leanback.widget.GuidanceStylist.Guidance;
23 import androidx.leanback.widget.GuidedAction;
24 import com.android.tv.R;
25 import com.android.tv.TvSingletons;
26 import com.android.tv.common.SoftPreconditions;
27 import com.android.tv.data.api.Channel;
28 import com.android.tv.dvr.DvrManager;
29 import com.android.tv.dvr.data.ScheduledRecording;
30 import com.android.tv.dvr.ui.DvrConflictFragment.DvrChannelRecordConflictFragment;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34 
35 public class DvrChannelRecordDurationOptionFragment extends DvrGuidedStepFragment {
36     private final List<Long> mDurations = new ArrayList<>();
37     private Channel mChannel;
38 
39     @Override
onCreate(Bundle savedInstanceState)40     public void onCreate(Bundle savedInstanceState) {
41         Bundle args = getArguments();
42         if (args != null) {
43             long channelId = args.getLong(DvrHalfSizedDialogFragment.KEY_CHANNEL_ID);
44             mChannel =
45                     TvSingletons.getSingletons(getContext())
46                             .getChannelDataManager()
47                             .getChannel(channelId);
48         }
49         SoftPreconditions.checkArgument(mChannel != null);
50         super.onCreate(savedInstanceState);
51     }
52 
53     @Override
onCreateGuidance(Bundle savedInstanceState)54     public Guidance onCreateGuidance(Bundle savedInstanceState) {
55         String title = getResources().getString(R.string.dvr_channel_record_duration_dialog_title);
56         Drawable icon = getResources().getDrawable(R.drawable.ic_dvr, null);
57         return new Guidance(title, null, null, icon);
58     }
59 
60     @Override
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)61     public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
62         int actionId = -1;
63         mDurations.clear();
64         mDurations.add(TimeUnit.MINUTES.toMillis(10));
65         mDurations.add(TimeUnit.MINUTES.toMillis(30));
66         mDurations.add(TimeUnit.HOURS.toMillis(1));
67         mDurations.add(TimeUnit.HOURS.toMillis(3));
68 
69         actions.add(
70                 new GuidedAction.Builder(getContext())
71                         .id(++actionId)
72                         .title(R.string.recording_start_dialog_10_min_duration)
73                         .build());
74         actions.add(
75                 new GuidedAction.Builder(getContext())
76                         .id(++actionId)
77                         .title(R.string.recording_start_dialog_30_min_duration)
78                         .build());
79         actions.add(
80                 new GuidedAction.Builder(getContext())
81                         .id(++actionId)
82                         .title(R.string.recording_start_dialog_1_hour_duration)
83                         .build());
84         actions.add(
85                 new GuidedAction.Builder(getContext())
86                         .id(++actionId)
87                         .title(R.string.recording_start_dialog_3_hours_duration)
88                         .build());
89     }
90 
91     @Override
onTrackedGuidedActionClicked(GuidedAction action)92     public void onTrackedGuidedActionClicked(GuidedAction action) {
93         DvrManager dvrManager = TvSingletons.getSingletons(getContext()).getDvrManager();
94         long duration = mDurations.get((int) action.getId());
95         long startTimeMs = System.currentTimeMillis();
96         long endTimeMs = System.currentTimeMillis() + duration;
97         List<ScheduledRecording> conflicts =
98                 dvrManager.getConflictingSchedules(mChannel.getId(), startTimeMs, endTimeMs);
99         dvrManager.addSchedule(mChannel, startTimeMs, endTimeMs);
100         if (conflicts.isEmpty()) {
101             dismissDialog();
102         } else {
103             GuidedStepFragment fragment = new DvrChannelRecordConflictFragment();
104             Bundle args = new Bundle();
105             args.putLong(DvrHalfSizedDialogFragment.KEY_CHANNEL_ID, mChannel.getId());
106             args.putLong(DvrHalfSizedDialogFragment.KEY_START_TIME_MS, startTimeMs);
107             args.putLong(DvrHalfSizedDialogFragment.KEY_END_TIME_MS, endTimeMs);
108             fragment.setArguments(args);
109             GuidedStepFragment.add(getFragmentManager(), fragment, R.id.halfsized_dialog_host);
110         }
111     }
112 
113     @Override
getTrackerPrefix()114     public String getTrackerPrefix() {
115         return "DvrChannelRecordDurationOptionFragment";
116     }
117 
118     @Override
getTrackerLabelForGuidedAction(GuidedAction action)119     public String getTrackerLabelForGuidedAction(GuidedAction action) {
120         long actionId = action.getId();
121         if (actionId == 0) {
122             return "record-10-minutes";
123         } else if (actionId == 1) {
124             return "record-30-minutes";
125         } else if (actionId == 2) {
126             return "record-1-hour";
127         } else if (actionId == 3) {
128             return "record-3-hour";
129         } else {
130             return super.getTrackerLabelForGuidedAction(action);
131         }
132     }
133 }
134