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