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