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