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.Handler;
     23 import android.os.Message;
     24 import android.preference.ListPreference;
     25 import android.provider.Settings.Secure;
     26 import android.util.AttributeSet;
     27 import android.util.Log;
     28 
     29 import com.android.internal.telephony.Phone;
     30 import com.android.internal.telephony.PhoneFactory;
     31 
     32 public class CdmaSubscriptionListPreference extends ListPreference {
     33 
     34     private static final String LOG_TAG = "CdmaSubscriptionListPreference";
     35 
     36     // Used for CDMA subscription mode
     37     private static final int CDMA_SUBSCRIPTION_RUIM_SIM = 0;
     38     private static final int CDMA_SUBSCRIPTION_NV = 1;
     39 
     40     //preferredSubscriptionMode  0 - RUIM/SIM, preferred
     41     //                           1 - NV
     42     static final int preferredSubscriptionMode = CDMA_SUBSCRIPTION_NV;
     43 
     44     private Phone mPhone;
     45     private CdmaSubscriptionButtonHandler mHandler;
     46 
     47     public CdmaSubscriptionListPreference(Context context, AttributeSet attrs) {
     48         super(context, attrs);
     49 
     50         mPhone = PhoneFactory.getDefaultPhone();
     51         mHandler = new CdmaSubscriptionButtonHandler();
     52         setCurrentCdmaSubscriptionModeValue();
     53     }
     54 
     55     private void setCurrentCdmaSubscriptionModeValue() {
     56         int cdmaSubscriptionMode = Secure.getInt(mPhone.getContext().getContentResolver(),
     57                 android.provider.Settings.Secure.CDMA_SUBSCRIPTION_MODE, preferredSubscriptionMode);
     58         setValue(Integer.toString(cdmaSubscriptionMode));
     59     }
     60 
     61     public CdmaSubscriptionListPreference(Context context) {
     62         this(context, null);
     63     }
     64 
     65     @Override
     66     protected void showDialog(Bundle state) {
     67         setCurrentCdmaSubscriptionModeValue();
     68 
     69         super.showDialog(state);
     70     }
     71 
     72     @Override
     73     protected void onDialogClosed(boolean positiveResult) {
     74         super.onDialogClosed(positiveResult);
     75 
     76         if (!positiveResult) {
     77             //The button was dismissed - no need to set new value
     78             return;
     79         }
     80 
     81         int buttonCdmaSubscriptionMode = Integer.valueOf(getValue()).intValue();
     82         Log.d(LOG_TAG, "Setting new value " + buttonCdmaSubscriptionMode);
     83         int statusCdmaSubscriptionMode;
     84         switch(buttonCdmaSubscriptionMode) {
     85             case CDMA_SUBSCRIPTION_NV:
     86                 statusCdmaSubscriptionMode = Phone.CDMA_SUBSCRIPTION_NV;
     87                 break;
     88             case CDMA_SUBSCRIPTION_RUIM_SIM:
     89                 statusCdmaSubscriptionMode = Phone.CDMA_SUBSCRIPTION_RUIM_SIM;
     90                 break;
     91             default:
     92                 statusCdmaSubscriptionMode = Phone.PREFERRED_CDMA_SUBSCRIPTION;
     93         }
     94 
     95         // Set the CDMA subscription mode, when mode has been successfully changed
     96         // handleSetCdmaSubscriptionMode will be invoked and the value saved.
     97         mPhone.setCdmaSubscription(statusCdmaSubscriptionMode, mHandler
     98                 .obtainMessage(CdmaSubscriptionButtonHandler.MESSAGE_SET_CDMA_SUBSCRIPTION,
     99                         getValue()));
    100 
    101     }
    102 
    103     private class CdmaSubscriptionButtonHandler extends Handler {
    104 
    105         private static final int MESSAGE_SET_CDMA_SUBSCRIPTION = 0;
    106 
    107         @Override
    108         public void handleMessage(Message msg) {
    109             switch (msg.what) {
    110                 case MESSAGE_SET_CDMA_SUBSCRIPTION:
    111                     handleSetCdmaSubscriptionMode(msg);
    112                     break;
    113             }
    114         }
    115 
    116         private void handleSetCdmaSubscriptionMode(Message msg) {
    117             mPhone = PhoneFactory.getDefaultPhone();
    118             AsyncResult ar = (AsyncResult) msg.obj;
    119 
    120             if (ar.exception == null) {
    121                 // Get the original string entered by the user
    122                 int cdmaSubscriptionMode = Integer.valueOf((String) ar.userObj).intValue();
    123                 Secure.putInt(mPhone.getContext().getContentResolver(),
    124                         Secure.CDMA_SUBSCRIPTION_MODE,
    125                         cdmaSubscriptionMode );
    126             } else {
    127                 Log.e(LOG_TAG, "Setting Cdma subscription source failed");
    128             }
    129         }
    130     }
    131 }
    132