blob: e355890c972618d9f97751f7ae5b1dfee1146890 [file] [log] [blame]
Sailesh Nepalc92c4362014-07-04 18:33:21 -07001/*
2 * Copyright 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
17package com.android.telecomm;
18
19import android.content.ComponentName;
20import android.os.Bundle;
21import android.os.Handler;
22import android.os.IBinder;
23import android.os.Message;
24import android.os.RemoteException;
25import android.telecomm.CallAudioState;
26import android.telecomm.ConnectionService;
27import android.telecomm.CallServiceDescriptor;
28import android.telecomm.ConnectionRequest;
29import android.telecomm.GatewayInfo;
Sailesh Nepal35faf8c2014-07-08 22:02:34 -070030import android.telecomm.StatusHints;
Sailesh Nepalc92c4362014-07-04 18:33:21 -070031import android.telecomm.TelecommConstants;
32import android.telephony.DisconnectCause;
33
34import com.android.internal.os.SomeArgs;
Sailesh Nepalc92c4362014-07-04 18:33:21 -070035import com.android.internal.telecomm.IConnectionService;
36import com.android.internal.telecomm.IConnectionServiceAdapter;
37import com.android.internal.telecomm.ICallServiceProvider;
38import com.android.internal.telecomm.ICallVideoProvider;
39import com.android.internal.telecomm.RemoteServiceCallback;
40import com.android.telecomm.BaseRepository.LookupCallback;
41import com.google.common.base.Preconditions;
42import com.google.common.collect.ImmutableList;
43
44import org.apache.http.conn.ClientConnectionRequest;
45
46import java.util.ArrayList;
47import java.util.Collection;
48import java.util.HashMap;
49import java.util.HashSet;
50import java.util.List;
51import java.util.Map;
52import java.util.Set;
53
54/**
55 * Wrapper for {@link IConnectionService}s, handles binding to {@link IConnectionService} and keeps
56 * track of when the object can safely be unbound. Other classes should not use
57 * {@link IConnectionService} directly and instead should use this class to invoke methods of
58 * {@link IConnectionService}.
59 */
60final class ConnectionServiceWrapper extends ServiceBinder<IConnectionService> {
61 private static final int MSG_NOTIFY_INCOMING_CALL = 1;
62 private static final int MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL = 2;
63 private static final int MSG_HANDLE_FAILED_OUTGOING_CALL = 3;
64 private static final int MSG_CANCEL_OUTGOING_CALL = 4;
65 private static final int MSG_SET_ACTIVE = 5;
66 private static final int MSG_SET_RINGING = 6;
67 private static final int MSG_SET_DIALING = 7;
68 private static final int MSG_SET_DISCONNECTED = 8;
69 private static final int MSG_SET_ON_HOLD = 9;
70 private static final int MSG_SET_REQUESTING_RINGBACK = 10;
71 private static final int MSG_CAN_CONFERENCE = 11;
72 private static final int MSG_SET_IS_CONFERENCED = 12;
73 private static final int MSG_ADD_CONFERENCE_CALL = 13;
74 private static final int MSG_REMOVE_CALL = 14;
75 private static final int MSG_ON_POST_DIAL_WAIT = 15;
76 private static final int MSG_QUERY_REMOTE_CALL_SERVICES = 16;
77 private static final int MSG_SET_CALL_VIDEO_PROVIDER = 17;
78 private static final int MSG_SET_FEATURES = 18;
Sailesh Nepal7e669572014-07-08 21:29:12 -070079 private static final int MSG_SET_AUDIO_MODE_IS_VOIP = 19;
Sailesh Nepal35faf8c2014-07-08 22:02:34 -070080 private static final int MSG_SET_STATUS_HINTS = 20;
Sailesh Nepalc92c4362014-07-04 18:33:21 -070081
82 private final Handler mHandler = new Handler() {
83 @Override
84 public void handleMessage(Message msg) {
85 Call call;
86 switch (msg.what) {
87 case MSG_NOTIFY_INCOMING_CALL: {
88 ConnectionRequest request = (ConnectionRequest) msg.obj;
89 call = mCallIdMapper.getCall(request.getCallId());
90 if (call != null && mPendingIncomingCalls.remove(call) &&
91 call.isIncoming()) {
92 mIncomingCallsManager.handleSuccessfulIncomingCall(call, request);
93 } else {
94 // TODO(santoscordon): For this an the other commented logging, we need
95 // to reenable it. At the moment all ConnectionServiceAdapters receive
96 // notification of changes to all calls, even calls which it may not own
97 // (ala remote connections). We need to fix that and then uncomment the
98 // logging calls here.
99 //Log.w(this, "notifyIncomingCall, unknown incoming call: %s, id: %s",
100 // call, request.getId());
101 }
102 break;
103 }
104 case MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL: {
105 ConnectionRequest request = (ConnectionRequest) msg.obj;
106 if (mPendingOutgoingCalls.containsKey(request.getCallId())) {
107 mPendingOutgoingCalls.remove(
108 request.getCallId()).onOutgoingCallSuccess();
109 } else {
110 //Log.w(this, "handleSuccessfulOutgoingCall, unknown call: %s", callId);
111 }
112 break;
113 }
114 case MSG_HANDLE_FAILED_OUTGOING_CALL: {
115 SomeArgs args = (SomeArgs) msg.obj;
116 try {
Sailesh Nepala49c6432014-07-07 22:47:11 -0700117 ConnectionRequest request = (ConnectionRequest) args.arg1;
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700118 int statusCode = args.argi1;
119 String statusMsg = (String) args.arg2;
120 // TODO(santoscordon): Do something with 'reason' or get rid of it.
121
122 if (mPendingOutgoingCalls.containsKey(request.getCallId())) {
123 mPendingOutgoingCalls.remove(request.getCallId())
124 .onOutgoingCallFailure(statusCode, statusMsg);
125 mCallIdMapper.removeCall(request.getCallId());
126 } else {
127 //Log.w(this, "handleFailedOutgoingCall, unknown call: %s", callId);
128 }
129 } finally {
130 args.recycle();
131 }
132 break;
133 }
134 case MSG_CANCEL_OUTGOING_CALL: {
135 ConnectionRequest request = (ConnectionRequest) msg.obj;
136 if (mPendingOutgoingCalls.containsKey(request.getCallId())) {
137 mPendingOutgoingCalls.remove(
138 request.getCallId()).onOutgoingCallCancel();
139 } else {
140 //Log.w(this, "cancelOutgoingCall, unknown call: %s", callId);
141 }
142 break;
143 }
144 case MSG_SET_ACTIVE:
145 call = mCallIdMapper.getCall(msg.obj);
146 if (call != null) {
147 mCallsManager.markCallAsActive(call);
148 } else {
149 //Log.w(this, "setActive, unknown call id: %s", msg.obj);
150 }
151 break;
152 case MSG_SET_RINGING:
153 call = mCallIdMapper.getCall(msg.obj);
154 if (call != null) {
155 mCallsManager.markCallAsRinging(call);
156 } else {
157 //Log.w(this, "setRinging, unknown call id: %s", msg.obj);
158 }
159 break;
160 case MSG_SET_DIALING:
161 call = mCallIdMapper.getCall(msg.obj);
162 if (call != null) {
163 mCallsManager.markCallAsDialing(call);
164 } else {
165 //Log.w(this, "setDialing, unknown call id: %s", msg.obj);
166 }
167 break;
168 case MSG_SET_DISCONNECTED: {
169 SomeArgs args = (SomeArgs) msg.obj;
170 try {
171 call = mCallIdMapper.getCall(args.arg1);
172 String disconnectMessage = (String) args.arg2;
173 int disconnectCause = args.argi1;
174 if (call != null) {
175 mCallsManager.markCallAsDisconnected(call, disconnectCause,
176 disconnectMessage);
177 } else {
178 //Log.w(this, "setDisconnected, unknown call id: %s", args.arg1);
179 }
180 } finally {
181 args.recycle();
182 }
183 break;
184 }
185 case MSG_SET_ON_HOLD:
186 call = mCallIdMapper.getCall(msg.obj);
187 if (call != null) {
188 mCallsManager.markCallAsOnHold(call);
189 } else {
190 //Log.w(this, "setOnHold, unknown call id: %s", msg.obj);
191 }
192 break;
193 case MSG_SET_REQUESTING_RINGBACK: {
194 SomeArgs args = (SomeArgs) msg.obj;
195 try {
196 call = mCallIdMapper.getCall(args.arg1);
197 boolean ringback = (boolean) args.arg2;
198 if (call != null) {
199 call.setRequestingRingback(ringback);
200 } else {
201 //Log.w(this, "setRingback, unknown call id: %s", args.arg1);
202 }
203 } finally {
204 args.recycle();
205 }
206 break;
207 }
208 case MSG_CAN_CONFERENCE: {
209 call = mCallIdMapper.getCall(msg.obj);
210 if (call != null) {
211 call.setIsConferenceCapable(msg.arg1 == 1);
212 } else {
213 //Log.w(ConnectionServiceWrapper.this,
214 // "canConference, unknown call id: %s", msg.obj);
215 }
216 break;
217 }
218 case MSG_SET_IS_CONFERENCED: {
219 SomeArgs args = (SomeArgs) msg.obj;
220 try {
221 Call childCall = mCallIdMapper.getCall(args.arg1);
222 if (childCall != null) {
223 String conferenceCallId = (String) args.arg2;
224 if (conferenceCallId == null) {
225 childCall.setParentCall(null);
226 } else {
227 Call conferenceCall = mCallIdMapper.getCall(conferenceCallId);
228 if (conferenceCall != null &&
229 !mPendingConferenceCalls.contains(conferenceCall)) {
230 childCall.setParentCall(conferenceCall);
231 } else {
232 //Log.w(this, "setIsConferenced, unknown conference id %s",
233 // conferenceCallId);
234 }
235 }
236 } else {
237 //Log.w(this, "setIsConferenced, unknown call id: %s", args.arg1);
238 }
239 } finally {
240 args.recycle();
241 }
242 break;
243 }
244 case MSG_ADD_CONFERENCE_CALL: {
245 Call conferenceCall = mCallIdMapper.getCall(msg.obj);
246 if (mPendingConferenceCalls.remove(conferenceCall)) {
247 Log.v(this, "confirming conf call %s", conferenceCall);
248 conferenceCall.confirmConference();
249 } else {
250 //Log.w(this, "addConference, unknown call id: %s", callId);
251 }
252 break;
253 }
254 case MSG_REMOVE_CALL:
255 break;
256 case MSG_ON_POST_DIAL_WAIT: {
257 SomeArgs args = (SomeArgs) msg.obj;
258 try {
259 call = mCallIdMapper.getCall(args.arg1);
260 if (call != null) {
261 String remaining = (String) args.arg2;
262 call.onPostDialWait(remaining);
263 } else {
264 //Log.w(this, "onPostDialWait, unknown call id: %s", args.arg1);
265 }
266 } finally {
267 args.recycle();
268 }
269 break;
270 }
271 case MSG_QUERY_REMOTE_CALL_SERVICES: {
272 ConnectionServiceWrapper.this.queryRemoteConnectionServices(
273 (RemoteServiceCallback) msg.obj);
274 break;
275 }
276 case MSG_SET_CALL_VIDEO_PROVIDER: {
277 SomeArgs args = (SomeArgs) msg.obj;
278 try {
279 call = mCallIdMapper.getCall(args.arg1);
280 ICallVideoProvider callVideoProvider = (ICallVideoProvider) args.arg2;
281 if (call != null) {
282 call.setCallVideoProvider(callVideoProvider);
283 }
284 } finally {
285 args.recycle();
286 }
287 break;
288 }
289 case MSG_SET_FEATURES: {
290 SomeArgs args = (SomeArgs) msg.obj;
291 try {
292 call = mCallIdMapper.getCall(args.arg1);
Sailesh Nepal7e669572014-07-08 21:29:12 -0700293 int features = (int) args.argi1;
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700294 if (call != null) {
295 call.setFeatures(features);
296 }
297 } finally {
298 args.recycle();
299 }
300 break;
301 }
Sailesh Nepal7e669572014-07-08 21:29:12 -0700302 case MSG_SET_AUDIO_MODE_IS_VOIP: {
303 SomeArgs args = (SomeArgs) msg.obj;
304 try {
305 call = mCallIdMapper.getCall(args.arg1);
306 boolean isVoip = args.argi1 == 1;
307 if (call != null) {
308 call.setAudioModeIsVoip(isVoip);
309 }
310 } finally {
311 args.recycle();
312 }
313 break;
314 }
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700315 case MSG_SET_STATUS_HINTS: {
316 SomeArgs args = (SomeArgs) msg.obj;
317 try {
318 call = mCallIdMapper.getCall(args.arg1);
319 StatusHints statusHints = (StatusHints) args.arg2;
320 if (call != null) {
321 call.setStatusHints(statusHints);
322 }
323 } finally {
324 args.recycle();
325 }
326 break;
327 }
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700328 }
329 }
330 };
331
332 private final class Adapter extends IConnectionServiceAdapter.Stub {
333 /** {@inheritDoc} */
334 @Override
335 public void notifyIncomingCall(ConnectionRequest request) {
336 logIncoming("notifyIncomingCall %s", request);
337 mCallIdMapper.checkValidCallId(request.getCallId());
338 mHandler.obtainMessage(MSG_NOTIFY_INCOMING_CALL, request).sendToTarget();
339 }
340
341 /** {@inheritDoc} */
342 @Override
343 public void handleSuccessfulOutgoingCall(ConnectionRequest request) {
344 logIncoming("handleSuccessfulOutgoingCall %s", request);
345 mCallIdMapper.checkValidCallId(request.getCallId());
346 mHandler.obtainMessage(MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL, request).sendToTarget();
347 }
348
349 /** {@inheritDoc} */
350 @Override
351 public void handleFailedOutgoingCall(
352 ConnectionRequest request,
353 int errorCode,
354 String errorMsg) {
355 logIncoming("handleFailedOutgoingCall %s %d %s", request, errorCode, errorMsg);
356 mCallIdMapper.checkValidCallId(request.getCallId());
357 SomeArgs args = SomeArgs.obtain();
358 args.arg1 = request;
359 args.argi1 = errorCode;
360 args.arg2 = errorMsg;
361 mHandler.obtainMessage(MSG_HANDLE_FAILED_OUTGOING_CALL, args).sendToTarget();
362 }
363
364 /** {@inheritDoc} */
365 @Override
366 public void cancelOutgoingCall(ConnectionRequest request) {
367 logIncoming("cancelOutgoingCall %s", request);
368 mCallIdMapper.checkValidCallId(request.getCallId());
369 mHandler.obtainMessage(MSG_CANCEL_OUTGOING_CALL, request).sendToTarget();
370 }
371
372 /** {@inheritDoc} */
373 @Override
374 public void setActive(String callId) {
375 logIncoming("setActive %s", callId);
376 mCallIdMapper.checkValidCallId(callId);
377 mHandler.obtainMessage(MSG_SET_ACTIVE, callId).sendToTarget();
378 }
379
380 /** {@inheritDoc} */
381 @Override
382 public void setRinging(String callId) {
383 logIncoming("setRinging %s", callId);
384 mCallIdMapper.checkValidCallId(callId);
385 mHandler.obtainMessage(MSG_SET_RINGING, callId).sendToTarget();
386 }
387
388 /** {@inheritDoc} */
389 @Override
390 public void setCallVideoProvider(String callId, ICallVideoProvider callVideoProvider) {
391 logIncoming("setCallVideoProvider %s", callId);
392 mCallIdMapper.checkValidCallId(callId);
393 SomeArgs args = SomeArgs.obtain();
394 args.arg1 = callId;
395 args.arg2 = callVideoProvider;
396 mHandler.obtainMessage(MSG_SET_CALL_VIDEO_PROVIDER, args).sendToTarget();
397 }
398
399 /** {@inheritDoc} */
400 @Override
401 public void setDialing(String callId) {
402 logIncoming("setDialing %s", callId);
403 mCallIdMapper.checkValidCallId(callId);
404 mHandler.obtainMessage(MSG_SET_DIALING, callId).sendToTarget();
405 }
406
407 /** {@inheritDoc} */
408 @Override
409 public void setDisconnected(
410 String callId, int disconnectCause, String disconnectMessage) {
411 logIncoming("setDisconnected %s %d %s", callId, disconnectCause, disconnectMessage);
412 mCallIdMapper.checkValidCallId(callId);
413 SomeArgs args = SomeArgs.obtain();
414 args.arg1 = callId;
415 args.arg2 = disconnectMessage;
416 args.argi1 = disconnectCause;
417 mHandler.obtainMessage(MSG_SET_DISCONNECTED, args).sendToTarget();
418 }
419
420 /** {@inheritDoc} */
421 @Override
422 public void setOnHold(String callId) {
423 logIncoming("setOnHold %s", callId);
424 mCallIdMapper.checkValidCallId(callId);
425 mHandler.obtainMessage(MSG_SET_ON_HOLD, callId).sendToTarget();
426 }
427
428 /** {@inheritDoc} */
429 @Override
430 public void setRequestingRingback(String callId, boolean ringback) {
431 logIncoming("setRequestingRingback %s %b", callId, ringback);
432 mCallIdMapper.checkValidCallId(callId);
433 SomeArgs args = SomeArgs.obtain();
434 args.arg1 = callId;
435 args.arg2 = ringback;
436 mHandler.obtainMessage(MSG_SET_REQUESTING_RINGBACK, args).sendToTarget();
437 }
438
439 /** ${inheritDoc} */
440 @Override
441 public void removeCall(String callId) {
442 logIncoming("removeCall %s", callId);
443 }
444
445 /** ${inheritDoc} */
446 @Override
447 public void setCanConference(String callId, boolean canConference) {
448 logIncoming("setCanConference %s %b", callId, canConference);
449 mHandler.obtainMessage(MSG_CAN_CONFERENCE, canConference ? 1 : 0, 0, callId)
450 .sendToTarget();
451 }
452
453 /** ${inheritDoc} */
454 @Override
455 public void setIsConferenced(String callId, String conferenceCallId) {
456 logIncoming("setIsConferenced %s %s", callId, conferenceCallId);
457 SomeArgs args = SomeArgs.obtain();
458 args.arg1 = callId;
459 args.arg2 = conferenceCallId;
460 mHandler.obtainMessage(MSG_SET_IS_CONFERENCED, args).sendToTarget();
461 }
462
463 /** ${InheritDoc} */
464 @Override
465 public void addConferenceCall(String callId) {
466 logIncoming("addConferenceCall %s", callId);
467 mCallIdMapper.checkValidCallId(callId);
468 mHandler.obtainMessage(MSG_ADD_CONFERENCE_CALL, callId).sendToTarget();
469 }
470
471 @Override
472 public void onPostDialWait(String callId, String remaining) throws RemoteException {
473 logIncoming("onPostDialWait %s %s", callId, remaining);
474 mCallIdMapper.checkValidCallId(callId);
475 SomeArgs args = SomeArgs.obtain();
476 args.arg1 = callId;
477 args.arg2 = remaining;
478 mHandler.obtainMessage(MSG_ON_POST_DIAL_WAIT, args).sendToTarget();
479 }
480
481 /** ${inheritDoc} */
482 @Override
483 public void queryRemoteConnectionServices(RemoteServiceCallback callback) {
484 logIncoming("queryRemoteCSs");
485 mHandler.obtainMessage(MSG_QUERY_REMOTE_CALL_SERVICES, callback).sendToTarget();
486 }
487
488 @Override
489 public void setFeatures(String callId, int features) {
490 logIncoming("setFeatures %s %d", callId, features);
491 mCallIdMapper.checkValidCallId(callId);
492 SomeArgs args = SomeArgs.obtain();
493 args.arg1 = callId;
Sailesh Nepal7e669572014-07-08 21:29:12 -0700494 args.argi1 = features;
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700495 mHandler.obtainMessage(MSG_SET_FEATURES, args).sendToTarget();
496 }
Sailesh Nepal7e669572014-07-08 21:29:12 -0700497
498 @Override
499 public void setAudioModeIsVoip(String callId, boolean isVoip) {
500 logIncoming("setAudioModeIsVoip %s %d", callId, isVoip);
501 mCallIdMapper.checkValidCallId(callId);
502 SomeArgs args = SomeArgs.obtain();
503 args.arg1 = callId;
504 args.argi1 = isVoip ? 1 : 0;
505 mHandler.obtainMessage(MSG_SET_AUDIO_MODE_IS_VOIP, args).sendToTarget();
506 }
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700507
508 @Override
509 public void setStatusHints(String callId, StatusHints statusHints) {
510 logIncoming("setStatusHints %s %s", callId, statusHints);
511 mCallIdMapper.checkValidCallId(callId);
512 SomeArgs args = SomeArgs.obtain();
513 args.arg1 = callId;
514 args.arg2 = statusHints;
515 mHandler.obtainMessage(MSG_SET_STATUS_HINTS, args).sendToTarget();
516 }
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700517 }
518
519 private final Adapter mAdapter = new Adapter();
520 private final CallsManager mCallsManager = CallsManager.getInstance();
521 private final Set<Call> mPendingIncomingCalls = new HashSet<>();
522 private final Set<Call> mPendingConferenceCalls = new HashSet<>();
523 private final CallServiceDescriptor mDescriptor;
524 private final CallIdMapper mCallIdMapper = new CallIdMapper("ConnectionService");
525 private final IncomingCallsManager mIncomingCallsManager;
526 private final Map<String, OutgoingCallResponse> mPendingOutgoingCalls = new HashMap<>();
527
528 private Binder mBinder = new Binder();
529 private IConnectionService mServiceInterface;
530 private final CallServiceRepository mCallServiceRepository;
531
532 /**
533 * Creates a call-service for the specified descriptor.
534 *
535 * @param descriptor The call-service descriptor from
536 * {@link ICallServiceProvider#lookupCallServices}.
537 * @param incomingCallsManager Manages the incoming call initialization flow.
538 * @param callServiceRepository Connection service repository.
539 */
540 ConnectionServiceWrapper(
541 CallServiceDescriptor descriptor,
542 IncomingCallsManager incomingCallsManager,
543 CallServiceRepository callServiceRepository) {
544 super(TelecommConstants.ACTION_CONNECTION_SERVICE, descriptor.getServiceComponent());
545 mDescriptor = descriptor;
546 mIncomingCallsManager = incomingCallsManager;
547 mCallServiceRepository = callServiceRepository;
548 }
549
550 CallServiceDescriptor getDescriptor() {
551 return mDescriptor;
552 }
553
554 /** See {@link IConnectionService#addConnectionServiceAdapter}. */
555 private void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) {
556 if (isServiceValid("addConnectionServiceAdapter")) {
557 try {
Sailesh Nepal3fe8b722014-07-08 10:07:26 -0700558 logOutgoing("addConnectionServiceAdapter %s", adapter);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700559 mServiceInterface.addConnectionServiceAdapter(adapter);
560 } catch (RemoteException e) {
561 }
562 }
563 }
564
565 /**
566 * Attempts to place the specified call, see {@link IConnectionService#call}. Returns the result
567 * asynchronously through the specified callback.
568 */
569 void call(final Call call, final OutgoingCallResponse callResponse) {
570 Log.d(this, "call(%s) via %s.", call, getComponentName());
571 BindCallback callback = new BindCallback() {
572 @Override
573 public void onSuccess() {
574 String callId = mCallIdMapper.getCallId(call);
575 mPendingOutgoingCalls.put(callId, callResponse);
576
577 GatewayInfo gatewayInfo = call.getGatewayInfo();
578 Bundle extras = call.getExtras();
579 if (gatewayInfo != null && gatewayInfo.getGatewayProviderPackageName() != null &&
580 gatewayInfo.getOriginalHandle() != null) {
581 extras = (Bundle) extras.clone();
582 extras.putString(
583 NewOutgoingCallIntentBroadcaster.EXTRA_GATEWAY_PROVIDER_PACKAGE,
584 gatewayInfo.getGatewayProviderPackageName());
585 extras.putParcelable(
586 NewOutgoingCallIntentBroadcaster.EXTRA_GATEWAY_ORIGINAL_URI,
587 gatewayInfo.getOriginalHandle());
588 }
Tyler Gunnc4abd912014-07-08 14:22:10 -0700589 ConnectionRequest request = new ConnectionRequest(callId, call.getHandle(), extras,
590 call.getVideoState());
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700591
592 try {
593 mServiceInterface.call(request);
594 } catch (RemoteException e) {
595 Log.e(this, e, "Failure to call -- %s", getDescriptor());
596 mPendingOutgoingCalls.remove(callId).onOutgoingCallFailure(
597 DisconnectCause.ERROR_UNSPECIFIED, e.toString());
598 }
599 }
600
601 @Override
602 public void onFailure() {
603 Log.e(this, new Exception(), "Failure to call %s", getDescriptor());
604 callResponse.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
605 }
606 };
607
608 mBinder.bind(callback);
609 }
610
611 /** @see ConnectionService#abort(String) */
612 void abort(Call call) {
613 // Clear out any pending outgoing call data
614 String callId = mCallIdMapper.getCallId(call);
615
616 // If still bound, tell the connection service to abort.
617 if (isServiceValid("abort")) {
618 try {
619 logOutgoing("abort %s", callId);
620 mServiceInterface.abort(callId);
621 } catch (RemoteException e) {
622 }
623 }
624
625 removeCall(call);
626 }
627
628 /** @see ConnectionService#hold(String) */
629 void hold(Call call) {
630 if (isServiceValid("hold")) {
631 try {
632 logOutgoing("hold %s", mCallIdMapper.getCallId(call));
633 mServiceInterface.hold(mCallIdMapper.getCallId(call));
634 } catch (RemoteException e) {
635 }
636 }
637 }
638
639 /** @see ConnectionService#unhold(String) */
640 void unhold(Call call) {
641 if (isServiceValid("unhold")) {
642 try {
643 logOutgoing("unhold %s", mCallIdMapper.getCallId(call));
644 mServiceInterface.unhold(mCallIdMapper.getCallId(call));
645 } catch (RemoteException e) {
646 }
647 }
648 }
649
650 /** @see ConnectionService#onAudioStateChanged(String,CallAudioState) */
651 void onAudioStateChanged(Call activeCall, CallAudioState audioState) {
652 if (isServiceValid("onAudioStateChanged")) {
653 try {
654 logOutgoing("onAudioStateChanged %s %s",
655 mCallIdMapper.getCallId(activeCall), audioState);
656 mServiceInterface.onAudioStateChanged(mCallIdMapper.getCallId(activeCall),
657 audioState);
658 } catch (RemoteException e) {
659 }
660 }
661 }
662
663 /**
664 * Starts retrieval of details for an incoming call. Details are returned through the
665 * call-service adapter using the specified call ID. Upon failure, the specified error callback
666 * is invoked. Can be invoked even when the connection service is unbound. See
667 * {@link IConnectionService#createIncomingCall}.
668 *
669 * @param call The call used for the incoming call.
670 * @param extras The {@link ConnectionService}-provided extras which need to be sent back.
671 * @param errorCallback The callback to invoke upon failure.
672 */
673 void createIncomingCall(final Call call, final Bundle extras, final Runnable errorCallback) {
674 Log.d(this, "createIncomingCall(%s) via %s.", call, getComponentName());
675 BindCallback callback = new BindCallback() {
676 @Override
677 public void onSuccess() {
678 if (isServiceValid("createIncomingCall")) {
679 mPendingIncomingCalls.add(call);
680 String callId = mCallIdMapper.getCallId(call);
681 logOutgoing("createIncomingCall %s %s", callId, extras);
682 ConnectionRequest request = new ConnectionRequest(
Tyler Gunnc4abd912014-07-08 14:22:10 -0700683 callId, call.getHandle(), extras, call.getVideoState());
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700684 try {
685 mServiceInterface.createIncomingCall(request);
686 } catch (RemoteException e) {
687 }
688 }
689 }
690
691 @Override
692 public void onFailure() {
693 errorCallback.run();
694 }
695 };
696
697 mBinder.bind(callback);
698 }
699
700 /** @see ConnectionService#disconnect(String) */
701 void disconnect(Call call) {
702 if (isServiceValid("disconnect")) {
703 try {
704 logOutgoing("disconnect %s", mCallIdMapper.getCallId(call));
705 mServiceInterface.disconnect(mCallIdMapper.getCallId(call));
706 } catch (RemoteException e) {
707 }
708 }
709 }
710
711 /** @see ConnectionService#answer(String) */
712 void answer(Call call) {
713 if (isServiceValid("answer")) {
714 try {
715 logOutgoing("answer %s", mCallIdMapper.getCallId(call));
716 mServiceInterface.answer(mCallIdMapper.getCallId(call));
717 } catch (RemoteException e) {
718 }
719 }
720 }
721
722 /** @see ConnectionService#reject(String) */
723 void reject(Call call) {
724 if (isServiceValid("reject")) {
725 try {
726 logOutgoing("reject %s", mCallIdMapper.getCallId(call));
727 mServiceInterface.reject(mCallIdMapper.getCallId(call));
728 } catch (RemoteException e) {
729 }
730 }
731 }
732
733 /** @see ConnectionService#playDtmfTone(String,char) */
734 void playDtmfTone(Call call, char digit) {
735 if (isServiceValid("playDtmfTone")) {
736 try {
737 logOutgoing("playDtmfTone %s %c", mCallIdMapper.getCallId(call), digit);
738 mServiceInterface.playDtmfTone(mCallIdMapper.getCallId(call), digit);
739 } catch (RemoteException e) {
740 }
741 }
742 }
743
744 /** @see ConnectionService#stopDtmfTone(String) */
745 void stopDtmfTone(Call call) {
746 if (isServiceValid("stopDtmfTone")) {
747 try {
748 logOutgoing("stopDtmfTone %s", mCallIdMapper.getCallId(call));
749 mServiceInterface.stopDtmfTone(mCallIdMapper.getCallId(call));
750 } catch (RemoteException e) {
751 }
752 }
753 }
754
755 void addCall(Call call) {
756 if (mCallIdMapper.getCallId(call) == null) {
757 mCallIdMapper.addCall(call);
758 }
759 }
760
761 /**
762 * Associates newCall with this connection service by replacing callToReplace.
763 */
764 void replaceCall(Call newCall, Call callToReplace) {
765 Preconditions.checkState(callToReplace.getConnectionService() == this);
766 mCallIdMapper.replaceCall(newCall, callToReplace);
767 }
768
769 void removeCall(Call call) {
770 mPendingIncomingCalls.remove(call);
771
772 OutgoingCallResponse outgoingResultCallback =
773 mPendingOutgoingCalls.remove(mCallIdMapper.getCallId(call));
774 if (outgoingResultCallback != null) {
775 outgoingResultCallback.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
776 }
777
778 mCallIdMapper.removeCall(call);
779 }
780
781 void onPostDialContinue(Call call, boolean proceed) {
782 if (isServiceValid("onPostDialContinue")) {
783 try {
784 logOutgoing("onPostDialContinue %s %b", mCallIdMapper.getCallId(call), proceed);
785 mServiceInterface.onPostDialContinue(mCallIdMapper.getCallId(call), proceed);
786 } catch (RemoteException ignored) {
787 }
788 }
789 }
790
791 void onPhoneAccountClicked(Call call) {
792 if (isServiceValid("onPhoneAccountClicked")) {
793 try {
794 logOutgoing("onPhoneAccountClicked %s", mCallIdMapper.getCallId(call));
795 mServiceInterface.onPhoneAccountClicked(mCallIdMapper.getCallId(call));
796 } catch (RemoteException ignored) {
797 }
798 }
799 }
800
801 void conference(final Call conferenceCall, Call call) {
802 if (isServiceValid("conference")) {
803 try {
804 conferenceCall.setConnectionService(this);
805 mPendingConferenceCalls.add(conferenceCall);
806 mHandler.postDelayed(new Runnable() {
807 @Override public void run() {
808 if (mPendingConferenceCalls.remove(conferenceCall)) {
809 conferenceCall.expireConference();
810 Log.i(this, "Conference call expired: %s", conferenceCall);
811 }
812 }
813 }, Timeouts.getConferenceCallExpireMillis());
814
815 logOutgoing("conference %s %s",
816 mCallIdMapper.getCallId(conferenceCall),
817 mCallIdMapper.getCallId(call));
818 mServiceInterface.conference(
819 mCallIdMapper.getCallId(conferenceCall),
820 mCallIdMapper.getCallId(call));
821 } catch (RemoteException ignored) {
822 }
823 }
824 }
825
826 void splitFromConference(Call call) {
827 if (isServiceValid("splitFromConference")) {
828 try {
829 logOutgoing("splitFromConference %s", mCallIdMapper.getCallId(call));
830 mServiceInterface.splitFromConference(mCallIdMapper.getCallId(call));
831 } catch (RemoteException ignored) {
832 }
833 }
834 }
835
836 /** {@inheritDoc} */
837 @Override
838 protected void setServiceInterface(IBinder binder) {
839 if (binder == null) {
840 // We have lost our service connection. Notify the world that this service is done.
841 // We must notify the adapter before CallsManager. The adapter will force any pending
842 // outgoing calls to try the next service. This needs to happen before CallsManager
843 // tries to clean up any calls still associated with this service.
844 handleConnectionServiceDeath();
845 CallsManager.getInstance().handleConnectionServiceDeath(this);
846 mServiceInterface = null;
847 } else {
848 mServiceInterface = IConnectionService.Stub.asInterface(binder);
849 addConnectionServiceAdapter(mAdapter);
850 }
851 }
852
853 /**
854 * Called when the associated connection service dies.
855 */
856 private void handleConnectionServiceDeath() {
857 if (!mPendingOutgoingCalls.isEmpty()) {
858 for (OutgoingCallResponse callback : mPendingOutgoingCalls.values()) {
859 callback.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
860 }
861 mPendingOutgoingCalls.clear();
862 }
863
864 if (!mPendingIncomingCalls.isEmpty()) {
865 // Iterate through a copy because the code inside the loop will modify the original
866 // list.
867 for (Call call : ImmutableList.copyOf(mPendingIncomingCalls)) {
868 Preconditions.checkState(call.isIncoming());
869 mIncomingCallsManager.handleFailedIncomingCall(call);
870 }
871
872 if (!mPendingIncomingCalls.isEmpty()) {
873 Log.wtf(this, "Pending calls did not get cleared.");
874 mPendingIncomingCalls.clear();
875 }
876 }
877
878 mCallIdMapper.clear();
879 }
880
881 private void logIncoming(String msg, Object... params) {
882 Log.d(this, "ConnectionService -> Telecomm: " + msg, params);
883 }
884
885 private void logOutgoing(String msg, Object... params) {
886 Log.d(this, "Telecomm -> ConnectionService: " + msg, params);
887 }
888
889 private void queryRemoteConnectionServices(final RemoteServiceCallback callback) {
890 final List<IBinder> connectionServices = new ArrayList<>();
891 final List<ComponentName> components = new ArrayList<>();
892
893 mCallServiceRepository.lookupServices(new LookupCallback<ConnectionServiceWrapper>() {
894 private int mRemainingResponses;
895
896 /** ${inheritDoc} */
897 @Override
898 public void onComplete(Collection<ConnectionServiceWrapper> services) {
899 mRemainingResponses = services.size() - 1;
900 for (ConnectionServiceWrapper cs : services) {
901 if (cs != ConnectionServiceWrapper.this) {
902 final ConnectionServiceWrapper currentConnectionService = cs;
903 cs.mBinder.bind(new BindCallback() {
904 @Override
905 public void onSuccess() {
906 Log.d(this, "Adding ***** %s",
907 currentConnectionService.getDescriptor());
908 connectionServices.add(
909 currentConnectionService.mServiceInterface.asBinder());
910 components.add(currentConnectionService.getComponentName());
911 maybeComplete();
912 }
913
914 @Override
915 public void onFailure() {
916 // add null so that we always add up to totalExpected even if
917 // some of the connection services fail to bind.
918 maybeComplete();
919 }
920
921 private void maybeComplete() {
922 if (--mRemainingResponses == 0) {
923 try {
924 callback.onResult(components, connectionServices);
925 } catch (RemoteException ignored) {
926 }
927 }
928 }
929 });
930 }
931 }
932 }
933 });
934 }
935}