1 /*
2  * Copyright (C) 2007 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.internal.app;
18 
19 import android.app.Activity;
20 import android.app.Dialog;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.content.DialogInterface;
23 import android.os.Bundle;
24 import android.view.KeyEvent;
25 import android.view.ViewGroup;
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 #mAlertParams
33  * @see #setupAlert()
34  */
35 public abstract class AlertActivity extends Activity implements DialogInterface {
36 
37     @UnsupportedAppUsage
AlertActivity()38     public AlertActivity() {
39     }
40 
41     /**
42      * The model for the alert.
43      *
44      * @see #mAlertParams
45      */
46     @UnsupportedAppUsage
47     protected AlertController mAlert;
48 
49     /**
50      * The parameters for the alert.
51      */
52     @UnsupportedAppUsage
53     protected AlertController.AlertParams mAlertParams;
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         mAlert = AlertController.create(this, this, getWindow());
60         mAlertParams = new AlertController.AlertParams(this);
61     }
62 
cancel()63     public void cancel() {
64         finish();
65     }
66 
dismiss()67     public void dismiss() {
68         // This is called after the click, since we finish when handling the
69         // click, don't do that again here.
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     public 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 
93     /**
94      * Sets up the alert, including applying the parameters to the alert model,
95      * and installing the alert's content.
96      *
97      * @see #mAlert
98      * @see #mAlertParams
99      */
100     @UnsupportedAppUsage
setupAlert()101     protected void setupAlert() {
102         mAlert.installContent(mAlertParams);
103     }
104 
105     @Override
onKeyDown(int keyCode, KeyEvent event)106     public boolean onKeyDown(int keyCode, KeyEvent event) {
107         if (mAlert.onKeyDown(keyCode, event)) return true;
108         return super.onKeyDown(keyCode, event);
109     }
110 
111     @Override
onKeyUp(int keyCode, KeyEvent event)112     public boolean onKeyUp(int keyCode, KeyEvent event) {
113         if (mAlert.onKeyUp(keyCode, event)) return true;
114         return super.onKeyUp(keyCode, event);
115     }
116 }
117