Home | History | Annotate | Download | only in cellbroadcastreceiver
      1 /*
      2  * Copyright (C) 2012 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.cellbroadcastreceiver;
     18 
     19 import android.app.Activity;
     20 import android.app.NotificationManager;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.graphics.drawable.Drawable;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 import android.os.Message;
     27 import android.provider.Telephony;
     28 import android.telephony.CellBroadcastMessage;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.view.Window;
     32 import android.view.WindowManager;
     33 import android.widget.Button;
     34 import android.widget.ImageView;
     35 import android.widget.TextView;
     36 
     37 /**
     38  * Full-screen emergency alert with flashing warning icon.
     39  * Alert audio and text-to-speech handled by {@link CellBroadcastAlertAudio}.
     40  * Keyguard handling based on {@code AlarmAlertFullScreen} class from DeskClock app.
     41  */
     42 public class CellBroadcastAlertFullScreen extends Activity {
     43 
     44     /**
     45      * Intent extra for full screen alert launched from dialog subclass as a result of the
     46      * screen turning off.
     47      */
     48     static final String SCREEN_OFF = "screen_off";
     49 
     50     /** Whether to show the flashing warning icon. */
     51     private boolean mIsEmergencyAlert;
     52 
     53     /** The cell broadcast message to display. */
     54     CellBroadcastMessage mMessage;
     55 
     56     /** Length of time for the warning icon to be visible. */
     57     private static final int WARNING_ICON_ON_DURATION_MSEC = 800;
     58 
     59     /** Length of time for the warning icon to be off. */
     60     private static final int WARNING_ICON_OFF_DURATION_MSEC = 800;
     61 
     62     /** Warning icon state. false = visible, true = off */
     63     private boolean mIconAnimationState;
     64 
     65     /** Stop animating icon after {@link #onStop()} is called. */
     66     private boolean mStopAnimation;
     67 
     68     /** The warning icon Drawable. */
     69     private Drawable mWarningIcon;
     70 
     71     /** The View containing the warning icon. */
     72     private ImageView mWarningIconView;
     73 
     74     /** Icon animation handler for flashing warning alerts. */
     75     private final Handler mAnimationHandler = new Handler() {
     76         @Override
     77         public void handleMessage(Message msg) {
     78             if (mIconAnimationState) {
     79                 mWarningIconView.setImageAlpha(255);
     80                 if (!mStopAnimation) {
     81                     mAnimationHandler.sendEmptyMessageDelayed(0, WARNING_ICON_ON_DURATION_MSEC);
     82                 }
     83             } else {
     84                 mWarningIconView.setImageAlpha(0);
     85                 if (!mStopAnimation) {
     86                     mAnimationHandler.sendEmptyMessageDelayed(0, WARNING_ICON_OFF_DURATION_MSEC);
     87                 }
     88             }
     89             mIconAnimationState = !mIconAnimationState;
     90             mWarningIconView.invalidateDrawable(mWarningIcon);
     91         }
     92     };
     93 
     94     @Override
     95     protected void onCreate(Bundle savedInstanceState) {
     96         super.onCreate(savedInstanceState);
     97 
     98         final Window win = getWindow();
     99 
    100         // We use a custom title, so remove the standard dialog title bar
    101         win.requestFeature(Window.FEATURE_NO_TITLE);
    102 
    103         // Full screen alerts display above the keyguard and when device is locked.
    104         win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
    105                 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
    106                 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    107 
    108         // Turn on the screen unless we're being launched from the dialog subclass as a result of
    109         // the screen turning off.
    110         if (!getIntent().getBooleanExtra(SCREEN_OFF, false)) {
    111             win.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    112                     | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    113         }
    114 
    115         // Save message for passing from dialog to fullscreen activity, and for marking read.
    116         mMessage = getIntent().getParcelableExtra(
    117                 CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA);
    118 
    119         updateLayout(mMessage);
    120     }
    121 
    122     protected int getLayoutResId() {
    123         return R.layout.cell_broadcast_alert_fullscreen;
    124     }
    125 
    126     private void updateLayout(CellBroadcastMessage message) {
    127         LayoutInflater inflater = LayoutInflater.from(this);
    128 
    129         setContentView(inflater.inflate(getLayoutResId(), null));
    130 
    131         /* Initialize dialog text from alert message. */
    132         int titleId = CellBroadcastResources.getDialogTitleResource(message);
    133         setTitle(titleId);
    134         ((TextView) findViewById(R.id.alertTitle)).setText(titleId);
    135         ((TextView) findViewById(R.id.message)).setText(
    136                 CellBroadcastResources.getFormattedMessageBody(this, message));
    137 
    138         /* dismiss button: close notification */
    139         findViewById(R.id.dismissButton).setOnClickListener(
    140                 new Button.OnClickListener() {
    141                     public void onClick(View v) {
    142                         dismiss();
    143                     }
    144                 });
    145 
    146         mIsEmergencyAlert = message.isPublicAlertMessage() || CellBroadcastConfigService
    147                 .isOperatorDefinedEmergencyId(message.getServiceCategory());
    148 
    149         if (mIsEmergencyAlert) {
    150             mWarningIcon = getResources().getDrawable(R.drawable.ic_warning_large);
    151             mWarningIconView = (ImageView) findViewById(R.id.icon);
    152             if (mWarningIconView != null) {
    153                 mWarningIconView.setImageDrawable(mWarningIcon);
    154             }
    155 
    156             // Dismiss the notification that brought us here
    157             ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
    158                     .cancel((int) message.getDeliveryTime());
    159         }
    160     }
    161 
    162     /**
    163      * Start animating warning icon.
    164      */
    165     @Override
    166     protected void onStart() {
    167         super.onStart();
    168         if (mIsEmergencyAlert) {
    169             // start icon animation
    170             mAnimationHandler.sendEmptyMessageDelayed(0, WARNING_ICON_ON_DURATION_MSEC);
    171         }
    172     }
    173 
    174     /**
    175      * Stop animating warning icon and stop the {@link CellBroadcastAlertAudio}
    176      * service if necessary.
    177      */
    178     void dismiss() {
    179         // Stop playing alert sound/vibration/speech (if started)
    180         stopService(new Intent(this, CellBroadcastAlertAudio.class));
    181 
    182         final long deliveryTime = mMessage.getDeliveryTime();
    183 
    184         // Mark broadcast as read on a background thread.
    185         new CellBroadcastContentProvider.AsyncCellBroadcastTask(getContentResolver())
    186                 .execute(new CellBroadcastContentProvider.CellBroadcastOperation() {
    187                     @Override
    188                     public boolean execute(CellBroadcastContentProvider provider) {
    189                         return provider.markBroadcastRead(
    190                                 Telephony.CellBroadcasts.DELIVERY_TIME, deliveryTime);
    191                     }
    192                 });
    193 
    194         if (mIsEmergencyAlert) {
    195             // stop animating emergency alert icon
    196             mStopAnimation = true;
    197         } else {
    198             // decrement unread non-emergency alert count
    199             CellBroadcastReceiverApp.decrementUnreadAlertCount();
    200         }
    201         finish();
    202     }
    203 
    204     /**
    205      * Ignore the back button for emergency alerts (overridden by alert dialog so that the dialog
    206      * is dismissed).
    207      */
    208     @Override
    209     public void onBackPressed() {
    210         // ignored
    211     }
    212 }
    213