1 /*
2  * Copyright (C) 2015 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.app.DialogFragment;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.view.KeyEvent;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import com.android.tv.MainActivity;
27 import com.android.tv.R;
28 
29 /** Dialog fragment with full screen. */
30 public class FullscreenDialogFragment extends SafeDismissDialogFragment {
31     public static final String DIALOG_TAG = FullscreenDialogFragment.class.getSimpleName();
32     public static final String VIEW_LAYOUT_ID = "viewLayoutId";
33     public static final String TRACKER_LABEL = "trackerLabel";
34 
35     /**
36      * Creates a FullscreenDialogFragment. View class of viewLayoutResId should implement {@link
37      * DialogView}.
38      */
newInstance(int viewLayoutResId, String trackerLabel)39     public static FullscreenDialogFragment newInstance(int viewLayoutResId, String trackerLabel) {
40         FullscreenDialogFragment f = new FullscreenDialogFragment();
41         Bundle args = new Bundle();
42         args.putInt(VIEW_LAYOUT_ID, viewLayoutResId);
43         args.putString(TRACKER_LABEL, trackerLabel);
44         f.setArguments(args);
45         return f;
46     }
47 
48     private String mTrackerLabel;
49     private DialogView mDialogView;
50 
51     @Override
onCreateDialog(Bundle savedInstanceState)52     public Dialog onCreateDialog(Bundle savedInstanceState) {
53         FullscreenDialog dialog =
54                 new FullscreenDialog(getActivity(), R.style.Theme_TV_dialog_Fullscreen);
55         LayoutInflater inflater = LayoutInflater.from(getActivity());
56         Bundle args = getArguments();
57         mTrackerLabel = args.getString(TRACKER_LABEL);
58         int viewLayoutResId = args.getInt(VIEW_LAYOUT_ID);
59         View v = inflater.inflate(viewLayoutResId, null);
60         dialog.setContentView(v);
61         mDialogView = (DialogView) v;
62         mDialogView.initialize((MainActivity) getActivity(), dialog);
63         return dialog;
64     }
65 
66     @Override
onDestroy()67     public void onDestroy() {
68         super.onDestroy();
69         mDialogView.onDestroy();
70     }
71 
72     @Override
getTrackerLabel()73     public String getTrackerLabel() {
74         return mTrackerLabel;
75     }
76 
77     private class FullscreenDialog extends Dialog {
FullscreenDialog(Context context, int theme)78         public FullscreenDialog(Context context, int theme) {
79             super(context, theme);
80         }
81 
82         @Override
setContentView(View dialogView)83         public void setContentView(View dialogView) {
84             super.setContentView(dialogView);
85             mDialogView = (DialogView) dialogView;
86         }
87 
88         @Override
dispatchKeyEvent(KeyEvent event)89         public boolean dispatchKeyEvent(KeyEvent event) {
90             boolean handled = super.dispatchKeyEvent(event);
91             return handled || ((View) mDialogView).dispatchKeyEvent(event);
92         }
93 
94         @Override
onBackPressed()95         public void onBackPressed() {
96             mDialogView.onBackPressed();
97         }
98     }
99 
100     /** Interface for the view of {@link FullscreenDialogFragment}. */
101     public interface DialogView {
102         /** Called after the view is inflated and attached to the dialog. */
initialize(MainActivity activity, Dialog dialog)103         void initialize(MainActivity activity, Dialog dialog);
104         /** Called when a back key is pressed. */
onBackPressed()105         void onBackPressed();
106         /** Called when {@link DialogFragment#onDestroy} is called. */
onDestroy()107         void onDestroy();
108     }
109 }
110