Home | History | Annotate | Download | only in ui
      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.IntDef;
     25 import android.support.annotation.NonNull;
     26 import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
     27 import android.support.v17.leanback.widget.GuidedAction;
     28 import com.android.tv.R;
     29 import com.android.tv.TvSingletons;
     30 import com.android.tv.dvr.DvrDataManager;
     31 import com.android.tv.dvr.DvrDataManager.ScheduledRecordingListener;
     32 import com.android.tv.dvr.data.ScheduledRecording;
     33 import java.lang.annotation.Retention;
     34 import java.lang.annotation.RetentionPolicy;
     35 import java.util.List;
     36 
     37 /**
     38  * A fragment which asks the user to make a recording schedule for the program.
     39  *
     40  * <p>If the program belongs to a series and the series recording is not created yet, we will show
     41  * the option to record all the episodes of the series.
     42  */
     43 @TargetApi(Build.VERSION_CODES.N)
     44 public class DvrStopRecordingFragment extends DvrGuidedStepFragment {
     45     /** The action ID for the stop action. */
     46     public static final int ACTION_STOP = 1;
     47     /** Key for the program. Type: {@link com.android.tv.data.Program}. */
     48     public static final String KEY_REASON = "DvrStopRecordingFragment.type";
     49 
     50     @Retention(RetentionPolicy.SOURCE)
     51     @IntDef({REASON_USER_STOP, REASON_ON_CONFLICT})
     52     public @interface ReasonType {}
     53     /** The dialog is shown because users want to stop some currently recording program. */
     54     public static final int REASON_USER_STOP = 1;
     55     /**
     56      * The dialog is shown because users want to record some program that is conflict to the current
     57      * recording program.
     58      */
     59     public static final int REASON_ON_CONFLICT = 2;
     60 
     61     private ScheduledRecording mSchedule;
     62     private DvrDataManager mDvrDataManager;
     63     private @ReasonType int mStopReason;
     64 
     65     private final ScheduledRecordingListener mScheduledRecordingListener =
     66             new ScheduledRecordingListener() {
     67                 @Override
     68                 public void onScheduledRecordingAdded(ScheduledRecording... schedules) {}
     69 
     70                 @Override
     71                 public void onScheduledRecordingRemoved(ScheduledRecording... schedules) {
     72                     for (ScheduledRecording schedule : schedules) {
     73                         if (schedule.getId() == mSchedule.getId()) {
     74                             dismissDialog();
     75                             return;
     76                         }
     77                     }
     78                 }
     79 
     80                 @Override
     81                 public void onScheduledRecordingStatusChanged(ScheduledRecording... schedules) {
     82                     for (ScheduledRecording schedule : schedules) {
     83                         if (schedule.getId() == mSchedule.getId()
     84                                 && schedule.getState()
     85                                         != ScheduledRecording.STATE_RECORDING_IN_PROGRESS) {
     86                             dismissDialog();
     87                             return;
     88                         }
     89                     }
     90                 }
     91             };
     92 
     93     @Override
     94     public void onAttach(Context context) {
     95         super.onAttach(context);
     96         Bundle args = getArguments();
     97         long channelId = args.getLong(DvrHalfSizedDialogFragment.KEY_CHANNEL_ID);
     98         mSchedule = getDvrManager().getCurrentRecording(channelId);
     99         if (mSchedule == null) {
    100             dismissDialog();
    101             return;
    102         }
    103         mDvrDataManager = TvSingletons.getSingletons(context).getDvrDataManager();
    104         mDvrDataManager.addScheduledRecordingListener(mScheduledRecordingListener);
    105         mStopReason = args.getInt(KEY_REASON);
    106     }
    107 
    108     @Override
    109     public void onDetach() {
    110         if (mDvrDataManager != null) {
    111             mDvrDataManager.removeScheduledRecordingListener(mScheduledRecordingListener);
    112         }
    113         super.onDetach();
    114     }
    115 
    116     @NonNull
    117     @Override
    118     public Guidance onCreateGuidance(Bundle savedInstanceState) {
    119         String title = getString(R.string.dvr_stop_recording_dialog_title);
    120         String description;
    121         if (mStopReason == REASON_ON_CONFLICT) {
    122             description =
    123                     getString(
    124                             R.string.dvr_stop_recording_dialog_description_on_conflict,
    125                             mSchedule.getProgramDisplayTitle(getContext()));
    126         } else {
    127             description = getString(R.string.dvr_stop_recording_dialog_description);
    128         }
    129         Drawable image = getResources().getDrawable(R.drawable.ic_warning_white_96dp, null);
    130         return new Guidance(title, description, null, image);
    131     }
    132 
    133     @Override
    134     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
    135         Context context = getContext();
    136         actions.add(
    137                 new GuidedAction.Builder(context)
    138                         .id(ACTION_STOP)
    139                         .title(R.string.dvr_action_stop)
    140                         .build());
    141         actions.add(
    142                 new GuidedAction.Builder(context)
    143                         .clickAction(GuidedAction.ACTION_ID_CANCEL)
    144                         .build());
    145     }
    146 
    147     @Override
    148     public String getTrackerPrefix() {
    149         return "DvrStopRecordingFragment";
    150     }
    151 
    152     @Override
    153     public String getTrackerLabelForGuidedAction(GuidedAction action) {
    154         long actionId = action.getId();
    155         if (actionId == ACTION_STOP) {
    156             return "stop";
    157         } else {
    158             return super.getTrackerLabelForGuidedAction(action);
    159         }
    160     }
    161 }
    162