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