• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.KeyguardManager;
21 import android.app.NotificationManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.content.res.Resources;
26 import android.graphics.drawable.Drawable;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Message;
30 import android.os.PowerManager;
31 import android.preference.PreferenceManager;
32 import android.provider.Telephony;
33 import android.telephony.CellBroadcastMessage;
34 import android.telephony.SmsCbCmasInfo;
35 import android.util.Log;
36 import android.view.KeyEvent;
37 import android.view.LayoutInflater;
38 import android.view.View;
39 import android.view.Window;
40 import android.view.WindowManager;
41 import android.widget.Button;
42 import android.widget.ImageView;
43 import android.widget.TextView;
44 
45 import java.util.ArrayList;
46 import java.util.concurrent.atomic.AtomicInteger;
47 
48 /**
49  * Custom alert dialog with optional flashing warning icon.
50  * Alert audio and text-to-speech handled by {@link CellBroadcastAlertAudio}.
51  */
52 public class CellBroadcastAlertDialog extends Activity {
53 
54     private static final String TAG = "CellBroadcastAlertDialog";
55 
56     /** Intent extra for non-emergency alerts sent when user selects the notification. */
57     static final String FROM_NOTIFICATION_EXTRA = "from_notification";
58 
59     // Intent extra to identify if notification was sent while trying to move away from the dialog
60     //  without acknowleding the dialog
61     static final String FROM_SAVE_STATE_NOTIFICATION_EXTRA = "from_save_state_notification";
62 
63     /** List of cell broadcast messages to display (oldest to newest). */
64     protected ArrayList<CellBroadcastMessage> mMessageList;
65 
66     /** Whether a CMAS alert other than Presidential Alert was displayed. */
67     private boolean mShowOptOutDialog;
68 
69     /** Length of time for the warning icon to be visible. */
70     private static final int WARNING_ICON_ON_DURATION_MSEC = 800;
71 
72     /** Length of time for the warning icon to be off. */
73     private static final int WARNING_ICON_OFF_DURATION_MSEC = 800;
74 
75     /** Length of time to keep the screen turned on. */
76     private static final int KEEP_SCREEN_ON_DURATION_MSEC = 60000;
77 
78     /** Animation handler for the flashing warning icon (emergency alerts only). */
79     private final AnimationHandler mAnimationHandler = new AnimationHandler();
80 
81     /** Handler to add and remove screen on flags for emergency alerts. */
82     private final ScreenOffHandler mScreenOffHandler = new ScreenOffHandler();
83 
84     /**
85      * Animation handler for the flashing warning icon (emergency alerts only).
86      */
87     private class AnimationHandler extends Handler {
88         /** Latest {@code message.what} value for detecting old messages. */
89         private final AtomicInteger mCount = new AtomicInteger();
90 
91         /** Warning icon state: visible == true, hidden == false. */
92         private boolean mWarningIconVisible;
93 
94         /** The warning icon Drawable. */
95         private Drawable mWarningIcon;
96 
97         /** The View containing the warning icon. */
98         private ImageView mWarningIconView;
99 
100         /** Package local constructor (called from outer class). */
AnimationHandler()101         AnimationHandler() {}
102 
103         /** Start the warning icon animation. */
startIconAnimation()104         void startIconAnimation() {
105             if (!initDrawableAndImageView()) {
106                 return;     // init failure
107             }
108             mWarningIconVisible = true;
109             mWarningIconView.setVisibility(View.VISIBLE);
110             updateIconState();
111             queueAnimateMessage();
112         }
113 
114         /** Stop the warning icon animation. */
stopIconAnimation()115         void stopIconAnimation() {
116             // Increment the counter so the handler will ignore the next message.
117             mCount.incrementAndGet();
118             if (mWarningIconView != null) {
119                 mWarningIconView.setVisibility(View.GONE);
120             }
121         }
122 
123         /** Update the visibility of the warning icon. */
updateIconState()124         private void updateIconState() {
125             mWarningIconView.setImageAlpha(mWarningIconVisible ? 255 : 0);
126             mWarningIconView.invalidateDrawable(mWarningIcon);
127         }
128 
129         /** Queue a message to animate the warning icon. */
queueAnimateMessage()130         private void queueAnimateMessage() {
131             int msgWhat = mCount.incrementAndGet();
132             sendEmptyMessageDelayed(msgWhat, mWarningIconVisible ? WARNING_ICON_ON_DURATION_MSEC
133                     : WARNING_ICON_OFF_DURATION_MSEC);
134         }
135 
136         @Override
handleMessage(Message msg)137         public void handleMessage(Message msg) {
138             if (msg.what == mCount.get()) {
139                 mWarningIconVisible = !mWarningIconVisible;
140                 updateIconState();
141                 queueAnimateMessage();
142             }
143         }
144 
145         /**
146          * Initialize the Drawable and ImageView fields.
147          * @return true if successful; false if any field failed to initialize
148          */
initDrawableAndImageView()149         private boolean initDrawableAndImageView() {
150             if (mWarningIcon == null) {
151                 try {
152                     mWarningIcon = getResources().getDrawable(R.drawable.ic_warning_large);
153                 } catch (Resources.NotFoundException e) {
154                     Log.e(TAG, "warning icon resource not found", e);
155                     return false;
156                 }
157             }
158             if (mWarningIconView == null) {
159                 mWarningIconView = (ImageView) findViewById(R.id.icon);
160                 if (mWarningIconView != null) {
161                     mWarningIconView.setImageDrawable(mWarningIcon);
162                 } else {
163                     Log.e(TAG, "failed to get ImageView for warning icon");
164                     return false;
165                 }
166             }
167             return true;
168         }
169     }
170 
171     /**
172      * Handler to add {@code FLAG_KEEP_SCREEN_ON} for emergency alerts. After a short delay,
173      * remove the flag so the screen can turn off to conserve the battery.
174      */
175     private class ScreenOffHandler extends Handler {
176         /** Latest {@code message.what} value for detecting old messages. */
177         private final AtomicInteger mCount = new AtomicInteger();
178 
179         /** Package local constructor (called from outer class). */
ScreenOffHandler()180         ScreenOffHandler() {}
181 
182         /** Add screen on window flags and queue a delayed message to remove them later. */
startScreenOnTimer()183         void startScreenOnTimer() {
184             addWindowFlags();
185             int msgWhat = mCount.incrementAndGet();
186             removeMessages(msgWhat - 1);    // Remove previous message, if any.
187             sendEmptyMessageDelayed(msgWhat, KEEP_SCREEN_ON_DURATION_MSEC);
188             Log.d(TAG, "added FLAG_KEEP_SCREEN_ON, queued screen off message id " + msgWhat);
189         }
190 
191         /** Remove the screen on window flags and any queued screen off message. */
stopScreenOnTimer()192         void stopScreenOnTimer() {
193             removeMessages(mCount.get());
194             clearWindowFlags();
195         }
196 
197         /** Set the screen on window flags. */
addWindowFlags()198         private void addWindowFlags() {
199             getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
200                     | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
201         }
202 
203         /** Clear the screen on window flags. */
clearWindowFlags()204         private void clearWindowFlags() {
205             getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
206                     | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
207         }
208 
209         @Override
handleMessage(Message msg)210         public void handleMessage(Message msg) {
211             int msgWhat = msg.what;
212             if (msgWhat == mCount.get()) {
213                 clearWindowFlags();
214                 Log.d(TAG, "removed FLAG_KEEP_SCREEN_ON with id " + msgWhat);
215             } else {
216                 Log.e(TAG, "discarding screen off message with id " + msgWhat);
217             }
218         }
219     }
220 
221     @Override
onCreate(Bundle savedInstanceState)222     protected void onCreate(Bundle savedInstanceState) {
223         super.onCreate(savedInstanceState);
224 
225         final Window win = getWindow();
226 
227         // We use a custom title, so remove the standard dialog title bar
228         win.requestFeature(Window.FEATURE_NO_TITLE);
229 
230         // Full screen alerts display above the keyguard and when device is locked.
231         win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
232                 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
233                 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
234 
235         setFinishOnTouchOutside(false);
236 
237         // Initialize the view.
238         LayoutInflater inflater = LayoutInflater.from(this);
239         setContentView(inflater.inflate(R.layout.cell_broadcast_alert, null));
240 
241         findViewById(R.id.dismissButton).setOnClickListener(
242                 new Button.OnClickListener() {
243                     @Override
244                     public void onClick(View v) {
245                         dismiss();
246                     }
247                 });
248 
249         // Get message list from saved Bundle or from Intent.
250         if (savedInstanceState != null) {
251             Log.d(TAG, "onCreate getting message list from saved instance state");
252             mMessageList = savedInstanceState.getParcelableArrayList(
253                     CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA);
254         } else {
255             Log.d(TAG, "onCreate getting message list from intent");
256             Intent intent = getIntent();
257             mMessageList = intent.getParcelableArrayListExtra(
258                     CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA);
259 
260             // If we were started from a notification, dismiss it.
261             clearNotification(intent);
262         }
263 
264         if (mMessageList == null || mMessageList.size() == 0) {
265             Log.e(TAG, "onCreate failed as message list is null or empty");
266             finish();
267         } else {
268             Log.d(TAG, "onCreate loaded message list of size " + mMessageList.size());
269         }
270 
271         // For emergency alerts, keep screen on so the user can read it
272         CellBroadcastMessage message = getLatestMessage();
273         if (message != null && CellBroadcastAlertService.
274                 isEmergencyMessage(this, message)) {
275             Log.d(TAG, "onCreate setting screen on timer for emergency alert");
276             mScreenOffHandler.startScreenOnTimer();
277         }
278 
279         updateAlertText(message);
280     }
281 
282     /**
283      * Start animating warning icon.
284      */
285     @Override
onResume()286     protected void onResume() {
287         super.onResume();
288         CellBroadcastMessage message = getLatestMessage();
289         if (message != null && CellBroadcastAlertService.
290                 isEmergencyMessage(this, message)) {
291             mAnimationHandler.startIconAnimation();
292         }
293     }
294 
295     /**
296      * Stop animating warning icon.
297      */
298     @Override
onPause()299     protected void onPause() {
300         Log.d(TAG, "onPause called");
301         mAnimationHandler.stopIconAnimation();
302         super.onPause();
303     }
304 
305     @Override
onStop()306     protected void onStop() {
307         super.onStop();
308         // When the activity goes in background eg. clicking Home button, send notification.
309         // Avoid doing this when activity will be recreated because of orientation change or if
310         // screen goes off
311         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
312         if (!(isChangingConfigurations() || getLatestMessage() == null) && pm.isScreenOn()) {
313             CellBroadcastAlertService.addToNotificationBar(getLatestMessage(), mMessageList,
314                     getApplicationContext(), true);
315         }
316     }
317 
318     /** Returns the currently displayed message. */
getLatestMessage()319     CellBroadcastMessage getLatestMessage() {
320         int index = mMessageList.size() - 1;
321         if (index >= 0) {
322             return mMessageList.get(index);
323         } else {
324             Log.d(TAG, "getLatestMessage returns null");
325             return null;
326         }
327     }
328 
329     /** Removes and returns the currently displayed message. */
removeLatestMessage()330     private CellBroadcastMessage removeLatestMessage() {
331         int index = mMessageList.size() - 1;
332         if (index >= 0) {
333             return mMessageList.remove(index);
334         } else {
335             return null;
336         }
337     }
338 
339     /**
340      * Save the list of messages so the state can be restored later.
341      * @param outState Bundle in which to place the saved state.
342      */
343     @Override
onSaveInstanceState(Bundle outState)344     protected void onSaveInstanceState(Bundle outState) {
345         super.onSaveInstanceState(outState);
346         outState.putParcelableArrayList(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA, mMessageList);
347     }
348 
349     /**
350      * Update alert text when a new emergency alert arrives.
351      * @param message CB message which is used to update alert text.
352      */
updateAlertText(CellBroadcastMessage message)353     private void updateAlertText(CellBroadcastMessage message) {
354         int titleId = CellBroadcastResources.getDialogTitleResource(
355                 getApplicationContext(), message);
356         setTitle(titleId);
357         ((TextView) findViewById(R.id.alertTitle)).setText(titleId);
358         ((TextView) findViewById(R.id.message)).setText(message.getMessageBody());
359 
360         String dismissButtonText = getApplicationContext().getResources()
361                 .getString(R.string.button_dismiss);
362 
363         if (mMessageList.size() > 1) {
364             dismissButtonText += "  (1/" + mMessageList.size() + ")";
365         }
366 
367         ((TextView) findViewById(R.id.dismissButton)).setText(dismissButtonText);
368         // Set alert reminder depending on user preference
369         CellBroadcastAlertReminder.queueAlertReminder(this, true);
370     }
371 
372     /**
373      * Called by {@link CellBroadcastAlertService} to add a new alert to the stack.
374      * @param intent The new intent containing one or more {@link CellBroadcastMessage}s.
375      */
376     @Override
onNewIntent(Intent intent)377     protected void onNewIntent(Intent intent) {
378         ArrayList<CellBroadcastMessage> newMessageList = intent.getParcelableArrayListExtra(
379                 CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA);
380         if (newMessageList != null) {
381             if (intent.getBooleanExtra(FROM_SAVE_STATE_NOTIFICATION_EXTRA, false)) {
382                 mMessageList = newMessageList;
383             } else {
384                 mMessageList.addAll(newMessageList);
385             }
386             Log.d(TAG, "onNewIntent called with message list of size " + newMessageList.size());
387             updateAlertText(getLatestMessage());
388             // If the new intent was sent from a notification, dismiss it.
389             clearNotification(intent);
390         } else {
391             Log.e(TAG, "onNewIntent called without SMS_CB_MESSAGE_EXTRA, ignoring");
392         }
393     }
394 
395     /**
396      * Try to cancel any notification that may have started this activity.
397      * @param intent Intent containing extras used to identify if notification needs to be cleared
398      */
clearNotification(Intent intent)399     private void clearNotification(Intent intent) {
400         if (intent.getBooleanExtra(FROM_NOTIFICATION_EXTRA, false)) {
401             NotificationManager notificationManager =
402                     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
403             notificationManager.cancel(CellBroadcastAlertService.NOTIFICATION_ID);
404             CellBroadcastReceiverApp.clearNewMessageList();
405         }
406     }
407 
408     /**
409      * Stop animating warning icon and stop the {@link CellBroadcastAlertAudio}
410      * service if necessary.
411      */
dismiss()412     void dismiss() {
413         Log.d(TAG, "dismiss");
414         // Stop playing alert sound/vibration/speech (if started)
415         stopService(new Intent(this, CellBroadcastAlertAudio.class));
416 
417         // Cancel any pending alert reminder
418         CellBroadcastAlertReminder.cancelAlertReminder();
419 
420         // Remove the current alert message from the list.
421         CellBroadcastMessage lastMessage = removeLatestMessage();
422         if (lastMessage == null) {
423             Log.e(TAG, "dismiss() called with empty message list!");
424             finish();
425             return;
426         }
427 
428         // Mark the alert as read.
429         final long deliveryTime = lastMessage.getDeliveryTime();
430 
431         // Mark broadcast as read on a background thread.
432         new CellBroadcastContentProvider.AsyncCellBroadcastTask(getContentResolver())
433                 .execute(new CellBroadcastContentProvider.CellBroadcastOperation() {
434                     @Override
435                     public boolean execute(CellBroadcastContentProvider provider) {
436                         return provider.markBroadcastRead(
437                                 Telephony.CellBroadcasts.DELIVERY_TIME, deliveryTime);
438                     }
439                 });
440 
441         // Set the opt-out dialog flag if this is a CMAS alert (other than Presidential Alert).
442         if (lastMessage.isCmasMessage() && lastMessage.getCmasMessageClass() !=
443                 SmsCbCmasInfo.CMAS_CLASS_PRESIDENTIAL_LEVEL_ALERT) {
444             mShowOptOutDialog = true;
445         }
446 
447         // If there are older emergency alerts to display, update the alert text and return.
448         CellBroadcastMessage nextMessage = getLatestMessage();
449         if (nextMessage != null) {
450             updateAlertText(nextMessage);
451             if (CellBroadcastAlertService.isEmergencyMessage(
452                     this, nextMessage)) {
453                 mAnimationHandler.startIconAnimation();
454             } else {
455                 mAnimationHandler.stopIconAnimation();
456             }
457             return;
458         }
459 
460         // Remove pending screen-off messages (animation messages are removed in onPause()).
461         mScreenOffHandler.stopScreenOnTimer();
462 
463         // Show opt-in/opt-out dialog when the first CMAS alert is received.
464         if (mShowOptOutDialog) {
465             SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
466             if (prefs.getBoolean(CellBroadcastSettings.KEY_SHOW_CMAS_OPT_OUT_DIALOG, true)) {
467                 // Clear the flag so the user will only see the opt-out dialog once.
468                 prefs.edit().putBoolean(CellBroadcastSettings.KEY_SHOW_CMAS_OPT_OUT_DIALOG, false)
469                         .apply();
470 
471                 KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
472                 if (km.inKeyguardRestrictedInputMode()) {
473                     Log.d(TAG, "Showing opt-out dialog in new activity (secure keyguard)");
474                     Intent intent = new Intent(this, CellBroadcastOptOutActivity.class);
475                     startActivity(intent);
476                 } else {
477                     Log.d(TAG, "Showing opt-out dialog in current activity");
478                     CellBroadcastOptOutActivity.showOptOutDialog(this);
479                     return; // don't call finish() until user dismisses the dialog
480                 }
481             }
482         }
483         NotificationManager notificationManager =
484                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
485         notificationManager.cancel(CellBroadcastAlertService.NOTIFICATION_ID);
486         finish();
487     }
488 
489     @Override
dispatchKeyEvent(KeyEvent event)490     public boolean dispatchKeyEvent(KeyEvent event) {
491         CellBroadcastMessage message = getLatestMessage();
492         if (message != null && !message.isEtwsMessage()) {
493             switch (event.getKeyCode()) {
494                 // Volume keys and camera keys mute the alert sound/vibration (except ETWS).
495                 case KeyEvent.KEYCODE_VOLUME_UP:
496                 case KeyEvent.KEYCODE_VOLUME_DOWN:
497                 case KeyEvent.KEYCODE_VOLUME_MUTE:
498                 case KeyEvent.KEYCODE_CAMERA:
499                 case KeyEvent.KEYCODE_FOCUS:
500                     // Stop playing alert sound/vibration/speech (if started)
501                     stopService(new Intent(this, CellBroadcastAlertAudio.class));
502                     return true;
503 
504                 default:
505                     break;
506             }
507         }
508         return super.dispatchKeyEvent(event);
509     }
510 
511     @Override
onBackPressed()512     public void onBackPressed() {
513         // Disable back key
514     }
515 }
516