blob: 5a93464205bf37a48e8b484ae0ea3a02ba7a57a1 [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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Ben Gilad9f2bed32013-12-12 17:43:26 -080018
Santos Cordone3d76ab2014-01-28 17:25:20 -080019import android.os.Handler;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070020import android.os.Message;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070021import android.telecom.PhoneAccountHandle;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070022
Evan Charlton352105c2014-06-03 14:10:54 -070023import com.android.internal.os.SomeArgs;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070024import com.android.internal.telecom.IInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080025
Santos Cordone3d76ab2014-01-28 17:25:20 -080026/**
27 * Receives call commands and updates from in-call app and passes them through to CallsManager.
28 * {@link InCallController} creates an instance of this class and passes it to the in-call app after
29 * binding to it. This adapter can receive commands and updates until the in-call app is unbound.
30 */
31class InCallAdapter extends IInCallAdapter.Stub {
Sailesh Nepal10ea4602014-04-01 17:23:32 -070032 private static final int MSG_ANSWER_CALL = 0;
33 private static final int MSG_REJECT_CALL = 1;
34 private static final int MSG_PLAY_DTMF_TONE = 2;
35 private static final int MSG_STOP_DTMF_TONE = 3;
36 private static final int MSG_POST_DIAL_CONTINUE = 4;
37 private static final int MSG_DISCONNECT_CALL = 5;
38 private static final int MSG_HOLD_CALL = 6;
39 private static final int MSG_UNHOLD_CALL = 7;
Nancy Chen2d8eb3a2014-09-08 18:29:45 -070040 private static final int MSG_MUTE = 8;
41 private static final int MSG_SET_AUDIO_ROUTE = 9;
42 private static final int MSG_CONFERENCE = 10;
43 private static final int MSG_SPLIT_FROM_CONFERENCE = 11;
44 private static final int MSG_SWAP_WITH_BACKGROUND_CALL = 12;
45 private static final int MSG_PHONE_ACCOUNT_SELECTED = 13;
46 private static final int MSG_TURN_ON_PROXIMITY_SENSOR = 14;
47 private static final int MSG_TURN_OFF_PROXIMITY_SENSOR = 15;
48 private static final int MSG_MERGE_CONFERENCE = 16;
49 private static final int MSG_SWAP_CONFERENCE = 17;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070050
51 private final class InCallAdapterHandler extends Handler {
52 @Override
53 public void handleMessage(Message msg) {
Ihab Awadff7493a2014-06-10 13:47:44 -070054 Call call;
Sailesh Nepal10ea4602014-04-01 17:23:32 -070055 switch (msg.what) {
Andrew Lee38931d02014-07-16 10:17:36 -070056 case MSG_ANSWER_CALL: {
57 SomeArgs args = (SomeArgs) msg.obj;
58 try {
59 call = mCallIdMapper.getCall(args.arg1);
60 int videoState = (int) args.arg2;
61 if (call != null) {
62 mCallsManager.answerCall(call, videoState);
63 } else {
64 Log.w(this, "answerCall, unknown call id: %s", msg.obj);
65 }
66 } finally {
67 args.recycle();
Ihab Awadff7493a2014-06-10 13:47:44 -070068 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070069 break;
Andrew Lee38931d02014-07-16 10:17:36 -070070 }
Nancy Chen53ceedc2014-07-08 18:56:51 -070071 case MSG_REJECT_CALL: {
Ihab Awadff7493a2014-06-10 13:47:44 -070072 SomeArgs args = (SomeArgs) msg.obj;
73 try {
74 call = mCallIdMapper.getCall(args.arg1);
75 boolean rejectWithMessage = args.argi1 == 1;
76 String textMessage = (String) args.arg2;
77 if (call != null) {
78 mCallsManager.rejectCall(call, rejectWithMessage, textMessage);
79 } else {
80 Log.w(this, "setRingback, unknown call id: %s", args.arg1);
81 }
82 } finally {
83 args.recycle();
84 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070085 break;
Nancy Chen53ceedc2014-07-08 18:56:51 -070086 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070087 case MSG_PLAY_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070088 call = mCallIdMapper.getCall(msg.obj);
89 if (call != null) {
90 mCallsManager.playDtmfTone(call, (char) msg.arg1);
91 } else {
92 Log.w(this, "playDtmfTone, unknown call id: %s", msg.obj);
93 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -070094 break;
95 case MSG_STOP_DTMF_TONE:
Ihab Awadff7493a2014-06-10 13:47:44 -070096 call = mCallIdMapper.getCall(msg.obj);
97 if (call != null) {
98 mCallsManager.stopDtmfTone(call);
99 } else {
100 Log.w(this, "stopDtmfTone, unknown call id: %s", msg.obj);
101 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700102 break;
103 case MSG_POST_DIAL_CONTINUE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700104 call = mCallIdMapper.getCall(msg.obj);
Evan Charlton352105c2014-06-03 14:10:54 -0700105 mCallsManager.postDialContinue(call, msg.arg1 == 1);
Ihab Awadff7493a2014-06-10 13:47:44 -0700106 call = mCallIdMapper.getCall(msg.obj);
107 if (call != null) {
108 mCallsManager.postDialContinue(call, msg.arg1 == 1);
109 } else {
110 Log.w(this, "postDialContinue, unknown call id: %s", msg.obj);
111 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700112 break;
113 case MSG_DISCONNECT_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700114 call = mCallIdMapper.getCall(msg.obj);
115 if (call != null) {
116 mCallsManager.disconnectCall(call);
117 } else {
118 Log.w(this, "disconnectCall, unknown call id: %s", msg.obj);
119 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700120 break;
121 case MSG_HOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700122 call = mCallIdMapper.getCall(msg.obj);
123 if (call != null) {
124 mCallsManager.holdCall(call);
125 } else {
126 Log.w(this, "holdCall, unknown call id: %s", msg.obj);
127 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700128 break;
129 case MSG_UNHOLD_CALL:
Ihab Awadff7493a2014-06-10 13:47:44 -0700130 call = mCallIdMapper.getCall(msg.obj);
131 if (call != null) {
132 mCallsManager.unholdCall(call);
133 } else {
134 Log.w(this, "unholdCall, unknown call id: %s", msg.obj);
135 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700136 break;
Nancy Chen53ceedc2014-07-08 18:56:51 -0700137 case MSG_PHONE_ACCOUNT_SELECTED: {
138 SomeArgs args = (SomeArgs) msg.obj;
139 try {
140 call = mCallIdMapper.getCall(args.arg1);
141 if (call != null) {
Evan Charlton89176372014-07-19 18:23:09 -0700142 mCallsManager.phoneAccountSelected(call, (PhoneAccountHandle) args.arg2);
Nancy Chen53ceedc2014-07-08 18:56:51 -0700143 } else {
144 Log.w(this, "phoneAccountSelected, unknown call id: %s", args.arg1);
145 }
146 } finally {
147 args.recycle();
148 }
149 break;
150 }
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700151 case MSG_MUTE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700152 mCallsManager.mute(msg.arg1 == 1);
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700153 break;
154 case MSG_SET_AUDIO_ROUTE:
155 mCallsManager.setAudioRoute(msg.arg1);
156 break;
Santos Cordon12d61822014-07-29 16:02:20 -0700157 case MSG_CONFERENCE: {
158 SomeArgs args = (SomeArgs) msg.obj;
159 try {
160 call = mCallIdMapper.getCall(args.arg1);
161 Call otherCall = mCallIdMapper.getCall(args.arg2);
162 if (call != null && otherCall != null) {
163 mCallsManager.conference(call, otherCall);
164 } else {
165 Log.w(this, "conference, unknown call id: %s", msg.obj);
166 }
167 } finally {
168 args.recycle();
Ihab Awadff7493a2014-06-10 13:47:44 -0700169 }
Santos Cordona1610702014-06-04 20:22:56 -0700170 break;
Santos Cordon12d61822014-07-29 16:02:20 -0700171 }
Santos Cordona1610702014-06-04 20:22:56 -0700172 case MSG_SPLIT_FROM_CONFERENCE:
Ihab Awadff7493a2014-06-10 13:47:44 -0700173 call = mCallIdMapper.getCall(msg.obj);
174 if (call != null) {
175 call.splitFromConference();
176 } else {
177 Log.w(this, "splitFromConference, unknown call id: %s", msg.obj);
178 }
Santos Cordona1610702014-06-04 20:22:56 -0700179 break;
Yorke Leed1346872014-07-28 14:38:45 -0700180 case MSG_TURN_ON_PROXIMITY_SENSOR:
181 mCallsManager.turnOnProximitySensor();
182 break;
183 case MSG_TURN_OFF_PROXIMITY_SENSOR:
184 mCallsManager.turnOffProximitySensor((boolean) msg.obj);
185 break;
Santos Cordon68059232014-09-04 20:09:42 -0700186 case MSG_MERGE_CONFERENCE:
187 call = mCallIdMapper.getCall(msg.obj);
188 if (call != null) {
189 call.mergeConference();
190 } else {
191 Log.w(this, "mergeConference, unknown call id: %s", msg.obj);
192 }
193 break;
194 case MSG_SWAP_CONFERENCE:
195 call = mCallIdMapper.getCall(msg.obj);
196 if (call != null) {
197 call.swapConference();
198 } else {
199 Log.w(this, "swapConference, unknown call id: %s", msg.obj);
200 }
201 break;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700202 }
203 }
204 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800205
Santos Cordone3d76ab2014-01-28 17:25:20 -0800206 private final CallsManager mCallsManager;
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700207 private final Handler mHandler = new InCallAdapterHandler();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700208 private final CallIdMapper mCallIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800209
210 /** Persists the specified parameters. */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700211 public InCallAdapter(CallsManager callsManager, CallIdMapper callIdMapper) {
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700212 ThreadUtil.checkOnMainThread();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800213 mCallsManager = callsManager;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700214 mCallIdMapper = callIdMapper;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800215 }
216
Santos Cordone3d76ab2014-01-28 17:25:20 -0800217 @Override
Andrew Lee38931d02014-07-16 10:17:36 -0700218 public void answerCall(String callId, int videoState) {
219 Log.d(this, "answerCall(%s,%d)", callId, videoState);
Jay Shrauner969755a2014-08-11 20:34:43 -0700220 if (mCallIdMapper.isValidCallId(callId)) {
221 SomeArgs args = SomeArgs.obtain();
222 args.arg1 = callId;
223 args.arg2 = videoState;
224 mHandler.obtainMessage(MSG_ANSWER_CALL, args).sendToTarget();
225 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800226 }
227
Santos Cordone3d76ab2014-01-28 17:25:20 -0800228 @Override
Ihab Awadff7493a2014-06-10 13:47:44 -0700229 public void rejectCall(String callId, boolean rejectWithMessage, String textMessage) {
230 Log.d(this, "rejectCall(%s,%b,%s)", callId, rejectWithMessage, textMessage);
Jay Shrauner969755a2014-08-11 20:34:43 -0700231 if (mCallIdMapper.isValidCallId(callId)) {
232 SomeArgs args = SomeArgs.obtain();
233 args.arg1 = callId;
234 args.argi1 = rejectWithMessage ? 1 : 0;
235 args.arg2 = textMessage;
236 mHandler.obtainMessage(MSG_REJECT_CALL, args).sendToTarget();
237 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800238 }
239
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700240 @Override
241 public void playDtmfTone(String callId, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700242 Log.d(this, "playDtmfTone(%s,%c)", callId, digit);
Jay Shrauner969755a2014-08-11 20:34:43 -0700243 if (mCallIdMapper.isValidCallId(callId)) {
244 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, (int) digit, 0, callId).sendToTarget();
245 }
Ihab Awad74549ec2014-03-10 15:33:25 -0700246 }
247
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700248 @Override
249 public void stopDtmfTone(String callId) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700250 Log.d(this, "stopDtmfTone(%s)", callId);
Jay Shrauner969755a2014-08-11 20:34:43 -0700251 if (mCallIdMapper.isValidCallId(callId)) {
252 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget();
253 }
Ihab Awad74549ec2014-03-10 15:33:25 -0700254 }
255
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700256 @Override
Evan Charlton352105c2014-06-03 14:10:54 -0700257 public void postDialContinue(String callId, boolean proceed) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700258 Log.d(this, "postDialContinue(%s)", callId);
Jay Shrauner969755a2014-08-11 20:34:43 -0700259 if (mCallIdMapper.isValidCallId(callId)) {
260 mHandler.obtainMessage(MSG_POST_DIAL_CONTINUE, proceed ? 1 : 0, 0, callId).sendToTarget();
261 }
Ihab Awad74549ec2014-03-10 15:33:25 -0700262 }
263
Santos Cordone3d76ab2014-01-28 17:25:20 -0800264 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700265 public void disconnectCall(String callId) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700266 Log.v(this, "disconnectCall: %s", callId);
Jay Shrauner969755a2014-08-11 20:34:43 -0700267 if (mCallIdMapper.isValidCallId(callId)) {
268 mHandler.obtainMessage(MSG_DISCONNECT_CALL, callId).sendToTarget();
269 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800270 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700271
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700272 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700273 public void holdCall(String callId) {
Jay Shrauner969755a2014-08-11 20:34:43 -0700274 if (mCallIdMapper.isValidCallId(callId)) {
275 mHandler.obtainMessage(MSG_HOLD_CALL, callId).sendToTarget();
276 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700277 }
278
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700279 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700280 public void unholdCall(String callId) {
Jay Shrauner969755a2014-08-11 20:34:43 -0700281 if (mCallIdMapper.isValidCallId(callId)) {
282 mHandler.obtainMessage(MSG_UNHOLD_CALL, callId).sendToTarget();
283 }
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700284 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700285
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700286 @Override
Evan Charlton89176372014-07-19 18:23:09 -0700287 public void phoneAccountSelected(String callId, PhoneAccountHandle accountHandle) {
Jay Shrauner969755a2014-08-11 20:34:43 -0700288 if (mCallIdMapper.isValidCallId(callId)) {
289 SomeArgs args = SomeArgs.obtain();
290 args.arg1 = callId;
291 args.arg2 = accountHandle;
292 mHandler.obtainMessage(MSG_PHONE_ACCOUNT_SELECTED, args).sendToTarget();
293 }
Nancy Chen53ceedc2014-07-08 18:56:51 -0700294 }
295
296 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700297 public void mute(boolean shouldMute) {
298 mHandler.obtainMessage(MSG_MUTE, shouldMute ? 1 : 0, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700299 }
300
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700301 @Override
Sailesh Nepal10ea4602014-04-01 17:23:32 -0700302 public void setAudioRoute(int route) {
303 mHandler.obtainMessage(MSG_SET_AUDIO_ROUTE, route, 0).sendToTarget();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700304 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700305
Santos Cordon8f3282c2014-06-01 13:56:02 -0700306 @Override
Santos Cordon12d61822014-07-29 16:02:20 -0700307 public void conference(String callId, String otherCallId) {
Jay Shrauner969755a2014-08-11 20:34:43 -0700308 if (mCallIdMapper.isValidCallId(callId) &&
309 mCallIdMapper.isValidCallId(otherCallId)) {
310 SomeArgs args = SomeArgs.obtain();
311 args.arg1 = callId;
312 args.arg2 = otherCallId;
313 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget();
314 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700315 }
316
Santos Cordon8f3282c2014-06-01 13:56:02 -0700317 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700318 public void splitFromConference(String callId) {
Jay Shrauner969755a2014-08-11 20:34:43 -0700319 if (mCallIdMapper.isValidCallId(callId)) {
320 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget();
321 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700322 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700323
324 @Override
Santos Cordon68059232014-09-04 20:09:42 -0700325 public void mergeConference(String callId) {
326 if (mCallIdMapper.isValidCallId(callId)) {
327 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget();
328 }
329 }
330
331 @Override
332 public void swapConference(String callId) {
333 if (mCallIdMapper.isValidCallId(callId)) {
334 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget();
335 }
336 }
337
338 @Override
Yorke Leed1346872014-07-28 14:38:45 -0700339 public void turnOnProximitySensor() {
340 mHandler.obtainMessage(MSG_TURN_ON_PROXIMITY_SENSOR).sendToTarget();
341 }
342
343 @Override
344 public void turnOffProximitySensor(boolean screenOnImmediately) {
345 mHandler.obtainMessage(MSG_TURN_OFF_PROXIMITY_SENSOR, screenOnImmediately).sendToTarget();
346 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800347}