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.content.res.Resources;
     21 import android.os.Bundle;
     22 import android.preference.ListPreference;
     23 import android.preference.Preference;
     24 import android.preference.PreferenceActivity;
     25 import android.preference.PreferenceCategory;
     26 import android.preference.PreferenceFragment;
     27 import android.preference.PreferenceScreen;
     28 import android.provider.Settings;
     29 import android.telephony.TelephonyManager;
     30 
     31 /**
     32  * Settings activity for the cell broadcast receiver.
     33  */
     34 public class CellBroadcastSettings extends PreferenceActivity {
     35 
     36     // Preference key for whether to enable emergency notifications (default enabled).
     37     public static final String KEY_ENABLE_EMERGENCY_ALERTS = "enable_emergency_alerts";
     38 
     39     // Duration of alert sound (in seconds).
     40     public static final String KEY_ALERT_SOUND_DURATION = "alert_sound_duration";
     41 
     42     // Default alert duration (in seconds).
     43     public static final String ALERT_SOUND_DEFAULT_DURATION = "4";
     44 
     45     // Enable vibration on alert (unless master volume is silent).
     46     public static final String KEY_ENABLE_ALERT_VIBRATE = "enable_alert_vibrate";
     47 
     48     // Speak contents of alert after playing the alert sound.
     49     public static final String KEY_ENABLE_ALERT_SPEECH = "enable_alert_speech";
     50 
     51     // Preference category for emergency alert and CMAS settings.
     52     public static final String KEY_CATEGORY_ALERT_SETTINGS = "category_alert_settings";
     53 
     54     // Preference category for ETWS related settings.
     55     public static final String KEY_CATEGORY_ETWS_SETTINGS = "category_etws_settings";
     56 
     57     // Whether to display CMAS extreme threat notifications (default is enabled).
     58     public static final String KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS =
     59             "enable_cmas_extreme_threat_alerts";
     60 
     61     // Whether to display CMAS severe threat notifications (default is enabled).
     62     public static final String KEY_ENABLE_CMAS_SEVERE_THREAT_ALERTS =
     63             "enable_cmas_severe_threat_alerts";
     64 
     65     // Whether to display CMAS amber alert messages (default is enabled).
     66     public static final String KEY_ENABLE_CMAS_AMBER_ALERTS = "enable_cmas_amber_alerts";
     67 
     68     // Preference category for development settings (enabled by settings developer options toggle).
     69     public static final String KEY_CATEGORY_DEV_SETTINGS = "category_dev_settings";
     70 
     71     // Whether to display ETWS test messages (default is disabled).
     72     public static final String KEY_ENABLE_ETWS_TEST_ALERTS = "enable_etws_test_alerts";
     73 
     74     // Whether to display CMAS monthly test messages (default is disabled).
     75     public static final String KEY_ENABLE_CMAS_TEST_ALERTS = "enable_cmas_test_alerts";
     76 
     77     // Preference category for Brazil specific settings.
     78     public static final String KEY_CATEGORY_BRAZIL_SETTINGS = "category_brazil_settings";
     79 
     80     // Preference key for whether to enable channel 50 notifications
     81     // Enabled by default for phones sold in Brazil, otherwise this setting may be hidden.
     82     public static final String KEY_ENABLE_CHANNEL_50_ALERTS = "enable_channel_50_alerts";
     83 
     84     // Preference key for initial opt-in/opt-out dialog.
     85     public static final String KEY_SHOW_CMAS_OPT_OUT_DIALOG = "show_cmas_opt_out_dialog";
     86 
     87     // Alert reminder interval ("once" = single 2 minute reminder).
     88     public static final String KEY_ALERT_REMINDER_INTERVAL = "alert_reminder_interval";
     89 
     90     @Override
     91     public void onCreate(Bundle savedInstanceState) {
     92         super.onCreate(savedInstanceState);
     93 
     94         // Display the fragment as the main content.
     95         getFragmentManager().beginTransaction().replace(android.R.id.content,
     96                 new CellBroadcastSettingsFragment()).commit();
     97     }
     98 
     99     /**
    100      * New fragment-style implementation of preferences.
    101      */
    102     public static class CellBroadcastSettingsFragment extends PreferenceFragment {
    103 
    104         @Override
    105         public void onCreate(Bundle savedInstanceState) {
    106             super.onCreate(savedInstanceState);
    107 
    108             // Load the preferences from an XML resource
    109             addPreferencesFromResource(R.xml.preferences);
    110 
    111             PreferenceScreen preferenceScreen = getPreferenceScreen();
    112 
    113             // Handler for settings that require us to reconfigure enabled channels in radio
    114             Preference.OnPreferenceChangeListener startConfigServiceListener =
    115                     new Preference.OnPreferenceChangeListener() {
    116                         @Override
    117                         public boolean onPreferenceChange(Preference pref, Object newValue) {
    118                             CellBroadcastReceiver.startConfigService(pref.getContext());
    119                             return true;
    120                         }
    121                     };
    122 
    123             // Show extra settings when developer options is enabled in settings.
    124             boolean enableDevSettings = Settings.Global.getInt(getActivity().getContentResolver(),
    125                     Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
    126 
    127             Resources res = getResources();
    128             boolean showEtwsSettings = res.getBoolean(R.bool.show_etws_settings);
    129 
    130             // Emergency alert preference category (general and CMAS preferences).
    131             PreferenceCategory alertCategory = (PreferenceCategory)
    132                     findPreference(KEY_CATEGORY_ALERT_SETTINGS);
    133 
    134             // alert reminder interval
    135             ListPreference interval = (ListPreference) findPreference(KEY_ALERT_REMINDER_INTERVAL);
    136             interval.setSummary(interval.getEntry());
    137             interval.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
    138                 @Override
    139                 public boolean onPreferenceChange(Preference pref, Object newValue) {
    140                     final ListPreference listPref = (ListPreference) pref;
    141                     final int idx = listPref.findIndexOfValue((String) newValue);
    142                     listPref.setSummary(listPref.getEntries()[idx]);
    143                     return true;
    144                 }
    145             });
    146 
    147             // Show alert settings and ETWS categories for ETWS builds and developer mode.
    148             if (enableDevSettings || showEtwsSettings) {
    149                 // enable/disable all alerts
    150                 Preference enablePwsAlerts = findPreference(KEY_ENABLE_EMERGENCY_ALERTS);
    151                 if (enablePwsAlerts != null) {
    152                     enablePwsAlerts.setOnPreferenceChangeListener(startConfigServiceListener);
    153                 }
    154 
    155                 // alert sound duration
    156                 ListPreference duration = (ListPreference) findPreference(KEY_ALERT_SOUND_DURATION);
    157                 duration.setSummary(duration.getEntry());
    158                 duration.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
    159                     @Override
    160                     public boolean onPreferenceChange(Preference pref, Object newValue) {
    161                         final ListPreference listPref = (ListPreference) pref;
    162                         final int idx = listPref.findIndexOfValue((String) newValue);
    163                         listPref.setSummary(listPref.getEntries()[idx]);
    164                         return true;
    165                     }
    166                 });
    167             } else {
    168                 // Remove general emergency alert preference items (not shown for CMAS builds).
    169                 alertCategory.removePreference(findPreference(KEY_ENABLE_EMERGENCY_ALERTS));
    170                 alertCategory.removePreference(findPreference(KEY_ALERT_SOUND_DURATION));
    171                 alertCategory.removePreference(findPreference(KEY_ENABLE_ALERT_SPEECH));
    172                 // Remove ETWS preference category.
    173                 preferenceScreen.removePreference(findPreference(KEY_CATEGORY_ETWS_SETTINGS));
    174             }
    175 
    176             if (!res.getBoolean(R.bool.show_cmas_settings)) {
    177                 // Remove CMAS preference items in emergency alert category.
    178                 alertCategory.removePreference(
    179                         findPreference(KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS));
    180                 alertCategory.removePreference(
    181                         findPreference(KEY_ENABLE_CMAS_SEVERE_THREAT_ALERTS));
    182                 alertCategory.removePreference(findPreference(KEY_ENABLE_CMAS_AMBER_ALERTS));
    183             }
    184 
    185             TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(
    186                     Context.TELEPHONY_SERVICE);
    187 
    188             boolean enableChannel50Support = res.getBoolean(R.bool.show_brazil_settings) ||
    189                     "br".equals(tm.getSimCountryIso());
    190 
    191             if (!enableChannel50Support) {
    192                 preferenceScreen.removePreference(findPreference(KEY_CATEGORY_BRAZIL_SETTINGS));
    193             }
    194             if (!enableDevSettings) {
    195                 preferenceScreen.removePreference(findPreference(KEY_CATEGORY_DEV_SETTINGS));
    196             }
    197 
    198             Preference enableChannel50Alerts = findPreference(KEY_ENABLE_CHANNEL_50_ALERTS);
    199             if (enableChannel50Alerts != null) {
    200                 enableChannel50Alerts.setOnPreferenceChangeListener(startConfigServiceListener);
    201             }
    202             Preference enableEtwsAlerts = findPreference(KEY_ENABLE_ETWS_TEST_ALERTS);
    203             if (enableEtwsAlerts != null) {
    204                 enableEtwsAlerts.setOnPreferenceChangeListener(startConfigServiceListener);
    205             }
    206             Preference enableCmasExtremeAlerts =
    207                     findPreference(KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS);
    208             if (enableCmasExtremeAlerts != null) {
    209                 enableCmasExtremeAlerts.setOnPreferenceChangeListener(startConfigServiceListener);
    210             }
    211             Preference enableCmasSevereAlerts =
    212                     findPreference(KEY_ENABLE_CMAS_SEVERE_THREAT_ALERTS);
    213             if (enableCmasSevereAlerts != null) {
    214                 enableCmasSevereAlerts.setOnPreferenceChangeListener(startConfigServiceListener);
    215             }
    216             Preference enableCmasAmberAlerts = findPreference(KEY_ENABLE_CMAS_AMBER_ALERTS);
    217             if (enableCmasAmberAlerts != null) {
    218                 enableCmasAmberAlerts.setOnPreferenceChangeListener(startConfigServiceListener);
    219             }
    220             Preference enableCmasTestAlerts = findPreference(KEY_ENABLE_CMAS_TEST_ALERTS);
    221             if (enableCmasTestAlerts != null) {
    222                 enableCmasTestAlerts.setOnPreferenceChangeListener(startConfigServiceListener);
    223             }
    224         }
    225     }
    226 }
    227