Home | History | Annotate | Download | only in phone
      1 package com.android.phone;
      2 
      3 import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
      4 import com.android.internal.telephony.CommandException;
      5 import com.android.internal.telephony.CommandsInterface;
      6 import com.android.internal.telephony.Phone;
      7 import com.android.internal.telephony.PhoneFactory;
      8 
      9 import android.content.Context;
     10 import android.os.AsyncResult;
     11 import android.os.Handler;
     12 import android.os.Message;
     13 import android.os.Parcelable;
     14 import android.preference.ListPreference;
     15 import android.util.AttributeSet;
     16 import android.util.Log;
     17 
     18 public class CLIRListPreference extends ListPreference {
     19     private static final String LOG_TAG = "CLIRListPreference";
     20     private final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);
     21 
     22     private MyHandler mHandler = new MyHandler();
     23     Phone phone;
     24     TimeConsumingPreferenceListener tcpListener;
     25 
     26     int clirArray[];
     27 
     28     public CLIRListPreference(Context context, AttributeSet attrs) {
     29         super(context, attrs);
     30 
     31         phone = PhoneFactory.getDefaultPhone();
     32     }
     33 
     34     public CLIRListPreference(Context context) {
     35         this(context, null);
     36     }
     37 
     38     @Override
     39     protected void onDialogClosed(boolean positiveResult) {
     40         super.onDialogClosed(positiveResult);
     41 
     42         phone.setOutgoingCallerIdDisplay(findIndexOfValue(getValue()),
     43                 mHandler.obtainMessage(MyHandler.MESSAGE_SET_CLIR));
     44         if (tcpListener != null) {
     45             tcpListener.onStarted(this, false);
     46         }
     47     }
     48 
     49     void init(TimeConsumingPreferenceListener listener, boolean skipReading) {
     50         tcpListener = listener;
     51         if (!skipReading) {
     52             phone.getOutgoingCallerIdDisplay(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CLIR,
     53                     MyHandler.MESSAGE_GET_CLIR, MyHandler.MESSAGE_GET_CLIR));
     54             if (tcpListener != null) {
     55                 tcpListener.onStarted(this, true);
     56             }
     57         }
     58     }
     59 
     60     void handleGetCLIRResult(int tmpClirArray[]) {
     61         clirArray = tmpClirArray;
     62         final boolean enabled = tmpClirArray[1] == 1 || tmpClirArray[1] == 3 || tmpClirArray[1] == 4;
     63         setEnabled(enabled);
     64 
     65         // set the value of the preference based upon the clirArgs.
     66         int value = CommandsInterface.CLIR_DEFAULT;
     67         switch (tmpClirArray[1]) {
     68             case 1: // Permanently provisioned
     69             case 3: // Temporary presentation disallowed
     70             case 4: // Temporary presentation allowed
     71                 switch (tmpClirArray[0]) {
     72                     case 1: // CLIR invoked
     73                         value = CommandsInterface.CLIR_INVOCATION;
     74                         break;
     75                     case 2: // CLIR suppressed
     76                         value = CommandsInterface.CLIR_SUPPRESSION;
     77                         break;
     78                     case 0: // Network default
     79                     default:
     80                         value = CommandsInterface.CLIR_DEFAULT;
     81                         break;
     82                 }
     83                 break;
     84             case 0: // Not Provisioned
     85             case 2: // Unknown (network error, etc)
     86             default:
     87                 value = CommandsInterface.CLIR_DEFAULT;
     88                 break;
     89         }
     90         setValueIndex(value);
     91 
     92         // set the string summary to reflect the value
     93         int summary = R.string.sum_default_caller_id;
     94         switch (value) {
     95             case CommandsInterface.CLIR_SUPPRESSION:
     96                 summary = R.string.sum_show_caller_id;
     97                 break;
     98             case CommandsInterface.CLIR_INVOCATION:
     99                 summary = R.string.sum_hide_caller_id;
    100                 break;
    101             case CommandsInterface.CLIR_DEFAULT:
    102                 summary = R.string.sum_default_caller_id;
    103                 break;
    104         }
    105         setSummary(summary);
    106     }
    107 
    108     private class MyHandler extends Handler {
    109         private static final int MESSAGE_GET_CLIR = 0;
    110         private static final int MESSAGE_SET_CLIR = 1;
    111 
    112         @Override
    113         public void handleMessage(Message msg) {
    114             switch (msg.what) {
    115                 case MESSAGE_GET_CLIR:
    116                     handleGetCLIRResponse(msg);
    117                     break;
    118                 case MESSAGE_SET_CLIR:
    119                     handleSetCLIRResponse(msg);
    120                     break;
    121             }
    122         }
    123 
    124         private void handleGetCLIRResponse(Message msg) {
    125             AsyncResult ar = (AsyncResult) msg.obj;
    126 
    127             if (msg.arg2 == MESSAGE_SET_CLIR) {
    128                 tcpListener.onFinished(CLIRListPreference.this, false);
    129             } else {
    130                 tcpListener.onFinished(CLIRListPreference.this, true);
    131             }
    132             clirArray = null;
    133             if (ar.exception != null) {
    134                 if (DBG) Log.d(LOG_TAG, "handleGetCLIRResponse: ar.exception="+ar.exception);
    135                 tcpListener.onException(CLIRListPreference.this, (CommandException) ar.exception);
    136             } else if (ar.userObj instanceof Throwable) {
    137                 tcpListener.onError(CLIRListPreference.this, RESPONSE_ERROR);
    138             } else {
    139                 int clirArray[] = (int[]) ar.result;
    140                 if (clirArray.length != 2) {
    141                     tcpListener.onError(CLIRListPreference.this, RESPONSE_ERROR);
    142                 } else {
    143                     if (DBG) Log.d(LOG_TAG, "handleGetCLIRResponse: CLIR successfully queried, clirArray[0]="
    144                             + clirArray[0] + ", clirArray[1]=" + clirArray[1]);
    145                     handleGetCLIRResult(clirArray);
    146                 }
    147             }
    148         }
    149 
    150         private void handleSetCLIRResponse(Message msg) {
    151             AsyncResult ar = (AsyncResult) msg.obj;
    152 
    153             if (ar.exception != null) {
    154                 if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception="+ar.exception);
    155                 //setEnabled(false);
    156             }
    157             if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
    158 
    159             phone.getOutgoingCallerIdDisplay(obtainMessage(MESSAGE_GET_CLIR,
    160                     MESSAGE_SET_CLIR, MESSAGE_SET_CLIR, ar.exception));
    161         }
    162     }
    163 }