blob: 5970846d186c36010c56be06053af9b71f3ea05e [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);
Nancy Chena65d41f2014-06-24 12:06:03 -0700290 ICallVideoProvider callVideoProvider = (ICallVideoProvider) args.arg2;
Andrew Leee9a77652014-06-26 13:07:57 -0700291 if (call != null) {
Nancy Chena65d41f2014-06-24 12:06:03 -0700292 call.setCallVideoProvider(callVideoProvider);
Andrew Leee9a77652014-06-26 13:07:57 -0700293 }
294 } finally {
295 args.recycle();
296 }
297 break;
Santos Cordon5924bea2014-06-18 06:39:51 -0700298 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700299 }
300 }
301 };
302
303 /** {@inheritDoc} */
304 @Override
Santos Cordon3d3b4052014-05-05 12:05:36 -0700305 public void notifyIncomingCall(CallInfo callInfo) {
Ihab Awad55a34282014-06-18 10:31:09 -0700306 logIncoming("notifyIncomingCall %s", callInfo);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700307 mCallIdMapper.checkValidCallId(callInfo.getId());
308 mHandler.obtainMessage(MSG_NOTIFY_INCOMING_CALL, callInfo).sendToTarget();
309 }
310
311 /** {@inheritDoc} */
312 @Override
313 public void handleSuccessfulOutgoingCall(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700314 logIncoming("handleSuccessfulOutgoingCall %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700315 mCallIdMapper.checkValidCallId(callId);
316 mHandler.obtainMessage(MSG_HANDLE_SUCCESSFUL_OUTGOING_CALL, callId).sendToTarget();
317 }
318
319 /** {@inheritDoc} */
320 @Override
Ihab Awada3cb9e32014-06-03 18:45:05 -0700321 public void handleFailedOutgoingCall(
322 ConnectionRequest request,
323 int errorCode,
324 String errorMsg) {
Ihab Awad55a34282014-06-18 10:31:09 -0700325 logIncoming("handleFailedOutgoingCall %s %d %s", request, errorCode, errorMsg);
Ihab Awada3cb9e32014-06-03 18:45:05 -0700326 mCallIdMapper.checkValidCallId(request.getCallId());
Santos Cordon3d3b4052014-05-05 12:05:36 -0700327 SomeArgs args = SomeArgs.obtain();
Ihab Awada3cb9e32014-06-03 18:45:05 -0700328 args.arg1 = request.getCallId();
329 args.argi1 = errorCode;
330 args.arg2 = errorMsg;
Santos Cordon3d3b4052014-05-05 12:05:36 -0700331 mHandler.obtainMessage(MSG_HANDLE_FAILED_OUTGOING_CALL, args).sendToTarget();
332 }
333
334 /** {@inheritDoc} */
335 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700336 public void cancelOutgoingCall(String callId) {
337 logIncoming("cancelOutgoingCall %s", callId);
338 mCallIdMapper.checkValidCallId(callId);
339 mHandler.obtainMessage(MSG_CANCEL_OUTGOING_CALL, callId).sendToTarget();
340 }
341
342 /** {@inheritDoc} */
343 @Override
Santos Cordon3d3b4052014-05-05 12:05:36 -0700344 public void setActive(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700345 logIncoming("setActive %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700346 mCallIdMapper.checkValidCallId(callId);
347 mHandler.obtainMessage(MSG_SET_ACTIVE, callId).sendToTarget();
348 }
349
350 /** {@inheritDoc} */
351 @Override
352 public void setRinging(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700353 logIncoming("setRinging %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700354 mCallIdMapper.checkValidCallId(callId);
355 mHandler.obtainMessage(MSG_SET_RINGING, callId).sendToTarget();
356 }
357
358 /** {@inheritDoc} */
359 @Override
Andrew Leee9a77652014-06-26 13:07:57 -0700360 public void setCallVideoProvider(String callId, ICallVideoProvider callVideoProvider) {
361 logIncoming("setCallVideoProvider %s", callId);
362 mCallIdMapper.checkValidCallId(callId);
363 SomeArgs args = SomeArgs.obtain();
364 args.arg1 = callId;
365 args.arg2 = callVideoProvider;
366 mHandler.obtainMessage(MSG_SET_CALL_VIDEO_PROVIDER, args).sendToTarget();
367 }
368
369 /** {@inheritDoc} */
370 @Override
Santos Cordon3d3b4052014-05-05 12:05:36 -0700371 public void setDialing(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700372 logIncoming("setDialing %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700373 mCallIdMapper.checkValidCallId(callId);
374 mHandler.obtainMessage(MSG_SET_DIALING, callId).sendToTarget();
375 }
376
377 /** {@inheritDoc} */
378 @Override
379 public void setDisconnected(
380 String callId, int disconnectCause, String disconnectMessage) {
Ihab Awad55a34282014-06-18 10:31:09 -0700381 logIncoming("setDisconnected %s %d %s", callId, disconnectCause, disconnectMessage);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700382 mCallIdMapper.checkValidCallId(callId);
383 SomeArgs args = SomeArgs.obtain();
384 args.arg1 = callId;
385 args.arg2 = disconnectMessage;
386 args.argi1 = disconnectCause;
387 mHandler.obtainMessage(MSG_SET_DISCONNECTED, args).sendToTarget();
388 }
389
390 /** {@inheritDoc} */
391 @Override
392 public void setOnHold(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700393 logIncoming("setOnHold %s", callId);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700394 mCallIdMapper.checkValidCallId(callId);
395 mHandler.obtainMessage(MSG_SET_ON_HOLD, callId).sendToTarget();
396 }
Ihab Awad50a57132014-05-28 16:49:38 -0700397
398 /** {@inheritDoc} */
399 @Override
400 public void setRequestingRingback(String callId, boolean ringback) {
Ihab Awad55a34282014-06-18 10:31:09 -0700401 logIncoming("setRequestingRingback %s %b", callId, ringback);
Ihab Awad50a57132014-05-28 16:49:38 -0700402 mCallIdMapper.checkValidCallId(callId);
403 SomeArgs args = SomeArgs.obtain();
404 args.arg1 = callId;
405 args.arg2 = ringback;
406 mHandler.obtainMessage(MSG_SET_REQUESTING_RINGBACK, args).sendToTarget();
407 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700408
409 /** ${inheritDoc} */
410 @Override
411 public void removeCall(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700412 logIncoming("removeCall %s", callId);
Santos Cordon8f3282c2014-06-01 13:56:02 -0700413 }
414
415 /** ${inheritDoc} */
416 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700417 public void setCanConference(String callId, boolean canConference) {
Ihab Awad55a34282014-06-18 10:31:09 -0700418 logIncoming("setCanConference %s %b", callId, canConference);
Santos Cordona1610702014-06-04 20:22:56 -0700419 mHandler.obtainMessage(MSG_CAN_CONFERENCE, canConference ? 1 : 0, 0, callId)
420 .sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700421 }
422
423 /** ${inheritDoc} */
424 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700425 public void setIsConferenced(String callId, String conferenceCallId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700426 logIncoming("setIsConferenced %s %s", callId, conferenceCallId);
Santos Cordona1610702014-06-04 20:22:56 -0700427 SomeArgs args = SomeArgs.obtain();
428 args.arg1 = callId;
429 args.arg2 = conferenceCallId;
430 mHandler.obtainMessage(MSG_SET_IS_CONFERENCED, args).sendToTarget();
431 }
432
433 /** ${InheritDoc} */
434 @Override
435 public void addConferenceCall(String callId, CallInfo callInfo) {
Ihab Awad55a34282014-06-18 10:31:09 -0700436 logIncoming("addConferenceCall %s %s", callId, callInfo);
Santos Cordona1610702014-06-04 20:22:56 -0700437 mCallIdMapper.checkValidCallId(callId);
438 SomeArgs args = SomeArgs.obtain();
439 args.arg1 = callId;
440 args.arg2 = callInfo;
441 mHandler.obtainMessage(MSG_ADD_CONFERENCE_CALL, args).sendToTarget();
Santos Cordon8f3282c2014-06-01 13:56:02 -0700442 }
Evan Charlton352105c2014-06-03 14:10:54 -0700443
444 @Override
445 public void onPostDialWait(String callId, String remaining) throws RemoteException {
Ihab Awad55a34282014-06-18 10:31:09 -0700446 logIncoming("onPostDialWait %s %s", callId, remaining);
Evan Charlton352105c2014-06-03 14:10:54 -0700447 mCallIdMapper.checkValidCallId(callId);
448 SomeArgs args = SomeArgs.obtain();
449 args.arg1 = callId;
450 args.arg2 = remaining;
451 mHandler.obtainMessage(MSG_ON_POST_DIAL_WAIT, args).sendToTarget();
452 }
Sailesh Nepal6098d2c2014-06-06 10:56:53 -0700453
454 /** {@inheritDoc} */
455 @Override
456 public void handoffCall(String callId) {
Ihab Awad55a34282014-06-18 10:31:09 -0700457 logIncoming("handoffCall %s", callId);
Sailesh Nepal6098d2c2014-06-06 10:56:53 -0700458 mCallIdMapper.checkValidCallId(callId);
459 mHandler.obtainMessage(MSG_HANDOFF_CALL, callId).sendToTarget();
460 }
Santos Cordon5924bea2014-06-18 06:39:51 -0700461
462 /** ${inheritDoc} */
463 @Override
464 public void queryRemoteConnectionServices(RemoteServiceCallback callback) {
465 logIncoming("queryRemoteCSs");
466 mHandler.obtainMessage(MSG_QUERY_REMOTE_CALL_SERVICES, callback).sendToTarget();
467 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700468 }
469
470 private final Adapter mAdapter = new Adapter();
471 private final CallsManager mCallsManager = CallsManager.getInstance();
Santos Cordona1610702014-06-04 20:22:56 -0700472 private final Set<Call> mPendingIncomingCalls = new HashSet<>();
473 private final Set<Call> mPendingConferenceCalls = new HashSet<>();
Ben Giladc5b22692014-02-18 20:03:22 -0800474 private final CallServiceDescriptor mDescriptor;
Santos Cordon3d3b4052014-05-05 12:05:36 -0700475 private final CallIdMapper mCallIdMapper = new CallIdMapper("CallService");
Santos Cordon3d3b4052014-05-05 12:05:36 -0700476 private final IncomingCallsManager mIncomingCallsManager;
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700477 private final Map<String, OutgoingCallResponse> mPendingOutgoingCalls = new HashMap<>();
Santos Cordona1610702014-06-04 20:22:56 -0700478 private final Handler mHandler = new Handler();
Santos Cordonc195e362014-02-11 17:05:31 -0800479
Ben Gilad61925612014-03-11 19:06:36 -0700480 private Binder mBinder = new Binder();
Santos Cordon3d3b4052014-05-05 12:05:36 -0700481 private ICallService mServiceInterface;
Santos Cordon5924bea2014-06-18 06:39:51 -0700482 private final CallServiceRepository mCallServiceRepository;
Ben Gilad61925612014-03-11 19:06:36 -0700483
Santos Cordon63aeb162014-02-10 09:20:40 -0800484 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -0700485 * Creates a call-service for the specified descriptor.
Santos Cordonc195e362014-02-11 17:05:31 -0800486 *
Santos Cordon61d0f702014-02-19 02:52:23 -0800487 * @param descriptor The call-service descriptor from
Santos Cordon3d3b4052014-05-05 12:05:36 -0700488 * {@link ICallServiceProvider#lookupCallServices}.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700489 * @param incomingCallsManager Manages the incoming call initialization flow.
Santos Cordon5924bea2014-06-18 06:39:51 -0700490 * @param callServiceRepository Call service repository.
Santos Cordon63aeb162014-02-10 09:20:40 -0800491 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700492 CallServiceWrapper(
493 CallServiceDescriptor descriptor,
Santos Cordon5924bea2014-06-18 06:39:51 -0700494 IncomingCallsManager incomingCallsManager,
495 CallServiceRepository callServiceRepository) {
Sailesh Nepala439e1b2014-03-11 18:19:58 -0700496 super(TelecommConstants.ACTION_CALL_SERVICE, descriptor.getServiceComponent());
Ben Giladc5b22692014-02-18 20:03:22 -0800497 mDescriptor = descriptor;
Santos Cordon3d3b4052014-05-05 12:05:36 -0700498 mIncomingCallsManager = incomingCallsManager;
Santos Cordon5924bea2014-06-18 06:39:51 -0700499 mCallServiceRepository = callServiceRepository;
Santos Cordon63aeb162014-02-10 09:20:40 -0800500 }
501
Ben Gilad61925612014-03-11 19:06:36 -0700502 CallServiceDescriptor getDescriptor() {
Ben Giladc5b22692014-02-18 20:03:22 -0800503 return mDescriptor;
Santos Cordonc195e362014-02-11 17:05:31 -0800504 }
505
Santos Cordon63aeb162014-02-10 09:20:40 -0800506 /** See {@link ICallService#setCallServiceAdapter}. */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700507 private void setCallServiceAdapter(ICallServiceAdapter callServiceAdapter) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800508 if (isServiceValid("setCallServiceAdapter")) {
509 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700510 logOutgoing("setCallServiceAdapter %s", callServiceAdapter);
Santos Cordon63aeb162014-02-10 09:20:40 -0800511 mServiceInterface.setCallServiceAdapter(callServiceAdapter);
Santos Cordon61d0f702014-02-19 02:52:23 -0800512 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800513 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800514 }
515 }
516
Ben Gilad61925612014-03-11 19:06:36 -0700517 /**
Santos Cordon682fe6b2014-05-20 08:56:39 -0700518 * Attempts to place the specified call, see {@link ICallService#call}. Returns the result
519 * asynchronously through the specified callback.
Ben Gilad61925612014-03-11 19:06:36 -0700520 */
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700521 void call(final Call call, final OutgoingCallResponse callResponse) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700522 Log.d(this, "call(%s) via %s.", call, getComponentName());
Ben Gilad61925612014-03-11 19:06:36 -0700523 BindCallback callback = new BindCallback() {
Santos Cordon3d3b4052014-05-05 12:05:36 -0700524 @Override
525 public void onSuccess() {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700526 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700527 mPendingOutgoingCalls.put(callId, callResponse);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700528
529 try {
530 CallInfo callInfo = call.toCallInfo(callId);
Ihab Awad55a34282014-06-18 10:31:09 -0700531 logOutgoing("call %s", callInfo);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700532 mServiceInterface.call(callInfo);
533 } catch (RemoteException e) {
Santos Cordon5924bea2014-06-18 06:39:51 -0700534 Log.e(this, e, "Failure to call -- %s", getDescriptor());
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700535 mPendingOutgoingCalls.remove(callId).onOutgoingCallFailure(
536 DisconnectCause.ERROR_UNSPECIFIED, e.toString());
Ben Gilad61925612014-03-11 19:06:36 -0700537 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800538 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700539
540 @Override
541 public void onFailure() {
Santos Cordon5924bea2014-06-18 06:39:51 -0700542 Log.e(this, new Exception(), "Failure to call %s", getDescriptor());
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700543 callResponse.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
Ben Gilad61925612014-03-11 19:06:36 -0700544 }
545 };
546
547 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800548 }
549
Ihab Awad74549ec2014-03-10 15:33:25 -0700550 /** @see CallService#abort(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700551 void abort(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700552 // Clear out any pending outgoing call data
553 String callId = mCallIdMapper.getCallId(call);
554
555 // If still bound, tell the call service to abort.
Ben Gilad28e8ad62014-03-06 17:01:54 -0800556 if (isServiceValid("abort")) {
557 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700558 logOutgoing("abort %s", callId);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700559 mServiceInterface.abort(callId);
Ben Gilad28e8ad62014-03-06 17:01:54 -0800560 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800561 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800562 }
Santos Cordon682fe6b2014-05-20 08:56:39 -0700563
564 removeCall(call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800565 }
566
Ihab Awad74549ec2014-03-10 15:33:25 -0700567 /** @see CallService#hold(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700568 void hold(Call call) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700569 if (isServiceValid("hold")) {
570 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700571 logOutgoing("hold %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700572 mServiceInterface.hold(mCallIdMapper.getCallId(call));
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700573 } catch (RemoteException e) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700574 }
575 }
576 }
577
Ihab Awad74549ec2014-03-10 15:33:25 -0700578 /** @see CallService#unhold(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700579 void unhold(Call call) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700580 if (isServiceValid("unhold")) {
581 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700582 logOutgoing("unhold %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700583 mServiceInterface.unhold(mCallIdMapper.getCallId(call));
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700584 } catch (RemoteException e) {
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700585 }
586 }
587 }
588
Ihab Awad74549ec2014-03-10 15:33:25 -0700589 /** @see CallService#onAudioStateChanged(String,CallAudioState) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700590 void onAudioStateChanged(Call activeCall, CallAudioState audioState) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700591 if (isServiceValid("onAudioStateChanged")) {
592 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700593 logOutgoing("onAudioStateChanged %s %s",
594 mCallIdMapper.getCallId(activeCall), audioState);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700595 mServiceInterface.onAudioStateChanged(mCallIdMapper.getCallId(activeCall),
596 audioState);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700597 } catch (RemoteException e) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700598 }
599 }
600 }
601
Ben Gilad61925612014-03-11 19:06:36 -0700602 /**
603 * Starts retrieval of details for an incoming call. Details are returned through the
604 * call-service adapter using the specified call ID. Upon failure, the specified error callback
Santos Cordon3d3b4052014-05-05 12:05:36 -0700605 * is invoked. Can be invoked even when the call service is unbound. See
606 * {@link ICallService#setIncomingCallId}.
Ben Gilad61925612014-03-11 19:06:36 -0700607 *
Sailesh Nepale59bb192014-04-01 18:33:59 -0700608 * @param call The call used for the incoming call.
Ben Gilad61925612014-03-11 19:06:36 -0700609 * @param extras The {@link CallService}-provided extras which need to be sent back.
610 * @param errorCallback The callback to invoke upon failure.
611 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700612 void setIncomingCallId(final Call call, final Bundle extras, final Runnable errorCallback) {
613 Log.d(this, "setIncomingCall(%s) via %s.", call, getComponentName());
Ben Gilad61925612014-03-11 19:06:36 -0700614 BindCallback callback = new BindCallback() {
Santos Cordon3d3b4052014-05-05 12:05:36 -0700615 @Override
616 public void onSuccess() {
Ben Gilad61925612014-03-11 19:06:36 -0700617 if (isServiceValid("setIncomingCallId")) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700618 mPendingIncomingCalls.add(call);
Ben Gilad61925612014-03-11 19:06:36 -0700619 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700620 logOutgoing("setIncomingCallId %s %s",
621 mCallIdMapper.getCallId(call), extras);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700622 mServiceInterface.setIncomingCallId(mCallIdMapper.getCallId(call),
623 extras);
Ben Gilad61925612014-03-11 19:06:36 -0700624 } catch (RemoteException e) {
Ben Gilad61925612014-03-11 19:06:36 -0700625 }
626 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800627 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700628
629 @Override
630 public void onFailure() {
Ben Gilad61925612014-03-11 19:06:36 -0700631 errorCallback.run();
632 }
633 };
634
635 mBinder.bind(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800636 }
637
Ihab Awad74549ec2014-03-10 15:33:25 -0700638 /** @see CallService#disconnect(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700639 void disconnect(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800640 if (isServiceValid("disconnect")) {
641 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700642 logOutgoing("disconnect %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700643 mServiceInterface.disconnect(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800644 } catch (RemoteException e) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800645 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800646 }
647 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800648
Ihab Awad74549ec2014-03-10 15:33:25 -0700649 /** @see CallService#answer(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700650 void answer(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800651 if (isServiceValid("answer")) {
652 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700653 logOutgoing("answer %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700654 mServiceInterface.answer(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800655 } catch (RemoteException e) {
Santos Cordon7917d382014-02-14 02:31:18 -0800656 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800657 }
658 }
659
Ihab Awad74549ec2014-03-10 15:33:25 -0700660 /** @see CallService#reject(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700661 void reject(Call call) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800662 if (isServiceValid("reject")) {
663 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700664 logOutgoing("reject %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700665 mServiceInterface.reject(mCallIdMapper.getCallId(call));
Santos Cordon61d0f702014-02-19 02:52:23 -0800666 } catch (RemoteException e) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700667 }
668 }
669 }
670
671 /** @see CallService#playDtmfTone(String,char) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700672 void playDtmfTone(Call call, char digit) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700673 if (isServiceValid("playDtmfTone")) {
674 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700675 logOutgoing("playDtmfTone %s %c", mCallIdMapper.getCallId(call), digit);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700676 mServiceInterface.playDtmfTone(mCallIdMapper.getCallId(call), digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700677 } catch (RemoteException e) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700678 }
679 }
680 }
681
682 /** @see CallService#stopDtmfTone(String) */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700683 void stopDtmfTone(Call call) {
Ihab Awad74549ec2014-03-10 15:33:25 -0700684 if (isServiceValid("stopDtmfTone")) {
685 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700686 logOutgoing("stopDtmfTone %s", mCallIdMapper.getCallId(call));
Sailesh Nepale59bb192014-04-01 18:33:59 -0700687 mServiceInterface.stopDtmfTone(mCallIdMapper.getCallId(call));
Ihab Awad74549ec2014-03-10 15:33:25 -0700688 } catch (RemoteException e) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800689 }
Santos Cordon7917d382014-02-14 02:31:18 -0800690 }
691 }
692
Sailesh Nepale59bb192014-04-01 18:33:59 -0700693 void addCall(Call call) {
Santos Cordona1610702014-06-04 20:22:56 -0700694 if (mCallIdMapper.getCallId(call) == null) {
695 mCallIdMapper.addCall(call);
696 }
Santos Cordon7917d382014-02-14 02:31:18 -0800697 }
698
Sailesh Nepal0e5410a2014-04-04 01:20:58 -0700699 /**
700 * Associates newCall with this call service by replacing callToReplace.
701 */
702 void replaceCall(Call newCall, Call callToReplace) {
703 Preconditions.checkState(callToReplace.getCallService() == this);
704 mCallIdMapper.replaceCall(newCall, callToReplace);
705 }
706
Sailesh Nepale59bb192014-04-01 18:33:59 -0700707 void removeCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700708 mPendingIncomingCalls.remove(call);
709
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700710 OutgoingCallResponse outgoingResultCallback =
711 mPendingOutgoingCalls.remove(mCallIdMapper.getCallId(call));
Santos Cordon682fe6b2014-05-20 08:56:39 -0700712 if (outgoingResultCallback != null) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700713 outgoingResultCallback.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700714 }
715
Sailesh Nepale59bb192014-04-01 18:33:59 -0700716 mCallIdMapper.removeCall(call);
Yorke Leeadee12d2014-03-13 12:08:30 -0700717 }
718
Evan Charlton352105c2014-06-03 14:10:54 -0700719 void onPostDialContinue(Call call, boolean proceed) {
720 if (isServiceValid("onPostDialContinue")) {
721 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700722 logOutgoing("onPostDialContinue %s %b", mCallIdMapper.getCallId(call), proceed);
Evan Charlton352105c2014-06-03 14:10:54 -0700723 mServiceInterface.onPostDialContinue(mCallIdMapper.getCallId(call), proceed);
724 } catch (RemoteException ignored) {
725 }
726 }
727 }
728
Santos Cordona1610702014-06-04 20:22:56 -0700729 void conference(final Call conferenceCall, Call call) {
730 if (isServiceValid("conference")) {
731 try {
732 conferenceCall.setCallService(this);
733 mPendingConferenceCalls.add(conferenceCall);
734 mHandler.postDelayed(new Runnable() {
735 @Override public void run() {
736 if (mPendingConferenceCalls.remove(conferenceCall)) {
737 conferenceCall.expireConference();
738 Log.i(this, "Conference call expired: %s", conferenceCall);
739 }
740 }
741 }, Timeouts.getConferenceCallExpireMillis());
742
Ihab Awad55a34282014-06-18 10:31:09 -0700743 logOutgoing("conference %s %s",
744 mCallIdMapper.getCallId(conferenceCall),
745 mCallIdMapper.getCallId(call));
Santos Cordona1610702014-06-04 20:22:56 -0700746 mServiceInterface.conference(
747 mCallIdMapper.getCallId(conferenceCall),
748 mCallIdMapper.getCallId(call));
749 } catch (RemoteException ignored) {
750 }
751 }
752 }
753
754 void splitFromConference(Call call) {
755 if (isServiceValid("splitFromConference")) {
756 try {
Ihab Awad55a34282014-06-18 10:31:09 -0700757 logOutgoing("splitFromConference %s", mCallIdMapper.getCallId(call));
Santos Cordona1610702014-06-04 20:22:56 -0700758 mServiceInterface.splitFromConference(mCallIdMapper.getCallId(call));
759 } catch (RemoteException ignored) {
760 }
761 }
762 }
763
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800764 /** {@inheritDoc} */
Santos Cordon3d3b4052014-05-05 12:05:36 -0700765 @Override
766 protected void setServiceInterface(IBinder binder) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700767 if (binder == null) {
768 // We have lost our service connection. Notify the world that this call service is done.
769 // We must notify the adapter before CallsManager. The adapter will force any pending
770 // outgoing calls to try the next call service. This needs to happen before CallsManager
771 // tries to clean up any calls still associated with this call service.
Santos Cordon3d3b4052014-05-05 12:05:36 -0700772 handleCallServiceDeath();
Santos Cordon4b2c1192014-03-19 18:15:38 -0700773 CallsManager.getInstance().handleCallServiceDeath(this);
774 mServiceInterface = null;
775 } else {
776 mServiceInterface = ICallService.Stub.asInterface(binder);
777 setCallServiceAdapter(mAdapter);
778 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800779 }
Santos Cordon3d3b4052014-05-05 12:05:36 -0700780
781 /**
782 * Called when the associated call service dies.
783 */
784 private void handleCallServiceDeath() {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700785 if (!mPendingOutgoingCalls.isEmpty()) {
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700786 for (OutgoingCallResponse callback : mPendingOutgoingCalls.values()) {
787 callback.onOutgoingCallFailure(DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon682fe6b2014-05-20 08:56:39 -0700788 }
789 mPendingOutgoingCalls.clear();
790 }
791
792 if (!mPendingIncomingCalls.isEmpty()) {
Santos Cordon3d3b4052014-05-05 12:05:36 -0700793 // Iterate through a copy because the code inside the loop will modify the original
794 // list.
Santos Cordon682fe6b2014-05-20 08:56:39 -0700795 for (Call call : ImmutableList.copyOf(mPendingIncomingCalls)) {
796 Preconditions.checkState(call.isIncoming());
797 mIncomingCallsManager.handleFailedIncomingCall(call);
Santos Cordon3d3b4052014-05-05 12:05:36 -0700798 }
799
Santos Cordona1610702014-06-04 20:22:56 -0700800 if (!mPendingIncomingCalls.isEmpty()) {
801 Log.wtf(this, "Pending calls did not get cleared.");
802 mPendingIncomingCalls.clear();
803 }
804 }
Santos Cordon8f3282c2014-06-01 13:56:02 -0700805
Santos Cordona1610702014-06-04 20:22:56 -0700806 mCallIdMapper.clear();
807 }
Ihab Awad55a34282014-06-18 10:31:09 -0700808
809 private void logIncoming(String msg, Object... params) {
810 Log.d(this, "CallService -> Telecomm: " + msg, params);
811 }
812
813 private void logOutgoing(String msg, Object... params) {
814 Log.d(this, "Telecomm -> CallService: " + msg, params);
815 }
Santos Cordon5924bea2014-06-18 06:39:51 -0700816
817 private void queryRemoteConnectionServices(final RemoteServiceCallback callback) {
818 final List<IBinder> callServices = new ArrayList<>();
819 final List<ComponentName> components = new ArrayList<>();
820
821 mCallServiceRepository.lookupServices(new LookupCallback<CallServiceWrapper>() {
822 private int mRemainingResponses;
823
824 /** ${inheritDoc} */
825 @Override
826 public void onComplete(Collection<CallServiceWrapper> services) {
827 mRemainingResponses = services.size() - 1;
828 for (CallServiceWrapper cs : services) {
829 if (cs != CallServiceWrapper.this) {
830 final CallServiceWrapper currentCallService = cs;
831 cs.mBinder.bind(new BindCallback() {
832 @Override
833 public void onSuccess() {
834 Log.d(this, "Adding ***** %s", currentCallService.getDescriptor());
835 callServices.add(currentCallService.mServiceInterface.asBinder());
836 components.add(currentCallService.getComponentName());
837 maybeComplete();
838 }
839
840 @Override
841 public void onFailure() {
842 // add null so that we always add up to totalExpected even if
843 // some of the call services fail to bind.
844 maybeComplete();
845 }
846
847 private void maybeComplete() {
848 if (--mRemainingResponses == 0) {
849 try {
850 callback.onResult(components, callServices);
851 } catch (RemoteException ignored) {
852 }
853 }
854 }
855 });
856 }
857 }
858 }
859 });
860 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800861}