Home | History | Annotate | Download | only in app
      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.content.DialogInterface;
     22 import android.os.Bundle;
     23 import android.view.KeyEvent;
     24 import android.view.ViewGroup;
     25 import 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  */
     34 public 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 = AlertController.create(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         return dispatchPopulateAccessibilityEvent(this, event);
     71     }
     72 
     73     public static boolean dispatchPopulateAccessibilityEvent(Activity act,
     74             AccessibilityEvent event) {
     75         event.setClassName(Dialog.class.getName());
     76         event.setPackageName(act.getPackageName());
     77 
     78         ViewGroup.LayoutParams params = act.getWindow().getAttributes();
     79         boolean isFullScreen = (params.width == ViewGroup.LayoutParams.MATCH_PARENT) &&
     80                 (params.height == ViewGroup.LayoutParams.MATCH_PARENT);
     81         event.setFullScreen(isFullScreen);
     82 
     83         return false;
     84     }
     85 
     86     /**
     87      * Sets up the alert, including applying the parameters to the alert model,
     88      * and installing the alert's content.
     89      *
     90      * @see #mAlert
     91      * @see #mAlertParams
     92      */
     93     protected void setupAlert() {
     94         mAlert.installContent(mAlertParams);
     95     }
     96 
     97     @Override
     98     public boolean onKeyDown(int keyCode, KeyEvent event) {
     99         if (mAlert.onKeyDown(keyCode, event)) return true;
    100         return super.onKeyDown(keyCode, event);
    101     }
    102 
    103     @Override
    104     public boolean onKeyUp(int keyCode, KeyEvent event) {
    105         if (mAlert.onKeyUp(keyCode, event)) return true;
    106         return super.onKeyUp(keyCode, event);
    107     }
    108 }
    109