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 
     29 public class Use2GOnlyCheckBoxPreference extends CheckBoxPreference {
     30     private static final String LOG_TAG = "Use2GOnlyCheckBoxPreference";
     31 
     32     private Phone mPhone;
     33     private MyHandler mHandler;
     34 
     35     public Use2GOnlyCheckBoxPreference(Context context) {
     36         this(context, null);
     37     }
     38 
     39     public Use2GOnlyCheckBoxPreference(Context context, AttributeSet attrs) {
     40         this(context, attrs,com.android.internal.R.attr.checkBoxPreferenceStyle);
     41     }
     42 
     43     public Use2GOnlyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
     44         super(context, attrs, defStyle);
     45         mPhone = PhoneApp.getPhone();
     46         mHandler = new MyHandler();
     47         mPhone.getPreferredNetworkType(
     48                 mHandler.obtainMessage(MyHandler.MESSAGE_GET_PREFERRED_NETWORK_TYPE));
     49     }
     50 
     51     @Override
     52     protected void  onClick() {
     53         super.onClick();
     54 
     55         int networkType = isChecked() ? Phone.NT_MODE_GSM_ONLY : Phone.NT_MODE_WCDMA_PREF;
     56         Log.i(LOG_TAG, "set preferred network type="+networkType);
     57         android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
     58                 android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, networkType);
     59         mPhone.setPreferredNetworkType(networkType, mHandler
     60                 .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
     61    }
     62 
     63     private class MyHandler extends Handler {
     64 
     65         static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0;
     66         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                 if (type != Phone.NT_MODE_GSM_ONLY) {
     87                     // Allow only NT_MODE_GSM_ONLY or NT_MODE_WCDMA_PREF
     88                     type = Phone.NT_MODE_WCDMA_PREF;
     89                 }
     90                 Log.i(LOG_TAG, "get preferred network type="+type);
     91                 setChecked(type == Phone.NT_MODE_GSM_ONLY);
     92                 android.provider.Settings.Secure.putInt(mPhone.getContext().getContentResolver(),
     93                         android.provider.Settings.Secure.PREFERRED_NETWORK_MODE, type);
     94             } else {
     95                 // Weird state, disable the setting
     96                 Log.i(LOG_TAG, "get preferred network type, exception="+ar.exception);
     97                 setEnabled(false);
     98             }
     99         }
    100 
    101         private void handleSetPreferredNetworkTypeResponse(Message msg) {
    102             AsyncResult ar = (AsyncResult) msg.obj;
    103 
    104             if (ar.exception != null) {
    105                 // Yikes, error, disable the setting
    106                 setEnabled(false);
    107                 // Set UI to current state
    108                 Log.i(LOG_TAG, "set preferred network type, exception=" + ar.exception);
    109                 mPhone.getPreferredNetworkType(obtainMessage(MESSAGE_GET_PREFERRED_NETWORK_TYPE));
    110             } else {
    111                 Log.i(LOG_TAG, "set preferred network type done");
    112             }
    113         }
    114     }
    115 }
    116