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.support.v17.leanback.app.GuidedStepFragment; 26 import android.support.v17.leanback.widget.GuidanceStylist.Guidance; 27 import android.support.v17.leanback.widget.GuidedAction; 28 import android.text.format.DateUtils; 29 import com.android.tv.R; 30 import com.android.tv.TvSingletons; 31 import com.android.tv.common.SoftPreconditions; 32 import com.android.tv.data.Program; 33 import com.android.tv.dvr.DvrManager; 34 import com.android.tv.dvr.data.ScheduledRecording; 35 import com.android.tv.dvr.data.SeriesRecording; 36 import com.android.tv.dvr.ui.DvrConflictFragment.DvrProgramConflictFragment; 37 import java.util.Collections; 38 import java.util.List; 39 40 /** 41 * A fragment which asks the user the type of the recording. 42 * 43 * <p>The program should be episodic and the series recording should not had been created yet. 44 */ 45 @TargetApi(Build.VERSION_CODES.N) 46 public class DvrScheduleFragment extends DvrGuidedStepFragment { 47 /** Key for the whether to add the current program to series. Type: boolean */ 48 public static final String KEY_ADD_CURRENT_PROGRAM_TO_SERIES = "add_current_program_to_series"; 49 50 private static final String TAG = "DvrScheduleFragment"; 51 52 private static final int ACTION_RECORD_EPISODE = 1; 53 private static final int ACTION_RECORD_SERIES = 2; 54 55 private Program mProgram; 56 private boolean mAddCurrentProgramToSeries; 57 58 @Override onCreate(Bundle savedInstanceState)59 public void onCreate(Bundle savedInstanceState) { 60 Bundle args = getArguments(); 61 if (args != null) { 62 mProgram = args.getParcelable(DvrHalfSizedDialogFragment.KEY_PROGRAM); 63 mAddCurrentProgramToSeries = args.getBoolean(KEY_ADD_CURRENT_PROGRAM_TO_SERIES, false); 64 } 65 DvrManager dvrManager = TvSingletons.getSingletons(getContext()).getDvrManager(); 66 SoftPreconditions.checkArgument( 67 mProgram != null && mProgram.isEpisodic(), 68 TAG, 69 "The program should be episodic: %s ", 70 mProgram); 71 SeriesRecording seriesRecording = dvrManager.getSeriesRecording(mProgram); 72 SoftPreconditions.checkArgument( 73 seriesRecording == null || seriesRecording.isStopped(), 74 TAG, 75 "The series recording should be stopped or null: %s", 76 seriesRecording); 77 super.onCreate(savedInstanceState); 78 } 79 80 @Override onProvideTheme()81 public int onProvideTheme() { 82 return R.style.Theme_TV_Dvr_GuidedStep_Twoline_Action; 83 } 84 85 @NonNull 86 @Override onCreateGuidance(Bundle savedInstanceState)87 public Guidance onCreateGuidance(Bundle savedInstanceState) { 88 String title = getString(R.string.dvr_schedule_dialog_title); 89 Drawable icon = getResources().getDrawable(R.drawable.ic_dvr, null); 90 return new Guidance(title, null, null, icon); 91 } 92 93 @Override onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)94 public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) { 95 Context context = getContext(); 96 String description; 97 if (mProgram.getStartTimeUtcMillis() <= System.currentTimeMillis()) { 98 description = 99 getString( 100 R.string.dvr_action_record_episode_from_now_description, 101 DateUtils.formatDateTime( 102 context, 103 mProgram.getEndTimeUtcMillis(), 104 DateUtils.FORMAT_SHOW_TIME)); 105 } else { 106 description = mProgram.getDurationString(context); 107 } 108 actions.add( 109 new GuidedAction.Builder(context) 110 .id(ACTION_RECORD_EPISODE) 111 .title(R.string.dvr_action_record_episode) 112 .description(description) 113 .build()); 114 actions.add( 115 new GuidedAction.Builder(context) 116 .id(ACTION_RECORD_SERIES) 117 .title(R.string.dvr_action_record_series) 118 .description(mProgram.getTitle()) 119 .build()); 120 } 121 122 @Override onTrackedGuidedActionClicked(GuidedAction action)123 public void onTrackedGuidedActionClicked(GuidedAction action) { 124 if (action.getId() == ACTION_RECORD_EPISODE) { 125 getDvrManager().addSchedule(mProgram); 126 List<ScheduledRecording> conflicts = getDvrManager().getConflictingSchedules(mProgram); 127 if (conflicts.isEmpty()) { 128 DvrUiHelper.showAddScheduleToast( 129 getContext(), 130 mProgram.getTitle(), 131 mProgram.getStartTimeUtcMillis(), 132 mProgram.getEndTimeUtcMillis()); 133 dismissDialog(); 134 } else { 135 GuidedStepFragment fragment = new DvrProgramConflictFragment(); 136 Bundle args = new Bundle(); 137 args.putParcelable(DvrHalfSizedDialogFragment.KEY_PROGRAM, mProgram); 138 fragment.setArguments(args); 139 GuidedStepFragment.add(getFragmentManager(), fragment, R.id.halfsized_dialog_host); 140 } 141 } else if (action.getId() == ACTION_RECORD_SERIES) { 142 SeriesRecording seriesRecording = 143 TvSingletons.getSingletons(getContext()) 144 .getDvrDataManager() 145 .getSeriesRecording(mProgram.getSeriesId()); 146 if (seriesRecording == null) { 147 seriesRecording = 148 getDvrManager() 149 .addSeriesRecording( 150 mProgram, 151 Collections.emptyList(), 152 SeriesRecording.STATE_SERIES_STOPPED); 153 } else { 154 // Reset priority to the highest. 155 seriesRecording = 156 SeriesRecording.buildFrom(seriesRecording) 157 .setPriority( 158 TvSingletons.getSingletons(getContext()) 159 .getDvrScheduleManager() 160 .suggestNewSeriesPriority()) 161 .build(); 162 getDvrManager().updateSeriesRecording(seriesRecording); 163 } 164 165 DvrUiHelper.startSeriesSettingsActivity( 166 getContext(), 167 seriesRecording.getId(), 168 null, 169 true, 170 true, 171 true, 172 mAddCurrentProgramToSeries ? mProgram : null); 173 dismissDialog(); 174 } 175 } 176 177 @Override getTrackerPrefix()178 public String getTrackerPrefix() { 179 return "DvrSmallSizedStorageErrorFragment"; 180 } 181 182 @Override getTrackerLabelForGuidedAction(GuidedAction action)183 public String getTrackerLabelForGuidedAction(GuidedAction action) { 184 long actionId = action.getId(); 185 if (actionId == ACTION_RECORD_EPISODE) { 186 return "record-episode"; 187 } else if (actionId == ACTION_RECORD_SERIES) { 188 return "record-series"; 189 } else { 190 return super.getTrackerLabelForGuidedAction(action); 191 } 192 } 193 } 194