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