Teleca 2b changes.
[android/platform/packages/apps/Phone.git] / src / com / android / phone / CellBroadcastSms.java
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 package com.android.phone;
19
20 import android.os.Bundle;
21 import android.preference.CheckBoxPreference;
22 import android.preference.ListPreference;
23 import android.preference.Preference;
24 import android.preference.PreferenceScreen;
25 import android.preference.PreferenceActivity;
26 import com.android.internal.telephony.Phone;
27 import com.android.internal.telephony.PhoneFactory;
28 import com.android.internal.telephony.RILConstants;
29
30 import android.os.AsyncResult;
31 import android.os.Handler;
32 import android.os.Message;
33 import android.util.Log;
34
35
36 /**
37  * List of Phone-specific settings screens.
38  */
39 public class CellBroadcastSms extends PreferenceActivity
40         implements Preference.OnPreferenceChangeListener{
41     // debug data
42     private static final String LOG_TAG = "CellBroadcastSms";
43     private static final boolean DBG = true;
44
45     //String keys for preference lookup
46     private static final String BUTTON_ENABLE_DISABLE_BC_SMS_KEY =
47         "button_enable_disable_cell_bc_sms";
48     private static final String LIST_LANGUAGE_KEY =
49         "list_language";
50     private static final String BUTTON_EMERGENCY_BROADCAST_KEY =
51         "button_emergency_broadcast";
52     private static final String BUTTON_ADMINISTRATIVE_KEY =
53         "button_administrative";
54     private static final String BUTTON_MAINTENANCE_KEY =
55         "button_maintenance";
56     private static final String BUTTON_LOCAL_WEATHER_KEY =
57         "button_local_weather";
58     private static final String BUTTON_ATR_KEY =
59         "button_atr";
60     private static final String BUTTON_LAFS_KEY =
61         "button_lafs";
62     private static final String BUTTON_RESTAURANTS_KEY =
63         "button_restaurants";
64     private static final String BUTTON_LODGINGS_KEY =
65         "button_lodgings";
66     private static final String BUTTON_RETAIL_DIRECTORY_KEY =
67         "button_retail_directory";
68     private static final String BUTTON_ADVERTISEMENTS_KEY =
69         "button_advertisements";
70     private static final String BUTTON_STOCK_QUOTES_KEY =
71         "button_stock_quotes";
72     private static final String BUTTON_EO_KEY =
73         "button_eo";
74     private static final String BUTTON_MHH_KEY =
75         "button_mhh";
76     private static final String BUTTON_TECHNOLOGY_NEWS_KEY =
77         "button_technology_news";
78     private static final String BUTTON_MULTI_CATEGORY_KEY =
79         "button_multi_category";
80
81     private static final String BUTTON_LOCAL_GENERAL_NEWS_KEY =
82         "button_local_general_news";
83     private static final String BUTTON_REGIONAL_GENERAL_NEWS_KEY =
84         "button_regional_general_news";
85     private static final String BUTTON_NATIONAL_GENERAL_NEWS_KEY =
86         "button_national_general_news";
87     private static final String BUTTON_INTERNATIONAL_GENERAL_NEWS_KEY =
88         "button_international_general_news";
89     
90     private static final String BUTTON_LOCAL_BF_NEWS_KEY =
91         "button_local_bf_news";
92     private static final String BUTTON_REGIONAL_BF_NEWS_KEY =
93         "button_regional_bf_news";
94     private static final String BUTTON_NATIONAL_BF_NEWS_KEY =
95         "button_national_bf_news";
96     private static final String BUTTON_INTERNATIONAL_BF_NEWS_KEY =
97         "button_international_bf_news";
98     
99     private static final String BUTTON_LOCAL_SPORTS_NEWS_KEY =
100         "button_local_sports_news";
101     private static final String BUTTON_REGIONAL_SPORTS_NEWS_KEY =
102         "button_regional_sports_news";
103     private static final String BUTTON_NATIONAL_SPORTS_NEWS_KEY =
104         "button_national_sports_news";
105     private static final String BUTTON_INTERNATIONAL_SPORTS_NEWS_KEY =
106         "button_international_sports_news";
107     
108     private static final String BUTTON_LOCAL_ENTERTAINMENT_NEWS_KEY =
109         "button_local_entertainment_news";
110     private static final String BUTTON_REGIONAL_ENTERTAINMENT_NEWS_KEY =
111         "button_regional_entertainment_news";
112     private static final String BUTTON_NATIONAL_ENTERTAINMENT_NEWS_KEY =
113         "button_national_entertainment_news";
114     private static final String BUTTON_INTERNATIONAL_ENTERTAINMENT_NEWS_KEY =
115         "button_international_entertainment_news";
116
117     //Class constants
118     //These values are related to the C structs. See the comments in  method
119     //setCbSmsConfig for more information.
120     private static final int NO_OF_SERVICE_CATEGORIES = 31;
121     private static final int NO_OF_INTS_STRUCT_1 = 3;
122     private static final int MAX_LENGTH_RESULT = NO_OF_SERVICE_CATEGORIES * NO_OF_INTS_STRUCT_1 + 1;
123     //Handler keys
124     private static final int MESSAGE_ACTIVATE_CB_SMS = 1;
125     private static final int MESSAGE_GET_CB_SMS_CONFIG = 2;
126     private static final int MESSAGE_SET_CB_SMS_CONFIG = 3;
127
128     //UI objects
129     private CheckBoxPreference mButtonBcSms;
130     
131     private ListPreference mListLanguage;
132     
133     private CheckBoxPreference mButtonEmergencyBroadcast;
134     private CheckBoxPreference mButtonAdministrative;
135     private CheckBoxPreference mButtonMaintenance;
136     private CheckBoxPreference mButtonLocalWeather;
137     private CheckBoxPreference mButtonAtr;
138     private CheckBoxPreference mButtonLafs;
139     private CheckBoxPreference mButtonRestaurants;
140     private CheckBoxPreference mButtonLodgings;
141     private CheckBoxPreference mButtonRetailDirectory;
142     private CheckBoxPreference mButtonAdvertisements;
143     private CheckBoxPreference mButtonStockQuotes;
144     private CheckBoxPreference mButtonEo;
145     private CheckBoxPreference mButtonMhh;
146     private CheckBoxPreference mButtonTechnologyNews;
147     private CheckBoxPreference mButtonMultiCategory;
148
149     private CheckBoxPreference mButtonLocal1;
150     private CheckBoxPreference mButtonRegional1;
151     private CheckBoxPreference mButtonNational1;
152     private CheckBoxPreference mButtonInternational1;
153     
154     private CheckBoxPreference mButtonLocal2;
155     private CheckBoxPreference mButtonRegional2;
156     private CheckBoxPreference mButtonNational2;
157     private CheckBoxPreference mButtonInternational2;
158     
159     private CheckBoxPreference mButtonLocal3;
160     private CheckBoxPreference mButtonRegional3;
161     private CheckBoxPreference mButtonNational3;
162     private CheckBoxPreference mButtonInternational3;
163     
164     private CheckBoxPreference mButtonLocal4;
165     private CheckBoxPreference mButtonRegional4;
166     private CheckBoxPreference mButtonNational4;
167     private CheckBoxPreference mButtonInternational4;
168     
169     
170     //Member variables
171     private Phone mPhone;
172     private MyHandler mHandler;
173
174     /** 
175      * Invoked on each preference click in this hierarchy, overrides 
176      * PreferenceActivity's implementation.  Used to make sure we track the
177      * preference click events.
178      */
179     @Override
180     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
181             Preference preference) {
182         if (preference == mButtonBcSms) {
183             if (DBG) Log.d(LOG_TAG, "onPreferenceTreeClick: preference == mButtonBcSms.");
184             if(mButtonBcSms.isChecked()) {
185                 mPhone.activateCellBroadcastSms(RILConstants.CDMA_CELL_BROADCAST_SMS_ENABLED,
186                         Message.obtain(mHandler, MESSAGE_ACTIVATE_CB_SMS));
187                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
188                         android.provider.Settings.Secure.CDMA_CELL_BROADCAST_SMS,
189                         RILConstants.CDMA_CELL_BROADCAST_SMS_ENABLED);
190                 enableDisableAllCbConfigButtons(true);
191             } else {
192                 mPhone.activateCellBroadcastSms(RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED,
193                         Message.obtain(mHandler, MESSAGE_ACTIVATE_CB_SMS));
194                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
195                         android.provider.Settings.Secure.CDMA_CELL_BROADCAST_SMS,
196                         RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED);
197                 enableDisableAllCbConfigButtons(false);
198             }
199         } else if (preference == mListLanguage) {
200             //Do nothing here, because this click will be handled in onPreferenceChange
201         } else if (preference == mButtonEmergencyBroadcast) {
202             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
203                     mButtonEmergencyBroadcast.isChecked(), 1);
204             CellBroadcastSmsConfig.setCbSmsBSelectedValue(
205                     mButtonEmergencyBroadcast.isChecked(), 1);
206         } else if (preference == mButtonAdministrative) {
207             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
208                     mButtonAdministrative.isChecked(), 2);
209             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonAdministrative.isChecked(), 2);
210         } else if (preference == mButtonMaintenance) {
211             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
212                     mButtonMaintenance.isChecked(), 3);
213             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonMaintenance.isChecked(), 3);
214         } else if (preference == mButtonLocalWeather) {
215             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
216                     mButtonLocalWeather.isChecked(), 20);
217             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonLocalWeather.isChecked(), 20);
218         } else if (preference == mButtonAtr) {
219             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonAtr.isChecked(), 21);
220             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonAtr.isChecked(), 21);
221         } else if (preference == mButtonLafs) {
222             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonLafs.isChecked(), 22);
223             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonLafs.isChecked(), 22);
224         } else if (preference == mButtonRestaurants) {
225             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
226                     mButtonRestaurants.isChecked(), 23);
227             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonRestaurants.isChecked(), 23);
228         } else if (preference == mButtonLodgings) {
229             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonLodgings.isChecked(), 24);
230             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonLodgings.isChecked(), 24);
231         } else if (preference == mButtonRetailDirectory) {
232             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
233                     mButtonRetailDirectory.isChecked(), 25);
234             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonRetailDirectory.isChecked(), 25);
235         } else if (preference == mButtonAdvertisements) {
236             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
237                     mButtonAdvertisements.isChecked(), 26);
238             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonAdvertisements.isChecked(), 26);
239         } else if (preference == mButtonStockQuotes) {
240             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
241                     mButtonStockQuotes.isChecked(), 27);
242             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonStockQuotes.isChecked(), 27);
243         } else if (preference == mButtonEo) {
244             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonEo.isChecked(), 28);
245             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonEo.isChecked(), 28);
246         } else if (preference == mButtonMhh) {
247             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonMhh.isChecked(), 29);
248             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonMhh.isChecked(), 29);
249         } else if (preference == mButtonTechnologyNews) {
250             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
251                     mButtonTechnologyNews.isChecked(), 30);
252             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonTechnologyNews.isChecked(), 30);
253         } else if (preference == mButtonMultiCategory) {
254             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
255                     mButtonMultiCategory.isChecked(), 31);
256             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonMultiCategory.isChecked(), 31);
257         } else if (preference == mButtonLocal1) {
258             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonLocal1.isChecked(), 4);
259             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonLocal1.isChecked(), 4);
260         } else if (preference == mButtonRegional1) {
261             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
262                     mButtonRegional1.isChecked(), 5);
263             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonRegional1.isChecked(), 5);
264         } else if (preference == mButtonNational1) {
265             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
266                     mButtonNational1.isChecked(), 6);
267             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonNational1.isChecked(), 6);
268         } else if (preference == mButtonInternational1) {
269             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
270                     mButtonInternational1.isChecked(), 7);
271             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonInternational1.isChecked(), 7);
272         } else if (preference == mButtonLocal2) {
273             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonLocal2.isChecked(), 8);
274             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonLocal2.isChecked(), 8);
275         } else if (preference == mButtonRegional2) {
276             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
277                     mButtonRegional2.isChecked(), 9);
278             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonRegional2.isChecked(), 9);
279         } else if (preference == mButtonNational2) {
280             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
281                     mButtonNational2.isChecked(), 10);
282             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonNational2.isChecked(), 10);
283         } else if (preference == mButtonInternational2) {
284             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
285                     mButtonInternational2.isChecked(), 11);
286             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonInternational2.isChecked(), 11);
287         } else if (preference == mButtonLocal3) {
288             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonLocal3.isChecked(), 12);
289             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonLocal3.isChecked(), 12);
290         } else if (preference == mButtonRegional3) {
291             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
292                     mButtonRegional3.isChecked(), 13);
293             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonRegional3.isChecked(), 13);
294         } else if (preference == mButtonNational3) {
295             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
296                     mButtonNational3.isChecked(), 14);
297             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonNational3.isChecked(), 14);
298         } else if (preference == mButtonInternational3) {
299             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
300                     mButtonInternational3.isChecked(), 15);
301             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonInternational3.isChecked(), 15);
302         } else if (preference == mButtonLocal4) {
303             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(mButtonLocal4.isChecked(), 16);
304             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonLocal4.isChecked(), 16);
305         } else if (preference == mButtonRegional4) {
306             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
307                     mButtonRegional4.isChecked(), 17);
308             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonRegional4.isChecked(), 17);
309         } else if (preference == mButtonNational4) {
310             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
311                     mButtonNational4.isChecked(), 18);
312             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonNational4.isChecked(), 18);
313         } else if (preference == mButtonInternational4) {
314             CellBroadcastSmsConfig.setConfigDataCompleteBSelected(
315                     mButtonInternational4.isChecked(), 19);
316             CellBroadcastSmsConfig.setCbSmsBSelectedValue(mButtonInternational4.isChecked(), 19);
317         } else {
318             preferenceScreen.setEnabled(false);
319             return false;
320         }
321
322         return true;
323     }
324     
325     public boolean onPreferenceChange(Preference preference, Object objValue) {
326         if (preference == mListLanguage) {
327             // set the new language to the array which will be transmitted later
328             CellBroadcastSmsConfig.setConfigDataCompleteLanguage(
329                     mListLanguage.findIndexOfValue((String) objValue) + 1);
330         }
331
332         // always let the preference setting proceed.
333         return true;
334     }
335
336     public void onCreate(Bundle icicle) {
337         super.onCreate(icicle);
338
339         addPreferencesFromResource(R.xml.cell_broadcast_sms);
340
341         mPhone = PhoneFactory.getDefaultPhone();
342         mHandler = new MyHandler();
343
344         PreferenceScreen prefSet = getPreferenceScreen();
345
346         mButtonBcSms = (CheckBoxPreference) prefSet.findPreference(
347                 BUTTON_ENABLE_DISABLE_BC_SMS_KEY);
348         mListLanguage = (ListPreference) prefSet.findPreference(
349                 LIST_LANGUAGE_KEY);
350         // set the listener for the language list preference
351         mListLanguage.setOnPreferenceChangeListener(this);
352         mButtonEmergencyBroadcast = (CheckBoxPreference) prefSet.findPreference(
353                 BUTTON_EMERGENCY_BROADCAST_KEY);
354         mButtonAdministrative = (CheckBoxPreference) prefSet.findPreference(
355                 BUTTON_ADMINISTRATIVE_KEY);
356         mButtonMaintenance = (CheckBoxPreference) prefSet.findPreference(
357                 BUTTON_MAINTENANCE_KEY);
358         mButtonLocalWeather = (CheckBoxPreference) prefSet.findPreference(
359                 BUTTON_LOCAL_WEATHER_KEY);
360         mButtonAtr = (CheckBoxPreference) prefSet.findPreference(
361                 BUTTON_ATR_KEY);
362         mButtonLafs = (CheckBoxPreference) prefSet.findPreference(
363                 BUTTON_LAFS_KEY);
364         mButtonRestaurants = (CheckBoxPreference) prefSet.findPreference(
365                 BUTTON_RESTAURANTS_KEY);
366         mButtonLodgings = (CheckBoxPreference) prefSet.findPreference(
367                 BUTTON_LODGINGS_KEY);
368         mButtonRetailDirectory = (CheckBoxPreference) prefSet.findPreference(
369                 BUTTON_RETAIL_DIRECTORY_KEY);
370         mButtonAdvertisements = (CheckBoxPreference) prefSet.findPreference(
371                 BUTTON_ADVERTISEMENTS_KEY);
372         mButtonStockQuotes = (CheckBoxPreference) prefSet.findPreference(
373                 BUTTON_STOCK_QUOTES_KEY);
374         mButtonEo = (CheckBoxPreference) prefSet.findPreference(
375                 BUTTON_EO_KEY);
376         mButtonMhh = (CheckBoxPreference) prefSet.findPreference(
377                 BUTTON_MHH_KEY);
378         mButtonTechnologyNews = (CheckBoxPreference) prefSet.findPreference(
379                 BUTTON_TECHNOLOGY_NEWS_KEY);
380         mButtonMultiCategory = (CheckBoxPreference) prefSet.findPreference(
381                 BUTTON_MULTI_CATEGORY_KEY);
382         
383         mButtonLocal1 = (CheckBoxPreference) prefSet.findPreference(
384                 BUTTON_LOCAL_GENERAL_NEWS_KEY);
385         mButtonRegional1 = (CheckBoxPreference) prefSet.findPreference(
386                 BUTTON_REGIONAL_GENERAL_NEWS_KEY);
387         mButtonNational1 = (CheckBoxPreference) prefSet.findPreference(
388                 BUTTON_NATIONAL_GENERAL_NEWS_KEY);
389         mButtonInternational1 = (CheckBoxPreference) prefSet.findPreference(
390                 BUTTON_INTERNATIONAL_GENERAL_NEWS_KEY);
391         
392         mButtonLocal2 = (CheckBoxPreference) prefSet.findPreference(
393                 BUTTON_LOCAL_BF_NEWS_KEY);
394         mButtonRegional2 = (CheckBoxPreference) prefSet.findPreference(
395                 BUTTON_REGIONAL_BF_NEWS_KEY);
396         mButtonNational2 = (CheckBoxPreference) prefSet.findPreference(
397                 BUTTON_NATIONAL_BF_NEWS_KEY);
398         mButtonInternational2 = (CheckBoxPreference) prefSet.findPreference(
399                 BUTTON_INTERNATIONAL_BF_NEWS_KEY);
400         
401         mButtonLocal3 = (CheckBoxPreference) prefSet.findPreference(
402                 BUTTON_LOCAL_SPORTS_NEWS_KEY);
403         mButtonRegional3 = (CheckBoxPreference) prefSet.findPreference(
404                 BUTTON_REGIONAL_SPORTS_NEWS_KEY);
405         mButtonNational3 = (CheckBoxPreference) prefSet.findPreference(
406                 BUTTON_NATIONAL_SPORTS_NEWS_KEY);
407         mButtonInternational3 = (CheckBoxPreference) prefSet.findPreference(
408                 BUTTON_INTERNATIONAL_SPORTS_NEWS_KEY);
409         
410         mButtonLocal4 = (CheckBoxPreference) prefSet.findPreference(
411                 BUTTON_LOCAL_ENTERTAINMENT_NEWS_KEY);
412         mButtonRegional4 = (CheckBoxPreference) prefSet.findPreference(
413                 BUTTON_REGIONAL_ENTERTAINMENT_NEWS_KEY);
414         mButtonNational4 = (CheckBoxPreference) prefSet.findPreference(
415                 BUTTON_NATIONAL_ENTERTAINMENT_NEWS_KEY);
416         mButtonInternational4 = (CheckBoxPreference) prefSet.findPreference(
417                 BUTTON_INTERNATIONAL_ENTERTAINMENT_NEWS_KEY);
418     }
419
420     @Override
421     protected void onResume() {
422         super.onResume();
423
424         getPreferenceScreen().setEnabled(true);
425
426         int settingCbSms = android.provider.Settings.Secure.getInt(
427                 mPhone.getContext().getContentResolver(),
428                 android.provider.Settings.Secure.CDMA_CELL_BROADCAST_SMS,
429                 RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED);            
430         mButtonBcSms.setChecked(settingCbSms == RILConstants.CDMA_CELL_BROADCAST_SMS_ENABLED);
431
432         if(mButtonBcSms.isChecked()) {
433             enableDisableAllCbConfigButtons(true);
434         } else {
435             enableDisableAllCbConfigButtons(false);
436         }
437
438         mPhone.getCellBroadcastSmsConfig(Message.obtain(mHandler, MESSAGE_GET_CB_SMS_CONFIG));
439     }
440
441     @Override
442     protected void onPause() {
443         super.onPause();
444
445             CellBroadcastSmsConfig.setCbSmsNoOfStructs(NO_OF_SERVICE_CATEGORIES);
446
447             mPhone.setCellBroadcastSmsConfig(CellBroadcastSmsConfig.getCbSmsAllValues(),
448                     Message.obtain(mHandler, MESSAGE_SET_CB_SMS_CONFIG));
449         }
450
451     private void enableDisableAllCbConfigButtons(boolean enable) {
452         mButtonEmergencyBroadcast.setEnabled(enable);
453         mListLanguage.setEnabled(enable);
454         mButtonAdministrative.setEnabled(enable);
455         mButtonMaintenance.setEnabled(enable);
456         mButtonLocalWeather.setEnabled(enable);
457         mButtonAtr.setEnabled(enable);
458         mButtonLafs.setEnabled(enable);
459         mButtonRestaurants.setEnabled(enable);
460         mButtonLodgings.setEnabled(enable);
461         mButtonRetailDirectory.setEnabled(enable);
462         mButtonAdvertisements.setEnabled(enable);
463         mButtonStockQuotes.setEnabled(enable);
464         mButtonEo.setEnabled(enable);
465         mButtonMhh.setEnabled(enable);
466         mButtonTechnologyNews.setEnabled(enable);
467         mButtonMultiCategory.setEnabled(enable);
468         
469         mButtonLocal1.setEnabled(enable);
470         mButtonRegional1.setEnabled(enable);
471         mButtonNational1.setEnabled(enable);
472         mButtonInternational1.setEnabled(enable);
473         
474         mButtonLocal2.setEnabled(enable);
475         mButtonRegional2.setEnabled(enable);
476         mButtonNational2.setEnabled(enable);
477         mButtonInternational2.setEnabled(enable);
478         
479         mButtonLocal3.setEnabled(enable);
480         mButtonRegional3.setEnabled(enable);
481         mButtonNational3.setEnabled(enable);
482         mButtonInternational3.setEnabled(enable);
483         
484         mButtonLocal4.setEnabled(enable);
485         mButtonRegional4.setEnabled(enable);
486         mButtonNational4.setEnabled(enable);
487         mButtonInternational4.setEnabled(enable);
488     }
489     
490     private void setAllCbConfigButtons(int[] configArray) {
491         //These buttons are in a well defined sequence. If you want to change it,
492         //be sure to map the buttons to their corresponding slot in the configArray !
493         mButtonEmergencyBroadcast.setChecked(configArray[1] != 0);
494         //subtract 1, because the values are handled in an array which starts with 0 and not with 1
495         mListLanguage.setValueIndex(CellBroadcastSmsConfig.getConfigDataLanguage() - 1);
496         mButtonAdministrative.setChecked(configArray[2] != 0);
497         mButtonMaintenance.setChecked(configArray[3] != 0);
498         mButtonLocalWeather.setChecked(configArray[20] != 0);
499         mButtonAtr.setChecked(configArray[21] != 0);
500         mButtonLafs.setChecked(configArray[22] != 0);
501         mButtonRestaurants.setChecked(configArray[23] != 0);
502         mButtonLodgings.setChecked(configArray[24] != 0);
503         mButtonRetailDirectory.setChecked(configArray[25] != 0);
504         mButtonAdvertisements.setChecked(configArray[26] != 0);
505         mButtonStockQuotes.setChecked(configArray[27] != 0);
506         mButtonEo.setChecked(configArray[28] != 0);
507         mButtonMhh.setChecked(configArray[29] != 0);
508         mButtonTechnologyNews.setChecked(configArray[30] != 0);
509         mButtonMultiCategory.setChecked(configArray[31] != 0);
510
511         mButtonLocal1.setChecked(configArray[4] != 0);
512         mButtonRegional1.setChecked(configArray[5] != 0);
513         mButtonNational1.setChecked(configArray[6] != 0);
514         mButtonInternational1.setChecked(configArray[7] != 0);
515
516         mButtonLocal2.setChecked(configArray[8] != 0);
517         mButtonRegional2.setChecked(configArray[9] != 0);
518         mButtonNational2.setChecked(configArray[10] != 0);
519         mButtonInternational2.setChecked(configArray[11] != 0);
520
521         mButtonLocal3.setChecked(configArray[12] != 0);
522         mButtonRegional3.setChecked(configArray[13] != 0);
523         mButtonNational3.setChecked(configArray[14] != 0);
524         mButtonInternational3.setChecked(configArray[15] != 0);
525
526         mButtonLocal4.setChecked(configArray[16] != 0);
527         mButtonRegional4.setChecked(configArray[17] != 0);
528         mButtonNational4.setChecked(configArray[18] != 0);
529         mButtonInternational4.setChecked(configArray[19] != 0);
530     }
531
532     private class MyHandler extends Handler {
533
534         @Override
535         public void handleMessage(Message msg) {
536             switch (msg.what) {
537             case MESSAGE_ACTIVATE_CB_SMS:
538                 //Only a log message here, because the received response is always null
539                 if (DBG) Log.d(LOG_TAG, "Cell Broadcast SMS enabled/disabled.");
540                 break;
541             case MESSAGE_GET_CB_SMS_CONFIG:
542                 int result[] = (int[])((AsyncResult)msg.obj).result;
543
544                 // check if the actual service categoties table size on the NV is '0'
545                 if (result[0] == 0) {
546                     result[0] = NO_OF_SERVICE_CATEGORIES;
547
548                     mButtonBcSms.setChecked(false);
549                     mPhone.activateCellBroadcastSms(RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED,
550                             Message.obtain(mHandler, MESSAGE_ACTIVATE_CB_SMS));
551                     android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
552                             android.provider.Settings.Secure.CDMA_CELL_BROADCAST_SMS,
553                             RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED);
554                     enableDisableAllCbConfigButtons(false);
555                 }
556
557                 CellBroadcastSmsConfig.setCbSmsConfig(result);
558                 setAllCbConfigButtons(CellBroadcastSmsConfig.getCbSmsBselectedValues());
559
560                 break;
561             case MESSAGE_SET_CB_SMS_CONFIG:
562                 //Only a log message here, because the received response is always null
563                 if (DBG) Log.d(LOG_TAG, "Set Cell Broadcast SMS values.");
564                 break;
565             default:
566                 Log.e(LOG_TAG, "Error! Unhandled message in CellBroadcastSms.java. Message: "
567                         + msg.what);
568             break;
569             }
570         }
571     }
572
573     private static final class CellBroadcastSmsConfig {
574
575         //The values in this array are stored in a particular order. This order
576         //is calculated in the setCbSmsConfig method of this class.
577         //For more information see comments below...
578         //NO_OF_SERVICE_CATEGORIES +1 is used, because we will leave the first array entry 0
579         private static int mBSelected[] = new int[NO_OF_SERVICE_CATEGORIES + 1];
580         private static int mConfigDataComplete[] = new int[MAX_LENGTH_RESULT];
581
582         private static void setCbSmsConfig(int[] configData) {
583             if(configData == null) {
584                 Log.e(LOG_TAG, "Error! No cell broadcast service categories returned.");
585                 return;
586             }
587
588             if(configData[0] > MAX_LENGTH_RESULT) {
589                 Log.e(LOG_TAG, "Error! Wrong number of service categories returned from RIL");
590                 return;
591             }
592             
593             //The required config values for broadcast SMS are stored in a C struct:
594             //
595             //  typedef struct {
596             //      int size;
597             //      RIL_CDMA_BcServiceInfo *entries;
598             //  } RIL_CDMA_BcSMSConfig;
599             //
600             //  typedef struct {
601             //      int uServiceCategory;
602             //      int uLanguage;
603             //      unsigned char bSelected;
604             //  } RIL_CDMA_BcServiceInfo;
605             //
606             // This means, that we have to ignore the first value and check every
607             // 3rd value starting with the 2nd of all. This value indicates, where we
608             // will store the appropriate bSelected value, which is 2 values behind it.
609             for(int i = 1; i < configData.length; i += NO_OF_INTS_STRUCT_1) {
610                 mBSelected[configData[i]] = configData[i +2];
611             }
612             
613             //Store all values in an extra array
614             mConfigDataComplete = configData;
615         }
616         
617         private static void setCbSmsBSelectedValue(boolean value, int pos) {
618             if(pos < mBSelected.length) {
619                 mBSelected[pos] = (value == true ? 1 : 0);
620             } else {
621                 Log.e(LOG_TAG,"Error! Invalid value position.");
622             }
623         }
624         
625         private static int[] getCbSmsBselectedValues() {
626             return(mBSelected);
627         }
628         
629         private static int[] getCbSmsAllValues() {
630             return(mConfigDataComplete);
631         }
632         
633         private static void setCbSmsNoOfStructs(int value) {
634             //Sets the size parameter, which contains the number of structs
635             //that will be transmitted
636             mConfigDataComplete[0] = value;
637         }
638         
639         private static void setConfigDataCompleteBSelected(boolean value, int serviceCategory) {
640             //Sets the bSelected value for a specific serviceCategory
641             for(int i = 1; i < mConfigDataComplete.length; i += NO_OF_INTS_STRUCT_1) {
642                 if(mConfigDataComplete[i] == serviceCategory) {
643                     mConfigDataComplete[i + 2] = value == true ? 1 : 0;
644                     break;
645                 }
646             }
647         }
648         
649         private static void setConfigDataCompleteLanguage(int language) {
650             //It is only possible to set the same language for all entries 
651             for(int i = 2; i < mConfigDataComplete.length; i += NO_OF_INTS_STRUCT_1) {
652                 mConfigDataComplete[i] = language;
653             }
654         }
655         
656         private static int getConfigDataLanguage() {
657             int language = mConfigDataComplete[2];
658             //2 is the language value of the first entry
659             //It is only possible to set the same language for all entries 
660             if (language < 1 || language > 7) {
661                 Log.e(LOG_TAG, "Error! Wrong language returned from RIL...defaulting to 1, english");
662                 return 1;
663             }
664             else {
665                 return language;
666             }
667         }
668     }
669 }