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