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