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