blob: 41442fe98aba770ebae71d7feaf4932b9f46b570 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001package com.android.phone;
2
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
4
5import android.content.Context;
6import android.os.AsyncResult;
7import android.os.Handler;
8import android.os.Message;
fionaxue46e69f2017-04-27 14:32:46 -07009import android.preference.SwitchPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070010import android.util.AttributeSet;
11import android.util.Log;
12
Aravind Sreekumarc94dea82018-04-10 15:34:32 -070013import com.android.internal.telephony.CommandException;
14import com.android.internal.telephony.Phone;
15
fionaxue46e69f2017-04-27 14:32:46 -070016public class CallWaitingSwitchPreference extends SwitchPreference {
17 private static final String LOG_TAG = "CallWaitingSwitchPreference";
Santos Cordon7d4ddf62013-07-10 11:58:08 -070018 private final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
19
20 private final MyHandler mHandler = new MyHandler();
Andrew Lee2b36ba22014-11-05 17:08:49 -080021 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022 private TimeConsumingPreferenceListener mTcpListener;
23
fionaxue46e69f2017-04-27 14:32:46 -070024 public CallWaitingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025 super(context, attrs, defStyle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026 }
27
fionaxue46e69f2017-04-27 14:32:46 -070028 public CallWaitingSwitchPreference(Context context, AttributeSet attrs) {
29 this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030 }
31
fionaxue46e69f2017-04-27 14:32:46 -070032 public CallWaitingSwitchPreference(Context context) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033 this(context, null);
34 }
35
Andrew Lee2b36ba22014-11-05 17:08:49 -080036 /* package */ void init(
37 TimeConsumingPreferenceListener listener, boolean skipReading, Phone phone) {
38 mPhone = phone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039 mTcpListener = listener;
40
41 if (!skipReading) {
42 mPhone.getCallWaiting(mHandler.obtainMessage(MyHandler.MESSAGE_GET_CALL_WAITING,
43 MyHandler.MESSAGE_GET_CALL_WAITING, MyHandler.MESSAGE_GET_CALL_WAITING));
44 if (mTcpListener != null) {
45 mTcpListener.onStarted(this, true);
46 }
47 }
48 }
49
50 @Override
51 protected void onClick() {
52 super.onClick();
53
54 mPhone.setCallWaiting(isChecked(),
55 mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_WAITING));
56 if (mTcpListener != null) {
57 mTcpListener.onStarted(this, false);
58 }
59 }
60
61 private class MyHandler extends Handler {
62 static final int MESSAGE_GET_CALL_WAITING = 0;
63 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 (mTcpListener != null) {
81 if (msg.arg2 == MESSAGE_SET_CALL_WAITING) {
fionaxue46e69f2017-04-27 14:32:46 -070082 mTcpListener.onFinished(CallWaitingSwitchPreference.this, false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083 } else {
fionaxue46e69f2017-04-27 14:32:46 -070084 mTcpListener.onFinished(CallWaitingSwitchPreference.this, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085 }
86 }
87
Anthony Lee2333fc12014-11-11 15:43:21 -080088 if (ar.exception instanceof CommandException) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089 if (DBG) {
Anthony Lee2333fc12014-11-11 15:43:21 -080090 Log.d(LOG_TAG, "handleGetCallWaitingResponse: CommandException=" +
91 ar.exception);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092 }
93 if (mTcpListener != null) {
fionaxue46e69f2017-04-27 14:32:46 -070094 mTcpListener.onException(CallWaitingSwitchPreference.this,
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 (CommandException)ar.exception);
96 }
Anthony Lee2333fc12014-11-11 15:43:21 -080097 } else if (ar.userObj instanceof Throwable || ar.exception != null) {
98 // Still an error case but just not a CommandException.
99 if (DBG) {
100 Log.d(LOG_TAG, "handleGetCallWaitingResponse: Exception" + ar.exception);
101 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 if (mTcpListener != null) {
fionaxue46e69f2017-04-27 14:32:46 -0700103 mTcpListener.onError(CallWaitingSwitchPreference.this, RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104 }
105 } else {
106 if (DBG) {
107 Log.d(LOG_TAG, "handleGetCallWaitingResponse: CW state successfully queried.");
108 }
109 int[] cwArray = (int[])ar.result;
110 // If cwArray[0] is = 1, then cwArray[1] must follow,
111 // with the TS 27.007 service class bit vector of services
112 // for which call waiting is enabled.
113 try {
114 setChecked(((cwArray[0] == 1) && ((cwArray[1] & 0x01) == 0x01)));
115 } catch (ArrayIndexOutOfBoundsException e) {
116 Log.e(LOG_TAG, "handleGetCallWaitingResponse: improper result: err ="
117 + e.getMessage());
118 }
119 }
120 }
121
122 private void handleSetCallWaitingResponse(Message msg) {
123 AsyncResult ar = (AsyncResult) msg.obj;
124
125 if (ar.exception != null) {
126 if (DBG) {
127 Log.d(LOG_TAG, "handleSetCallWaitingResponse: ar.exception=" + ar.exception);
128 }
129 //setEnabled(false);
130 }
131 if (DBG) Log.d(LOG_TAG, "handleSetCallWaitingResponse: re get");
132
133 mPhone.getCallWaiting(obtainMessage(MESSAGE_GET_CALL_WAITING,
134 MESSAGE_SET_CALL_WAITING, MESSAGE_SET_CALL_WAITING, ar.exception));
135 }
136 }
137}