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