blob: 365a5ce5e9326a850c3877ac0f387e1b0244a96c [file] [log] [blame]
Santos Cordon63aeb162014-02-10 09:20:40 -08001/*
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
Santos Cordon5924bea2014-06-18 06:39:51 -070019import android.content.ComponentName;
Evan Charltona05805b2014-03-05 08:21:46 -080020import android.os.Bundle;
Santos Cordon3d3b4052014-05-05 12:05:36 -070021import android.os.Handler;
Santos Cordon63aeb162014-02-10 09:20:40 -080022import android.os.IBinder;
Santos Cordon3d3b4052014-05-05 12:05:36 -070023import android.os.Message;
Santos Cordon63aeb162014-02-10 09:20:40 -080024import android.os.RemoteException;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070025import android.telecomm.CallAudioState;
Santos Cordon63aeb162014-02-10 09:20:40 -080026import android.telecomm.CallInfo;
Evan Charltona05805b2014-03-05 08:21:46 -080027import android.telecomm.CallService;
Ben Giladc5b22692014-02-18 20:03:22 -080028import android.telecomm.CallServiceDescriptor;
Ihab Awada3cb9e32014-06-03 18:45:05 -070029import android.telecomm.ConnectionRequest;
Sailesh Nepal83cfe7c2014-03-11 19:54:22 -070030import android.telecomm.TelecommConstants;
Ihab Awada3cb9e32014-06-03 18:45:05 -070031import android.telephony.DisconnectCause;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070032
Santos Cordon3d3b4052014-05-05 12:05:36 -070033import com.android.internal.os.SomeArgs;
Andrew Leee9a77652014-06-26 13:07:57 -070034
Sailesh Nepala439e1b2014-03-11 18:19:58 -070035import com.android.internal.telecomm.ICallService;
36import com.android.internal.telecomm.ICallServiceAdapter;
37import com.android.internal.telecomm.ICallServiceProvider;
Andrew Leee9a77652014-06-26 13:07:57 -070038import com.android.internal.telecomm.ICallVideoProvider;
Santos Cordon5924bea2014-06-18 06:39:51 -070039import com.android.internal.telecomm.RemoteServiceCallback;
40import com.android.telecomm.BaseRepository.LookupCallback;
Sailesh Nepal0e5410a2014-04-04 01:20:58 -070041import com.google.common.base.Preconditions;
Santos Cordon3d3b4052014-05-05 12:05:36 -070042import com.google.common.collect.ImmutableList;
Santos Cordon3d3b4052014-05-05 12:05:36 -070043
Ihab Awada3cb9e32014-06-03 18:45:05 -070044import org.apache.http.conn.ClientConnectionRequest;
45
Santos Cordon5924bea2014-06-18 06:39:51 -070046import java.util.ArrayList;
47import java.util.Collection;
Santos Cordon682fe6b2014-05-20 08:56:39 -070048import java.util.HashMap;
Santos Cordona1610702014-06-04 20:22:56 -070049import java.util.HashSet;
Santos Cordon8f3282c2014-06-01 13:56:02 -070050import java.util.List;
Santos Cordon682fe6b2014-05-20 08:56:39 -070051import java.util.Map;
Santos Cordon3d3b4052014-05-05 12:05:36 -070052import java.util.Set;
Santos Cordon63aeb162014-02-10 09:20:40 -080053
54/**
55 * Wrapper for {@link ICallService}s, handles binding to {@link ICallService} and keeps track of
56 * when the object can safely be unbound. Other classes should not use {@link ICallService} directly
57 * and instead should use this class to invoke methods of {@link ICallService}.
Santos Cordon63aeb162014-02-10 09:20:40 -080058 */
Ben Gilad61925612014-03-11 19:06:36 -070059final class CallServiceWrapper extends ServiceBinder<ICallService> {
Santos Cordona1610702014-06-04 20:22:56 -070060 private static final String TAG = CallServiceWrapper.class.getSimpleName();
Santos Cordon63aeb162014-02-10 09:20:40 -080061
Santos Cordon3d3b4052014-05-05 12:05:36 -070062 private final class Adapter extends ICallServiceAdapter.Stub {
Santos Cordon3d3b4052014-05-05 12:05:36 -070063 private static final int MSG_NOTIFY_INCOMING_CALL = 1;
64 private static final int MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL = 2;
65 private static final int MSG_HANDLE_FAILED_OUTGOING_CALL = 3;
Sailesh Nepal5a73b032014-06-25 15:53:21 -070066 private static final int MSG_CANCEL_OUTGOING_CALL = 4;
67 private static final int MSG_SET_ACTIVE = 5;
68 private static final int MSG_SET_RINGING = 6;
69 private static final int MSG_SET_DIALING = 7;
70 private static final int MSG_SET_DISCONNECTED = 8;
71 private static final int MSG_SET_ON_HOLD = 9;
72 private static final int MSG_SET_REQUESTING_RINGBACK = 10;
73 private static final int MSG_ON_POST_DIAL_WAIT = 11;
74 private static final int MSG_CAN_CONFERENCE = 12;
75 private static final int MSG_SET_IS_CONFERENCED = 13;
76 private static final int MSG_ADD_CONFERENCE_CALL = 14;
77 private static final int MSG_HANDOFF_CALL = 15;
78 private static final int MSG_QUERY_REMOTE_CALL_SERVICES = 16;
Andrew Leee9a77652014-06-26 13:07:57 -070079 private static final int MSG_SET_CALL_VIDEO_PROVIDER = 17;
Santos Cordon3d3b4052014-05-05 12:05:36 -070080
81 private final Handler mHandler = new Handler() {
82 @Override
83 public void handleMessage(Message msg) {
84 Call call;
85 switch (msg.what) {
Santos Cordon3d3b4052014-05-05 12:05:36 -070086 case MSG_NOTIFY_INCOMING_CALL:
87 CallInfo clientCallInfo = (CallInfo) msg.obj;
88 call = mCallIdMapper.getCall(clientCallInfo.getId());
Santos Cordon682fe6b2014-05-20 08:56:39 -070089 if (call != null && mPendingIncomingCalls.remove(call) &&
90 call.isIncoming()) {
Santos Cordon3d3b4052014-05-05 12:05:36 -070091 CallInfo callInfo = new CallInfo(null, clientCallInfo.getState(),
92 clientCallInfo.getHandle());
93 mIncomingCallsManager.handleSuccessfulIncomingCall(call, callInfo);
94 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -070095 // TODO(santoscordon): For this an the other commented logging, we need
96 // to reenable it. At the moment all CallServiceAdapters receive
97 // notification of changes to all calls, even calls which it may not own
98 // (ala remote connections). We need to fix that and then uncomment the
99 // logging calls here.
100 //Log.w(this, "notifyIncomingCall, unknown incoming call: %s, id: %s",
101 // call, clientCallInfo.getId());
Santos Cordon3d3b4052014-05-05 12:05:36 -0700102 }
103 break;
Santos Cordon682fe6b2014-05-20 08:56:39 -0700104 case MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL: {
105 String callId = (String) msg.obj;
106 if (mPendingOutgoingCalls.containsKey(callId)) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700107 mPendingOutgoingCalls.remove(callId).onOutgoingCallSuccess();
Santos Cordon3d3b4052014-05-05 12:05:36 -0700108 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700109 //Log.w(this, "handleSuccessfulOutgoingCall, unknown call: %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700110 }
111 break;
Santos Cordon682fe6b2014-05-20 08:56:39 -0700112 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700113 case MSG_HANDLE_FAILED_OUTGOING_CALL: {
114 SomeArgs args = (SomeArgs) msg.obj;
115 try {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700116 String callId = (String) args.arg1;
Ihab Awada3cb9e32014-06-03 18:45:05 -0700117 int statusCode = args.argi1;
118 String statusMsg = (String) args.arg2;
Santos Cordon682fe6b2014-05-20 08:56:39 -0700119 // TODO(santoscordon): Do something with 'reason' or get rid of it.
120
121 if (mPendingOutgoingCalls.containsKey(callId)) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700122 mPendingOutgoingCalls.remove(callId).onOutgoingCallFailure(
123 statusCode, statusMsg);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700124 mCallIdMapper.removeCall(callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700125 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700126 //Log.w(this, "handleFailedOutgoingCall, unknown call: %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700127 }
128 } finally {
129 args.recycle();
130 }
131 break;
132 }
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700133 case MSG_CANCEL_OUTGOING_CALL: {
134 String callId = (String) msg.obj;
135 if (mPendingOutgoingCalls.containsKey(callId)) {
136 mPendingOutgoingCalls.remove(callId).onOutgoingCallCancel();
137 } else {
138 //Log.w(this, "cancelOutgoingCall, unknown call: %s", callId);
139 }
140 break;
141 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700142 case MSG_SET_ACTIVE:
143 call = mCallIdMapper.getCall(msg.obj);
144 if (call != null) {
145 mCallsManager.markCallAsActive(call);
146 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700147 //Log.w(this, "setActive, unknown call id: %s", msg.obj);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700148 }
149 break;
150 case MSG_SET_RINGING:
151 call = mCallIdMapper.getCall(msg.obj);
152 if (call != null) {
153 mCallsManager.markCallAsRinging(call);
154 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700155 //Log.w(this, "setRinging, unknown call id: %s", msg.obj);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700156 }
157 break;
158 case MSG_SET_DIALING:
159 call = mCallIdMapper.getCall(msg.obj);
160 if (call != null) {
161 mCallsManager.markCallAsDialing(call);
162 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700163 //Log.w(this, "setDialing, unknown call id: %s", msg.obj);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700164 }
165 break;
166 case MSG_SET_DISCONNECTED: {
167 SomeArgs args = (SomeArgs) msg.obj;
168 try {
169 call = mCallIdMapper.getCall(args.arg1);
170 String disconnectMessage = (String) args.arg2;
171 int disconnectCause = args.argi1;
172 if (call != null) {
173 mCallsManager.markCallAsDisconnected(call, disconnectCause,
174 disconnectMessage);
175 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700176 //Log.w(this, "setDisconnected, unknown call id: %s", args.arg1);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700177 }
178 } finally {
179 args.recycle();
180 }
181 break;
182 }
183 case MSG_SET_ON_HOLD:
184 call = mCallIdMapper.getCall(msg.obj);
185 if (call != null) {
186 mCallsManager.markCallAsOnHold(call);
187 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700188 //Log.w(this, "setOnHold, unknown call id: %s", msg.obj);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700189 }
190 break;
Evan Charlton352105c2014-06-03 14:10:54 -0700191 case MSG_SET_REQUESTING_RINGBACK: {
Ihab Awad50a57132014-05-28 16:49:38 -0700192 SomeArgs args = (SomeArgs) msg.obj;
193 try {
194 call = mCallIdMapper.getCall(args.arg1);
195 boolean ringback = (boolean) args.arg2;
196 if (call != null) {
197 call.setRequestingRingback(ringback);
198 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700199 //Log.w(this, "setRingback, unknown call id: %s", args.arg1);
Ihab Awad50a57132014-05-28 16:49:38 -0700200 }
201 } finally {
202 args.recycle();
203 }
204 break;
Evan Charlton352105c2014-06-03 14:10:54 -0700205 }
Santos Cordona1610702014-06-04 20:22:56 -0700206 case MSG_ON_POST_DIAL_WAIT: {
Evan Charlton352105c2014-06-03 14:10:54 -0700207 SomeArgs args = (SomeArgs) msg.obj;
208 try {
209 call = mCallIdMapper.getCall(args.arg1);
210 if (call != null) {
211 String remaining = (String) args.arg2;
212 call.onPostDialWait(remaining);
213 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700214 //Log.w(this, "onPostDialWait, unknown call id: %s", args.arg1);
Evan Charlton352105c2014-06-03 14:10:54 -0700215 }
216 } finally {
217 args.recycle();
218 }
Santos Cordona1610702014-06-04 20:22:56 -0700219 break;
220 }
Sailesh Nepal6098d2c2014-06-06 10:56:53 -0700221 case MSG_HANDOFF_CALL:
222 call = mCallIdMapper.getCall(msg.obj);
223 if (call != null) {
224 mCallsManager.startHandoffForCall(call);
225 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700226 //Log.w(this, "handoffCall, unknown call id: %s", msg.obj);
Sailesh Nepal6098d2c2014-06-06 10:56:53 -0700227 }
228 break;
Santos Cordona1610702014-06-04 20:22:56 -0700229 case MSG_CAN_CONFERENCE: {
230 call = mCallIdMapper.getCall(msg.obj);
231 if (call != null) {
232 call.setIsConferenceCapable(msg.arg1 == 1);
233 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700234 //Log.w(CallServiceWrapper.this, "canConference, unknown call id: %s",
235 // msg.obj);
Santos Cordona1610702014-06-04 20:22:56 -0700236 }
237 break;
238 }
239 case MSG_SET_IS_CONFERENCED: {
240 SomeArgs args = (SomeArgs) msg.obj;
241 try {
242 Call childCall = mCallIdMapper.getCall(args.arg1);
243 if (childCall != null) {
244 String conferenceCallId = (String) args.arg2;
Santos Cordona1610702014-06-04 20:22:56 -0700245 if (conferenceCallId == null) {
246 childCall.setParentCall(null);
247 } else {
248 Call conferenceCall = mCallIdMapper.getCall(conferenceCallId);
249 if (conferenceCall != null &&
250 !mPendingConferenceCalls.contains(conferenceCall)) {
251 childCall.setParentCall(conferenceCall);
252 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700253 //Log.w(this, "setIsConferenced, unknown conference id %s",
254 // conferenceCallId);
Santos Cordona1610702014-06-04 20:22:56 -0700255 }
256 }
257 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700258 //Log.w(this, "setIsConferenced, unknown call id: %s", args.arg1);
Santos Cordona1610702014-06-04 20:22:56 -0700259 }
260 } finally {
261 args.recycle();
262 }
263 break;
264 }
265 case MSG_ADD_CONFERENCE_CALL: {
266 SomeArgs args = (SomeArgs) msg.obj;
267 try {
268 String callId = (String) args.arg1;
Santos Cordona1610702014-06-04 20:22:56 -0700269 Call conferenceCall = mCallIdMapper.getCall(callId);
270 if (mPendingConferenceCalls.remove(conferenceCall)) {
271 Log.v(this, "confirming conf call %s", conferenceCall);
272 conferenceCall.confirmConference();
273 } else {
Santos Cordon5924bea2014-06-18 06:39:51 -0700274 //Log.w(this, "addConference, unknown call id: %s", callId);
Santos Cordona1610702014-06-04 20:22:56 -0700275 }
276 } finally {
277 args.recycle();
278 }
279 break;
280 }
Santos Cordon5924bea2014-06-18 06:39:51 -0700281 case MSG_QUERY_REMOTE_CALL_SERVICES: {
282 CallServiceWrapper.this.queryRemoteConnectionServices(
283 (RemoteServiceCallback) msg.obj);
Andrew Leee9a77652014-06-26 13:07:57 -0700284 break;
285 }
286 case MSG_SET_CALL_VIDEO_PROVIDER: {
287 SomeArgs args = (SomeArgs) msg.obj;
288 try {
289 call = mCallIdMapper.getCall(args.arg1);
290 if (call != null) {
291 call.setCallVideoProvider();
292 }
293 } finally {
294 args.recycle();
295 }
296 break;
Santos Cordon5924bea2014-06-18 06:39:51 -0700297 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700298 }
299 }
300 };
301
302 /** {@inheritDoc} */
303 @Override
Santos Cordon3d3b4052014-05-05 12:05:36 -0700304 public void notifyIncomingCall(CallInfo callInfo) {
Ihab Awad55a34282014-06-18 10:31:09 -0700305 logIncoming("notifyIncomingCall %s", callInfo);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700306 mCallIdMapper.checkValidCallId(callInfo.getId());
307 mHandler.obtainMessage(MSG_NOTIFY_INCOMING_CALL, callInfo).sendToTarget();
308 }
309
310 /** {@inheritDoc} */
311 @Override
312 public void handleSuccessfulOutgoingCall(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700313 logIncoming("handleSuccessfulOutgoingCall %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700314 mCallIdMapper.checkValidCallId(callId);
315 mHandler.obtainMessage(MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL, callId).sendToTarget();
316 }
317
318 /** {@inheritDoc} */
319 @Override
Ihab Awada3cb9e32014-06-03 18:45:05 -0700320 public void handleFailedOutgoingCall(
321 ConnectionRequest request,
322 int errorCode,
323 String errorMsg) {
Ihab Awad55a34282014-06-18 10:31:09 -0700324 logIncoming("handleFailedOutgoingCall %s %d %s", request, errorCode, errorMsg);
Ihab Awada3cb9e32014-06-03 18:45:05 -0700325 mCallIdMapper.checkValidCallId(request.getCallId());
Santos Cordon3d3b4052014-05-05 12:05:36 -0700326 SomeArgs args = SomeArgs.obtain();
Ihab Awada3cb9e32014-06-03 18:45:05 -0700327 args.arg1 = request.getCallId();
328 args.argi1 = errorCode;
329 args.arg2 = errorMsg;
Santos Cordon3d3b4052014-05-05 12:05:36 -0700330 mHandler.obtainMessage(MSG_HANDLE_FAILED_OUTGOING_CALL, args).sendToTarget();
331 }
332
333 /** {@inheritDoc} */
334 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700335 public void cancelOutgoingCall(String callId) {
336 logIncoming("cancelOutgoingCall %s", callId);
337 mCallIdMapper.checkValidCallId(callId);
338 mHandler.obtainMessage(MSG_CANCEL_OUTGOING_CALL, callId).sendToTarget();
339 }
340
341 /** {@inheritDoc} */
342 @Override
Santos Cordon3d3b4052014-05-05 12:05:36 -0700343 public void setActive(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700344 logIncoming("setActive %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700345 mCallIdMapper.checkValidCallId(callId);
346 mHandler.obtainMessage(MSG_SET_ACTIVE, callId).sendToTarget();
347 }
348
349 /** {@inheritDoc} */
350 @Override
351 public void setRinging(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700352 logIncoming("setRinging %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700353 mCallIdMapper.checkValidCallId(callId);
354 mHandler.obtainMessage(MSG_SET_RINGING, callId).sendToTarget();
355 }
356
357 /** {@inheritDoc} */
358 @Override
Andrew Leee9a77652014-06-26 13:07:57 -0700359 public void setCallVideoProvider(String callId, ICallVideoProvider callVideoProvider) {
360 logIncoming("setCallVideoProvider %s", callId);
361 mCallIdMapper.checkValidCallId(callId);
362 SomeArgs args = SomeArgs.obtain();
363 args.arg1 = callId;
364 args.arg2 = callVideoProvider;
365 mHandler.obtainMessage(MSG_SET_CALL_VIDEO_PROVIDER, args).sendToTarget();
366 }
367
368 /** {@inheritDoc} */
369 @Override
Santos Cordon3d3b4052014-05-05 12:05:36 -0700370 public void setDialing(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700371 logIncoming("setDialing %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700372 mCallIdMapper.checkValidCallId(callId);
373 mHandler.obtainMessage(MSG_SET_DIALING, callId).sendToTarget();
374 }
375
376 /** {@inheritDoc} */
377 @Override
378 public void setDisconnected(
379 String callId, int disconnectCause, String disconnectMessage) {
Ihab Awad55a34282014-06-18 10:31:09 -0700380 logIncoming("setDisconnected %s %d %s", callId, disconnectCause, disconnectMessage);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700381 mCallIdMapper.checkValidCallId(callId);
382 SomeArgs args = SomeArgs.obtain();
383 args.arg1 = callId;
384 args.arg2 = disconnectMessage;
385 args.argi1 = disconnectCause;
386 mHandler.obtainMessage(MSG_SET_DISCONNECTED, args).sendToTarget();
387 }
388
389 /** {@inheritDoc} */
390 @Override
391 public void setOnHold(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700392 logIncoming("setOnHold %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700393 mCallIdMapper.checkValidCallId(callId);
394 mHandler.obtainMessage(MSG_SET_ON_HOLD, callId).sendToTarget();
395 }
Ihab Awad50a57132014-05-28 16:49:38 -0700396
397 /** {@inheritDoc} */
398 @Override
399 public void setRequestingRingback(String callId, boolean ringback) {
Ihab Awad55a34282014-06-18 10:31:09 -0700400 logIncoming("setRequestingRingback %s %b", callId, ringback);
Ihab Awad50a57132014-05-28 16:49:38 -0700401 mCallIdMapper.checkValidCallId(callId);
402 SomeArgs args = SomeArgs.obtain();
403 args.arg1 = callId;
404 args.arg2 = ringback;
405 mHandler.obtainMessage(MSG_SET_REQUESTING_RINGBACK, args).sendToTarget();
406 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700407
408 /** ${inheritDoc} */
409 @Override
410 public void removeCall(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700411 logIncoming("removeCall %s", callId);
Santos Cordon8f3282c2014-06-01 13:56:02 -0700412 }
413
414 /** ${inheritDoc} */
415 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700416 public void setCanConference(String callId, boolean canConference) {
Ihab Awad55a34282014-06-18 10:31:09 -0700417 logIncoming("setCanConference %s %b", callId, canConference);
Santos Cordona1610702014-06-04 20:22:56 -0700418 mHandler.obtainMessage(MSG_CAN_CONFERENCE, canConference ? 1 : 0, 0, callId)
419 .sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700420 }
421
422 /** ${inheritDoc} */
423 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700424 public void setIsConferenced(String callId, String conferenceCallId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700425 logIncoming("setIsConferenced %s %s", callId, conferenceCallId);
Santos Cordona1610702014-06-04 20:22:56 -0700426 SomeArgs args = SomeArgs.obtain();
427 args.arg1 = callId;
428 args.arg2 = conferenceCallId;
429 mHandler.obtainMessage(MSG_SET_IS_CONFERENCED, args).sendToTarget();
430 }
431
432 /** ${InheritDoc} */
433 @Override
434 public void addConferenceCall(String callId, CallInfo callInfo) {
Ihab Awad55a34282014-06-18 10:31:09 -0700435 logIncoming("addConferenceCall %s %s", callId, callInfo);
Santos Cordona1610702014-06-04 20:22:56 -0700436 mCallIdMapper.checkValidCallId(callId);
437 SomeArgs args = SomeArgs.obtain();
438 args.arg1 = callId;
439 args.arg2 = callInfo;
440 mHandler.obtainMessage(MSG_ADD_CONFERENCE_CALL, args).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700441 }
Evan Charlton352105c2014-06-03 14:10:54 -0700442
443 @Override
444 public void onPostDialWait(String callId, String remaining) throws RemoteException {
Ihab Awad55a34282014-06-18 10:31:09 -0700445 logIncoming("onPostDialWait %s %s", callId, remaining);
Evan Charlton352105c2014-06-03 14:10:54 -0700446 mCallIdMapper.checkValidCallId(callId);
447 SomeArgs args = SomeArgs.obtain();
448 args.arg1 = callId;
449 args.arg2 = remaining;
450 mHandler.obtainMessage(MSG_ON_POST_DIAL_WAIT, args).sendToTarget();
451 }
Sailesh Nepal6098d2c2014-06-06 10:56:53 -0700452
453 /** {@inheritDoc} */
454 @Override
455 public void handoffCall(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700456 logIncoming("handoffCall %s", callId);
Sailesh Nepal6098d2c2014-06-06 10:56:53 -0700457 mCallIdMapper.checkValidCallId(callId);
458 mHandler.obtainMessage(MSG_HANDOFF_CALL, callId).sendToTarget();
459 }
Santos Cordon5924bea2014-06-18 06:39:51 -0700460
461 /** ${inheritDoc} */
462 @Override
463 public void queryRemoteConnectionServices(RemoteServiceCallback callback) {
464 logIncoming("queryRemoteCSs");
465 mHandler.obtainMessage(MSG_QUERY_REMOTE_CALL_SERVICES, callback).sendToTarget();
466 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700467 }
468
469 private final Adapter mAdapter = new Adapter();
470 private final CallsManager mCallsManager = CallsManager.getInstance();
Santos Cordona1610702014-06-04 20:22:56 -0700471 private final Set<Call> mPendingIncomingCalls = new HashSet<>();
472 private final Set<Call> mPendingConferenceCalls = new HashSet<>();
Ben Giladc5b22692014-02-18 20:03:22 -0800473 private final CallServiceDescriptor mDescriptor;
Santos Cordon3d3b4052014-05-05 12:05:36 -0700474 private final CallIdMapper mCallIdMapper = new CallIdMapper("CallService");
Santos Cordon3d3b4052014-05-05 12:05:36 -0700475 private final IncomingCallsManager mIncomingCallsManager;
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700476 private final Map<String, OutgoingCallResponse> mPendingOutgoingCalls = new HashMap<>();
Santos Cordona1610702014-06-04 20:22:56 -0700477 private final Handler mHandler = new Handler();
Santos Cordonc195e362014-02-11 17:05:31 -0800478
Ben Gilad61925612014-03-11 19:06:36 -0700479 private Binder mBinder = new Binder();
Santos Cordon3d3b4052014-05-05 12:05:36 -0700480 private ICallService mServiceInterface;
Santos Cordon5924bea2014-06-18 06:39:51 -0700481 private final CallServiceRepository mCallServiceRepository;
Ben Gilad61925612014-03-11 19:06:36 -0700482
Santos Cordon63aeb162014-02-10 09:20:40 -0800483 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700484 * Creates a call-service for the specified descriptor.
Santos Cordonc195e362014-02-11 17:05:31 -0800485 *
Santos Cordon61d0f702014-02-19 02:52:23 -0800486 * @param descriptor The call-service descriptor from
Santos Cordon3d3b4052014-05-05 12:05:36 -0700487 * {@link ICallServiceProvider#lookupCallServices}.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700488 * @param incomingCallsManager Manages the incoming call initialization flow.
Santos Cordon5924bea2014-06-18 06:39:51 -0700489 * @param callServiceRepository Call service repository.
Santos Cordon63aeb162014-02-10 09:20:40 -0800490 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700491 CallServiceWrapper(
492 CallServiceDescriptor descriptor,
Santos Cordon5924bea2014-06-18 06:39:51 -0700493 IncomingCallsManager incomingCallsManager,
494 CallServiceRepository callServiceRepository) {
Sailesh Nepala439e1b2014-03-11 18:19:58 -0700495 super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent());
Ben Giladc5b22692014-02-18 20:03:22 -0800496 mDescriptor = descriptor;
Santos Cordon3d3b4052014-05-05 12:05:36 -0700497 mIncomingCallsManager = incomingCallsManager;
Santos Cordon5924bea2014-06-18 06:39:51 -0700498 mCallServiceRepository = callServiceRepository;
Santos Cordon63aeb162014-02-10 09:20:40 -0800499 }
500
Ben Gilad61925612014-03-11 19:06:36 -0700501 CallServiceDescriptor getDescriptor() {
Ben Giladc5b22692014-02-18 20:03:22 -0800502 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -0800503 }
504
Santos Cordon63aeb162014-02-10 09:20:40 -0800505 /** See {@link ICallService#setCallServiceAdapter}. */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700506 private void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800507 if (isServiceValid("setCallServiceAdapter")) {
508 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700509 logOutgoing("setCallServiceAdapter %s", callServiceAdapter);
Santos Cordon63aeb162014-02-10 09:20:40 -0800510 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -0800511 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800512 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800513 }
514 }
515
Ben Gilad61925612014-03-11 19:06:36 -0700516 /**
Santos Cordon682fe6b2014-05-20 08:56:39 -0700517 * Attempts to place the specified call, see {@link ICallService#call}. Returns the result
518 * asynchronously through the specified callback.
Ben Gilad61925612014-03-11 19:06:36 -0700519 */
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700520 void call(final Call call, final OutgoingCallResponse callResponse) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700521 Log.d(this, "call(%s) via %s.", call, getComponentName());
Ben Gilad61925612014-03-11 19:06:36 -0700522 BindCallback callback = new BindCallback() {
Santos Cordon3d3b4052014-05-05 12:05:36 -0700523 @Override
524 public void onSuccess() {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700525 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700526 mPendingOutgoingCalls.put(callId, callResponse);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700527
528 try {
529 CallInfo callInfo = call.toCallInfo(callId);
Ihab Awad55a34282014-06-18 10:31:09 -0700530 logOutgoing("call %s", callInfo);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700531 mServiceInterface.call(callInfo);
532 } catch (RemoteException e) {
Santos Cordon5924bea2014-06-18 06:39:51 -0700533 Log.e(this, e, "Failure to call -- %s", getDescriptor());
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700534 mPendingOutgoingCalls.remove(callId).onOutgoingCallFailure(
535 DisconnectCause.ERROR_UNSPECIFIED, e.toString());
Ben Gilad61925612014-03-11 19:06:36 -0700536 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800537 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700538
539 @Override
540 public void onFailure() {
Santos Cordon5924bea2014-06-18 06:39:51 -0700541 Log.e(this, new Exception(), "Failure to call %s", getDescriptor());
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700542 callResponse.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
Ben Gilad61925612014-03-11 19:06:36 -0700543 }
544 };
545
546 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800547 }
548
Ihab Awad74549ec2014-03-10 15:33:25 -0700549 /** @see CallService#abort(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700550 void abort(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700551 // Clear out any pending outgoing call data
552 String callId = mCallIdMapper.getCallId(call);
553
554 // If still bound, tell the call service to abort.
Ben Gilad28e8ad62014-03-06 17:01:54 -0800555 if (isServiceValid("abort")) {
556 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700557 logOutgoing("abort %s", callId);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700558 mServiceInterface.abort(callId);
Ben Gilad28e8ad62014-03-06 17:01:54 -0800559 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800560 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800561 }
Santos Cordon682fe6b2014-05-20 08:56:39 -0700562
563 removeCall(call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800564 }
565
Ihab Awad74549ec2014-03-10 15:33:25 -0700566 /** @see CallService#hold(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700567 void hold(Call call) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700568 if (isServiceValid("hold")) {
569 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700570 logOutgoing("hold %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700571 mServiceInterface.hold(mCallIdMapper.getCallId(call));
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700572 } catch (RemoteException e) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700573 }
574 }
575 }
576
Ihab Awad74549ec2014-03-10 15:33:25 -0700577 /** @see CallService#unhold(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700578 void unhold(Call call) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700579 if (isServiceValid("unhold")) {
580 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700581 logOutgoing("unhold %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700582 mServiceInterface.unhold(mCallIdMapper.getCallId(call));
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700583 } catch (RemoteException e) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700584 }
585 }
586 }
587
Ihab Awad74549ec2014-03-10 15:33:25 -0700588 /** @see CallService#onAudioStateChanged(String,CallAudioState) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700589 void onAudioStateChanged(Call activeCall, CallAudioState audioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700590 if (isServiceValid("onAudioStateChanged")) {
591 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700592 logOutgoing("onAudioStateChanged %s %s",
593 mCallIdMapper.getCallId(activeCall), audioState);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700594 mServiceInterface.onAudioStateChanged(mCallIdMapper.getCallId(activeCall),
595 audioState);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700596 } catch (RemoteException e) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700597 }
598 }
599 }
600
Ben Gilad61925612014-03-11 19:06:36 -0700601 /**
602 * Starts retrieval of details for an incoming call. Details are returned through the
603 * call-service adapter using the specified call ID. Upon failure, the specified error callback
Santos Cordon3d3b4052014-05-05 12:05:36 -0700604 * is invoked. Can be invoked even when the call service is unbound. See
605 * {@link ICallService#setIncomingCallId}.
Ben Gilad61925612014-03-11 19:06:36 -0700606 *
Sailesh Nepale59bb192014-04-01 18:33:59 -0700607 * @param call The call used for the incoming call.
Ben Gilad61925612014-03-11 19:06:36 -0700608 * @param extras The {@link CallService}-provided extras which need to be sent back.
609 * @param errorCallback The callback to invoke upon failure.
610 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700611 void setIncomingCallId(final Call call, final Bundle extras, final Runnable errorCallback) {
612 Log.d(this, "setIncomingCall(%s) via %s.", call, getComponentName());
Ben Gilad61925612014-03-11 19:06:36 -0700613 BindCallback callback = new BindCallback() {
Santos Cordon3d3b4052014-05-05 12:05:36 -0700614 @Override
615 public void onSuccess() {
Ben Gilad61925612014-03-11 19:06:36 -0700616 if (isServiceValid("setIncomingCallId")) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700617 mPendingIncomingCalls.add(call);
Ben Gilad61925612014-03-11 19:06:36 -0700618 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700619 logOutgoing("setIncomingCallId %s %s",
620 mCallIdMapper.getCallId(call), extras);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700621 mServiceInterface.setIncomingCallId(mCallIdMapper.getCallId(call),
622 extras);
Ben Gilad61925612014-03-11 19:06:36 -0700623 } catch (RemoteException e) {
Ben Gilad61925612014-03-11 19:06:36 -0700624 }
625 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800626 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700627
628 @Override
629 public void onFailure() {
Ben Gilad61925612014-03-11 19:06:36 -0700630 errorCallback.run();
631 }
632 };
633
634 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800635 }
636
Ihab Awad74549ec2014-03-10 15:33:25 -0700637 /** @see CallService#disconnect(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700638 void disconnect(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800639 if (isServiceValid("disconnect")) {
640 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700641 logOutgoing("disconnect %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700642 mServiceInterface.disconnect(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800643 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800644 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800645 }
646 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800647
Ihab Awad74549ec2014-03-10 15:33:25 -0700648 /** @see CallService#answer(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700649 void answer(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800650 if (isServiceValid("answer")) {
651 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700652 logOutgoing("answer %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700653 mServiceInterface.answer(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800654 } catch (RemoteException e) {
Santos Cordon7917d382014-02-14 02:31:18 -0800655 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800656 }
657 }
658
Ihab Awad74549ec2014-03-10 15:33:25 -0700659 /** @see CallService#reject(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700660 void reject(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800661 if (isServiceValid("reject")) {
662 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700663 logOutgoing("reject %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700664 mServiceInterface.reject(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800665 } catch (RemoteException e) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700666 }
667 }
668 }
669
670 /** @see CallService#playDtmfTone(String,char) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700671 void playDtmfTone(Call call, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700672 if (isServiceValid("playDtmfTone")) {
673 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700674 logOutgoing("playDtmfTone %s %c", mCallIdMapper.getCallId(call), digit);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700675 mServiceInterface.playDtmfTone(mCallIdMapper.getCallId(call), digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700676 } catch (RemoteException e) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700677 }
678 }
679 }
680
681 /** @see CallService#stopDtmfTone(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700682 void stopDtmfTone(Call call) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700683 if (isServiceValid("stopDtmfTone")) {
684 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700685 logOutgoing("stopDtmfTone %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700686 mServiceInterface.stopDtmfTone(mCallIdMapper.getCallId(call));
Ihab Awad74549ec2014-03-10 15:33:25 -0700687 } catch (RemoteException e) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800688 }
Santos Cordon7917d382014-02-14 02:31:18 -0800689 }
690 }
691
Sailesh Nepale59bb192014-04-01 18:33:59 -0700692 void addCall(Call call) {
Santos Cordona1610702014-06-04 20:22:56 -0700693 if (mCallIdMapper.getCallId(call) == null) {
694 mCallIdMapper.addCall(call);
695 }
Santos Cordon7917d382014-02-14 02:31:18 -0800696 }
697
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700698 /**
699 * Associates newCall with this call service by replacing callToReplace.
700 */
701 void replaceCall(Call newCall, Call callToReplace) {
702 Preconditions.checkState(callToReplace.getCallService() == this);
703 mCallIdMapper.replaceCall(newCall, callToReplace);
704 }
705
Sailesh Nepale59bb192014-04-01 18:33:59 -0700706 void removeCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700707 mPendingIncomingCalls.remove(call);
708
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700709 OutgoingCallResponse outgoingResultCallback =
710 mPendingOutgoingCalls.remove(mCallIdMapper.getCallId(call));
Santos Cordon682fe6b2014-05-20 08:56:39 -0700711 if (outgoingResultCallback != null) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700712 outgoingResultCallback.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700713 }
714
Sailesh Nepale59bb192014-04-01 18:33:59 -0700715 mCallIdMapper.removeCall(call);
Yorke Leeadee12d2014-03-13 12:08:30 -0700716 }
717
Evan Charlton352105c2014-06-03 14:10:54 -0700718 void onPostDialContinue(Call call, boolean proceed) {
719 if (isServiceValid("onPostDialContinue")) {
720 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700721 logOutgoing("onPostDialContinue %s %b", mCallIdMapper.getCallId(call), proceed);
Evan Charlton352105c2014-06-03 14:10:54 -0700722 mServiceInterface.onPostDialContinue(mCallIdMapper.getCallId(call), proceed);
723 } catch (RemoteException ignored) {
724 }
725 }
726 }
727
Santos Cordona1610702014-06-04 20:22:56 -0700728 void conference(final Call conferenceCall, Call call) {
729 if (isServiceValid("conference")) {
730 try {
731 conferenceCall.setCallService(this);
732 mPendingConferenceCalls.add(conferenceCall);
733 mHandler.postDelayed(new Runnable() {
734 @Override public void run() {
735 if (mPendingConferenceCalls.remove(conferenceCall)) {
736 conferenceCall.expireConference();
737 Log.i(this, "Conference call expired: %s", conferenceCall);
738 }
739 }
740 }, Timeouts.getConferenceCallExpireMillis());
741
Ihab Awad55a34282014-06-18 10:31:09 -0700742 logOutgoing("conference %s %s",
743 mCallIdMapper.getCallId(conferenceCall),
744 mCallIdMapper.getCallId(call));
Santos Cordona1610702014-06-04 20:22:56 -0700745 mServiceInterface.conference(
746 mCallIdMapper.getCallId(conferenceCall),
747 mCallIdMapper.getCallId(call));
748 } catch (RemoteException ignored) {
749 }
750 }
751 }
752
753 void splitFromConference(Call call) {
754 if (isServiceValid("splitFromConference")) {
755 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700756 logOutgoing("splitFromConference %s", mCallIdMapper.getCallId(call));
Santos Cordona1610702014-06-04 20:22:56 -0700757 mServiceInterface.splitFromConference(mCallIdMapper.getCallId(call));
758 } catch (RemoteException ignored) {
759 }
760 }
761 }
762
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800763 /** {@inheritDoc} */
Santos Cordon3d3b4052014-05-05 12:05:36 -0700764 @Override
765 protected void setServiceInterface(IBinder binder) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700766 if (binder == null) {
767 // We have lost our service connection. Notify the world that this call service is done.
768 // We must notify the adapter before CallsManager. The adapter will force any pending
769 // outgoing calls to try the next call service. This needs to happen before CallsManager
770 // tries to clean up any calls still associated with this call service.
Santos Cordon3d3b4052014-05-05 12:05:36 -0700771 handleCallServiceDeath();
Santos Cordon4b2c1192014-03-19 18:15:38 -0700772 CallsManager.getInstance().handleCallServiceDeath(this);
773 mServiceInterface = null;
774 } else {
775 mServiceInterface = ICallService.Stub.asInterface(binder);
776 setCallServiceAdapter(mAdapter);
777 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800778 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700779
780 /**
781 * Called when the associated call service dies.
782 */
783 private void handleCallServiceDeath() {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700784 if (!mPendingOutgoingCalls.isEmpty()) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700785 for (OutgoingCallResponse callback : mPendingOutgoingCalls.values()) {
786 callback.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700787 }
788 mPendingOutgoingCalls.clear();
789 }
790
791 if (!mPendingIncomingCalls.isEmpty()) {
Santos Cordon3d3b4052014-05-05 12:05:36 -0700792 // Iterate through a copy because the code inside the loop will modify the original
793 // list.
Santos Cordon682fe6b2014-05-20 08:56:39 -0700794 for (Call call : ImmutableList.copyOf(mPendingIncomingCalls)) {
795 Preconditions.checkState(call.isIncoming());
796 mIncomingCallsManager.handleFailedIncomingCall(call);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700797 }
798
Santos Cordona1610702014-06-04 20:22:56 -0700799 if (!mPendingIncomingCalls.isEmpty()) {
800 Log.wtf(this, "Pending calls did not get cleared.");
801 mPendingIncomingCalls.clear();
802 }
803 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700804
Santos Cordona1610702014-06-04 20:22:56 -0700805 mCallIdMapper.clear();
806 }
Ihab Awad55a34282014-06-18 10:31:09 -0700807
808 private void logIncoming(String msg, Object... params) {
809 Log.d(this, "CallService -> Telecomm: " + msg, params);
810 }
811
812 private void logOutgoing(String msg, Object... params) {
813 Log.d(this, "Telecomm -> CallService: " + msg, params);
814 }
Santos Cordon5924bea2014-06-18 06:39:51 -0700815
816 private void queryRemoteConnectionServices(final RemoteServiceCallback callback) {
817 final List<IBinder> callServices = new ArrayList<>();
818 final List<ComponentName> components = new ArrayList<>();
819
820 mCallServiceRepository.lookupServices(new LookupCallback<CallServiceWrapper>() {
821 private int mRemainingResponses;
822
823 /** ${inheritDoc} */
824 @Override
825 public void onComplete(Collection<CallServiceWrapper> services) {
826 mRemainingResponses = services.size() - 1;
827 for (CallServiceWrapper cs : services) {
828 if (cs != CallServiceWrapper.this) {
829 final CallServiceWrapper currentCallService = cs;
830 cs.mBinder.bind(new BindCallback() {
831 @Override
832 public void onSuccess() {
833 Log.d(this, "Adding ***** %s", currentCallService.getDescriptor());
834 callServices.add(currentCallService.mServiceInterface.asBinder());
835 components.add(currentCallService.getComponentName());
836 maybeComplete();
837 }
838
839 @Override
840 public void onFailure() {
841 // add null so that we always add up to totalExpected even if
842 // some of the call services fail to bind.
843 maybeComplete();
844 }
845
846 private void maybeComplete() {
847 if (--mRemainingResponses == 0) {
848 try {
849 callback.onResult(components, callServices);
850 } catch (RemoteException ignored) {
851 }
852 }
853 }
854 });
855 }
856 }
857 }
858 });
859 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800860}