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 package com.android.phone;
18 
19 import android.content.Context;
20 import android.os.AsyncResult;
21 import android.os.Bundle;
22 import android.os.SystemProperties;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.preference.ListPreference;
26 import android.provider.Settings;
27 import android.provider.Settings.Secure;
28 import android.util.AttributeSet;
29 import android.util.Log;
30 
31 import com.android.internal.telephony.Phone;
32 import com.android.internal.telephony.TelephonyProperties;
33 
34 public class CdmaSystemSelectListPreference extends ListPreference {
35 
36     private static final String LOG_TAG = "CdmaRoamingListPreference";
37     private static final boolean DBG = false;
38 
39     private Phone mPhone;
40     private MyHandler mHandler = new MyHandler();
41 
CdmaSystemSelectListPreference(Context context, AttributeSet attrs)42     public CdmaSystemSelectListPreference(Context context, AttributeSet attrs) {
43         super(context, attrs);
44 
45         mPhone = PhoneGlobals.getPhone();
46         mHandler = new MyHandler();
47         mPhone.queryCdmaRoamingPreference(
48                 mHandler.obtainMessage(MyHandler.MESSAGE_GET_ROAMING_PREFERENCE));
49     }
50 
CdmaSystemSelectListPreference(Context context)51     public CdmaSystemSelectListPreference(Context context) {
52         this(context, null);
53     }
54 
55     @Override
showDialog(Bundle state)56     protected void showDialog(Bundle state) {
57         if (Boolean.parseBoolean(
58                     SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
59             // In ECM mode do not show selection options
60         } else {
61             super.showDialog(state);
62         }
63     }
64 
65     @Override
onDialogClosed(boolean positiveResult)66     protected void onDialogClosed(boolean positiveResult) {
67         super.onDialogClosed(positiveResult);
68 
69         if (positiveResult && (getValue() != null)) {
70             int buttonCdmaRoamingMode = Integer.parseInt(getValue());
71             int settingsCdmaRoamingMode =
72                     Settings.Global.getInt(mPhone.getContext().getContentResolver(),
73                     Settings.Global.CDMA_ROAMING_MODE, Phone.CDMA_RM_HOME);
74             if (buttonCdmaRoamingMode != settingsCdmaRoamingMode) {
75                 int statusCdmaRoamingMode;
76                 switch(buttonCdmaRoamingMode) {
77                     case Phone.CDMA_RM_ANY:
78                         statusCdmaRoamingMode = Phone.CDMA_RM_ANY;
79                         break;
80                     case Phone.CDMA_RM_HOME:
81                     default:
82                         statusCdmaRoamingMode = Phone.CDMA_RM_HOME;
83                 }
84                 //Set the Settings.Secure network mode
85                 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
86                         Settings.Global.CDMA_ROAMING_MODE,
87                         buttonCdmaRoamingMode );
88                 //Set the roaming preference mode
89                 mPhone.setCdmaRoamingPreference(statusCdmaRoamingMode, mHandler
90                         .obtainMessage(MyHandler.MESSAGE_SET_ROAMING_PREFERENCE));
91             }
92         } else {
93             Log.d(LOG_TAG, String.format("onDialogClosed: positiveResult=%b value=%s -- do nothing",
94                     positiveResult, getValue()));
95         }
96     }
97 
98     private class MyHandler extends Handler {
99 
100         static final int MESSAGE_GET_ROAMING_PREFERENCE = 0;
101         static final int MESSAGE_SET_ROAMING_PREFERENCE = 1;
102 
103         @Override
handleMessage(Message msg)104         public void handleMessage(Message msg) {
105             switch (msg.what) {
106                 case MESSAGE_GET_ROAMING_PREFERENCE:
107                     handleQueryCdmaRoamingPreference(msg);
108                     break;
109 
110                 case MESSAGE_SET_ROAMING_PREFERENCE:
111                     handleSetCdmaRoamingPreference(msg);
112                     break;
113             }
114         }
115 
handleQueryCdmaRoamingPreference(Message msg)116         private void handleQueryCdmaRoamingPreference(Message msg) {
117             AsyncResult ar = (AsyncResult) msg.obj;
118 
119             if (ar.exception == null) {
120                 int statusCdmaRoamingMode = ((int[])ar.result)[0];
121                 int settingsRoamingMode = Settings.Global.getInt(
122                         mPhone.getContext().getContentResolver(),
123                         Settings.Global.CDMA_ROAMING_MODE, Phone.CDMA_RM_HOME);
124                 //check that statusCdmaRoamingMode is from an accepted value
125                 if (statusCdmaRoamingMode == Phone.CDMA_RM_HOME ||
126                         statusCdmaRoamingMode == Phone.CDMA_RM_ANY ) {
127                     //check changes in statusCdmaRoamingMode and updates settingsRoamingMode
128                     if (statusCdmaRoamingMode != settingsRoamingMode) {
129                         settingsRoamingMode = statusCdmaRoamingMode;
130                         //changes the Settings.Secure accordingly to statusCdmaRoamingMode
131                         Settings.Global.putInt(
132                                 mPhone.getContext().getContentResolver(),
133                                 Settings.Global.CDMA_ROAMING_MODE,
134                                 settingsRoamingMode );
135                     }
136                     //changes the mButtonPreferredNetworkMode accordingly to modemNetworkMode
137                     setValue(Integer.toString(statusCdmaRoamingMode));
138                 }
139                 else {
140                     if(DBG) Log.i(LOG_TAG, "reset cdma roaming mode to default" );
141                     resetCdmaRoamingModeToDefault();
142                 }
143             }
144         }
145 
handleSetCdmaRoamingPreference(Message msg)146         private void handleSetCdmaRoamingPreference(Message msg) {
147             AsyncResult ar = (AsyncResult) msg.obj;
148 
149             if ((ar.exception == null) && (getValue() != null)) {
150                 int cdmaRoamingMode = Integer.parseInt(getValue());
151                 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
152                         Settings.Global.CDMA_ROAMING_MODE,
153                         cdmaRoamingMode );
154             } else {
155                 mPhone.queryCdmaRoamingPreference(obtainMessage(MESSAGE_GET_ROAMING_PREFERENCE));
156             }
157         }
158 
resetCdmaRoamingModeToDefault()159         private void resetCdmaRoamingModeToDefault() {
160             //set the mButtonCdmaRoam
161             setValue(Integer.toString(Phone.CDMA_RM_ANY));
162             //set the Settings.System
163             Settings.Global.putInt(mPhone.getContext().getContentResolver(),
164                         Settings.Global.CDMA_ROAMING_MODE,
165                         Phone.CDMA_RM_ANY );
166             //Set the Status
167             mPhone.setCdmaRoamingPreference(Phone.CDMA_RM_ANY,
168                     obtainMessage(MyHandler.MESSAGE_SET_ROAMING_PREFERENCE));
169         }
170     }
171 
172 }
173