Home | History | Annotate | Download | only in dialog
      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.dialog;
     18 
     19 import android.app.Dialog;
     20 import android.content.DialogInterface;
     21 import android.os.Bundle;
     22 import android.os.Handler;
     23 import android.view.KeyEvent;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import com.android.tv.R;
     28 import java.util.concurrent.TimeUnit;
     29 
     30 public class HalfSizedDialogFragment extends SafeDismissDialogFragment {
     31     public static final String DIALOG_TAG = HalfSizedDialogFragment.class.getSimpleName();
     32     public static final String TRACKER_LABEL = "Half sized dialog";
     33 
     34     private static final long AUTO_DISMISS_TIME_THRESHOLD_MS = TimeUnit.SECONDS.toMillis(30);
     35 
     36     private OnActionClickListener mOnActionClickListener;
     37 
     38     private Handler mHandler = new Handler();
     39     private Runnable mAutoDismisser =
     40             new Runnable() {
     41                 @Override
     42                 public void run() {
     43                     dismiss();
     44                 }
     45             };
     46 
     47     @Override
     48     public View onCreateView(
     49             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     50         return inflater.inflate(R.layout.halfsized_dialog, container, false);
     51     }
     52 
     53     @Override
     54     public void onStart() {
     55         super.onStart();
     56         mHandler.postDelayed(mAutoDismisser, AUTO_DISMISS_TIME_THRESHOLD_MS);
     57     }
     58 
     59     @Override
     60     public void onPause() {
     61         super.onPause();
     62         if (mOnActionClickListener != null) {
     63             // Dismisses the dialog to prevent the callback being forgotten during
     64             // fragment re-creating.
     65             dismiss();
     66         }
     67     }
     68 
     69     @Override
     70     public void onStop() {
     71         super.onStop();
     72         mHandler.removeCallbacks(mAutoDismisser);
     73     }
     74 
     75     @Override
     76     public Dialog onCreateDialog(Bundle savedInstanceState) {
     77         Dialog dialog = super.onCreateDialog(savedInstanceState);
     78         dialog.setOnKeyListener(
     79                 new DialogInterface.OnKeyListener() {
     80                     public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent keyEvent) {
     81                         mHandler.removeCallbacks(mAutoDismisser);
     82                         mHandler.postDelayed(mAutoDismisser, AUTO_DISMISS_TIME_THRESHOLD_MS);
     83                         return false;
     84                     }
     85                 });
     86         return dialog;
     87     }
     88 
     89     @Override
     90     public int getTheme() {
     91         return R.style.Theme_TV_dialog_HalfSizedDialog;
     92     }
     93 
     94     @Override
     95     public String getTrackerLabel() {
     96         return TRACKER_LABEL;
     97     }
     98 
     99     /**
    100      * Sets {@link OnActionClickListener} for the dialog fragment. If listener is set, the dialog
    101      * will be automatically closed when it's paused to prevent the fragment being re-created by the
    102      * framework, which will result the listener being forgotten.
    103      */
    104     public void setOnActionClickListener(OnActionClickListener listener) {
    105         mOnActionClickListener = listener;
    106     }
    107 
    108     /** Returns {@link OnActionClickListener} for sub-classes or any inner fragments. */
    109     protected OnActionClickListener getOnActionClickListener() {
    110         return mOnActionClickListener;
    111     }
    112 
    113     /**
    114      * An interface to provide callbacks for half-sized dialogs. Subclasses or inner fragments
    115      * should invoke {@link OnActionClickListener#onActionClick(long)} and provide the identifier of
    116      * the action user clicked.
    117      */
    118     public interface OnActionClickListener {
    119         void onActionClick(long actionId);
    120     }
    121 }
    122