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 = (PhoneApp.DBG_LEVEL >= 2); 21 22 private final MyHandler mHandler = new MyHandler(); 23 Phone phone; 24 TimeConsumingPreferenceListener tcpListener; 25 26 public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) { 27 super(context, attrs, defStyle); 28 29 phone = PhoneApp.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 void init(TimeConsumingPreferenceListener listener, boolean skipReading) { 41 tcpListener = listener; 42 43 if (!skipReading) { 44 phone.getCallWaiting(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CALL_WAITING, 45 MyHandler.MESSAGE_GET_CALL_WAITING, MyHandler.MESSAGE_GET_CALL_WAITING)); 46 if (tcpListener != null) { 47 tcpListener.onStarted(this, true); 48 } 49 } 50 } 51 52 @Override 53 protected void onClick() { 54 super.onClick(); 55 56 phone.setCallWaiting(isChecked(), 57 mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING)); 58 if (tcpListener != null) { 59 tcpListener.onStarted(this, false); 60 } 61 } 62 63 private class MyHandler extends Handler { 64 private static final int MESSAGE_GET_CALL_WAITING = 0; 65 private 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 (tcpListener != null) { 83 if (msg.arg2 == MESSAGE_SET_CALL_WAITING) { 84 tcpListener.onFinished(CallWaitingCheckBoxPreference.this, false); 85 } else { 86 tcpListener.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 (tcpListener != null) { 95 tcpListener.onException(CallWaitingCheckBoxPreference.this, 96 (CommandException)ar.exception); 97 } 98 } else if (ar.userObj instanceof Throwable) { 99 if (tcpListener != null) tcpListener.onError(CallWaitingCheckBoxPreference.this, RESPONSE_ERROR); 100 } else { 101 if (DBG) Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried."); 102 int[] cwArray = (int[])ar.result; 103 // If cwArray[0] is = 1, then cwArray[1] must follow, 104 // with the TS 27.007 service class bit vector of services 105 // for which call waiting is enabled. 106 try { 107 setChecked(((cwArray[0] == 1) && ((cwArray[1] & 0x01) == 0x01))); 108 } catch (ArrayIndexOutOfBoundsException e) { 109 Log.e(LOG_TAG, "handleGetCallWaitingResponse: improper result: err =" 110 + e.getMessage()); 111 } 112 } 113 } 114 115 private void handleSetCallWaitingResponse(Message msg) { 116 AsyncResult ar = (AsyncResult) msg.obj; 117 118 if (ar.exception != null) { 119 if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception); 120 //setEnabled(false); 121 } 122 if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get"); 123 124 phone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING, 125 MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception)); 126 } 127 } 128 } 129