• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 implements DialogInterface.OnDismissListener,
42         DialogInterface.OnCancelListener {
43 
44     /**
45      * The model for the alert.
46      *
47      */
48     protected AlertDialog.Builder mAlertBuilder;
49     private AlertDialog mAlert;
50 
AlertActivity()51     public AlertActivity() {}
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56 
57         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
58         requestWindowFeature(Window.FEATURE_NO_TITLE);
59         mAlertBuilder = new AlertDialog.Builder(this);
60         mAlertBuilder.setOnDismissListener(this);
61         mAlertBuilder.setOnCancelListener(this);
62     }
63 
64     @Override
onDismiss(DialogInterface dialog)65     public void onDismiss(DialogInterface dialog) {
66         if (!isFinishing()) {
67             finish();
68         }
69     }
70 
71     @Override
onCancel(DialogInterface dialog)72     public void onCancel(DialogInterface dialog) {
73         if (!isFinishing()) {
74             finish();
75         }
76     }
77 
78     @Override
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)79     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
80         return dispatchPopulateAccessibilityEvent(this, event);
81     }
82 
dispatchPopulateAccessibilityEvent(Activity act, AccessibilityEvent event)83     private static boolean dispatchPopulateAccessibilityEvent(Activity act,
84             AccessibilityEvent event) {
85         event.setClassName(Dialog.class.getName());
86         event.setPackageName(act.getPackageName());
87 
88         ViewGroup.LayoutParams params = act.getWindow().getAttributes();
89         boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT)
90                 && (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
91         event.setFullScreen(isFullScreen);
92 
93         return false;
94     }
95 
setupAlert()96     protected void setupAlert() {
97         mAlert = mAlertBuilder.create();
98         mAlert.show();
99     }
100 
changeIconAttribute(int attrId)101     protected void changeIconAttribute(int attrId) {
102         if (mAlert == null) return;
103         mAlert.setIconAttribute(attrId);
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 }
139