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.Secure; 27 import android.util.AttributeSet; 28 import android.util.Log; 29 30 import com.android.internal.telephony.Phone; 31 import com.android.internal.telephony.PhoneFactory; 32 import com.android.internal.telephony.TelephonyProperties; 33 34 public class CdmaRoamingListPreference extends ListPreference { 35 36 private static final String LOG_TAG = "CdmaRoamingListPreference"; 37 private static final boolean DBG = true; 38 39 private Phone mPhone; 40 private MyHandler mHandler = new MyHandler();; 41 CdmaRoamingListPreference(Context context, AttributeSet attrs)42 public CdmaRoamingListPreference(Context context, AttributeSet attrs) { 43 super(context, attrs); 44 45 mPhone = PhoneFactory.getDefaultPhone(); 46 mHandler = new MyHandler(); 47 mPhone.queryCdmaRoamingPreference( 48 mHandler.obtainMessage(MyHandler.MESSAGE_GET_ROAMING_PREFERENCE)); 49 } 50 CdmaRoamingListPreference(Context context)51 public CdmaRoamingListPreference(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 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 } 92 93 private class MyHandler extends Handler { 94 95 private static final int MESSAGE_GET_ROAMING_PREFERENCE = 0; 96 private static final int MESSAGE_SET_ROAMING_PREFERENCE = 1; 97 98 @Override handleMessage(Message msg)99 public void handleMessage(Message msg) { 100 switch (msg.what) { 101 case MESSAGE_GET_ROAMING_PREFERENCE: 102 handleQueryCdmaRoamingPreference(msg); 103 break; 104 105 case MESSAGE_SET_ROAMING_PREFERENCE: 106 handleSetCdmaRoamingPreference(msg); 107 break; 108 } 109 } 110 handleQueryCdmaRoamingPreference(Message msg)111 private void handleQueryCdmaRoamingPreference(Message msg) { 112 AsyncResult ar = (AsyncResult) msg.obj; 113 114 if (ar.exception == null) { 115 int statusCdmaRoamingMode = ((int[])ar.result)[0]; 116 int settingsRoamingMode = Secure.getInt( 117 mPhone.getContext().getContentResolver(), 118 Secure.CDMA_ROAMING_MODE, Phone.CDMA_RM_HOME); 119 //check that statusCdmaRoamingMode is from an accepted value 120 if (statusCdmaRoamingMode == Phone.CDMA_RM_HOME || 121 statusCdmaRoamingMode == Phone.CDMA_RM_ANY ) { 122 //check changes in statusCdmaRoamingMode and updates settingsRoamingMode 123 if (statusCdmaRoamingMode != settingsRoamingMode) { 124 settingsRoamingMode = statusCdmaRoamingMode; 125 //changes the Settings.Secure accordingly to statusCdmaRoamingMode 126 Secure.putInt( 127 mPhone.getContext().getContentResolver(), 128 Secure.CDMA_ROAMING_MODE, 129 settingsRoamingMode ); 130 } 131 //changes the mButtonPreferredNetworkMode accordingly to modemNetworkMode 132 setValue(Integer.toString(statusCdmaRoamingMode)); 133 } 134 else { 135 if(DBG) Log.i(LOG_TAG, "reset cdma roaming mode to default" ); 136 resetCdmaRoamingModeToDefault(); 137 } 138 } 139 } 140 handleSetCdmaRoamingPreference(Message msg)141 private void handleSetCdmaRoamingPreference(Message msg) { 142 AsyncResult ar = (AsyncResult) msg.obj; 143 144 if (ar.exception == null) { 145 int cdmaRoamingMode = Integer.valueOf(getValue()).intValue(); 146 Secure.putInt(mPhone.getContext().getContentResolver(), 147 Secure.CDMA_ROAMING_MODE, 148 cdmaRoamingMode ); 149 } else { 150 mPhone.queryCdmaRoamingPreference(obtainMessage(MESSAGE_GET_ROAMING_PREFERENCE)); 151 } 152 } 153 resetCdmaRoamingModeToDefault()154 private void resetCdmaRoamingModeToDefault() { 155 //set the mButtonCdmaRoam 156 setValue(Integer.toString(Phone.CDMA_RM_HOME)); 157 //set the Settings.System 158 Secure.putInt(mPhone.getContext().getContentResolver(), 159 Secure.CDMA_ROAMING_MODE, 160 Phone.CDMA_RM_HOME ); 161 //Set the Status 162 mPhone.setCdmaRoamingPreference(Phone.CDMA_RM_HOME, 163 obtainMessage(MyHandler.MESSAGE_SET_ROAMING_PREFERENCE)); 164 } 165 } 166 167 } 168