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