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 import com.android.internal.telephony.PhoneFactory;
      6 
      7 import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
      8 
      9 import android.content.Context;
     10 import android.os.AsyncResult;
     11 import android.os.Handler;
     12 import android.os.Message;
     13 import android.preference.CheckBoxPreference;
     14 import android.util.AttributeSet;
     15 import android.util.Log;
     16 
     17 import com.android.internal.telephony.Phone;
     18 import com.android.internal.telephony.PhoneFactory;
     19 
     20 public class CallWaitingCheckBoxPreference extends CheckBoxPreference {
     21     private static final String LOG_TAG = "CallWaitingCheckBoxPreference";
     22     private final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);
     23 
     24     private final MyHandler mHandler = new MyHandler();
     25     Phone phone;
     26     TimeConsumingPreferenceListener tcpListener;
     27 
     28     public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
     29         super(context, attrs, defStyle);
     30 
     31         phone = PhoneFactory.getDefaultPhone();
     32     }
     33 
     34     public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs) {
     35         this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
     36     }
     37 
     38     public CallWaitingCheckBoxPreference(Context context) {
     39         this(context, null);
     40     }
     41 
     42     void init(TimeConsumingPreferenceListener listener, boolean skipReading) {
     43         tcpListener = listener;
     44 
     45         if (!skipReading) {
     46             phone.getCallWaiting(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CALL_WAITING,
     47                     MyHandler.MESSAGE_GET_CALL_WAITING, MyHandler.MESSAGE_GET_CALL_WAITING));
     48             if (tcpListener != null) {
     49                 tcpListener.onStarted(this, true);
     50             }
     51         }
     52     }
     53 
     54     @Override
     55     protected void onClick() {
     56         super.onClick();
     57 
     58         phone.setCallWaiting(isChecked(),
     59                 mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING));
     60         if (tcpListener != null) {
     61             tcpListener.onStarted(this, false);
     62         }
     63     }
     64 
     65     private class MyHandler extends Handler {
     66         private static final int MESSAGE_GET_CALL_WAITING = 0;
     67         private static final int MESSAGE_SET_CALL_WAITING = 1;
     68 
     69         @Override
     70         public void handleMessage(Message msg) {
     71             switch (msg.what) {
     72                 case MESSAGE_GET_CALL_WAITING:
     73                     handleGetCallWaitingResponse(msg);
     74                     break;
     75                 case MESSAGE_SET_CALL_WAITING:
     76                     handleSetCallWaitingResponse(msg);
     77                     break;
     78             }
     79         }
     80 
     81         private void handleGetCallWaitingResponse(Message msg) {
     82             AsyncResult ar = (AsyncResult) msg.obj;
     83 
     84             if (tcpListener != null) {
     85                 if (msg.arg2 == MESSAGE_SET_CALL_WAITING) {
     86                     tcpListener.onFinished(CallWaitingCheckBoxPreference.this, false);
     87                 } else {
     88                     tcpListener.onFinished(CallWaitingCheckBoxPreference.this, true);
     89                 }
     90             }
     91 
     92             if (ar.exception != null) {
     93                 if (DBG) {
     94                     Log.d(LOG_TAG, "handleGetCallWaitingResponse: ar.exception=" + ar.exception);
     95                 }
     96                 if (tcpListener != null) {
     97                     tcpListener.onException(CallWaitingCheckBoxPreference.this,
     98                             (CommandException)ar.exception);
     99                 }
    100             } else if (ar.userObj instanceof Throwable) {
    101                 if (tcpListener != null) tcpListener.onError(CallWaitingCheckBoxPreference.this, RESPONSE_ERROR);
    102             } else {
    103                 if (DBG) Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried.");
    104                 setChecked(((int[]) ar.result)[0] == 1);
    105             }
    106         }
    107 
    108         private void handleSetCallWaitingResponse(Message msg) {
    109             AsyncResult ar = (AsyncResult) msg.obj;
    110 
    111             if (ar.exception != null) {
    112                 if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception);
    113                 //setEnabled(false);
    114             }
    115             if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
    116 
    117             phone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING,
    118                     MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception));
    119         }
    120     }
    121 }
    122