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.Handler; 22 import android.os.Message; 23 import android.preference.CheckBoxPreference; 24 import android.util.AttributeSet; 25 import android.util.Log; 26 27 import com.android.internal.telephony.Phone; 28 29 public class Use2GOnlyCheckBoxPreference extends CheckBoxPreference { 30 private static final String LOG_TAG = "Use2GOnlyCheckBoxPreference"; 31 private static final boolean DBG = true; 32 33 private Phone mPhone; 34 private MyHandler mHandler; 35 36 public Use2GOnlyCheckBoxPreference(Context context) { 37 this(context, null); 38 } 39 40 public Use2GOnlyCheckBoxPreference(Context context, AttributeSet attrs) { 41 this(context, attrs,com.android.internal.R.attr.checkBoxPreferenceStyle); 42 } 43 44 public Use2GOnlyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) { 45 super(context, attrs, defStyle); 46 mPhone = PhoneApp.getPhone(); 47 mHandler = new MyHandler(); 48 mPhone.getPreferredNetworkType( 49 mHandler.obtainMessage(MyHandler.MESSAGE_GET_PREFERRED_NETWORK_TYPE)); 50 } 51 52 @Override 53 protected void onClick() { 54 super.onClick(); 55 56 int networkType = isChecked() ? Phone.NT_MODE_GSM_ONLY : Phone.NT_MODE_WCDMA_PREF; 57 Log.i(LOG_TAG, "set preferred network type="+networkType); 58 mPhone.setPreferredNetworkType(networkType, mHandler 59 .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE)); 60 } 61 62 private class MyHandler extends Handler { 63 64 private static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0; 65 private static final int MESSAGE_SET_PREFERRED_NETWORK_TYPE = 1; 66 67 @Override 68 public void handleMessage(Message msg) { 69 switch (msg.what) { 70 case MESSAGE_GET_PREFERRED_NETWORK_TYPE: 71 handleGetPreferredNetworkTypeResponse(msg); 72 break; 73 74 case MESSAGE_SET_PREFERRED_NETWORK_TYPE: 75 handleSetPreferredNetworkTypeResponse(msg); 76 break; 77 } 78 } 79 80 private void handleGetPreferredNetworkTypeResponse(Message msg) { 81 AsyncResult ar = (AsyncResult) msg.obj; 82 83 if (ar.exception == null) { 84 int type = ((int[])ar.result)[0]; 85 Log.i(LOG_TAG, "get preferred network type="+type); 86 setChecked(type == Phone.NT_MODE_GSM_ONLY); 87 } else { 88 // Weird state, disable the setting 89 Log.i(LOG_TAG, "get preferred network type, exception="+ar.exception); 90 setEnabled(false); 91 } 92 } 93 94 private void handleSetPreferredNetworkTypeResponse(Message msg) { 95 AsyncResult ar = (AsyncResult) msg.obj; 96 97 if (ar.exception != null) { 98 // Yikes, error, disable the setting 99 setEnabled(false); 100 // Set UI to current state 101 Log.i(LOG_TAG, "set preferred network type, exception=" + ar.exception); 102 mPhone.getPreferredNetworkType(obtainMessage(MESSAGE_GET_PREFERRED_NETWORK_TYPE)); 103 } else { 104 Log.i(LOG_TAG, "set preferred network type done"); 105 } 106 } 107 } 108 } 109