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