Use2GOnlyCheckBoxPreference.java revision e64151a3e3c27689f242fc59aa7d888cb70310f6
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.Handler; 22import android.os.Message; 23import android.preference.CheckBoxPreference; 24import android.util.AttributeSet; 25import android.util.Log; 26 27import com.android.internal.telephony.Phone; 28 29public 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 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(), 59 android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, networkType); 60 mPhone.setPreferredNetworkType(networkType, mHandler 61 .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE)); 62 } 63 64 private class MyHandler extends Handler { 65 66 private static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0; 67 private static final int MESSAGE_SET_PREFERRED_NETWORK_TYPE = 1; 68 69 @Override 70 public void handleMessage(Message msg) { 71 switch (msg.what) { 72 case MESSAGE_GET_PREFERRED_NETWORK_TYPE: 73 handleGetPreferredNetworkTypeResponse(msg); 74 break; 75 76 case MESSAGE_SET_PREFERRED_NETWORK_TYPE: 77 handleSetPreferredNetworkTypeResponse(msg); 78 break; 79 } 80 } 81 82 private void handleGetPreferredNetworkTypeResponse(Message msg) { 83 AsyncResult ar = (AsyncResult) msg.obj; 84 85 if (ar.exception == null) { 86 int type = ((int[])ar.result)[0]; 87 Log.i(LOG_TAG, "get preferred network type="+type); 88 setChecked(type == Phone.NT_MODE_GSM_ONLY); 89 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(), 90 android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, type); 91 } else { 92 // Weird state, disable the setting 93 Log.i(LOG_TAG, "get preferred network type, exception="+ar.exception); 94 setEnabled(false); 95 } 96 } 97 98 private void handleSetPreferredNetworkTypeResponse(Message msg) { 99 AsyncResult ar = (AsyncResult) msg.obj; 100 101 if (ar.exception != null) { 102 // Yikes, error, disable the setting 103 setEnabled(false); 104 // Set UI to current state 105 Log.i(LOG_TAG, "set preferred network type, exception=" + ar.exception); 106 mPhone.getPreferredNetworkType(obtainMessage(MESSAGE_GET_PREFERRED_NETWORK_TYPE)); 107 } else { 108 Log.i(LOG_TAG, "set preferred network type done"); 109 } 110 } 111 } 112} 113