AI 144185: Integrate cdma into the main code base.
[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     
49     static final int preferredNetworkMode = Phone.PREFERRED_NT_MODE;
50     
51     //UI objects
52     private ListPreference mButtonPreferredNetworkMode;
53     private CheckBoxPreference mButtonDataRoam;
54     
55     private Phone mPhone;
56     private MyHandler mHandler;
57     private boolean mOkClicked;
58         
59     
60     //This is a method implemented for DialogInterface.OnClickListener.
61     //  Used to dismiss the dialogs when they come up.
62     public void onClick(DialogInterface dialog, int which) {
63         if (which == DialogInterface.BUTTON1) {
64             mPhone.setDataRoamingEnabled(true);
65             mOkClicked = true;
66         } else {
67             // Reset the toggle
68             mButtonDataRoam.setChecked(false);
69         }
70     }
71     
72     public void onDismiss(DialogInterface dialog) {
73         // Assuming that onClick gets called first
74         if (!mOkClicked) {
75             mButtonDataRoam.setChecked(false);
76         }
77     }
78     
79     /** 
80      * Invoked on each preference click in this hierarchy, overrides 
81      * PreferenceActivity's implementation.  Used to make sure we track the
82      * preference click events.
83      */
84     @Override
85     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
86         if (preference == mButtonPreferredNetworkMode) {
87             //displays the value taken from the Settings.System
88             int settingsNetworkMode = android.provider.Settings.Secure.getInt(mPhone.getContext().
89                     getContentResolver(), android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, 
90                     preferredNetworkMode);            
91             mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode));
92             return true;
93         }
94         else if (preference == mButtonDataRoam) {
95             if (DBG) log("onPreferenceTreeClick: preference == mButtonDataRoam.");
96         
97             //normally called on the toggle click
98             if (mButtonDataRoam.isChecked()) {
99                 // First confirm with a warning dialog about charges
100                 mOkClicked = false;
101                 new AlertDialog.Builder(this).setMessage(
102                         getResources().getString(R.string.roaming_warning))
103                         .setTitle(android.R.string.dialog_alert_title)
104                         .setIcon(android.R.drawable.ic_dialog_alert)
105                         .setPositiveButton(android.R.string.yes, this)
106                         .setNegativeButton(android.R.string.no, this)
107                         .show()
108                         .setOnDismissListener(this);
109             } 
110             else {
111                 mPhone.setDataRoamingEnabled(false);
112             }
113             return true;
114         } 
115         else {
116             // if the button is anything but the simple toggle preference,
117             // we'll need to disable all preferences to reject all click
118             // events until the sub-activity's UI comes up.
119             preferenceScreen.setEnabled(false);
120             // Let the intents be launched by the Preference manager
121             return false;
122         }
123     }
124     
125     @Override
126     protected void onCreate(Bundle icicle) {
127         super.onCreate(icicle);
128
129         addPreferencesFromResource(R.xml.network_setting);
130         
131         mPhone = PhoneFactory.getDefaultPhone();
132         mHandler = new MyHandler();
133
134         //get UI object references
135         PreferenceScreen prefSet = getPreferenceScreen();
136
137         mButtonDataRoam = (CheckBoxPreference) prefSet.findPreference(BUTTON_ROAMING_KEY);
138         mButtonPreferredNetworkMode = (ListPreference) prefSet.findPreference(
139                 BUTTON_PREFERED_NETWORK_MODE);
140         
141         // set the listener for the mButtonPreferredNetworkMode list preference so we can issue 
142         // change Preferred Network Mode.
143         mButtonPreferredNetworkMode.setOnPreferenceChangeListener(this);
144         
145         //Get the networkMode from Settings.System and displays it
146         int settingsNetworkMode = android.provider.Settings.Secure.getInt(mPhone.getContext().
147                 getContentResolver(),android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, 
148                 preferredNetworkMode);
149         mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode));
150         // The intent code that resided here in the past has been moved into the
151         // more conventional location in network_setting.xml
152     }
153     
154     @Override
155     protected void onResume() {
156         super.onResume();
157         
158         // upon resumption from the sub-activity, make sure we re-enable the
159         // preferences.
160         getPreferenceScreen().setEnabled(true);
161         
162         // Set UI state in onResume because a user could go home, launch some
163         // app to change this setting's backend, and re-launch this settings app
164         // and the UI state would be inconsistent with actual state
165         mButtonDataRoam.setChecked(mPhone.getDataRoamingEnabled());
166         mPhone.getPreferredNetworkType(mHandler.obtainMessage(
167                 MyHandler.MESSAGE_GET_PREFERRED_NETWORK_TYPE));
168     }
169     
170     /**
171      * Implemented to support onPreferenceChangeListener to look for preference
172      * changes specifically on CLIR.
173      *
174      * @param preference is the preference to be changed, should be mButtonCLIR.
175      * @param objValue should be the value of the selection, NOT its localized
176      * display value.
177      */
178     public boolean onPreferenceChange(Preference preference, Object objValue) {
179         if (preference == mButtonPreferredNetworkMode) {
180             //NOTE onPreferenceChange seems to be called even if there is no change
181             //Check if the button value is changed from the System.Setting
182             mButtonPreferredNetworkMode.setValue((String) objValue);
183             int buttonNetworkMode;
184             buttonNetworkMode = Integer.valueOf((String) objValue).intValue();
185             int settingsNetworkMode = android.provider.Settings.Secure.getInt(
186                     mPhone.getContext().getContentResolver(),
187                     android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, preferredNetworkMode);
188             if (buttonNetworkMode != settingsNetworkMode) {
189                 int modemNetworkMode; 
190                 switch(buttonNetworkMode) { //Extend to enable user choise of other network modes
191                     case Phone.NT_MODE_GSM_UMTS:
192                         modemNetworkMode = Phone.NT_MODE_GSM_UMTS;
193                         break;
194                     case Phone.NT_MODE_CDMA:
195                         modemNetworkMode = Phone.NT_MODE_CDMA;
196                         break;
197                     case Phone.NT_MODE_GLOBAL:
198                         modemNetworkMode = Phone.NT_MODE_GLOBAL;
199                         break;
200                     default:
201                         modemNetworkMode = Phone.PREFERRED_NT_MODE;
202                 } 
203                 UpdatePreferredNetworkModeSummary(buttonNetworkMode);
204
205                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
206                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, 
207                         buttonNetworkMode );
208                 //Set the modem network moode
209                 mPhone.setPreferredNetworkType(modemNetworkMode, mHandler
210                         .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
211             }
212         }
213
214         // always let the preference setting proceed.
215         return true;
216     }
217
218     private class MyHandler extends Handler {
219
220         private static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0;
221         private static final int MESSAGE_SET_PREFERRED_NETWORK_TYPE = 1;
222         
223         @Override
224         public void handleMessage(Message msg) {
225             switch (msg.what) {
226                 case MESSAGE_GET_PREFERRED_NETWORK_TYPE:
227                     handleGetPreferredNetworkTypeResponse(msg);
228                     break;
229                     
230                 case MESSAGE_SET_PREFERRED_NETWORK_TYPE:
231                     handleSetPreferredNetworkTypeResponse(msg);
232                     break;
233             }
234         }
235         
236         private void handleGetPreferredNetworkTypeResponse(Message msg) {
237             AsyncResult ar = (AsyncResult) msg.obj;
238             
239             if (ar.exception == null) {
240                 int modemNetworkMode = ((int[])ar.result)[0];
241                 
242                 if (DBG) {
243                     log ("handleGetPreferredNetworkTypeResponse: modemNetworkMode = " + 
244                             modemNetworkMode);
245                 }
246                 
247                 int settingsNetworkMode = android.provider.Settings.Secure.getInt(
248                         mPhone.getContext().getContentResolver(),
249                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, 
250                         preferredNetworkMode);
251                 
252                 if (DBG) {
253                     log("handleGetPreferredNetworkTypeReponse: settingsNetworkMode = " + 
254                             settingsNetworkMode);
255                 }
256                 
257                 //check that modemNetworkMode is from an accepted value
258                 if (modemNetworkMode == Phone.NT_MODE_WCDMA_PREF ||  
259                         modemNetworkMode == Phone.NT_MODE_GSM_ONLY ||
260                         modemNetworkMode == Phone.NT_MODE_WCDMA_ONLY ||
261                         modemNetworkMode == Phone.NT_MODE_GSM_UMTS ||
262                         modemNetworkMode == Phone.NT_MODE_CDMA ||
263                         modemNetworkMode == Phone.NT_MODE_CDMA_NO_EVDO ||
264                         modemNetworkMode == Phone.NT_MODE_EVDO_NO_CDMA ||
265                         modemNetworkMode == Phone.NT_MODE_GLOBAL ) {
266                     if (DBG) {
267                         log("handleGetPreferredNetworkTypeResponse: if 1: modemNetworkMode = " + 
268                                 modemNetworkMode);
269                     }
270                 
271                     //check changes in modemNetworkMode and updates settingsNetworkMode 
272                     if (modemNetworkMode != settingsNetworkMode) {
273                         if (DBG) {
274                             log("handleGetPreferredNetworkTypeResponse: if 2: " +
275                                     "modemNetworkMode != settingsNetworkMode");
276                         }
277                         
278                         settingsNetworkMode = modemNetworkMode;
279                         
280                         if (DBG) { log("handleGetPreferredNetworkTypeResponse: if 2: " + 
281                                 "settingsNetworkMode = " + settingsNetworkMode);
282                         }
283                         
284                         //changes the Settings.System accordingly to modemNetworkMode
285                         android.provider.Settings.Secure.putInt(
286                                 mPhone.getContext().getContentResolver(),
287                                 android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, 
288                                 settingsNetworkMode );
289                     }
290
291                     UpdatePreferredNetworkModeSummary(modemNetworkMode);
292                     // changes the mButtonPreferredNetworkMode accordingly to modemNetworkMode
293                     mButtonPreferredNetworkMode.setValue(Integer.toString(modemNetworkMode)); 
294                 } else {
295                     if (DBG) log("handleGetPreferredNetworkTypeResponse: else: reset to default");
296                     resetNetworkModeToDefault();
297                 }
298             }
299         }
300
301         private void handleSetPreferredNetworkTypeResponse(Message msg) {
302             AsyncResult ar = (AsyncResult) msg.obj;
303
304             if (ar.exception == null) {
305                 int networkMode = Integer.valueOf(
306                         mButtonPreferredNetworkMode.getValue()).intValue();
307                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
308                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, 
309                         networkMode );
310             } else {
311                 mPhone.getPreferredNetworkType(obtainMessage(MESSAGE_GET_PREFERRED_NETWORK_TYPE));
312             }
313         }
314         
315         private void resetNetworkModeToDefault() {
316             //set the mButtonPreferredNetworkMode
317             mButtonPreferredNetworkMode.setValue(Integer.toString(preferredNetworkMode));
318             //set the Settings.System 
319             android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
320                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, 
321                         preferredNetworkMode );            
322             //Set the Modem
323             mPhone.setPreferredNetworkType(preferredNetworkMode, 
324                     this.obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
325         }
326     }
327         
328     private void UpdatePreferredNetworkModeSummary(int NetworkMode) {
329         switch(NetworkMode) { //Extend to enable log of other network modes
330             case Phone.NT_MODE_WCDMA_PREF:
331             case Phone.NT_MODE_GSM_ONLY:
332             case Phone.NT_MODE_WCDMA_ONLY:
333             case Phone.NT_MODE_GSM_UMTS:
334                 mButtonPreferredNetworkMode.setSummary("Preferred network mode: GSM");
335                 break;
336             case Phone.NT_MODE_CDMA:
337             case Phone.NT_MODE_CDMA_NO_EVDO:
338             case Phone.NT_MODE_EVDO_NO_CDMA:
339                 mButtonPreferredNetworkMode.setSummary("Preferred network mode: CDMA");
340                 break;
341             case Phone.NT_MODE_GLOBAL:
342             default:
343                 mButtonPreferredNetworkMode.setSummary("Preferred network mode: Global");
344         }
345     }
346     
347     private static void log(String msg) {
348         Log.d(LOG_TAG, msg);
349     }
350 }
351