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