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.app.Activity;
     20 import android.app.DialogFragment;
     21 import android.content.Context;
     22 import android.os.Bundle;
     23 import android.support.v17.leanback.widget.GuidanceStylist;
     24 import android.support.v17.leanback.widget.GuidedAction;
     25 import android.support.v17.leanback.widget.VerticalGridView;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import com.android.tv.MainActivity;
     30 import com.android.tv.R;
     31 import com.android.tv.TvSingletons;
     32 import com.android.tv.common.recording.RecordingStorageStatusManager;
     33 import com.android.tv.dialog.HalfSizedDialogFragment.OnActionClickListener;
     34 import com.android.tv.dialog.SafeDismissDialogFragment;
     35 import com.android.tv.dvr.DvrManager;
     36 import java.util.List;
     37 
     38 public abstract class DvrGuidedStepFragment extends TrackedGuidedStepFragment {
     39     /** Action ID for "recording/scheduling the program anyway". */
     40     public static final int ACTION_RECORD_ANYWAY = 1;
     41     /** Action ID for "deleting existed recordings". */
     42     public static final int ACTION_DELETE_RECORDINGS = 2;
     43     /** Action ID for "cancelling current recording request". */
     44     public static final int ACTION_CANCEL_RECORDING = 3;
     45 
     46     public static final String UNKNOWN_DVR_ACTION = "Unknown DVR Action";
     47 
     48     private DvrManager mDvrManager;
     49     private OnActionClickListener mOnActionClickListener;
     50 
     51     protected DvrManager getDvrManager() {
     52         return mDvrManager;
     53     }
     54 
     55     @Override
     56     public void onAttach(Context context) {
     57         super.onAttach(context);
     58         TvSingletons singletons = TvSingletons.getSingletons(context);
     59         mDvrManager = singletons.getDvrManager();
     60     }
     61 
     62     @Override
     63     public View onCreateView(
     64             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     65         View view = super.onCreateView(inflater, container, savedInstanceState);
     66         VerticalGridView actionsList = getGuidedActionsStylist().getActionsGridView();
     67         actionsList.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_BOTH_EDGE);
     68         VerticalGridView buttonActionsList = getGuidedButtonActionsStylist().getActionsGridView();
     69         buttonActionsList.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_BOTH_EDGE);
     70         return view;
     71     }
     72 
     73     @Override
     74     public int onProvideTheme() {
     75         return R.style.Theme_TV_Dvr_GuidedStep;
     76     }
     77 
     78     @Override
     79     public void onTrackedGuidedActionClicked(GuidedAction action) {
     80         if (mOnActionClickListener != null) {
     81             mOnActionClickListener.onActionClick(action.getId());
     82         }
     83         dismissDialog();
     84     }
     85 
     86     @Override
     87     public String getTrackerLabelForGuidedAction(GuidedAction action) {
     88         long actionId = action.getId();
     89         if (actionId == ACTION_RECORD_ANYWAY) {
     90             return "record-anyway";
     91         } else if (actionId == ACTION_DELETE_RECORDINGS) {
     92             return "delete-recordings";
     93         } else if (actionId == ACTION_CANCEL_RECORDING) {
     94             return "cancel-recording";
     95         } else {
     96             return super.getTrackerLabelForGuidedAction(action);
     97         }
     98     }
     99 
    100     protected void dismissDialog() {
    101         if (getActivity() instanceof MainActivity) {
    102             SafeDismissDialogFragment currentDialog =
    103                     ((MainActivity) getActivity()).getOverlayManager().getCurrentDialog();
    104             if (currentDialog instanceof DvrHalfSizedDialogFragment) {
    105                 currentDialog.dismiss();
    106             }
    107         } else if (getParentFragment() instanceof DialogFragment) {
    108             ((DialogFragment) getParentFragment()).dismiss();
    109         }
    110     }
    111 
    112     protected void setOnActionClickListener(OnActionClickListener listener) {
    113         mOnActionClickListener = listener;
    114     }
    115 
    116     /**
    117      * The inner guided step fragment for {@link com.android.tv.dvr.ui.DvrHalfSizedDialogFragment
    118      * .DvrNoFreeSpaceErrorDialogFragment}.
    119      */
    120     public static class DvrNoFreeSpaceErrorFragment extends DvrGuidedStepFragment {
    121         @Override
    122         public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
    123             return new GuidanceStylist.Guidance(
    124                     getString(R.string.dvr_error_no_free_space_title),
    125                     getString(R.string.dvr_error_no_free_space_description),
    126                     null,
    127                     null);
    128         }
    129 
    130         @Override
    131         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
    132             Activity activity = getActivity();
    133             actions.add(
    134                     new GuidedAction.Builder(activity)
    135                             .id(ACTION_RECORD_ANYWAY)
    136                             .title(R.string.dvr_action_record_anyway)
    137                             .build());
    138             actions.add(
    139                     new GuidedAction.Builder(activity)
    140                             .id(ACTION_DELETE_RECORDINGS)
    141                             .title(R.string.dvr_action_delete_recordings)
    142                             .build());
    143             actions.add(
    144                     new GuidedAction.Builder(activity)
    145                             .id(ACTION_CANCEL_RECORDING)
    146                             .title(R.string.dvr_action_record_cancel)
    147                             .build());
    148         }
    149 
    150         @Override
    151         public String getTrackerPrefix() {
    152             return "DvrNoFreeSpaceErrorFragment";
    153         }
    154     }
    155 
    156     /**
    157      * The inner guided step fragment for {@link com.android.tv.dvr.ui.DvrHalfSizedDialogFragment
    158      * .DvrSmallSizedStorageErrorDialogFragment}.
    159      */
    160     public static class DvrSmallSizedStorageErrorFragment extends DvrGuidedStepFragment {
    161         @Override
    162         public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
    163             String title = getResources().getString(R.string.dvr_error_small_sized_storage_title);
    164             String description =
    165                     getResources()
    166                             .getString(
    167                                     R.string.dvr_error_small_sized_storage_description,
    168                                     RecordingStorageStatusManager.MIN_STORAGE_SIZE_FOR_DVR_IN_BYTES
    169                                             / 1024
    170                                             / 1024
    171                                             / 1024);
    172             return new GuidanceStylist.Guidance(title, description, null, null);
    173         }
    174 
    175         @Override
    176         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
    177             Activity activity = getActivity();
    178             actions.add(
    179                     new GuidedAction.Builder(activity)
    180                             .id(GuidedAction.ACTION_ID_OK)
    181                             .title(android.R.string.ok)
    182                             .build());
    183         }
    184 
    185         @Override
    186         public void onTrackedGuidedActionClicked(GuidedAction action) {
    187             dismissDialog();
    188         }
    189 
    190         @Override
    191         public String getTrackerPrefix() {
    192             return "DvrSmallSizedStorageErrorFragment";
    193         }
    194     }
    195 }
    196