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.content.Context; 20 import android.content.Intent; 21 import android.graphics.drawable.Drawable; 22 import android.os.Bundle; 23 import android.support.v17.leanback.widget.GuidanceStylist; 24 import android.support.v17.leanback.widget.GuidedAction; 25 import com.android.tv.R; 26 import com.android.tv.TvSingletons; 27 import com.android.tv.data.Program; 28 import com.android.tv.dvr.DvrScheduleManager; 29 import com.android.tv.dvr.data.ScheduledRecording; 30 import com.android.tv.dvr.data.SeriesRecording; 31 import com.android.tv.dvr.ui.list.DvrSchedulesActivity; 32 import com.android.tv.dvr.ui.list.DvrSeriesSchedulesFragment; 33 import java.util.List; 34 35 public class DvrSeriesScheduledFragment extends DvrGuidedStepFragment { 36 /** 37 * The key for program list which will be passed to {@link DvrSeriesSchedulesFragment}. Type: 38 * List<{@link Program}> 39 */ 40 public static final String SERIES_SCHEDULED_KEY_PROGRAMS = "series_scheduled_key_programs"; 41 42 private static final long SERIES_RECORDING_ID_NOT_SET = -1; 43 44 private static final int ACTION_VIEW_SCHEDULES = 1; 45 46 private SeriesRecording mSeriesRecording; 47 private boolean mShowViewScheduleOption; 48 private List<Program> mPrograms; 49 50 private int mSchedulesAddedCount = 0; 51 private boolean mHasConflict = false; 52 private int mInThisSeriesConflictCount = 0; 53 private int mOutThisSeriesConflictCount = 0; 54 55 @Override 56 public void onAttach(Context context) { 57 super.onAttach(context); 58 long seriesRecordingId = 59 getArguments() 60 .getLong( 61 DvrSeriesScheduledDialogActivity.SERIES_RECORDING_ID, 62 SERIES_RECORDING_ID_NOT_SET); 63 if (seriesRecordingId == SERIES_RECORDING_ID_NOT_SET) { 64 getActivity().finish(); 65 return; 66 } 67 mShowViewScheduleOption = 68 getArguments() 69 .getBoolean(DvrSeriesScheduledDialogActivity.SHOW_VIEW_SCHEDULE_OPTION); 70 mSeriesRecording = 71 TvSingletons.getSingletons(context) 72 .getDvrDataManager() 73 .getSeriesRecording(seriesRecordingId); 74 if (mSeriesRecording == null) { 75 getActivity().finish(); 76 return; 77 } 78 mPrograms = (List<Program>) BigArguments.getArgument(SERIES_SCHEDULED_KEY_PROGRAMS); 79 BigArguments.reset(); 80 mSchedulesAddedCount = 81 TvSingletons.getSingletons(getContext()) 82 .getDvrManager() 83 .getAvailableScheduledRecording(mSeriesRecording.getId()) 84 .size(); 85 DvrScheduleManager dvrScheduleManager = 86 TvSingletons.getSingletons(context).getDvrScheduleManager(); 87 List<ScheduledRecording> conflictingRecordings = 88 dvrScheduleManager.getConflictingSchedules(mSeriesRecording); 89 mHasConflict = !conflictingRecordings.isEmpty(); 90 for (ScheduledRecording recording : conflictingRecordings) { 91 if (recording.getSeriesRecordingId() == mSeriesRecording.getId()) { 92 ++mInThisSeriesConflictCount; 93 } else if (recording.getPriority() < mSeriesRecording.getPriority()) { 94 ++mOutThisSeriesConflictCount; 95 } 96 } 97 } 98 99 @Override 100 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 101 String title = getString(R.string.dvr_series_recording_dialog_title); 102 Drawable icon; 103 if (!mHasConflict) { 104 icon = getResources().getDrawable(R.drawable.ic_check_circle_white_48dp, null); 105 } else { 106 icon = getResources().getDrawable(R.drawable.ic_error_white_48dp, null); 107 } 108 return new GuidanceStylist.Guidance(title, getDescription(), null, icon); 109 } 110 111 @Override 112 public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) { 113 Context context = getContext(); 114 actions.add( 115 new GuidedAction.Builder(context).clickAction(GuidedAction.ACTION_ID_OK).build()); 116 if (mShowViewScheduleOption) { 117 actions.add( 118 new GuidedAction.Builder(context) 119 .id(ACTION_VIEW_SCHEDULES) 120 .title(R.string.dvr_action_view_schedules) 121 .build()); 122 } 123 } 124 125 @Override 126 public void onTrackedGuidedActionClicked(GuidedAction action) { 127 if (action.getId() == ACTION_VIEW_SCHEDULES) { 128 Intent intent = new Intent(getActivity(), DvrSchedulesActivity.class); 129 intent.putExtra( 130 DvrSchedulesActivity.KEY_SCHEDULES_TYPE, 131 DvrSchedulesActivity.TYPE_SERIES_SCHEDULE); 132 intent.putExtra( 133 DvrSeriesSchedulesFragment.SERIES_SCHEDULES_KEY_SERIES_RECORDING, 134 mSeriesRecording); 135 BigArguments.reset(); 136 BigArguments.setArgument( 137 DvrSeriesSchedulesFragment.SERIES_SCHEDULES_KEY_SERIES_PROGRAMS, mPrograms); 138 startActivity(intent); 139 } 140 getActivity().finish(); 141 } 142 143 @Override 144 public String getTrackerPrefix() { 145 return "DvrMissingStorageErrorFragment"; 146 } 147 148 @Override 149 public String getTrackerLabelForGuidedAction(GuidedAction action) { 150 long actionId = action.getId(); 151 if (actionId == ACTION_VIEW_SCHEDULES) { 152 return "view-schedules"; 153 } else { 154 return super.getTrackerLabelForGuidedAction(action); 155 } 156 } 157 158 private String getDescription() { 159 if (!mHasConflict) { 160 return getResources() 161 .getQuantityString( 162 R.plurals.dvr_series_scheduled_no_conflict, 163 mSchedulesAddedCount, 164 mSchedulesAddedCount, 165 mSeriesRecording.getTitle()); 166 } else { 167 // mInThisSeriesConflictCount equals 0 and mOutThisSeriesConflictCount equals 0 means 168 // mHasConflict is false. So we don't need to check that case. 169 if (mInThisSeriesConflictCount != 0 && mOutThisSeriesConflictCount != 0) { 170 return getResources() 171 .getQuantityString( 172 R.plurals.dvr_series_scheduled_this_and_other_series_conflict, 173 mSchedulesAddedCount, 174 mSchedulesAddedCount, 175 mSeriesRecording.getTitle(), 176 mInThisSeriesConflictCount + mOutThisSeriesConflictCount); 177 } else if (mInThisSeriesConflictCount != 0) { 178 return getResources() 179 .getQuantityString( 180 R.plurals.dvr_series_recording_scheduled_only_this_series_conflict, 181 mSchedulesAddedCount, 182 mSchedulesAddedCount, 183 mSeriesRecording.getTitle(), 184 mInThisSeriesConflictCount); 185 } else { 186 if (mOutThisSeriesConflictCount == 1) { 187 return getResources() 188 .getQuantityString( 189 R.plurals.dvr_series_scheduled_only_other_series_one_conflict, 190 mSchedulesAddedCount, 191 mSchedulesAddedCount, 192 mSeriesRecording.getTitle()); 193 } else { 194 return getResources() 195 .getQuantityString( 196 R.plurals.dvr_series_scheduled_only_other_series_many_conflicts, 197 mSchedulesAddedCount, 198 mSchedulesAddedCount, 199 mSeriesRecording.getTitle(), 200 mOutThisSeriesConflictCount); 201 } 202 } 203 } 204 } 205 } 206