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