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