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.dvr.ui; 18 19import android.content.DialogInterface; 20import android.os.Bundle; 21import android.os.Handler; 22import android.view.KeyEvent; 23import android.view.LayoutInflater; 24import android.view.View; 25import android.view.ViewGroup; 26 27import com.android.tv.R; 28import com.android.tv.dialog.SafeDismissDialogFragment; 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 getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() { 58 public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent keyEvent) { 59 mHandler.removeCallbacks(mAutoDismisser); 60 mHandler.postDelayed(mAutoDismisser, AUTO_DISMISS_TIME_THRESHOLD_MS); 61 return false; 62 } 63 }); 64 mHandler.postDelayed(mAutoDismisser, AUTO_DISMISS_TIME_THRESHOLD_MS); 65 } 66 67 @Override 68 public void onPause() { 69 super.onPause(); 70 if (mOnActionClickListener != null) { 71 // Dismisses the dialog to prevent the callback being forgotten during 72 // fragment re-creating. 73 dismiss(); 74 } 75 } 76 77 @Override 78 public void onStop() { 79 super.onStop(); 80 mHandler.removeCallbacks(mAutoDismisser); 81 } 82 83 @Override 84 public int getTheme() { 85 return R.style.Theme_TV_dialog_HalfSizedDialog; 86 } 87 88 @Override 89 public String getTrackerLabel() { 90 return TRACKER_LABEL; 91 } 92 93 /** 94 * Sets {@link OnActionClickListener} for the dialog fragment. If listener is set, the dialog 95 * will be automatically closed when it's paused to prevent the fragment being re-created by 96 * the framework, which will result the listener being forgotten. 97 */ 98 public void setOnActionClickListener(OnActionClickListener listener) { 99 mOnActionClickListener = listener; 100 } 101 102 /** 103 * Returns {@link OnActionClickListener} for sub-classes or any inner fragments. 104 */ 105 protected OnActionClickListener getOnActionClickListener() { 106 return mOnActionClickListener; 107 } 108 109 /** 110 * An interface to provide callbacks for half-sized dialogs. Subclasses or inner fragments 111 * should invoke {@link OnActionClickListener#onActionClick(long)} and provide the identifier 112 * of the action user clicked. 113 */ 114 public interface OnActionClickListener { 115 void onActionClick(long actionId); 116 } 117}