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.Bundle;
     22 import android.os.SystemProperties;
     23 import android.os.Handler;
     24 import android.os.Message;
     25 import android.preference.ListPreference;
     26 import android.provider.Settings;
     27 import android.provider.Settings.Secure;
     28 import android.util.AttributeSet;
     29 import android.util.Log;
     30 
     31 import com.android.internal.telephony.Phone;
     32 import com.android.internal.telephony.TelephonyProperties;
     33 
     34 public class CdmaSystemSelectListPreference extends ListPreference {
     35 
     36     private static final String LOG_TAG = "CdmaRoamingListPreference";
     37     private static final boolean DBG = false;
     38 
     39     private Phone mPhone;
     40     private MyHandler mHandler = new MyHandler();
     41 
     42     public CdmaSystemSelectListPreference(Context context, AttributeSet attrs) {
     43         super(context, attrs);
     44 
     45         mPhone = PhoneGlobals.getPhone();
     46         mHandler = new MyHandler();
     47         mPhone.queryCdmaRoamingPreference(
     48                 mHandler.obtainMessage(MyHandler.MESSAGE_GET_ROAMING_PREFERENCE));
     49     }
     50 
     51     public CdmaSystemSelectListPreference(Context context) {
     52         this(context, null);
     53     }
     54 
     55     @Override
     56     protected void showDialog(Bundle state) {
     57         if (mPhone.isInEcm()) {
     58             // In ECM mode do not show selection options
     59         } else {
     60             super.showDialog(state);
     61         }
     62     }
     63 
     64     @Override
     65     protected void onDialogClosed(boolean positiveResult) {
     66         super.onDialogClosed(positiveResult);
     67 
     68         if (positiveResult && (getValue() != null)) {
     69             int buttonCdmaRoamingMode = Integer.parseInt(getValue());
     70             int settingsCdmaRoamingMode =
     71                     Settings.Global.getInt(mPhone.getContext().getContentResolver(),
     72                     Settings.Global.CDMA_ROAMING_MODE, Phone.CDMA_RM_HOME);
     73             if (buttonCdmaRoamingMode != settingsCdmaRoamingMode) {
     74                 int statusCdmaRoamingMode;
     75                 switch(buttonCdmaRoamingMode) {
     76                     case Phone.CDMA_RM_ANY:
     77                         statusCdmaRoamingMode = Phone.CDMA_RM_ANY;
     78                         break;
     79                     case Phone.CDMA_RM_HOME:
     80                     default:
     81                         statusCdmaRoamingMode = Phone.CDMA_RM_HOME;
     82                 }
     83                 //Set the Settings.Secure network mode
     84                 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
     85                         Settings.Global.CDMA_ROAMING_MODE,
     86                         buttonCdmaRoamingMode );
     87                 //Set the roaming preference mode
     88                 mPhone.setCdmaRoamingPreference(statusCdmaRoamingMode, mHandler
     89                         .obtainMessage(MyHandler.MESSAGE_SET_ROAMING_PREFERENCE));
     90             }
     91         } else {
     92             Log.d(LOG_TAG, String.format("onDialogClosed: positiveResult=%b value=%s -- do nothing",
     93                     positiveResult, getValue()));
     94         }
     95     }
     96 
     97     private class MyHandler extends Handler {
     98 
     99         static final int MESSAGE_GET_ROAMING_PREFERENCE = 0;
    100         static final int MESSAGE_SET_ROAMING_PREFERENCE = 1;
    101 
    102         @Override
    103         public void handleMessage(Message msg) {
    104             switch (msg.what) {
    105                 case MESSAGE_GET_ROAMING_PREFERENCE:
    106                     handleQueryCdmaRoamingPreference(msg);
    107                     break;
    108 
    109                 case MESSAGE_SET_ROAMING_PREFERENCE:
    110                     handleSetCdmaRoamingPreference(msg);
    111                     break;
    112             }
    113         }
    114 
    115         private void handleQueryCdmaRoamingPreference(Message msg) {
    116             AsyncResult ar = (AsyncResult) msg.obj;
    117 
    118             if (ar.exception == null) {
    119                 int statusCdmaRoamingMode = ((int[])ar.result)[0];
    120                 int settingsRoamingMode = Settings.Global.getInt(
    121                         mPhone.getContext().getContentResolver(),
    122                         Settings.Global.CDMA_ROAMING_MODE, Phone.CDMA_RM_HOME);
    123                 //check that statusCdmaRoamingMode is from an accepted value
    124                 if (statusCdmaRoamingMode == Phone.CDMA_RM_HOME ||
    125                         statusCdmaRoamingMode == Phone.CDMA_RM_ANY ) {
    126                     //check changes in statusCdmaRoamingMode and updates settingsRoamingMode
    127                     if (statusCdmaRoamingMode != settingsRoamingMode) {
    128                         settingsRoamingMode = statusCdmaRoamingMode;
    129                         //changes the Settings.Secure accordingly to statusCdmaRoamingMode
    130                         Settings.Global.putInt(
    131                                 mPhone.getContext().getContentResolver(),
    132                                 Settings.Global.CDMA_ROAMING_MODE,
    133                                 settingsRoamingMode );
    134                     }
    135                     //changes the mButtonPreferredNetworkMode accordingly to modemNetworkMode
    136                     setValue(Integer.toString(statusCdmaRoamingMode));
    137                 }
    138                 else {
    139                     if(DBG) Log.i(LOG_TAG, "reset cdma roaming mode to default" );
    140                     resetCdmaRoamingModeToDefault();
    141                 }
    142             }
    143         }
    144 
    145         private void handleSetCdmaRoamingPreference(Message msg) {
    146             AsyncResult ar = (AsyncResult) msg.obj;
    147 
    148             if ((ar.exception == null) && (getValue() != null)) {
    149                 int cdmaRoamingMode = Integer.parseInt(getValue());
    150                 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
    151                         Settings.Global.CDMA_ROAMING_MODE,
    152                         cdmaRoamingMode );
    153             } else {
    154                 mPhone.queryCdmaRoamingPreference(obtainMessage(MESSAGE_GET_ROAMING_PREFERENCE));
    155             }
    156         }
    157 
    158         private void resetCdmaRoamingModeToDefault() {
    159             //set the mButtonCdmaRoam
    160             setValue(Integer.toString(Phone.CDMA_RM_ANY));
    161             //set the Settings.System
    162             Settings.Global.putInt(mPhone.getContext().getContentResolver(),
    163                         Settings.Global.CDMA_ROAMING_MODE,
    164                         Phone.CDMA_RM_ANY );
    165             //Set the Status
    166             mPhone.setCdmaRoamingPreference(Phone.CDMA_RM_ANY,
    167                     obtainMessage(MyHandler.MESSAGE_SET_ROAMING_PREFERENCE));
    168         }
    169     }
    170 
    171 }
    172