1 /*
2  * Copyright (C) 2019 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 android.bluetooth;
18 
19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
20 
21 import android.annotation.SuppressLint;
22 import android.app.Activity;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.DialogInterface;
26 import android.os.Bundle;
27 import android.view.ViewGroup;
28 import android.view.Window;
29 import android.view.accessibility.AccessibilityEvent;
30 import android.widget.Button;
31 
32 import com.android.internal.annotations.VisibleForTesting;
33 
34 /**
35  * An activity that follows the visual style of an AlertDialog.
36  *
37  * @see #mAlert
38  * @see #setupAlert()
39  */
40 @SuppressLint("AndroidFrameworkBluetoothPermission")
41 public abstract class AlertActivity extends Activity
42         implements DialogInterface.OnDismissListener, DialogInterface.OnCancelListener {
43 
44     /** The model for the alert. */
45     protected AlertDialog.Builder mAlertBuilder;
46 
47     private AlertDialog mAlert;
48 
AlertActivity()49     public AlertActivity() {}
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54 
55         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
56         requestWindowFeature(Window.FEATURE_NO_TITLE);
57         mAlertBuilder = new AlertDialog.Builder(this);
58         mAlertBuilder.setOnDismissListener(this);
59         mAlertBuilder.setOnCancelListener(this);
60     }
61 
62     @Override
onDismiss(DialogInterface dialog)63     public void onDismiss(DialogInterface dialog) {
64         if (!isFinishing()) {
65             finish();
66         }
67     }
68 
69     @Override
onCancel(DialogInterface dialog)70     public void onCancel(DialogInterface dialog) {
71         if (!isFinishing()) {
72             finish();
73         }
74     }
75 
76     @Override
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)77     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
78         return dispatchPopulateAccessibilityEvent(this, event);
79     }
80 
dispatchPopulateAccessibilityEvent( Activity act, AccessibilityEvent event)81     private static boolean dispatchPopulateAccessibilityEvent(
82             Activity act, AccessibilityEvent event) {
83         event.setClassName(Dialog.class.getName());
84         event.setPackageName(act.getPackageName());
85 
86         ViewGroup.LayoutParams params = act.getWindow().getAttributes();
87         boolean isFullScreen =
88                 (params.width == ViewGroup.LayoutParams.MATCH_PARENT)
89                         && (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
90         event.setFullScreen(isFullScreen);
91 
92         return false;
93     }
94 
setupAlert()95     protected void setupAlert() {
96         mAlert = mAlertBuilder.create();
97         mAlert.show();
98     }
99 
changeIconAttribute(int attrId)100     protected void changeIconAttribute(int attrId) {
101         if (mAlert == null) return;
102         mAlert.setIconAttribute(attrId);
103     }
104 
changeTitle(CharSequence title)105     protected void changeTitle(CharSequence title) {
106         if (mAlert == null) return;
107         mAlert.setTitle(title);
108     }
109 
changeButtonVisibility(int identifier, int visibility)110     protected void changeButtonVisibility(int identifier, int visibility) {
111         if (mAlert == null) return;
112         mAlert.getButton(identifier).setVisibility(visibility);
113     }
114 
changeButtonText(int identifier, CharSequence text)115     protected void changeButtonText(int identifier, CharSequence text) {
116         if (mAlert == null) return;
117         mAlert.getButton(identifier).setText(text);
118     }
119 
changeButtonEnabled(int identifier, boolean enable)120     protected void changeButtonEnabled(int identifier, boolean enable) {
121         if (mAlert == null) return;
122         mAlert.getButton(identifier).setEnabled(enable);
123     }
124 
125     @VisibleForTesting
getButton(int identifier)126     public Button getButton(int identifier) {
127         return mAlert.getButton(identifier);
128     }
129 
130     @Override
onDestroy()131     protected void onDestroy() {
132         if (mAlert != null) {
133             mAlert.dismiss();
134         }
135         super.onDestroy();
136     }
137 }
138