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