Home | History | Annotate | Download | only in phone
      1 package com.android.phone;
      2 
      3 import com.android.internal.telephony.CommandException;
      4 import com.android.internal.telephony.Phone;
      5 
      6 import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
      7 
      8 import android.content.Context;
      9 import android.os.AsyncResult;
     10 import android.os.Handler;
     11 import android.os.Message;
     12 import android.preference.CheckBoxPreference;
     13 import android.util.AttributeSet;
     14 import android.util.Log;
     15 
     16 import com.android.internal.telephony.Phone;
     17 
     18 public class CallWaitingCheckBoxPreference extends CheckBoxPreference {
     19     private static final String LOG_TAG = "CallWaitingCheckBoxPreference";
     20     private final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
     21 
     22     private final MyHandler mHandler = new MyHandler();
     23     private final Phone mPhone;
     24     private TimeConsumingPreferenceListener mTcpListener;
     25 
     26     public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
     27         super(context, attrs, defStyle);
     28 
     29         mPhone = PhoneGlobals.getPhone();
     30     }
     31 
     32     public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs) {
     33         this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
     34     }
     35 
     36     public CallWaitingCheckBoxPreference(Context context) {
     37         this(context, null);
     38     }
     39 
     40     /* package */ void init(TimeConsumingPreferenceListener listener, boolean skipReading) {
     41         mTcpListener = listener;
     42 
     43         if (!skipReading) {
     44             mPhone.getCallWaiting(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CALL_WAITING,
     45                     MyHandler.MESSAGE_GET_CALL_WAITING, MyHandler.MESSAGE_GET_CALL_WAITING));
     46             if (mTcpListener != null) {
     47                 mTcpListener.onStarted(this, true);
     48             }
     49         }
     50     }
     51 
     52     @Override
     53     protected void onClick() {
     54         super.onClick();
     55 
     56         mPhone.setCallWaiting(isChecked(),
     57                 mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING));
     58         if (mTcpListener != null) {
     59             mTcpListener.onStarted(this, false);
     60         }
     61     }
     62 
     63     private class MyHandler extends Handler {
     64         static final int MESSAGE_GET_CALL_WAITING = 0;
     65         static final int MESSAGE_SET_CALL_WAITING = 1;
     66 
     67         @Override
     68         public void handleMessage(Message msg) {
     69             switch (msg.what) {
     70                 case MESSAGE_GET_CALL_WAITING:
     71                     handleGetCallWaitingResponse(msg);
     72                     break;
     73                 case MESSAGE_SET_CALL_WAITING:
     74                     handleSetCallWaitingResponse(msg);
     75                     break;
     76             }
     77         }
     78 
     79         private void handleGetCallWaitingResponse(Message msg) {
     80             AsyncResult ar = (AsyncResult) msg.obj;
     81 
     82             if (mTcpListener != null) {
     83                 if (msg.arg2 == MESSAGE_SET_CALL_WAITING) {
     84                     mTcpListener.onFinished(CallWaitingCheckBoxPreference.this, false);
     85                 } else {
     86                     mTcpListener.onFinished(CallWaitingCheckBoxPreference.this, true);
     87                 }
     88             }
     89 
     90             if (ar.exception != null) {
     91                 if (DBG) {
     92                     Log.d(LOG_TAG, "handleGetCallWaitingResponse: ar.exception=" + ar.exception);
     93                 }
     94                 if (mTcpListener != null) {
     95                     mTcpListener.onException(CallWaitingCheckBoxPreference.this,
     96                             (CommandException)ar.exception);
     97                 }
     98             } else if (ar.userObj instanceof Throwable) {
     99                 if (mTcpListener != null) {
    100                     mTcpListener.onError(CallWaitingCheckBoxPreference.this, RESPONSE_ERROR);
    101                 }
    102             } else {
    103                 if (DBG) {
    104                     Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried.");
    105                 }
    106                 int[] cwArray = (int[])ar.result;
    107                 // If cwArray[0] is = 1, then cwArray[1] must follow,
    108                 // with the TS 27.007 service class bit vector of services
    109                 // for which call waiting is enabled.
    110                 try {
    111                     setChecked(((cwArray[0] == 1) && ((cwArray[1] & 0x01) == 0x01)));
    112                 } catch (ArrayIndexOutOfBoundsException e) {
    113                     Log.e(LOG_TAG, "handleGetCallWaitingResponse: improper result: err ="
    114                             + e.getMessage());
    115                 }
    116             }
    117         }
    118 
    119         private void handleSetCallWaitingResponse(Message msg) {
    120             AsyncResult ar = (AsyncResult) msg.obj;
    121 
    122             if (ar.exception != null) {
    123                 if (DBG) {
    124                     Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception);
    125                 }
    126                 //setEnabled(false);
    127             }
    128             if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
    129 
    130             mPhone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING,
    131                     MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception));
    132         }
    133     }
    134 }
    135