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