• 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 com.android.nfc.cardemulation.util;
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  * Copied from {@code packages/modules/Bluetooth/android/app/src/com/android/bluetooth/
41  * AlertActivity.java}
42  */
43 public abstract class AlertActivity extends Activity implements DialogInterface.OnDismissListener,
44         DialogInterface.OnCancelListener {
45 
46     /**
47      * The model for the alert.
48      *
49      */
50     protected AlertDialog.Builder mAlertBuilder;
51     private AlertDialog mAlert;
52 
AlertActivity()53     public AlertActivity() {}
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
60         requestWindowFeature(Window.FEATURE_NO_TITLE);
61         mAlertBuilder = new AlertDialog.Builder(this);
62         mAlertBuilder.setOnDismissListener(this);
63         mAlertBuilder.setOnCancelListener(this);
64     }
65 
66     @Override
onDismiss(DialogInterface dialog)67     public void onDismiss(DialogInterface dialog) {
68         if (!isFinishing()) {
69             finish();
70         }
71     }
72 
73     @Override
onCancel(DialogInterface dialog)74     public void onCancel(DialogInterface dialog) {
75         if (!isFinishing()) {
76             finish();
77         }
78     }
79 
80     @Override
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)81     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
82         return dispatchPopulateAccessibilityEvent(this, event);
83     }
84 
dispatchPopulateAccessibilityEvent(Activity act, AccessibilityEvent event)85     private static boolean dispatchPopulateAccessibilityEvent(Activity act,
86             AccessibilityEvent event) {
87         event.setClassName(Dialog.class.getName());
88         event.setPackageName(act.getPackageName());
89 
90         ViewGroup.LayoutParams params = act.getWindow().getAttributes();
91         boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT)
92                 && (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
93         event.setFullScreen(isFullScreen);
94 
95         return false;
96     }
97 
setupAlert()98     protected void setupAlert() {
99         mAlert = mAlertBuilder.create();
100         mAlert.show();
101     }
102 
103     @Override
onDestroy()104     protected void onDestroy() {
105         if (mAlert != null) {
106             mAlert.dismiss();
107         }
108         super.onDestroy();
109     }
110 
111 }
112