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 17package com.android.tv.dialog; 18 19import android.app.Dialog; 20import android.content.DialogInterface; 21import android.os.Bundle; 22import android.os.Handler; 23import android.view.KeyEvent; 24import android.view.LayoutInflater; 25import android.view.View; 26import android.view.ViewGroup; 27 28import com.android.tv.R; 29 30import java.util.concurrent.TimeUnit; 31 32public class HalfSizedDialogFragment extends SafeDismissDialogFragment { 33 public static final String DIALOG_TAG = HalfSizedDialogFragment.class.getSimpleName(); 34 public static final String TRACKER_LABEL = "Half sized dialog"; 35 36 private static final long AUTO_DISMISS_TIME_THRESHOLD_MS = TimeUnit.SECONDS.toMillis(30); 37 38 private OnActionClickListener mOnActionClickListener; 39 40 private Handler mHandler = new Handler(); 41 private Runnable mAutoDismisser = new Runnable() { 42 @Override 43 public void run() { 44 dismiss(); 45 } 46 }; 47 48 @Override 49 public View onCreateView(LayoutInflater inflater, ViewGroup container, 50 Bundle savedInstanceState) { 51 return inflater.inflate(R.layout.halfsized_dialog, container, false); 52 } 53 54 @Override 55 public void onStart() { 56 super.onStart(); 57 mHandler.postDelayed(mAutoDismisser, AUTO_DISMISS_TIME_THRESHOLD_MS); 58 } 59 60 @Override 61 public void onPause() { 62 super.onPause(); 63 if (mOnActionClickListener != null) { 64 // Dismisses the dialog to prevent the callback being forgotten during 65 // fragment re-creating. 66 dismiss(); 67 } 68 } 69 70 @Override 71 public void onStop() { 72 super.onStop(); 73 mHandler.removeCallbacks(mAutoDismisser); 74 } 75 76 @Override 77 public Dialog onCreateDialog(Bundle savedInstanceState) { 78 Dialog dialog = super.onCreateDialog(savedInstanceState); 79 dialog.setOnKeyListener(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 102 * the framework, which will result the listener being forgotten. 103 */ 104 public void setOnActionClickListener(OnActionClickListener listener) { 105 mOnActionClickListener = listener; 106 } 107 108 /** 109 * Returns {@link OnActionClickListener} for sub-classes or any inner fragments. 110 */ 111 protected OnActionClickListener getOnActionClickListener() { 112 return mOnActionClickListener; 113 } 114 115 /** 116 * An interface to provide callbacks for half-sized dialogs. Subclasses or inner fragments 117 * should invoke {@link OnActionClickListener#onActionClick(long)} and provide the identifier 118 * of the action user clicked. 119 */ 120 public interface OnActionClickListener { 121 void onActionClick(long actionId); 122 } 123}