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