blob: 7db932572b350b410c2247410f302379c417a24c [file] [log] [blame]
Santos Cordone3d76ab2014-01-28 17:25:20 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ben Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Santos Cordone3d76ab2014-01-28 17:25:20 -080019import android.os.Handler;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070020import android.os.Message;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070021
Evan Charlton352105c2014-06-03 14:10:54 -070022import com.android.internal.os.SomeArgs;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070023import com.android.internal.telecomm.IInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080024
Santos Cordone3d76ab2014-01-28 17:25:20 -080025/**
26 * Receives call commands and updates from in-call app and passes them through to CallsManager.
27 * {@link InCallController} creates an instance of this class and passes it to the in-call app after
28 * binding to it. This adapter can receive commands and updates until the in-call app is unbound.
29 */
30class InCallAdapter extends IInCallAdapter.Stub {
Sailesh Nepal10ea4602014-04-01 17:23:32 -070031 private static final int MSG_ANSWER_CALL = 0;
32 private static final int MSG_REJECT_CALL = 1;
33 private static final int MSG_PLAY_DTMF_TONE = 2;
34 private static final int MSG_STOP_DTMF_TONE = 3;
35 private static final int MSG_POST_DIAL_CONTINUE = 4;
36 private static final int MSG_DISCONNECT_CALL = 5;
37 private static final int MSG_HOLD_CALL = 6;
38 private static final int MSG_UNHOLD_CALL = 7;
Sailesh Nepal77da19e2014-07-02 21:31:16 -070039 private static final int MSG_PHONE_ACCOUNT_CLICKED = 8;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070040 private static final int MSG_MUTE = 9;
41 private static final int MSG_SET_AUDIO_ROUTE = 10;
Santos Cordona1610702014-06-04 20:22:56 -070042 private static final int MSG_CONFERENCE = 11;
43 private static final int MSG_SPLIT_FROM_CONFERENCE = 12;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070044 private static final int MSG_SWAP_WITH_BACKGROUND_CALL = 13;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070045
46 private final class InCallAdapterHandler extends Handler {
47 @Override
48 public void handleMessage(Message msg) {
Ihab Awadff7493a2014-06-10 13:47:44 -070049 Call call;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070050 switch (msg.what) {
51 case MSG_ANSWER_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -070052 call = mCallIdMapper.getCall(msg.obj);
53 if (call != null) {
54 mCallsManager.answerCall(call);
55 } else {
56 Log.w(this, "answerCall, unknown call id: %s", msg.obj);
57 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070058 break;
59 case MSG_REJECT_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -070060 SomeArgs args = (SomeArgs) msg.obj;
61 try {
62 call = mCallIdMapper.getCall(args.arg1);
63 boolean rejectWithMessage = args.argi1 == 1;
64 String textMessage = (String) args.arg2;
65 if (call != null) {
66 mCallsManager.rejectCall(call, rejectWithMessage, textMessage);
67 } else {
68 Log.w(this, "setRingback, unknown call id: %s", args.arg1);
69 }
70 } finally {
71 args.recycle();
72 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070073 break;
74 case MSG_PLAY_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070075 call = mCallIdMapper.getCall(msg.obj);
76 if (call != null) {
77 mCallsManager.playDtmfTone(call, (char) msg.arg1);
78 } else {
79 Log.w(this, "playDtmfTone, unknown call id: %s", msg.obj);
80 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070081 break;
82 case MSG_STOP_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070083 call = mCallIdMapper.getCall(msg.obj);
84 if (call != null) {
85 mCallsManager.stopDtmfTone(call);
86 } else {
87 Log.w(this, "stopDtmfTone, unknown call id: %s", msg.obj);
88 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070089 break;
90 case MSG_POST_DIAL_CONTINUE:
Ihab Awadff7493a2014-06-10 13:47:44 -070091 call = mCallIdMapper.getCall(msg.obj);
Evan Charlton352105c2014-06-03 14:10:54 -070092 mCallsManager.postDialContinue(call, msg.arg1 == 1);
Ihab Awadff7493a2014-06-10 13:47:44 -070093 call = mCallIdMapper.getCall(msg.obj);
94 if (call != null) {
95 mCallsManager.postDialContinue(call, msg.arg1 == 1);
96 } else {
97 Log.w(this, "postDialContinue, unknown call id: %s", msg.obj);
98 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070099 break;
100 case MSG_DISCONNECT_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700101 call = mCallIdMapper.getCall(msg.obj);
102 if (call != null) {
103 mCallsManager.disconnectCall(call);
104 } else {
105 Log.w(this, "disconnectCall, unknown call id: %s", msg.obj);
106 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700107 break;
108 case MSG_HOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700109 call = mCallIdMapper.getCall(msg.obj);
110 if (call != null) {
111 mCallsManager.holdCall(call);
112 } else {
113 Log.w(this, "holdCall, unknown call id: %s", msg.obj);
114 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700115 break;
116 case MSG_UNHOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700117 call = mCallIdMapper.getCall(msg.obj);
118 if (call != null) {
119 mCallsManager.unholdCall(call);
120 } else {
121 Log.w(this, "unholdCall, unknown call id: %s", msg.obj);
122 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700123 break;
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700124 case MSG_PHONE_ACCOUNT_CLICKED:
Ihab Awadff7493a2014-06-10 13:47:44 -0700125 call = mCallIdMapper.getCall(msg.obj);
126 if (call != null) {
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700127 mCallsManager.phoneAccountClicked(call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700128 } else {
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700129 Log.w(this, "phoneAccountClicked, unknown call id: %s", msg.obj);
Ihab Awadff7493a2014-06-10 13:47:44 -0700130 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700131 break;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700132 case MSG_MUTE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700133 mCallsManager.mute(msg.arg1 == 1);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700134 break;
135 case MSG_SET_AUDIO_ROUTE:
136 mCallsManager.setAudioRoute(msg.arg1);
137 break;
Santos Cordona1610702014-06-04 20:22:56 -0700138 case MSG_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700139 call = mCallIdMapper.getCall(msg.obj);
140 if (call != null) {
141 mCallsManager.conference(call);
142 } else {
143 Log.w(this, "conference, unknown call id: %s", msg.obj);
144 }
145
Santos Cordona1610702014-06-04 20:22:56 -0700146 break;
147 case MSG_SPLIT_FROM_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700148 call = mCallIdMapper.getCall(msg.obj);
149 if (call != null) {
150 call.splitFromConference();
151 } else {
152 Log.w(this, "splitFromConference, unknown call id: %s", msg.obj);
153 }
Santos Cordona1610702014-06-04 20:22:56 -0700154 break;
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700155 case MSG_SWAP_WITH_BACKGROUND_CALL:
156 call = mCallIdMapper.getCall(msg.obj);
157 if (call != null) {
158 call.swapWithBackgroundCall();
159 } else {
160 Log.w(this, "swapWithBackgroundCall, unknown call id: %s", msg.obj);
161 }
162 break;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700163 }
164 }
165 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800166
Santos Cordone3d76ab2014-01-28 17:25:20 -0800167 private final CallsManager mCallsManager;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700168 private final Handler mHandler = new InCallAdapterHandler();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700169 private final CallIdMapper mCallIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800170
171 /** Persists the specified parameters. */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700172 public InCallAdapter(CallsManager callsManager, CallIdMapper callIdMapper) {
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700173 ThreadUtil.checkOnMainThread();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800174 mCallsManager = callsManager;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700175 mCallIdMapper = callIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800176 }
177
Santos Cordone3d76ab2014-01-28 17:25:20 -0800178 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700179 public void answerCall(String callId) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800180 Log.d(this, "answerCall(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700181 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700182 mHandler.obtainMessage(MSG_ANSWER_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800183 }
184
Santos Cordone3d76ab2014-01-28 17:25:20 -0800185 @Override
Ihab Awadff7493a2014-06-10 13:47:44 -0700186 public void rejectCall(String callId, boolean rejectWithMessage, String textMessage) {
187 Log.d(this, "rejectCall(%s,%b,%s)", callId, rejectWithMessage, textMessage);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700188 mCallIdMapper.checkValidCallId(callId);
Ihab Awadff7493a2014-06-10 13:47:44 -0700189 SomeArgs args = SomeArgs.obtain();
190 args.arg1 = callId;
191 args.argi1 = rejectWithMessage ? 1 : 0;
192 args.arg2 = textMessage;
193 mHandler.obtainMessage(MSG_REJECT_CALL, args).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800194 }
195
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700196 @Override
197 public void playDtmfTone(String callId, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700198 Log.d(this, "playDtmfTone(%s,%c)", callId, digit);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700199 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700200 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, (int) digit, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700201 }
202
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700203 @Override
204 public void stopDtmfTone(String callId) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700205 Log.d(this, "stopDtmfTone(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700206 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700207 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700208 }
209
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700210 @Override
Evan Charlton352105c2014-06-03 14:10:54 -0700211 public void postDialContinue(String callId, boolean proceed) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700212 Log.d(this, "postDialContinue(%s)", callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700213 mCallIdMapper.checkValidCallId(callId);
Evan Charlton352105c2014-06-03 14:10:54 -0700214 mHandler.obtainMessage(MSG_POST_DIAL_CONTINUE, proceed ? 1 : 0, 0, callId).sendToTarget();
Ihab Awad74549ec2014-03-10 15:33:25 -0700215 }
216
Santos Cordone3d76ab2014-01-28 17:25:20 -0800217 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700218 public void disconnectCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700219 Log.v(this, "disconnectCall: %s", callId);
220 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700221 mHandler.obtainMessage(MSG_DISCONNECT_CALL, callId).sendToTarget();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800222 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700223
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700224 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700225 public void holdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700226 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700227 mHandler.obtainMessage(MSG_HOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700228 }
229
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700230 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700231 public void unholdCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700232 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700233 mHandler.obtainMessage(MSG_UNHOLD_CALL, callId).sendToTarget();
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700234 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700235
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700236 @Override
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700237 public void phoneAccountClicked(String callId) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700238 mCallIdMapper.checkValidCallId(callId);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700239 mHandler.obtainMessage(MSG_PHONE_ACCOUNT_CLICKED, callId).sendToTarget();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700240 }
241
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700242 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700243 public void mute(boolean shouldMute) {
244 mHandler.obtainMessage(MSG_MUTE, shouldMute ? 1 : 0, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700245 }
246
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700247 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700248 public void setAudioRoute(int route) {
249 mHandler.obtainMessage(MSG_SET_AUDIO_ROUTE, route, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700250 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700251
Santos Cordon8f3282c2014-06-01 13:56:02 -0700252 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700253 public void conference(String callId) {
254 mHandler.obtainMessage(MSG_CONFERENCE, callId).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700255 }
256
Santos Cordon8f3282c2014-06-01 13:56:02 -0700257 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700258 public void splitFromConference(String callId) {
259 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700260 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700261
262 @Override
263 public void swapWithBackgroundCall(String callId) {
264 mHandler.obtainMessage(MSG_SWAP_WITH_BACKGROUND_CALL, callId).sendToTarget();
265 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800266}