• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
6 import com.android.internal.telephony.Phone;
7 import com.android.internal.telephony.PhoneFactory;
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 public class CallWaitingCheckBoxPreference extends CheckBoxPreference {
18     private static final String LOG_TAG = "CallWaitingCheckBoxPreference";
19     private final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);
20 
21     private MyHandler mHandler = new MyHandler();
22     Phone phone;
23     TimeConsumingPreferenceListener tcpListener;
24 
CallWaitingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle)25     public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
26         super(context, attrs, defStyle);
27 
28         phone = PhoneFactory.getDefaultPhone();
29     }
30 
CallWaitingCheckBoxPreference(Context context, AttributeSet attrs)31     public CallWaitingCheckBoxPreference(Context context, AttributeSet attrs) {
32         this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
33     }
34 
CallWaitingCheckBoxPreference(Context context)35     public CallWaitingCheckBoxPreference(Context context) {
36         this(context, null);
37     }
38 
init(TimeConsumingPreferenceListener listener, boolean skipReading)39     void init(TimeConsumingPreferenceListener listener, boolean skipReading) {
40         tcpListener = listener;
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
onClick()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
handleMessage(Message msg)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 
handleGetCallWaitingResponse(Message msg)77         private void handleGetCallWaitingResponse(Message msg) {
78             AsyncResult ar = (AsyncResult) msg.obj;
79 
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             if (ar.exception != null) {
87                 if (DBG) Log.d(LOG_TAG, "handleGetCallWaitingResponse: ar.exception=" + ar.exception);
88                 setEnabled(false);
89                 tcpListener.onError(CallWaitingCheckBoxPreference.this, EXCEPTION_ERROR);
90             } else if (ar.userObj instanceof Throwable) {
91                 tcpListener.onError(CallWaitingCheckBoxPreference.this, RESPONSE_ERROR);
92             } else {
93                 if (DBG) Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried.");
94                 setChecked(((int[]) ar.result)[0] == 1);
95             }
96         }
97 
handleSetCallWaitingResponse(Message msg)98         private void handleSetCallWaitingResponse(Message msg) {
99             AsyncResult ar = (AsyncResult) msg.obj;
100 
101             if (ar.exception != null) {
102                 if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception);
103                 //setEnabled(false);
104             }
105             if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
106 
107             phone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING,
108                     MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception));
109         }
110     }
111 }
112