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.widget.GuidanceStylist.Guidance; 26 import android.support.v17.leanback.widget.GuidedAction; 27 import android.text.format.DateUtils; 28 import android.widget.Toast; 29 30 import com.android.tv.R; 31 import com.android.tv.TvApplication; 32 import com.android.tv.data.Program; 33 import com.android.tv.dvr.DvrManager; 34 import com.android.tv.dvr.DvrUiHelper; 35 import com.android.tv.dvr.ScheduledRecording; 36 import com.android.tv.util.Utils; 37 38 import java.util.List; 39 40 /** 41 * A fragment which notifies the user that the same episode has already been scheduled. 42 * 43 * <p>Note that the schedule has not been created yet. 44 */ 45 @TargetApi(Build.VERSION_CODES.N) 46 public class DvrAlreadyScheduledFragment extends DvrGuidedStepFragment { 47 private static final int ACTION_RECORD_ANYWAY = 1; 48 private static final int ACTION_RECORD_INSTEAD = 2; 49 private static final int ACTION_CANCEL = 3; 50 51 private Program mProgram; 52 private ScheduledRecording mDuplicate; 53 54 @Override 55 public void onAttach(Context context) { 56 super.onAttach(context); 57 mProgram = getArguments().getParcelable(DvrHalfSizedDialogFragment.KEY_PROGRAM); 58 DvrManager dvrManager = TvApplication.getSingletons(context).getDvrManager(); 59 mDuplicate = dvrManager.getScheduledRecording(mProgram.getTitle(), 60 mProgram.getSeasonNumber(), mProgram.getEpisodeNumber()); 61 if (mDuplicate == null) { 62 dvrManager.addSchedule(mProgram); 63 DvrUiHelper.showAddScheduleToast(context, mProgram.getTitle(), 64 mProgram.getStartTimeUtcMillis(), mProgram.getEndTimeUtcMillis()); 65 dismissDialog(); 66 } 67 } 68 69 @NonNull 70 @Override 71 public Guidance onCreateGuidance(Bundle savedInstanceState) { 72 String title = getString(R.string.dvr_already_scheduled_dialog_title); 73 String description = getString(R.string.dvr_already_scheduled_dialog_description, 74 DateUtils.formatDateTime(getContext(), mDuplicate.getStartTimeMs(), 75 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE)); 76 Drawable image = getResources().getDrawable(R.drawable.ic_warning_white_96dp, null); 77 return new Guidance(title, description, null, image); 78 } 79 80 @Override 81 public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) { 82 Context context = getContext(); 83 actions.add(new GuidedAction.Builder(context) 84 .id(ACTION_RECORD_ANYWAY) 85 .title(R.string.dvr_action_record_anyway) 86 .build()); 87 actions.add(new GuidedAction.Builder(context) 88 .id(ACTION_RECORD_INSTEAD) 89 .title(R.string.dvr_action_record_instead) 90 .build()); 91 actions.add(new GuidedAction.Builder(context) 92 .id(ACTION_CANCEL) 93 .title(R.string.dvr_action_record_cancel) 94 .build()); 95 } 96 97 @Override 98 public void onGuidedActionClicked(GuidedAction action) { 99 if (action.getId() == ACTION_RECORD_ANYWAY) { 100 getDvrManager().addSchedule(mProgram); 101 } else if (action.getId() == ACTION_RECORD_INSTEAD) { 102 getDvrManager().addSchedule(mProgram); 103 getDvrManager().removeScheduledRecording(mDuplicate); 104 } 105 dismissDialog(); 106 } 107 } 108