1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.cellbroadcastreceiver;
18
19import android.content.Context;
20import android.graphics.Typeface;
21import android.telephony.CellBroadcastMessage;
22import android.telephony.SmsCbCmasInfo;
23import android.telephony.SmsCbEtwsInfo;
24import android.text.Spannable;
25import android.text.SpannableStringBuilder;
26import android.text.style.StyleSpan;
27
28/**
29 * Returns the string resource ID's for CMAS and ETWS emergency alerts.
30 */
31public class CellBroadcastResources {
32
33    private CellBroadcastResources() {
34    }
35
36    /**
37     * Returns a styled CharSequence containing the message date/time and alert details.
38     * @param context a Context for resource string access
39     * @return a CharSequence for display in the broadcast alert dialog
40     */
41    public static CharSequence getMessageDetails(Context context, CellBroadcastMessage cbm) {
42        SpannableStringBuilder buf = new SpannableStringBuilder();
43
44        // Alert date/time
45        int start = buf.length();
46        buf.append(context.getString(R.string.delivery_time_heading));
47        int end = buf.length();
48        buf.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
49        buf.append(" ");
50        buf.append(cbm.getDateString(context));
51
52        if (cbm.isCmasMessage()) {
53            // CMAS category, response type, severity, urgency, certainty
54            appendCmasAlertDetails(context, buf, cbm.getCmasWarningInfo());
55        }
56
57        return buf;
58    }
59
60    private static void appendCmasAlertDetails(Context context, SpannableStringBuilder buf,
61            SmsCbCmasInfo cmasInfo) {
62        // CMAS category
63        int categoryId = getCmasCategoryResId(cmasInfo);
64        if (categoryId != 0) {
65            appendMessageDetail(context, buf, R.string.cmas_category_heading, categoryId);
66        }
67
68        // CMAS response type
69        int responseId = getCmasResponseResId(cmasInfo);
70        if (responseId != 0) {
71            appendMessageDetail(context, buf, R.string.cmas_response_heading, responseId);
72        }
73
74        // CMAS severity
75        int severityId = getCmasSeverityResId(cmasInfo);
76        if (severityId != 0) {
77            appendMessageDetail(context, buf, R.string.cmas_severity_heading, severityId);
78        }
79
80        // CMAS urgency
81        int urgencyId = getCmasUrgencyResId(cmasInfo);
82        if (urgencyId != 0) {
83            appendMessageDetail(context, buf, R.string.cmas_urgency_heading, urgencyId);
84        }
85
86        // CMAS certainty
87        int certaintyId = getCmasCertaintyResId(cmasInfo);
88        if (certaintyId != 0) {
89            appendMessageDetail(context, buf, R.string.cmas_certainty_heading, certaintyId);
90        }
91    }
92
93    private static void appendMessageDetail(Context context, SpannableStringBuilder buf,
94            int typeId, int valueId) {
95        if (buf.length() != 0) {
96            buf.append("\n");
97        }
98        int start = buf.length();
99        buf.append(context.getString(typeId));
100        int end = buf.length();
101        buf.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
102        buf.append(" ");
103        buf.append(context.getString(valueId));
104    }
105
106    /**
107     * Returns the string resource ID for the CMAS category.
108     * @return a string resource ID, or 0 if the CMAS category is unknown or not present
109     */
110    private static int getCmasCategoryResId(SmsCbCmasInfo cmasInfo) {
111        switch (cmasInfo.getCategory()) {
112            case SmsCbCmasInfo.CMAS_CATEGORY_GEO:
113                return R.string.cmas_category_geo;
114
115            case SmsCbCmasInfo.CMAS_CATEGORY_MET:
116                return R.string.cmas_category_met;
117
118            case SmsCbCmasInfo.CMAS_CATEGORY_SAFETY:
119                return R.string.cmas_category_safety;
120
121            case SmsCbCmasInfo.CMAS_CATEGORY_SECURITY:
122                return R.string.cmas_category_security;
123
124            case SmsCbCmasInfo.CMAS_CATEGORY_RESCUE:
125                return R.string.cmas_category_rescue;
126
127            case SmsCbCmasInfo.CMAS_CATEGORY_FIRE:
128                return R.string.cmas_category_fire;
129
130            case SmsCbCmasInfo.CMAS_CATEGORY_HEALTH:
131                return R.string.cmas_category_health;
132
133            case SmsCbCmasInfo.CMAS_CATEGORY_ENV:
134                return R.string.cmas_category_env;
135
136            case SmsCbCmasInfo.CMAS_CATEGORY_TRANSPORT:
137                return R.string.cmas_category_transport;
138
139            case SmsCbCmasInfo.CMAS_CATEGORY_INFRA:
140                return R.string.cmas_category_infra;
141
142            case SmsCbCmasInfo.CMAS_CATEGORY_CBRNE:
143                return R.string.cmas_category_cbrne;
144
145            case SmsCbCmasInfo.CMAS_CATEGORY_OTHER:
146                return R.string.cmas_category_other;
147
148            default:
149                return 0;
150        }
151    }
152
153    /**
154     * Returns the string resource ID for the CMAS response type.
155     * @return a string resource ID, or 0 if the CMAS response type is unknown or not present
156     */
157    private static int getCmasResponseResId(SmsCbCmasInfo cmasInfo) {
158        switch (cmasInfo.getResponseType()) {
159            case SmsCbCmasInfo.CMAS_RESPONSE_TYPE_SHELTER:
160                return R.string.cmas_response_shelter;
161
162            case SmsCbCmasInfo.CMAS_RESPONSE_TYPE_EVACUATE:
163                return R.string.cmas_response_evacuate;
164
165            case SmsCbCmasInfo.CMAS_RESPONSE_TYPE_PREPARE:
166                return R.string.cmas_response_prepare;
167
168            case SmsCbCmasInfo.CMAS_RESPONSE_TYPE_EXECUTE:
169                return R.string.cmas_response_execute;
170
171            case SmsCbCmasInfo.CMAS_RESPONSE_TYPE_MONITOR:
172                return R.string.cmas_response_monitor;
173
174            case SmsCbCmasInfo.CMAS_RESPONSE_TYPE_AVOID:
175                return R.string.cmas_response_avoid;
176
177            case SmsCbCmasInfo.CMAS_RESPONSE_TYPE_ASSESS:
178                return R.string.cmas_response_assess;
179
180            case SmsCbCmasInfo.CMAS_RESPONSE_TYPE_NONE:
181                return R.string.cmas_response_none;
182
183            default:
184                return 0;
185        }
186    }
187
188    /**
189     * Returns the string resource ID for the CMAS severity.
190     * @return a string resource ID, or 0 if the CMAS severity is unknown or not present
191     */
192    private static int getCmasSeverityResId(SmsCbCmasInfo cmasInfo) {
193        switch (cmasInfo.getSeverity()) {
194            case SmsCbCmasInfo.CMAS_SEVERITY_EXTREME:
195                return R.string.cmas_severity_extreme;
196
197            case SmsCbCmasInfo.CMAS_SEVERITY_SEVERE:
198                return R.string.cmas_severity_severe;
199
200            default:
201                return 0;
202        }
203    }
204
205    /**
206     * Returns the string resource ID for the CMAS urgency.
207     * @return a string resource ID, or 0 if the CMAS urgency is unknown or not present
208     */
209    private static int getCmasUrgencyResId(SmsCbCmasInfo cmasInfo) {
210        switch (cmasInfo.getUrgency()) {
211            case SmsCbCmasInfo.CMAS_URGENCY_IMMEDIATE:
212                return R.string.cmas_urgency_immediate;
213
214            case SmsCbCmasInfo.CMAS_URGENCY_EXPECTED:
215                return R.string.cmas_urgency_expected;
216
217            default:
218                return 0;
219        }
220    }
221
222    /**
223     * Returns the string resource ID for the CMAS certainty.
224     * @return a string resource ID, or 0 if the CMAS certainty is unknown or not present
225     */
226    private static int getCmasCertaintyResId(SmsCbCmasInfo cmasInfo) {
227        switch (cmasInfo.getCertainty()) {
228            case SmsCbCmasInfo.CMAS_CERTAINTY_OBSERVED:
229                return R.string.cmas_certainty_observed;
230
231            case SmsCbCmasInfo.CMAS_CERTAINTY_LIKELY:
232                return R.string.cmas_certainty_likely;
233
234            default:
235                return 0;
236        }
237    }
238
239    public static int getDialogTitleResource(CellBroadcastMessage cbm) {
240        // ETWS warning types
241        SmsCbEtwsInfo etwsInfo = cbm.getEtwsWarningInfo();
242        if (etwsInfo != null) {
243            switch (etwsInfo.getWarningType()) {
244                case SmsCbEtwsInfo.ETWS_WARNING_TYPE_EARTHQUAKE:
245                    return R.string.etws_earthquake_warning;
246
247                case SmsCbEtwsInfo.ETWS_WARNING_TYPE_TSUNAMI:
248                    return R.string.etws_tsunami_warning;
249
250                case SmsCbEtwsInfo.ETWS_WARNING_TYPE_EARTHQUAKE_AND_TSUNAMI:
251                    return R.string.etws_earthquake_and_tsunami_warning;
252
253                case SmsCbEtwsInfo.ETWS_WARNING_TYPE_TEST_MESSAGE:
254                    return R.string.etws_test_message;
255
256                case SmsCbEtwsInfo.ETWS_WARNING_TYPE_OTHER_EMERGENCY:
257                default:
258                    return R.string.etws_other_emergency_type;
259            }
260        }
261
262        // CMAS warning types
263        SmsCbCmasInfo cmasInfo = cbm.getCmasWarningInfo();
264        if (cmasInfo != null) {
265            switch (cmasInfo.getMessageClass()) {
266                case SmsCbCmasInfo.CMAS_CLASS_PRESIDENTIAL_LEVEL_ALERT:
267                    return R.string.cmas_presidential_level_alert;
268
269                case SmsCbCmasInfo.CMAS_CLASS_EXTREME_THREAT:
270                    return R.string.cmas_extreme_alert;
271
272                case SmsCbCmasInfo.CMAS_CLASS_SEVERE_THREAT:
273                    return R.string.cmas_severe_alert;
274
275                case SmsCbCmasInfo.CMAS_CLASS_CHILD_ABDUCTION_EMERGENCY:
276                    return R.string.cmas_amber_alert;
277
278                case SmsCbCmasInfo.CMAS_CLASS_REQUIRED_MONTHLY_TEST:
279                    return R.string.cmas_required_monthly_test;
280
281                case SmsCbCmasInfo.CMAS_CLASS_CMAS_EXERCISE:
282                    return R.string.cmas_exercise_alert;
283
284                case SmsCbCmasInfo.CMAS_CLASS_OPERATOR_DEFINED_USE:
285                    return R.string.cmas_operator_defined_alert;
286
287                default:
288                    return R.string.pws_other_message_identifiers;
289            }
290        }
291
292        if (CellBroadcastConfigService.isEmergencyAlertMessage(cbm)) {
293            return R.string.pws_other_message_identifiers;
294        } else {
295            return R.string.cb_other_message_identifiers;
296        }
297    }
298}
299