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.tests;
18 
19 import android.Manifest;
20 import android.app.Activity;
21 import android.app.AppOpsManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.UserHandle;
25 import android.provider.Telephony;
26 import android.telephony.SmsCbCmasInfo;
27 import android.telephony.SmsCbLocation;
28 import android.telephony.SmsCbMessage;
29 import android.text.TextUtils;
30 
31 import com.android.internal.telephony.CellBroadcastUtils;
32 import com.android.internal.telephony.cdma.sms.SmsEnvelope;
33 
34 /**
35  * Send some test CDMA CMAS warning notifications.
36  */
37 public class SendCdmaCmasMessages {
38 
39     private static final String PRES_ALERT =
40             "THE PRESIDENT HAS ISSUED AN EMERGENCY ALERT. CHECK LOCAL MEDIA FOR MORE DETAILS";
41 
42     private static final String EXTREME_ALERT = "FLASH FLOOD WARNING FOR SOUTH COCONINO COUNTY"
43             + " - NORTH CENTRAL ARIZONA UNTIL 415 PM MST";
44 
45     private static final String SEVERE_ALERT = "SEVERE WEATHER WARNING FOR SOMERSET COUNTY"
46             + " - NEW JERSEY UNTIL 415 PM MST";
47 
48     private static final String AMBER_ALERT =
49             "AMBER ALERT:Mountain View,CA VEH'07 Blue Honda Civic CA LIC 5ABC123. "
50                     + "Check https://www.amberalert.gov/active.htm for more information.";
51 
52     private static final String MONTHLY_TEST_ALERT = "This is a test of the emergency alert system."
53             + " This is only a test. Call (123)456-7890.";
54 
sendBroadcast(Context context, SmsCbMessage cbMessage)55     private static void sendBroadcast(Context context, SmsCbMessage cbMessage) {
56         Intent intent = new Intent(Telephony.Sms.Intents.ACTION_SMS_EMERGENCY_CB_RECEIVED);
57         intent.putExtra("message", cbMessage);
58         intent.setPackage(CellBroadcastUtils.getDefaultCellBroadcastReceiverPackageName(context));
59         context.sendOrderedBroadcastAsUser(intent, UserHandle.ALL,
60                 Manifest.permission.RECEIVE_EMERGENCY_BROADCAST,
61                 AppOpsManager.OP_RECEIVE_EMERGECY_SMS, null, null, Activity.RESULT_OK, null, null);
62     }
63 
testSendCmasPresAlert(Context context, int serialNumber, String messageBody, String languageCode)64     public static void testSendCmasPresAlert(Context context, int serialNumber,
65             String messageBody, String languageCode) {
66         SmsCbMessage cbMessage = createCmasSmsMessage(
67                 SmsEnvelope.SERVICE_CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT, serialNumber,
68                 TextUtils.isEmpty(languageCode) ? "en" : languageCode,
69                 TextUtils.isEmpty(messageBody) ? PRES_ALERT : messageBody,
70                 SmsCbCmasInfo.CMAS_CATEGORY_GEO,
71                 SmsCbCmasInfo.CMAS_RESPONSE_TYPE_PREPARE, SmsCbCmasInfo.CMAS_SEVERITY_EXTREME,
72                 SmsCbCmasInfo.CMAS_URGENCY_EXPECTED, SmsCbCmasInfo.CMAS_CERTAINTY_LIKELY);
73 
74         sendBroadcast(context, cbMessage);
75     }
76 
testSendCmasExtremeAlert(Context context, int serialNumber, String messageBody, String languageCode)77     public static void testSendCmasExtremeAlert(Context context, int serialNumber,
78             String messageBody, String languageCode) {
79         SmsCbMessage cbMessage = createCmasSmsMessage(
80                 SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT, serialNumber,
81                 TextUtils.isEmpty(languageCode) ? "en" : languageCode,
82                 TextUtils.isEmpty(messageBody) ? EXTREME_ALERT : messageBody,
83                 SmsCbCmasInfo.CMAS_CATEGORY_MET,
84                 SmsCbCmasInfo.CMAS_RESPONSE_TYPE_PREPARE, SmsCbCmasInfo.CMAS_SEVERITY_EXTREME,
85                 SmsCbCmasInfo.CMAS_URGENCY_EXPECTED, SmsCbCmasInfo.CMAS_CERTAINTY_OBSERVED);
86 
87         sendBroadcast(context, cbMessage);
88     }
89 
testSendCmasSevereAlert(Context context, int serialNumber, String messageBody, String languageCode)90     public static void testSendCmasSevereAlert(Context context, int serialNumber,
91             String messageBody, String languageCode) {
92         SmsCbMessage cbMessage = createCmasSmsMessage(
93                 SmsEnvelope.SERVICE_CATEGORY_CMAS_SEVERE_THREAT, serialNumber,
94                 TextUtils.isEmpty(languageCode) ? "en" : languageCode,
95                 TextUtils.isEmpty(messageBody) ? SEVERE_ALERT : messageBody,
96                 SmsCbCmasInfo.CMAS_CATEGORY_HEALTH,
97                 SmsCbCmasInfo.CMAS_RESPONSE_TYPE_AVOID, SmsCbCmasInfo.CMAS_SEVERITY_SEVERE,
98                 SmsCbCmasInfo.CMAS_URGENCY_IMMEDIATE, SmsCbCmasInfo.CMAS_CERTAINTY_LIKELY);
99 
100         sendBroadcast(context, cbMessage);
101     }
102 
testSendCmasAmberAlert(Context context, int serialNumber, String messageBody, String languageCode)103     public static void testSendCmasAmberAlert(Context context, int serialNumber,
104             String messageBody, String languageCode) {
105         SmsCbMessage cbMessage = createCmasSmsMessage(
106                 SmsEnvelope.SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY, serialNumber,
107                 TextUtils.isEmpty(languageCode) ? "en" : languageCode,
108                 TextUtils.isEmpty(messageBody) ? AMBER_ALERT : messageBody,
109                 SmsCbCmasInfo.CMAS_CATEGORY_UNKNOWN,
110                 SmsCbCmasInfo.CMAS_RESPONSE_TYPE_UNKNOWN, SmsCbCmasInfo.CMAS_SEVERITY_UNKNOWN,
111                 SmsCbCmasInfo.CMAS_URGENCY_UNKNOWN, SmsCbCmasInfo.CMAS_CERTAINTY_UNKNOWN);
112 
113         sendBroadcast(context, cbMessage);
114     }
115 
testSendCmasMonthlyTest(Context context, int serialNumber, String messageBody, String languageCode)116     public static void testSendCmasMonthlyTest(Context context, int serialNumber,
117             String messageBody, String languageCode) {
118         SmsCbMessage cbMessage = createCmasSmsMessage(
119                 SmsEnvelope.SERVICE_CATEGORY_CMAS_TEST_MESSAGE, serialNumber,
120                 TextUtils.isEmpty(languageCode) ? "en" : languageCode,
121                 TextUtils.isEmpty(messageBody) ? MONTHLY_TEST_ALERT : messageBody,
122                 SmsCbCmasInfo.CMAS_CATEGORY_UNKNOWN,
123                 SmsCbCmasInfo.CMAS_RESPONSE_TYPE_UNKNOWN, SmsCbCmasInfo.CMAS_SEVERITY_UNKNOWN,
124                 SmsCbCmasInfo.CMAS_URGENCY_UNKNOWN, SmsCbCmasInfo.CMAS_CERTAINTY_UNKNOWN);
125 
126         sendBroadcast(context, cbMessage);
127     }
128 
129     /**
130      * Create a new SmsCbMessage for testing CDMA CMAS support.
131      * @param serviceCategory the CDMA service category
132      * @param serialNumber the 16-bit message identifier
133      * @param language message language code
134      * @param body message body
135      * @param cmasCategory CMAS category (or -1 to skip adding CMAS type 1 elements record)
136      * @param responseType CMAS response type
137      * @param severity CMAS severity
138      * @param urgency CMAS urgency
139      * @param certainty CMAS certainty
140      * @return the newly created SmsMessage object
141      */
createCmasSmsMessage(int serviceCategory, int serialNumber, String language, String body, int cmasCategory, int responseType, int severity, int urgency, int certainty)142     private static SmsCbMessage createCmasSmsMessage(int serviceCategory, int serialNumber,
143             String language, String body, int cmasCategory, int responseType, int severity,
144             int urgency, int certainty) {
145         int cmasMessageClass = serviceCategoryToCmasMessageClass(serviceCategory);
146         SmsCbCmasInfo cmasInfo = new SmsCbCmasInfo(cmasMessageClass, cmasCategory, responseType,
147                 severity, urgency, certainty);
148         return new SmsCbMessage(SmsCbMessage.MESSAGE_FORMAT_3GPP2,
149                 SmsCbMessage.GEOGRAPHICAL_SCOPE_PLMN_WIDE, serialNumber,
150                 new SmsCbLocation("123456"), serviceCategory, language, body,
151                 SmsCbMessage.MESSAGE_PRIORITY_EMERGENCY, null, cmasInfo, 0, 1);
152     }
153 
154     /**
155      * Convert CDMA service category to CMAS message class. Copied from {@code BearerData}.
156      * @param serviceCategory CDMA service category
157      * @return CMAS message class
158      */
serviceCategoryToCmasMessageClass(int serviceCategory)159     private static int serviceCategoryToCmasMessageClass(int serviceCategory) {
160         switch (serviceCategory) {
161             case SmsEnvelope.SERVICE_CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT:
162                 return SmsCbCmasInfo.CMAS_CLASS_PRESIDENTIAL_LEVEL_ALERT;
163 
164             case SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT:
165                 return SmsCbCmasInfo.CMAS_CLASS_EXTREME_THREAT;
166 
167             case SmsEnvelope.SERVICE_CATEGORY_CMAS_SEVERE_THREAT:
168                 return SmsCbCmasInfo.CMAS_CLASS_SEVERE_THREAT;
169 
170             case SmsEnvelope.SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY:
171                 return SmsCbCmasInfo.CMAS_CLASS_CHILD_ABDUCTION_EMERGENCY;
172 
173             case SmsEnvelope.SERVICE_CATEGORY_CMAS_TEST_MESSAGE:
174                 return SmsCbCmasInfo.CMAS_CLASS_REQUIRED_MONTHLY_TEST;
175 
176             default:
177                 return SmsCbCmasInfo.CMAS_CLASS_UNKNOWN;
178         }
179     }
180 }
181