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