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.Activity;
20import android.app.Dialog;
21import android.app.DialogFragment;
22import android.content.Context;
23import android.os.Bundle;
24import android.view.KeyEvent;
25
26import com.android.tv.MainActivity;
27import com.android.tv.TvApplication;
28import com.android.tv.analytics.HasTrackerLabel;
29import com.android.tv.analytics.Tracker;
30
31/**
32 * Provides the safe dismiss feature regardless of the DialogFragment's life cycle.
33 */
34public abstract class SafeDismissDialogFragment extends DialogFragment
35        implements HasTrackerLabel {
36    private MainActivity mActivity;
37    private boolean mAttached = false;
38    private boolean mDismissPending = false;
39    private Tracker mTracker;
40
41    @Override
42    public Dialog onCreateDialog(Bundle savedInstanceState) {
43        return new TvDialog(getActivity(), getTheme());
44    }
45
46    @Override
47    public void onAttach(Activity activity) {
48        super.onAttach(activity);
49        mAttached = true;
50        mActivity = (MainActivity) activity;
51        mTracker = TvApplication.getSingletons(activity).getTracker();
52        if (mDismissPending) {
53            mDismissPending = false;
54            dismiss();
55        }
56    }
57
58    @Override
59    public void onResume() {
60        super.onResume();
61        mTracker.sendScreenView(getTrackerLabel());
62    }
63
64    @Override
65    public void onDestroy() {
66        if (mActivity != null) {
67            mActivity.getOverlayManager().onDialogDestroyed();
68        }
69        super.onDestroy();
70    }
71
72    @Override
73    public void onDetach() {
74        super.onDetach();
75        mAttached = false;
76        mTracker = null;
77    }
78
79    /**
80     * Dismiss safely regardless of the DialogFragment's life cycle.
81     */
82    @Override
83    public void dismiss() {
84        if (!mAttached) {
85            // dismiss() before getFragmentManager() is set cause NPE in dismissInternal().
86            // FragmentManager is set when a fragment is used in a transaction,
87            // so wait here until we can dismiss safely.
88            mDismissPending = true;
89        } else {
90            super.dismiss();
91        }
92    }
93
94    protected class TvDialog extends Dialog {
95        public TvDialog(Context context, int theme) {
96            super(context, theme);
97        }
98
99        @Override
100        public boolean onKeyUp(int keyCode, KeyEvent event) {
101            // When a dialog is showing, key events are handled by the dialog instead of
102            // MainActivity. Therefore, unless a key is a global key, it should be handled here.
103            if (mAttached && keyCode == KeyEvent.KEYCODE_SEARCH) {
104                mActivity.showSearchActivity();
105                return true;
106            }
107            return super.onKeyUp(keyCode, event);
108        }
109    }
110}
111