CallWaitingCheckBoxPreference.java revision 95eaf3985257317063592859e0b593cacc81808d
1package com.android.phone;
2
3import static com.android.phone.TimeConsumingPreferenceActivity.EXCEPTION_ERROR;
4import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
5import android.content.Context;
6import android.os.AsyncResult;
7import android.os.Handler;
8import android.os.Message;
9import android.preference.CheckBoxPreference;
10import android.util.AttributeSet;
11import android.util.Log;
12
13import com.android.internal.telephony.Phone;
14import com.android.internal.telephony.PhoneFactory;
15
16public 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        if (!skipReading) {
41            phone.getCallWaiting(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CALL_WAITING,
42                    MyHandler.MESSAGE_GET_CALL_WAITING, MyHandler.MESSAGE_GET_CALL_WAITING));
43            if (tcpListener != null) {
44                tcpListener.onStarted(this, true);
45            }
46        }
47    }
48
49    @Override
50    protected void onClick() {
51        super.onClick();
52
53        phone.setCallWaiting(isChecked(),
54                mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING));
55        if (tcpListener != null) {
56            tcpListener.onStarted(this, false);
57        }
58    }
59
60    private class MyHandler extends Handler {
61        private static final int MESSAGE_GET_CALL_WAITING = 0;
62        private static final int MESSAGE_SET_CALL_WAITING = 1;
63
64        @Override
65        public void handleMessage(Message msg) {
66            switch (msg.what) {
67                case MESSAGE_GET_CALL_WAITING:
68                    handleGetCallWaitingResponse(msg);
69                    break;
70                case MESSAGE_SET_CALL_WAITING:
71                    handleSetCallWaitingResponse(msg);
72                    break;
73            }
74        }
75
76        private void handleGetCallWaitingResponse(Message msg) {
77            AsyncResult ar = (AsyncResult) msg.obj;
78
79            if (tcpListener != null) {
80                if (msg.arg2 == MESSAGE_SET_CALL_WAITING) {
81                    tcpListener.onFinished(CallWaitingCheckBoxPreference.this, false);
82                } else {
83                    tcpListener.onFinished(CallWaitingCheckBoxPreference.this, true);
84                }
85            }
86
87            if (ar.exception != null) {
88                if (DBG) Log.d(LOG_TAG, "handleGetCallWaitingResponse: ar.exception=" + ar.exception);
89                setEnabled(false);
90                if (tcpListener != null) tcpListener.onError(CallWaitingCheckBoxPreference.this, EXCEPTION_ERROR);
91            } else if (ar.userObj instanceof Throwable) {
92                if (tcpListener != null) tcpListener.onError(CallWaitingCheckBoxPreference.this, RESPONSE_ERROR);
93            } else {
94                if (DBG) Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried.");
95                setChecked(((int[]) ar.result)[0] == 1);
96            }
97        }
98
99        private void handleSetCallWaitingResponse(Message msg) {
100            AsyncResult ar = (AsyncResult) msg.obj;
101
102            if (ar.exception != null) {
103                if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception);
104                //setEnabled(false);
105            }
106            if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
107
108            phone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING,
109                    MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception));
110        }
111    }
112}
113