98525408d1aa3ae3fd6a525f6a0dc4d11c189a59
[android/platform/packages/apps/Phone.git] / src / com / android / phone / Settings.java
1 /*
2  * Copyright (C) 2006 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 package com.android.phone;
18
19 import android.app.AlertDialog;
20 import android.content.DialogInterface;
21 import android.os.Message;
22 import android.os.Handler;
23 import android.os.Bundle;
24 import android.os.AsyncResult;
25 import android.preference.CheckBoxPreference;
26 import android.preference.ListPreference;
27 import android.preference.Preference;
28 import android.preference.PreferenceActivity;
29 import android.preference.PreferenceScreen;
30 import android.util.Log;
31
32 import com.android.internal.telephony.Phone;
33 import com.android.internal.telephony.PhoneFactory;
34
35 /**
36  * List of Phone-specific settings screens.
37  */
38 public class Settings extends PreferenceActivity implements DialogInterface.OnClickListener,
39         DialogInterface.OnDismissListener, Preference.OnPreferenceChangeListener{
40
41     // debug data
42     private static final String LOG_TAG = "NetworkSettings";
43     private static final boolean DBG = true;
44
45     //String keys for preference lookup
46     private static final String BUTTON_PREFERED_NETWORK_MODE = "preferred_network_mode_key";
47     private static final String BUTTON_ROAMING_KEY = "button_roaming_key";
48     private static final String BUTTON_PREFER_2G_KEY = "button_prefer_2g_key";
49     private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key";
50     private static final String GSM_OPTIONS_KEY = "gsm_umts_options_key";
51     private static final String CDMA_OPTIONS_KEY = "cdma_options_key";
52
53     // Used for CDMA roaming mode
54     private static final int CDMA_ROAMING_MODE_HOME = 0;
55     private static final int CDMA_ROAMING_MODE_AFFILIATED = 1;
56     private static final int CDMA_ROAMING_MODE_ANY = 2;
57
58     // PREFERRED_CDMA_ROAMING_MODE  0 - Home Networks only, preferred
59     //                              1 - Roaming on affiliated networks
60     //                              2 - Roaming on any network
61     static final int PREFERRED_CDMA_ROAMING_MODE = 0;
62
63     static final int preferredNetworkMode = Phone.PREFERRED_NT_MODE;
64
65     //UI objects
66     private ListPreference mButtonPreferredNetworkMode;
67     private CheckBoxPreference mButtonDataRoam;
68     private CheckBoxPreference mButtonPrefer2g;
69     private ListPreference mButtonCdmaRoam;
70
71     private Phone mPhone;
72     private MyHandler mHandler;
73     private boolean mOkClicked;
74
75
76     //This is a method implemented for DialogInterface.OnClickListener.
77     //  Used to dismiss the dialogs when they come up.
78     public void onClick(DialogInterface dialog, int which) {
79         if (which == DialogInterface.BUTTON1) {
80             mPhone.setDataRoamingEnabled(true);
81             mOkClicked = true;
82         } else {
83             // Reset the toggle
84             mButtonDataRoam.setChecked(false);
85         }
86     }
87
88     public void onDismiss(DialogInterface dialog) {
89         // Assuming that onClick gets called first
90         if (!mOkClicked) {
91             mButtonDataRoam.setChecked(false);
92         }
93     }
94
95     /**
96      * Invoked on each preference click in this hierarchy, overrides
97      * PreferenceActivity's implementation.  Used to make sure we track the
98      * preference click events.
99      */
100     @Override
101     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
102         if (preference == mButtonPreferredNetworkMode) {
103             //displays the value taken from the Settings.System
104             int settingsNetworkMode = android.provider.Settings.Secure.getInt(mPhone.getContext().
105                     getContentResolver(), android.provider.Settings.Secure.PREFERRED_NETWORK_MODE,
106                     preferredNetworkMode);
107             mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode));
108             return true;
109         }
110         else if (preference == mButtonDataRoam) {
111             if (DBG) log("onPreferenceTreeClick: preference == mButtonDataRoam.");
112
113             //normally called on the toggle click
114             if (mButtonDataRoam.isChecked()) {
115                 // First confirm with a warning dialog about charges
116                 mOkClicked = false;
117                 new AlertDialog.Builder(this).setMessage(
118                         getResources().getString(R.string.roaming_warning))
119                         .setTitle(android.R.string.dialog_alert_title)
120                         .setIcon(android.R.drawable.ic_dialog_alert)
121                         .setPositiveButton(android.R.string.yes, this)
122                         .setNegativeButton(android.R.string.no, this)
123                         .show()
124                         .setOnDismissListener(this);
125             }
126             else {
127                 mPhone.setDataRoamingEnabled(false);
128             }
129             return true;
130         }
131         else if (preference == mButtonCdmaRoam) {
132             if (DBG) log("onPreferenceTreeClick: preference == mButtonCdmaRoam.");
133             //displays the value taken from the Settings.System
134             int cdmaRoamingMode = android.provider.Settings.Secure.getInt(
135                     mPhone.getContext().getContentResolver(),
136                     android.provider.Settings.Secure.CDMA_ROAMING_MODE, PREFERRED_CDMA_ROAMING_MODE);
137             mButtonCdmaRoam.setValue(Integer.toString(cdmaRoamingMode));
138             return true;
139         }
140         else if (preference == mButtonPrefer2g) {
141             int networkType = mButtonPrefer2g.isChecked() ? Phone.NT_MODE_GSM_ONLY
142                     : Phone.NT_MODE_WCDMA_PREF;
143             android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
144                     android.provider.Settings.Secure.PREFERRED_NETWORK_MODE,
145                     networkType);
146             mPhone.setPreferredNetworkType(networkType,
147                     mHandler.obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
148             return true;
149         }
150         else {
151             // if the button is anything but the simple toggle preference,
152             // we'll need to disable all preferences to reject all click
153             // events until the sub-activity's UI comes up.
154             preferenceScreen.setEnabled(false);
155             // Let the intents be launched by the Preference manager
156             return false;
157         }
158     }
159
160     @Override
161     protected void onCreate(Bundle icicle) {
162         super.onCreate(icicle);
163
164         mPhone = PhoneFactory.getDefaultPhone();
165
166         if (mPhone.getPhoneName().equals("CDMA")) {
167             addPreferencesFromResource(R.xml.network_setting_cdma);
168
169         } else {
170             addPreferencesFromResource(R.xml.network_setting);
171         }
172
173         //get UI object references
174         PreferenceScreen prefSet = getPreferenceScreen();
175
176         mHandler = new MyHandler();
177         mButtonDataRoam = (CheckBoxPreference) prefSet.findPreference(BUTTON_ROAMING_KEY);
178
179         // Remove the other network type options
180         boolean isCdma = mPhone.getPhoneName().equals("CDMA");
181
182         // TODO: The radio technology could be changed dynamically after the phone has been created
183         if (isCdma) {
184             Preference otherNetworkType = getPreferenceScreen().findPreference(GSM_OPTIONS_KEY);
185             getPreferenceScreen().removePreference(otherNetworkType);
186             mButtonCdmaRoam =
187                 (ListPreference) prefSet.findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY);
188             // set the listener for the mButtonCdmaRoam list preference so we can issue
189             // change CDMA Roaming Mode.
190             mButtonCdmaRoam.setOnPreferenceChangeListener(this);
191
192             //Get the settingsCdmaRoamingMode from Settings.System and displays it
193             int settingsCdmaRoamingMode = android.provider.Settings.Secure.getInt(
194                     mPhone.getContext().getContentResolver(),
195                     android.provider.Settings.Secure.CDMA_ROAMING_MODE,
196                     PREFERRED_CDMA_ROAMING_MODE);
197             mButtonCdmaRoam.setValue(Integer.toString(settingsCdmaRoamingMode));
198         } else {
199             mButtonPrefer2g = (CheckBoxPreference) prefSet.findPreference(BUTTON_PREFER_2G_KEY);
200         }
201     }
202
203     @Override
204     protected void onResume() {
205         super.onResume();
206
207         // upon resumption from the sub-activity, make sure we re-enable the
208         // preferences.
209         getPreferenceScreen().setEnabled(true);
210
211         // Set UI state in onResume because a user could go home, launch some
212         // app to change this setting's backend, and re-launch this settings app
213         // and the UI state would be inconsistent with actual state
214         mButtonDataRoam.setChecked(mPhone.getDataRoamingEnabled());
215         if (mPhone.getPhoneName().equals("CDMA")) {
216             mPhone.queryCdmaRoamingPreference(
217                     mHandler.obtainMessage(MyHandler.MESSAGE_QUERY_ROAMING_PREFERENCE));
218         } else {
219             // Get the state for 'prefer 2g' setting
220             mPhone.getPreferredNetworkType(mHandler.obtainMessage(
221                     MyHandler.MESSAGE_GET_PREFERRED_NETWORK_TYPE));
222         }
223     }
224
225     /**
226      * Implemented to support onPreferenceChangeListener to look for preference
227      * changes specifically on CLIR.
228      *
229      * @param preference is the preference to be changed, should be mButtonCLIR.
230      * @param objValue should be the value of the selection, NOT its localized
231      * display value.
232      */
233     public boolean onPreferenceChange(Preference preference, Object objValue) {
234         if (preference == mButtonPreferredNetworkMode) {
235             //NOTE onPreferenceChange seems to be called even if there is no change
236             //Check if the button value is changed from the System.Setting
237             mButtonPreferredNetworkMode.setValue((String) objValue);
238             int buttonNetworkMode;
239             buttonNetworkMode = Integer.valueOf((String) objValue).intValue();
240             int settingsNetworkMode = android.provider.Settings.Secure.getInt(
241                     mPhone.getContext().getContentResolver(),
242                     android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, preferredNetworkMode);
243             if (buttonNetworkMode != settingsNetworkMode) {
244                 int modemNetworkMode;
245                 switch(buttonNetworkMode) {
246                     case Phone.NT_MODE_GLOBAL:
247                         modemNetworkMode = Phone.NT_MODE_GLOBAL;
248                         break;
249                     case Phone.NT_MODE_EVDO_NO_CDMA:
250                         modemNetworkMode = Phone.NT_MODE_EVDO_NO_CDMA;
251                         break;
252                     case Phone.NT_MODE_CDMA_NO_EVDO:
253                         modemNetworkMode = Phone.NT_MODE_CDMA_NO_EVDO;
254                         break;
255                     case Phone.NT_MODE_CDMA:
256                         modemNetworkMode = Phone.NT_MODE_CDMA;
257                         break;
258                     case Phone.NT_MODE_GSM_UMTS:
259                         modemNetworkMode = Phone.NT_MODE_GSM_UMTS;
260                         break;
261                     case Phone.NT_MODE_WCDMA_ONLY:
262                         modemNetworkMode = Phone.NT_MODE_WCDMA_ONLY;
263                         break;
264                     case Phone.NT_MODE_GSM_ONLY:
265                         modemNetworkMode = Phone.NT_MODE_GSM_ONLY;
266                         break;
267                     case Phone.NT_MODE_WCDMA_PREF:
268                         modemNetworkMode = Phone.NT_MODE_WCDMA_PREF;
269                         break;
270                     default:
271                         modemNetworkMode = Phone.PREFERRED_NT_MODE;
272                 }
273                 updatePreferredNetworkModeSummary(buttonNetworkMode);
274
275                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
276                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE,
277                         buttonNetworkMode );
278                 //Set the modem network mode
279                 mPhone.setPreferredNetworkType(modemNetworkMode, mHandler
280                         .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
281             }
282         } else if (preference == mButtonCdmaRoam) {
283             //NOTE onPreferenceChange seems to be called even if there is no change
284             //Check if the button value is changed from the System.Setting
285             mButtonCdmaRoam.setValue((String) objValue);
286             int buttonCdmaRoamingMode;
287             buttonCdmaRoamingMode = Integer.valueOf((String) objValue).intValue();
288             int settingsCdmaRoamingMode = android.provider.Settings.Secure.getInt(
289                     mPhone.getContext().getContentResolver(),
290                     android.provider.Settings.Secure.CDMA_ROAMING_MODE, PREFERRED_CDMA_ROAMING_MODE);
291             if (buttonCdmaRoamingMode != settingsCdmaRoamingMode) {
292                 int statusCdmaRoamingMode;
293                 switch(buttonCdmaRoamingMode) {
294                     case CDMA_ROAMING_MODE_ANY:
295                         statusCdmaRoamingMode = Phone.CDMA_RM_ANY;
296                         break;
297                         /*
298                     case CDMA_ROAMING_MODE_AFFILIATED:
299                         statusCdmaRoamingMode = Phone.CDMA_RM_AFFILIATED;
300                         break;
301                         */
302                     case CDMA_ROAMING_MODE_HOME:
303                     default:
304                         statusCdmaRoamingMode = Phone.CDMA_RM_HOME;
305                 }
306                 //Set the Settings.System network mode
307                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
308                         android.provider.Settings.Secure.CDMA_ROAMING_MODE,
309                         buttonCdmaRoamingMode );
310                 //Set the roaming preference mode
311                 mPhone.setCdmaRoamingPreference(statusCdmaRoamingMode, mHandler
312                         .obtainMessage(MyHandler.MESSAGE_SET_ROAMING_PREFERENCE));
313             }
314         }
315
316         // always let the preference setting proceed.
317         return true;
318     }
319
320     private class MyHandler extends Handler {
321
322         private static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0;
323         private static final int MESSAGE_SET_PREFERRED_NETWORK_TYPE = 1;
324         private static final int MESSAGE_QUERY_ROAMING_PREFERENCE = 2;
325         private static final int MESSAGE_SET_ROAMING_PREFERENCE = 3;
326
327         @Override
328         public void handleMessage(Message msg) {
329             switch (msg.what) {
330                 case MESSAGE_GET_PREFERRED_NETWORK_TYPE:
331                     handleGetPreferredNetworkTypeResponse(msg);
332                     break;
333
334                 case MESSAGE_SET_PREFERRED_NETWORK_TYPE:
335                     handleSetPreferredNetworkTypeResponse(msg);
336                     break;
337                 case MESSAGE_QUERY_ROAMING_PREFERENCE:
338                     handleQueryCdmaRoamingPreference(msg);
339                     break;
340
341                 case MESSAGE_SET_ROAMING_PREFERENCE:
342                     handleSetCdmaRoamingPreference(msg);
343                     break;
344              }
345         }
346
347         private void handleGetPreferredNetworkTypeResponse(Message msg) {
348             AsyncResult ar = (AsyncResult) msg.obj;
349
350             if (ar.exception == null) {
351                 int modemNetworkMode = ((int[])ar.result)[0];
352
353                 if (DBG) {
354                     log ("handleGetPreferredNetworkTypeResponse: modemNetworkMode = " +
355                             modemNetworkMode);
356                 }
357
358                 int settingsNetworkMode = android.provider.Settings.Secure.getInt(
359                         mPhone.getContext().getContentResolver(),
360                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE,
361                         preferredNetworkMode);
362
363                 if (DBG) {
364                     log("handleGetPreferredNetworkTypeReponse: settingsNetworkMode = " +
365                             settingsNetworkMode);
366                 }
367
368                 //check that modemNetworkMode is from an accepted value
369                 if (modemNetworkMode == Phone.NT_MODE_WCDMA_PREF ||
370                         modemNetworkMode == Phone.NT_MODE_GSM_ONLY ||
371                         modemNetworkMode == Phone.NT_MODE_WCDMA_ONLY ||
372                         modemNetworkMode == Phone.NT_MODE_GSM_UMTS ||
373                         modemNetworkMode == Phone.NT_MODE_CDMA ||
374                         modemNetworkMode == Phone.NT_MODE_CDMA_NO_EVDO ||
375                         modemNetworkMode == Phone.NT_MODE_EVDO_NO_CDMA ||
376                         modemNetworkMode == Phone.NT_MODE_GLOBAL ) {
377                     if (DBG) {
378                         log("handleGetPreferredNetworkTypeResponse: if 1: modemNetworkMode = " +
379                                 modemNetworkMode);
380                     }
381
382                     //check changes in modemNetworkMode and updates settingsNetworkMode
383                     if (modemNetworkMode != settingsNetworkMode) {
384                         if (DBG) {
385                             log("handleGetPreferredNetworkTypeResponse: if 2: " +
386                                     "modemNetworkMode != settingsNetworkMode");
387                         }
388
389                         settingsNetworkMode = modemNetworkMode;
390
391                         if (DBG) { log("handleGetPreferredNetworkTypeResponse: if 2: " +
392                                 "settingsNetworkMode = " + settingsNetworkMode);
393                         }
394
395                         //changes the Settings.System accordingly to modemNetworkMode
396                         android.provider.Settings.Secure.putInt(
397                                 mPhone.getContext().getContentResolver(),
398                                 android.provider.Settings.Secure.PREFERRED_NETWORK_MODE,
399                                 settingsNetworkMode );
400                     }
401                     if (mPhone.getPhoneName().equals("CDMA")) {
402                         updatePreferredNetworkModeSummary(modemNetworkMode);
403                         // changes the mButtonPreferredNetworkMode accordingly to modemNetworkMode
404                         mButtonPreferredNetworkMode.setValue(Integer.toString(modemNetworkMode));
405                     } else {
406                         mButtonPrefer2g.setChecked(modemNetworkMode == Phone.NT_MODE_GSM_ONLY);
407                     }
408                 } else {
409                     if (DBG) log("handleGetPreferredNetworkTypeResponse: else: reset to default");
410                     resetNetworkModeToDefault();
411                 }
412             }
413         }
414
415         private void handleSetPreferredNetworkTypeResponse(Message msg) {
416             AsyncResult ar = (AsyncResult) msg.obj;
417
418             if (ar.exception == null) {
419                 int networkMode;
420                 if (mPhone.getPhoneName().equals("CDMA")) {
421                     networkMode =
422                         Integer.valueOf(mButtonPreferredNetworkMode.getValue()).intValue();
423                 } else {
424                     networkMode = mButtonPrefer2g.isChecked()
425                             ? Phone.NT_MODE_GSM_ONLY : Phone.NT_MODE_WCDMA_PREF;
426                 }
427                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
428                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE,
429                         networkMode);
430             } else {
431                 mPhone.getPreferredNetworkType(obtainMessage(MESSAGE_GET_PREFERRED_NETWORK_TYPE));
432             }
433         }
434
435         private void resetNetworkModeToDefault() {
436             if (mPhone.getPhoneName().equals("CDMA")) {
437                 //set the mButtonPreferredNetworkMode
438                 mButtonPreferredNetworkMode.setValue(Integer.toString(preferredNetworkMode));
439             } else {
440                 mButtonPrefer2g.setChecked(preferredNetworkMode == Phone.NT_MODE_GSM_ONLY);
441             }
442             //set the Settings.System
443             android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
444                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE,
445                         preferredNetworkMode );
446             //Set the Modem
447             mPhone.setPreferredNetworkType(preferredNetworkMode,
448                     this.obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
449         }
450
451         private void handleQueryCdmaRoamingPreference(Message msg) {
452             mPhone = PhoneFactory.getDefaultPhone();
453             AsyncResult ar = (AsyncResult) msg.obj;
454
455             if (ar.exception == null) {
456                 int statusCdmaRoamingMode = ((int[])ar.result)[0];
457                 int settingsRoamingMode = android.provider.Settings.Secure.getInt(
458                         mPhone.getContext().getContentResolver(),
459                         android.provider.Settings.Secure.CDMA_ROAMING_MODE,
460                         PREFERRED_CDMA_ROAMING_MODE);
461                 //check that statusCdmaRoamingMode is from an accepted value
462                 if (statusCdmaRoamingMode == Phone.CDMA_RM_HOME ||
463                         statusCdmaRoamingMode == Phone.CDMA_RM_AFFILIATED ||
464                         statusCdmaRoamingMode == Phone.CDMA_RM_ANY ) {
465                     //check changes in statusCdmaRoamingMode and updates settingsRoamingMode
466                     if (statusCdmaRoamingMode != settingsRoamingMode) {
467                         settingsRoamingMode = statusCdmaRoamingMode;
468                         //changes the Settings.System accordingly to statusCdmaRoamingMode
469                         android.provider.Settings.Secure.putInt(
470                                 mPhone.getContext().getContentResolver(),
471                                 android.provider.Settings.Secure.CDMA_ROAMING_MODE,
472                                 settingsRoamingMode );
473                     }
474                     //changes the mButtonPreferredNetworkMode accordingly to modemNetworkMode
475                     mButtonCdmaRoam.setValue(
476                             Integer.toString(statusCdmaRoamingMode));
477                 } else {
478                     resetCdmaRoamingModeToDefault();
479                 }
480             }
481         }
482
483         private void handleSetCdmaRoamingPreference(Message msg) {
484             mPhone = PhoneFactory.getDefaultPhone();
485             AsyncResult ar = (AsyncResult) msg.obj;
486
487             if (ar.exception == null) {
488                 int cdmaRoamingMode = Integer.valueOf(mButtonCdmaRoam.getValue()).intValue();
489                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
490                         android.provider.Settings.Secure.CDMA_ROAMING_MODE,
491                         cdmaRoamingMode );
492             } else {
493                 mPhone.queryCdmaRoamingPreference(obtainMessage(MESSAGE_QUERY_ROAMING_PREFERENCE));
494             }
495         }
496
497         private void resetCdmaRoamingModeToDefault() {
498             mPhone = PhoneFactory.getDefaultPhone();
499             //set cdmaRoamingMode to default
500             int cdmaRoamingMode = PREFERRED_CDMA_ROAMING_MODE;
501             int statusCdmaRoamingMode = Phone.CDMA_RM_HOME;
502             //set the mButtonCdmaRoam
503             mButtonCdmaRoam.setValue(Integer.toString(cdmaRoamingMode));
504             //set the Settings.System
505             android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
506                         android.provider.Settings.Secure.CDMA_ROAMING_MODE,
507                         cdmaRoamingMode );
508             //Set the Status
509             mPhone.setCdmaRoamingPreference(statusCdmaRoamingMode,
510                     this.obtainMessage(MyHandler.MESSAGE_SET_ROAMING_PREFERENCE));
511         }
512     }
513
514     private void updatePreferredNetworkModeSummary(int NetworkMode) {
515         if (!mPhone.getPhoneName().equals("CDMA")) return;
516         mButtonPreferredNetworkMode.setSummary(mButtonPreferredNetworkMode.getEntry());
517     }
518
519     private static void log(String msg) {
520         Log.d(LOG_TAG, msg);
521     }
522 }