FullscreenDialogFragment.java revision 816a4be1a0f34f6a48877c8afd3dbbca19eac435
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
36    private final int mViewLayoutResId;
37    private final String mTrackerLabel;
38    private DialogView mDialogView;
39
40    /**
41     * Constructor of FullscreenDialogFragment. View class of viewLayoutResId should
42     * implement {@link DialogView}.
43     */
44    public FullscreenDialogFragment(int viewLayoutResId, String trackerLabel) {
45        mViewLayoutResId = viewLayoutResId;
46        mTrackerLabel = trackerLabel;
47    }
48
49    @Override
50    public Dialog onCreateDialog(Bundle savedInstanceState) {
51        FullscreenDialog dialog =
52                new FullscreenDialog(getActivity(), R.style.Theme_TV_dialog_Fullscreen);
53        LayoutInflater inflater = LayoutInflater.from(getActivity());
54        View v = inflater.inflate(mViewLayoutResId, null);
55        dialog.setContentView(v);
56        mDialogView = (DialogView) v;
57        mDialogView.initialize((MainActivity) getActivity(), dialog);
58        return dialog;
59    }
60
61    @Override
62    public void onDestroy() {
63        super.onDestroy();
64        mDialogView.onDestroy();
65    }
66
67    @Override
68    public String getTrackerLabel() {
69        return mTrackerLabel;
70    }
71
72    private class FullscreenDialog extends TvDialog {
73        public FullscreenDialog(Context context, int theme) {
74            super(context, theme);
75        }
76
77        @Override
78        public void setContentView(View dialogView) {
79            super.setContentView(dialogView);
80            mDialogView = (DialogView) dialogView;
81        }
82
83        @Override
84        public boolean dispatchKeyEvent(KeyEvent event) {
85            boolean handled = super.dispatchKeyEvent(event);
86            return handled || ((View) mDialogView).dispatchKeyEvent(event);
87        }
88
89        @Override
90        public void onBackPressed() {
91            mDialogView.onBackPressed();
92        }
93    }
94
95    /**
96     * Interface for the view of {@link FullscreenDialogFragment}.
97     */
98    public interface DialogView {
99        /**
100         * Called after the view is inflated and attached to the dialog.
101         */
102        void initialize(MainActivity activity, Dialog dialog);
103        /**
104         * Called when a back key is pressed.
105         */
106        void onBackPressed();
107        /**
108         * Called when {@link DialogFragment#onDestroy} is called.
109         */
110        void onDestroy();
111    }
112}
113