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.os.SystemProperties;
     24 import android.preference.CheckBoxPreference;
     25 import android.util.AttributeSet;
     26 import android.util.Log;
     27 
     28 import com.android.internal.telephony.Phone;
     29 
     30 public class Use2GOnlyCheckBoxPreference extends CheckBoxPreference {
     31     private static final String LOG_TAG = "Use2GOnlyCheckBoxPreference";
     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 = PhoneGlobals.getPhone();
     47         mHandler = new MyHandler();
     48         mPhone.getPreferredNetworkType(
     49                 mHandler.obtainMessage(MyHandler.MESSAGE_GET_PREFERRED_NETWORK_TYPE));
     50     }
     51 
     52     private int getDefaultNetworkMode() {
     53         int mode = SystemProperties.getInt("ro.telephony.default_network",
     54                 Phone.PREFERRED_NT_MODE);
     55         Log.i(LOG_TAG, "getDefaultNetworkMode: mode=" + mode);
     56         return mode;
     57     }
     58 
     59     @Override
     60     protected void  onClick() {
     61         super.onClick();
     62 
     63         int networkType = isChecked() ? Phone.NT_MODE_GSM_ONLY : getDefaultNetworkMode();
     64         Log.i(LOG_TAG, "set preferred network type="+networkType);
     65         android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
     66                 android.provider.Settings.Global.PREFERRED_NETWORK_MODE, networkType);
     67         mPhone.setPreferredNetworkType(networkType, mHandler
     68                 .obtainMessage(MyHandler.MESSAGE_SET_PREFERRED_NETWORK_TYPE));
     69    }
     70 
     71     private class MyHandler extends Handler {
     72 
     73         static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0;
     74         static final int MESSAGE_SET_PREFERRED_NETWORK_TYPE = 1;
     75 
     76         @Override
     77         public void handleMessage(Message msg) {
     78             switch (msg.what) {
     79                 case MESSAGE_GET_PREFERRED_NETWORK_TYPE:
     80                     handleGetPreferredNetworkTypeResponse(msg);
     81                     break;
     82 
     83                 case MESSAGE_SET_PREFERRED_NETWORK_TYPE:
     84                     handleSetPreferredNetworkTypeResponse(msg);
     85                     break;
     86             }
     87         }
     88 
     89         private void handleGetPreferredNetworkTypeResponse(Message msg) {
     90             AsyncResult ar = (AsyncResult) msg.obj;
     91 
     92             if (ar.exception == null) {
     93                 int type = ((int[])ar.result)[0];
     94                 if (type != Phone.NT_MODE_GSM_ONLY) {
     95                     // Back to default
     96                     type = getDefaultNetworkMode();
     97                 }
     98                 Log.i(LOG_TAG, "get preferred network type="+type);
     99                 setChecked(type == Phone.NT_MODE_GSM_ONLY);
    100                 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
    101                         android.provider.Settings.Global.PREFERRED_NETWORK_MODE, type);
    102             } else {
    103                 // Weird state, disable the setting
    104                 Log.i(LOG_TAG, "get preferred network type, exception="+ar.exception);
    105                 setEnabled(false);
    106             }
    107         }
    108 
    109         private void handleSetPreferredNetworkTypeResponse(Message msg) {
    110             AsyncResult ar = (AsyncResult) msg.obj;
    111 
    112             if (ar.exception != null) {
    113                 // Yikes, error, disable the setting
    114                 setEnabled(false);
    115                 // Set UI to current state
    116                 Log.i(LOG_TAG, "set preferred network type, exception=" + ar.exception);
    117                 mPhone.getPreferredNetworkType(obtainMessage(MESSAGE_GET_PREFERRED_NETWORK_TYPE));
    118             } else {
    119                 Log.i(LOG_TAG, "set preferred network type done");
    120             }
    121         }
    122     }
    123 }
    124