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