• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.Notification;
20 import android.app.NotificationManager;
21 import android.app.PendingIntent;
22 import android.app.Service;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.os.Bundle;
27 import android.os.IBinder;
28 import android.preference.PreferenceManager;
29 import android.provider.Telephony;
30 import android.telephony.SmsCbConstants;
31 import android.telephony.SmsCbMessage;
32 import android.util.Log;
33 
34 /**
35  * This service manages the display and animation of broadcast messages.
36  * Emergency messages display with a flashing animated exclamation mark icon,
37  * and an alert tone is played when the alert is first shown to the user
38  * (but not when the user views a previously received broadcast).
39  */
40 public class CellBroadcastAlertService extends Service {
41     private static final String TAG = "CellBroadcastAlertService";
42 
43     /** Identifier for notification ID extra. */
44     public static final String SMS_CB_NOTIFICATION_ID_EXTRA =
45             "com.android.cellbroadcastreceiver.SMS_CB_NOTIFICATION_ID";
46 
47     @Override
onStartCommand(Intent intent, int flags, int startId)48     public int onStartCommand(Intent intent, int flags, int startId) {
49         String action = intent.getAction();
50         if (Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION.equals(action) ||
51                 Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION.equals(action)) {
52             handleCellBroadcastIntent(intent);
53         } else {
54             Log.e(TAG, "Unrecognized intent action: " + action);
55         }
56         stopSelf(); // this service always stops after processing the intent
57         return START_NOT_STICKY;
58     }
59 
handleCellBroadcastIntent(Intent intent)60     private void handleCellBroadcastIntent(Intent intent) {
61         Bundle extras = intent.getExtras();
62         if (extras == null) {
63             Log.e(TAG, "received SMS_CB_RECEIVED_ACTION with no extras!");
64             return;
65         }
66 
67         Object[] pdus = (Object[]) extras.get("pdus");
68 
69         if (pdus == null || pdus.length < 1) {
70             Log.e(TAG, "received SMS_CB_RECEIVED_ACTION with no pdus");
71             return;
72         }
73 
74         // create message from first PDU
75         SmsCbMessage message = SmsCbMessage.createFromPdu((byte[]) pdus[0]);
76         if (message == null) {
77             Log.e(TAG, "failed to create SmsCbMessage from PDU: " + pdus[0]);
78             return;
79         }
80 
81         // append message bodies from any additional PDUs (GSM only)
82         for (int i = 1; i < pdus.length; i++) {
83             SmsCbMessage nextPage = SmsCbMessage.createFromPdu((byte[]) pdus[i]);
84             if (nextPage != null) {
85                 message.appendToBody(nextPage.getMessageBody());
86             } else {
87                 Log.w(TAG, "failed to append to SmsCbMessage from PDU: " + pdus[i]);
88                 // continue so we can show the first page of the broadcast
89             }
90         }
91 
92         final CellBroadcastMessage cbm = new CellBroadcastMessage(message);
93         if (!isMessageEnabledByUser(cbm)) {
94             Log.d(TAG, "ignoring alert of type " + cbm.getMessageIdentifier() +
95                     " by user preference");
96             return;
97         }
98 
99         // add notification to the bar
100         addToNotificationBar(cbm);
101         if (cbm.isEmergencyAlertMessage()) {
102             // start audio/vibration/speech service for emergency alerts
103             Intent audioIntent = new Intent(this, CellBroadcastAlertAudio.class);
104             audioIntent.setAction(CellBroadcastAlertAudio.ACTION_START_ALERT_AUDIO);
105             SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
106             String duration = prefs.getString(CellBroadcastSettings.KEY_ALERT_SOUND_DURATION,
107                     CellBroadcastSettings.ALERT_SOUND_DEFAULT_DURATION);
108             audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_DURATION_EXTRA,
109                     Integer.parseInt(duration));
110 
111             if (prefs.getBoolean(CellBroadcastSettings.KEY_ENABLE_ALERT_SPEECH, true)) {
112                 audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_BODY,
113                         cbm.getMessageBody());
114 
115                 String language = cbm.getLanguageCode();
116                 if (cbm.isEtwsMessage() && !"ja".equals(language)) {
117                     Log.w(TAG, "bad language code for ETWS - using Japanese TTS");
118                     language = "ja";
119                 } else if (cbm.isCmasMessage() && !"en".equals(language)) {
120                     Log.w(TAG, "bad language code for CMAS - using English TTS");
121                     language = "en";
122                 }
123                 audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_LANGUAGE,
124                         language);
125             }
126             startService(audioIntent);
127         }
128         // write to database on a separate service thread
129         Intent dbWriteIntent = new Intent(this, CellBroadcastDatabaseService.class);
130         dbWriteIntent.setAction(CellBroadcastDatabaseService.ACTION_INSERT_NEW_BROADCAST);
131         dbWriteIntent.putExtra(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA, cbm);
132         startService(dbWriteIntent);
133     }
134 
135     /**
136      * Filter out broadcasts on the test channels that the user has not enabled,
137      * and types of notifications that the user is not interested in receiving.
138      * This allows us to enable an entire range of message identifiers in the
139      * radio and not have to explicitly disable the message identifiers for
140      * test broadcasts. In the unlikely event that the default shared preference
141      * values were not initialized in CellBroadcastReceiverApp, the second parameter
142      * to the getBoolean() calls match the default values in res/xml/preferences.xml.
143      *
144      * @param message the message to check
145      * @return true if the user has enabled this message type; false otherwise
146      */
isMessageEnabledByUser(CellBroadcastMessage message)147     private boolean isMessageEnabledByUser(CellBroadcastMessage message) {
148         switch (message.getMessageIdentifier()) {
149             case SmsCbConstants.MESSAGE_ID_ETWS_TEST_MESSAGE:
150                 return PreferenceManager.getDefaultSharedPreferences(this)
151                         .getBoolean(CellBroadcastSettings.KEY_ENABLE_ETWS_TEST_ALERTS, false);
152 
153             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_OBSERVED:
154             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_LIKELY:
155             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_OBSERVED:
156             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_LIKELY:
157             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_IMMEDIATE_OBSERVED:
158             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_IMMEDIATE_LIKELY:
159             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_OBSERVED:
160             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_LIKELY:
161                 return PreferenceManager.getDefaultSharedPreferences(this)
162                         .getBoolean(CellBroadcastSettings.KEY_ENABLE_CMAS_IMMINENT_THREAT_ALERTS,
163                                 true);
164 
165             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY:
166                 return PreferenceManager.getDefaultSharedPreferences(this)
167                         .getBoolean(CellBroadcastSettings.KEY_ENABLE_CMAS_AMBER_ALERTS, false);
168 
169             case SmsCbConstants.MESSAGE_ID_CMAS_ALERT_REQUIRED_MONTHLY_TEST:
170                 return PreferenceManager.getDefaultSharedPreferences(this)
171                         .getBoolean(CellBroadcastSettings.KEY_ENABLE_CMAS_TEST_ALERTS, false);
172 
173             default:
174                 return true;
175         }
176     }
177 
addToNotificationBar(CellBroadcastMessage message)178     private void addToNotificationBar(CellBroadcastMessage message) {
179         int channelTitleId = message.getDialogTitleResource();
180         CharSequence channelName = getText(channelTitleId);
181         String messageBody = message.getMessageBody();
182 
183         Notification notification = new Notification(R.drawable.stat_color_warning,
184                 channelName, System.currentTimeMillis());
185 
186         int notificationId = CellBroadcastReceiverApp.getCellBroadcastReceiverApp()
187                 .getNextNotificationId();
188 
189         PendingIntent pi = PendingIntent.getActivity(this, 0, createDisplayMessageIntent(
190                 this, message, notificationId), 0);
191 
192         notification.setLatestEventInfo(this, channelName, messageBody, pi);
193 
194         if (message.isEmergencyAlertMessage()) {
195             // Emergency: open notification immediately
196             notification.fullScreenIntent = pi;
197             // use default notification lights (CellBroadcastAlertAudio plays sound/vibration)
198             notification.defaults = Notification.DEFAULT_LIGHTS;
199         } else {
200             // use default sound/vibration/lights for non-emergency broadcasts
201             notification.defaults = Notification.DEFAULT_ALL;
202         }
203 
204         Log.i(TAG, "addToNotificationBar notificationId: " + notificationId);
205 
206         NotificationManager notificationManager =
207             (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
208 
209         notificationManager.notify(notificationId, notification);
210     }
211 
createDisplayMessageIntent(Context context, CellBroadcastMessage message, int notificationId)212     static Intent createDisplayMessageIntent(Context context,
213             CellBroadcastMessage message, int notificationId) {
214         // Trigger the list activity to fire up a dialog that shows the received messages
215         Intent intent = new Intent(context, CellBroadcastListActivity.class);
216         intent.putExtra(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA, message);
217         intent.putExtra(SMS_CB_NOTIFICATION_ID_EXTRA, notificationId);
218 
219         // This line is needed to make this intent compare differently than the other intents
220         // created here for other messages. Without this line, the PendingIntent always gets the
221         // intent of a previous message and notification.
222         intent.setType(Integer.toString(notificationId));
223 
224         return intent;
225     }
226 
227     @Override
onBind(Intent intent)228     public IBinder onBind(Intent intent) {
229         return null;    // clients can't bind to this service
230     }
231 }
232