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
17package com.android.internal.app;
18
19import android.app.Activity;
20import android.app.Dialog;
21import android.content.DialogInterface;
22import android.os.Bundle;
23import android.view.KeyEvent;
24import android.view.ViewGroup;
25import android.view.accessibility.AccessibilityEvent;
26
27/**
28 * An activity that follows the visual style of an AlertDialog.
29 *
30 * @see #mAlert
31 * @see #mAlertParams
32 * @see #setupAlert()
33 */
34public abstract class AlertActivity extends Activity implements DialogInterface {
35
36    /**
37     * The model for the alert.
38     *
39     * @see #mAlertParams
40     */
41    protected AlertController mAlert;
42
43    /**
44     * The parameters for the alert.
45     */
46    protected AlertController.AlertParams mAlertParams;
47
48    @Override
49    protected void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51
52        mAlert = new AlertController(this, this, getWindow());
53        mAlertParams = new AlertController.AlertParams(this);
54    }
55
56    public void cancel() {
57        finish();
58    }
59
60    public void dismiss() {
61        // This is called after the click, since we finish when handling the
62        // click, don't do that again here.
63        if (!isFinishing()) {
64            finish();
65        }
66    }
67
68    @Override
69    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
70        event.setClassName(Dialog.class.getName());
71        event.setPackageName(getPackageName());
72
73        ViewGroup.LayoutParams params = getWindow().getAttributes();
74        boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT) &&
75                (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
76        event.setFullScreen(isFullScreen);
77
78        return false;
79    }
80
81    /**
82     * Sets up the alert, including applying the parameters to the alert model,
83     * and installing the alert's content.
84     *
85     * @see #mAlert
86     * @see #mAlertParams
87     */
88    protected void setupAlert() {
89        mAlertParams.apply(mAlert);
90        mAlert.installContent();
91    }
92
93    @Override
94    public boolean onKeyDown(int keyCode, KeyEvent event) {
95        if (mAlert.onKeyDown(keyCode, event)) return true;
96        return super.onKeyDown(keyCode, event);
97    }
98
99    @Override
100    public boolean onKeyUp(int keyCode, KeyEvent event) {
101        if (mAlert.onKeyUp(keyCode, event)) return true;
102        return super.onKeyUp(keyCode, event);
103    }
104}
105