ConnectionService.java revision 07366813cdf3768dcd69a1f744023747564d654a
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.telecom; 18 19import android.annotation.SdkConstant; 20import android.annotation.SystemApi; 21import android.app.Service; 22import android.content.ComponentName; 23import android.content.Intent; 24import android.net.Uri; 25import android.os.Handler; 26import android.os.IBinder; 27import android.os.Looper; 28import android.os.Message; 29 30import com.android.internal.os.SomeArgs; 31import com.android.internal.telecom.IConnectionService; 32import com.android.internal.telecom.IConnectionServiceAdapter; 33import com.android.internal.telecom.RemoteServiceCallback; 34 35import java.util.ArrayList; 36import java.util.Collection; 37import java.util.Collections; 38import java.util.List; 39import java.util.Map; 40import java.util.UUID; 41import java.util.concurrent.ConcurrentHashMap; 42 43/** 44 * {@code ConnectionService} is an abstract service that should be implemented by any app which can 45 * make phone calls and want those calls to be integrated into the built-in phone app. 46 * Once implemented, the {@code ConnectionService} needs two additional steps before it will be 47 * integrated into the phone app: 48 * <p> 49 * 1. <i>Registration in AndroidManifest.xml</i> 50 * <br/> 51 * <pre> 52 * <service android:name="com.example.package.MyConnectionService" 53 * android:label="@string/some_label_for_my_connection_service" 54 * android:permission="android.permission.BIND_CONNECTION_SERVICE"> 55 * <intent-filter> 56 * <action android:name="android.telecom.ConnectionService" /> 57 * </intent-filter> 58 * </service> 59 * </pre> 60 * <p> 61 * 2. <i> Registration of {@link PhoneAccount} with {@link TelecomManager}.</i> 62 * <br/> 63 * See {@link PhoneAccount} and {@link TelecomManager#registerPhoneAccount} for more information. 64 * <p> 65 * Once registered and enabled by the user in the dialer settings, telecom will bind to a 66 * {@code ConnectionService} implementation when it wants that {@code ConnectionService} to place 67 * a call or the service has indicated that is has an incoming call through 68 * {@link TelecomManager#addNewIncomingCall}. The {@code ConnectionService} can then expect a call 69 * to {@link #onCreateIncomingConnection} or {@link #onCreateOutgoingConnection} wherein it 70 * should provide a new instance of a {@link Connection} object. It is through this 71 * {@link Connection} object that telecom receives state updates and the {@code ConnectionService} 72 * receives call-commands such as answer, reject, hold and disconnect. 73 * <p> 74 * When there are no more live calls, telecom will unbind from the {@code ConnectionService}. 75 * @hide 76 */ 77@SystemApi 78public abstract class ConnectionService extends Service { 79 /** 80 * The {@link Intent} that must be declared as handled by the service. 81 */ 82 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 83 public static final String SERVICE_INTERFACE = "android.telecom.ConnectionService"; 84 85 // Flag controlling whether PII is emitted into the logs 86 private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG); 87 88 private static final int MSG_ADD_CONNECTION_SERVICE_ADAPTER = 1; 89 private static final int MSG_CREATE_CONNECTION = 2; 90 private static final int MSG_ABORT = 3; 91 private static final int MSG_ANSWER = 4; 92 private static final int MSG_REJECT = 5; 93 private static final int MSG_DISCONNECT = 6; 94 private static final int MSG_HOLD = 7; 95 private static final int MSG_UNHOLD = 8; 96 private static final int MSG_ON_AUDIO_STATE_CHANGED = 9; 97 private static final int MSG_PLAY_DTMF_TONE = 10; 98 private static final int MSG_STOP_DTMF_TONE = 11; 99 private static final int MSG_CONFERENCE = 12; 100 private static final int MSG_SPLIT_FROM_CONFERENCE = 13; 101 private static final int MSG_ON_POST_DIAL_CONTINUE = 14; 102 private static final int MSG_REMOVE_CONNECTION_SERVICE_ADAPTER = 16; 103 private static final int MSG_ANSWER_VIDEO = 17; 104 private static final int MSG_MERGE_CONFERENCE = 18; 105 private static final int MSG_SWAP_CONFERENCE = 19; 106 107 private static Connection sNullConnection; 108 109 private final Map<String, Connection> mConnectionById = new ConcurrentHashMap<>(); 110 private final Map<Connection, String> mIdByConnection = new ConcurrentHashMap<>(); 111 private final Map<String, Conference> mConferenceById = new ConcurrentHashMap<>(); 112 private final Map<Conference, String> mIdByConference = new ConcurrentHashMap<>(); 113 private final RemoteConnectionManager mRemoteConnectionManager = 114 new RemoteConnectionManager(this); 115 private final List<Runnable> mPreInitializationConnectionRequests = new ArrayList<>(); 116 private final ConnectionServiceAdapter mAdapter = new ConnectionServiceAdapter(); 117 118 private boolean mAreAccountsInitialized = false; 119 private Conference sNullConference; 120 121 private final IBinder mBinder = new IConnectionService.Stub() { 122 @Override 123 public void addConnectionServiceAdapter(IConnectionServiceAdapter adapter) { 124 mHandler.obtainMessage(MSG_ADD_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget(); 125 } 126 127 public void removeConnectionServiceAdapter(IConnectionServiceAdapter adapter) { 128 mHandler.obtainMessage(MSG_REMOVE_CONNECTION_SERVICE_ADAPTER, adapter).sendToTarget(); 129 } 130 131 @Override 132 public void createConnection( 133 PhoneAccountHandle connectionManagerPhoneAccount, 134 String id, 135 ConnectionRequest request, 136 boolean isIncoming, 137 boolean isUnknown) { 138 SomeArgs args = SomeArgs.obtain(); 139 args.arg1 = connectionManagerPhoneAccount; 140 args.arg2 = id; 141 args.arg3 = request; 142 args.argi1 = isIncoming ? 1 : 0; 143 args.argi2 = isUnknown ? 1 : 0; 144 mHandler.obtainMessage(MSG_CREATE_CONNECTION, args).sendToTarget(); 145 } 146 147 @Override 148 public void abort(String callId) { 149 mHandler.obtainMessage(MSG_ABORT, callId).sendToTarget(); 150 } 151 152 @Override 153 /** @hide */ 154 public void answerVideo(String callId, int videoState) { 155 SomeArgs args = SomeArgs.obtain(); 156 args.arg1 = callId; 157 args.argi1 = videoState; 158 mHandler.obtainMessage(MSG_ANSWER_VIDEO, args).sendToTarget(); 159 } 160 161 @Override 162 public void answer(String callId) { 163 mHandler.obtainMessage(MSG_ANSWER, callId).sendToTarget(); 164 } 165 166 @Override 167 public void reject(String callId) { 168 mHandler.obtainMessage(MSG_REJECT, callId).sendToTarget(); 169 } 170 171 @Override 172 public void disconnect(String callId) { 173 mHandler.obtainMessage(MSG_DISCONNECT, callId).sendToTarget(); 174 } 175 176 @Override 177 public void hold(String callId) { 178 mHandler.obtainMessage(MSG_HOLD, callId).sendToTarget(); 179 } 180 181 @Override 182 public void unhold(String callId) { 183 mHandler.obtainMessage(MSG_UNHOLD, callId).sendToTarget(); 184 } 185 186 @Override 187 public void onAudioStateChanged(String callId, AudioState audioState) { 188 SomeArgs args = SomeArgs.obtain(); 189 args.arg1 = callId; 190 args.arg2 = audioState; 191 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, args).sendToTarget(); 192 } 193 194 @Override 195 public void playDtmfTone(String callId, char digit) { 196 mHandler.obtainMessage(MSG_PLAY_DTMF_TONE, digit, 0, callId).sendToTarget(); 197 } 198 199 @Override 200 public void stopDtmfTone(String callId) { 201 mHandler.obtainMessage(MSG_STOP_DTMF_TONE, callId).sendToTarget(); 202 } 203 204 @Override 205 public void conference(String callId1, String callId2) { 206 SomeArgs args = SomeArgs.obtain(); 207 args.arg1 = callId1; 208 args.arg2 = callId2; 209 mHandler.obtainMessage(MSG_CONFERENCE, args).sendToTarget(); 210 } 211 212 @Override 213 public void splitFromConference(String callId) { 214 mHandler.obtainMessage(MSG_SPLIT_FROM_CONFERENCE, callId).sendToTarget(); 215 } 216 217 @Override 218 public void mergeConference(String callId) { 219 mHandler.obtainMessage(MSG_MERGE_CONFERENCE, callId).sendToTarget(); 220 } 221 222 @Override 223 public void swapConference(String callId) { 224 mHandler.obtainMessage(MSG_SWAP_CONFERENCE, callId).sendToTarget(); 225 } 226 227 @Override 228 public void onPostDialContinue(String callId, boolean proceed) { 229 SomeArgs args = SomeArgs.obtain(); 230 args.arg1 = callId; 231 args.argi1 = proceed ? 1 : 0; 232 mHandler.obtainMessage(MSG_ON_POST_DIAL_CONTINUE, args).sendToTarget(); 233 } 234 }; 235 236 private final Handler mHandler = new Handler(Looper.getMainLooper()) { 237 @Override 238 public void handleMessage(Message msg) { 239 switch (msg.what) { 240 case MSG_ADD_CONNECTION_SERVICE_ADAPTER: 241 mAdapter.addAdapter((IConnectionServiceAdapter) msg.obj); 242 onAdapterAttached(); 243 break; 244 case MSG_REMOVE_CONNECTION_SERVICE_ADAPTER: 245 mAdapter.removeAdapter((IConnectionServiceAdapter) msg.obj); 246 break; 247 case MSG_CREATE_CONNECTION: { 248 SomeArgs args = (SomeArgs) msg.obj; 249 try { 250 final PhoneAccountHandle connectionManagerPhoneAccount = 251 (PhoneAccountHandle) args.arg1; 252 final String id = (String) args.arg2; 253 final ConnectionRequest request = (ConnectionRequest) args.arg3; 254 final boolean isIncoming = args.argi1 == 1; 255 final boolean isUnknown = args.argi2 == 1; 256 if (!mAreAccountsInitialized) { 257 Log.d(this, "Enqueueing pre-init request %s", id); 258 mPreInitializationConnectionRequests.add(new Runnable() { 259 @Override 260 public void run() { 261 createConnection( 262 connectionManagerPhoneAccount, 263 id, 264 request, 265 isIncoming, 266 isUnknown); 267 } 268 }); 269 } else { 270 createConnection( 271 connectionManagerPhoneAccount, 272 id, 273 request, 274 isIncoming, 275 isUnknown); 276 } 277 } finally { 278 args.recycle(); 279 } 280 break; 281 } 282 case MSG_ABORT: 283 abort((String) msg.obj); 284 break; 285 case MSG_ANSWER: 286 answer((String) msg.obj); 287 break; 288 case MSG_ANSWER_VIDEO: { 289 SomeArgs args = (SomeArgs) msg.obj; 290 try { 291 String callId = (String) args.arg1; 292 int videoState = args.argi1; 293 answerVideo(callId, videoState); 294 } finally { 295 args.recycle(); 296 } 297 break; 298 } 299 case MSG_REJECT: 300 reject((String) msg.obj); 301 break; 302 case MSG_DISCONNECT: 303 disconnect((String) msg.obj); 304 break; 305 case MSG_HOLD: 306 hold((String) msg.obj); 307 break; 308 case MSG_UNHOLD: 309 unhold((String) msg.obj); 310 break; 311 case MSG_ON_AUDIO_STATE_CHANGED: { 312 SomeArgs args = (SomeArgs) msg.obj; 313 try { 314 String callId = (String) args.arg1; 315 AudioState audioState = (AudioState) args.arg2; 316 onAudioStateChanged(callId, audioState); 317 } finally { 318 args.recycle(); 319 } 320 break; 321 } 322 case MSG_PLAY_DTMF_TONE: 323 playDtmfTone((String) msg.obj, (char) msg.arg1); 324 break; 325 case MSG_STOP_DTMF_TONE: 326 stopDtmfTone((String) msg.obj); 327 break; 328 case MSG_CONFERENCE: { 329 SomeArgs args = (SomeArgs) msg.obj; 330 try { 331 String callId1 = (String) args.arg1; 332 String callId2 = (String) args.arg2; 333 conference(callId1, callId2); 334 } finally { 335 args.recycle(); 336 } 337 break; 338 } 339 case MSG_SPLIT_FROM_CONFERENCE: 340 splitFromConference((String) msg.obj); 341 break; 342 case MSG_MERGE_CONFERENCE: 343 mergeConference((String) msg.obj); 344 break; 345 case MSG_SWAP_CONFERENCE: 346 swapConference((String) msg.obj); 347 break; 348 case MSG_ON_POST_DIAL_CONTINUE: { 349 SomeArgs args = (SomeArgs) msg.obj; 350 try { 351 String callId = (String) args.arg1; 352 boolean proceed = (args.argi1 == 1); 353 onPostDialContinue(callId, proceed); 354 } finally { 355 args.recycle(); 356 } 357 break; 358 } 359 default: 360 break; 361 } 362 } 363 }; 364 365 private final Conference.Listener mConferenceListener = new Conference.Listener() { 366 @Override 367 public void onStateChanged(Conference conference, int oldState, int newState) { 368 String id = mIdByConference.get(conference); 369 switch (newState) { 370 case Connection.STATE_ACTIVE: 371 mAdapter.setActive(id); 372 break; 373 case Connection.STATE_HOLDING: 374 mAdapter.setOnHold(id); 375 break; 376 case Connection.STATE_DISCONNECTED: 377 // handled by onDisconnected 378 break; 379 } 380 } 381 382 @Override 383 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) { 384 String id = mIdByConference.get(conference); 385 mAdapter.setDisconnected(id, disconnectCause); 386 } 387 388 @Override 389 public void onConnectionAdded(Conference conference, Connection connection) { 390 } 391 392 @Override 393 public void onConnectionRemoved(Conference conference, Connection connection) { 394 } 395 396 @Override 397 public void onConferenceableConnectionsChanged( 398 Conference conference, List<Connection> conferenceableConnections) { 399 mAdapter.setConferenceableConnections( 400 mIdByConference.get(conference), 401 createConnectionIdList(conferenceableConnections)); 402 } 403 404 @Override 405 public void onDestroyed(Conference conference) { 406 removeConference(conference); 407 } 408 409 @Override 410 public void onConnectionCapabilitiesChanged( 411 Conference conference, 412 int connectionCapabilities) { 413 String id = mIdByConference.get(conference); 414 Log.d(this, "call capabilities: conference: %s", 415 Connection.capabilitiesToString(connectionCapabilities)); 416 mAdapter.setConnectionCapabilities(id, connectionCapabilities); 417 } 418 419 @Override 420 public void onVideoStateChanged(Conference c, int videoState) { 421 String id = mIdByConference.get(c); 422 Log.d(this, "onVideoStateChanged set video state %d", videoState); 423 mAdapter.setVideoState(id, videoState); 424 } 425 426 @Override 427 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) { 428 String id = mIdByConference.get(c); 429 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c, 430 videoProvider); 431 mAdapter.setVideoProvider(id, videoProvider); 432 } 433 }; 434 435 private final Connection.Listener mConnectionListener = new Connection.Listener() { 436 @Override 437 public void onStateChanged(Connection c, int state) { 438 String id = mIdByConnection.get(c); 439 Log.d(this, "Adapter set state %s %s", id, Connection.stateToString(state)); 440 switch (state) { 441 case Connection.STATE_ACTIVE: 442 mAdapter.setActive(id); 443 break; 444 case Connection.STATE_DIALING: 445 mAdapter.setDialing(id); 446 break; 447 case Connection.STATE_DISCONNECTED: 448 // Handled in onDisconnected() 449 break; 450 case Connection.STATE_HOLDING: 451 mAdapter.setOnHold(id); 452 break; 453 case Connection.STATE_NEW: 454 // Nothing to tell Telecom 455 break; 456 case Connection.STATE_RINGING: 457 mAdapter.setRinging(id); 458 break; 459 } 460 } 461 462 @Override 463 public void onDisconnected(Connection c, DisconnectCause disconnectCause) { 464 String id = mIdByConnection.get(c); 465 Log.d(this, "Adapter set disconnected %s", disconnectCause); 466 mAdapter.setDisconnected(id, disconnectCause); 467 } 468 469 @Override 470 public void onVideoStateChanged(Connection c, int videoState) { 471 String id = mIdByConnection.get(c); 472 Log.d(this, "Adapter set video state %d", videoState); 473 mAdapter.setVideoState(id, videoState); 474 } 475 476 @Override 477 public void onAddressChanged(Connection c, Uri address, int presentation) { 478 String id = mIdByConnection.get(c); 479 mAdapter.setAddress(id, address, presentation); 480 } 481 482 @Override 483 public void onCallerDisplayNameChanged( 484 Connection c, String callerDisplayName, int presentation) { 485 String id = mIdByConnection.get(c); 486 mAdapter.setCallerDisplayName(id, callerDisplayName, presentation); 487 } 488 489 @Override 490 public void onDestroyed(Connection c) { 491 removeConnection(c); 492 } 493 494 @Override 495 public void onPostDialWait(Connection c, String remaining) { 496 String id = mIdByConnection.get(c); 497 Log.d(this, "Adapter onPostDialWait %s, %s", c, remaining); 498 mAdapter.onPostDialWait(id, remaining); 499 } 500 501 @Override 502 public void onPostDialChar(Connection c, char nextChar) { 503 String id = mIdByConnection.get(c); 504 Log.d(this, "Adapter onPostDialChar %s, %s", c, nextChar); 505 mAdapter.onPostDialChar(id, nextChar); 506 } 507 508 @Override 509 public void onRingbackRequested(Connection c, boolean ringback) { 510 String id = mIdByConnection.get(c); 511 Log.d(this, "Adapter onRingback %b", ringback); 512 mAdapter.setRingbackRequested(id, ringback); 513 } 514 515 @Override 516 public void onConnectionCapabilitiesChanged(Connection c, int capabilities) { 517 String id = mIdByConnection.get(c); 518 Log.d(this, "capabilities: parcelableconnection: %s", 519 Connection.capabilitiesToString(capabilities)); 520 mAdapter.setConnectionCapabilities(id, capabilities); 521 } 522 523 @Override 524 public void onVideoProviderChanged(Connection c, Connection.VideoProvider videoProvider) { 525 String id = mIdByConnection.get(c); 526 Log.d(this, "onVideoProviderChanged: Connection: %s, VideoProvider: %s", c, 527 videoProvider); 528 mAdapter.setVideoProvider(id, videoProvider); 529 } 530 531 @Override 532 public void onAudioModeIsVoipChanged(Connection c, boolean isVoip) { 533 String id = mIdByConnection.get(c); 534 mAdapter.setIsVoipAudioMode(id, isVoip); 535 } 536 537 @Override 538 public void onStatusHintsChanged(Connection c, StatusHints statusHints) { 539 String id = mIdByConnection.get(c); 540 mAdapter.setStatusHints(id, statusHints); 541 } 542 543 @Override 544 public void onConferenceablesChanged( 545 Connection connection, List<IConferenceable> conferenceables) { 546 mAdapter.setConferenceableConnections( 547 mIdByConnection.get(connection), 548 createIdList(conferenceables)); 549 } 550 551 @Override 552 public void onConferenceChanged(Connection connection, Conference conference) { 553 String id = mIdByConnection.get(connection); 554 if (id != null) { 555 String conferenceId = null; 556 if (conference != null) { 557 conferenceId = mIdByConference.get(conference); 558 } 559 mAdapter.setIsConferenced(id, conferenceId); 560 } 561 } 562 563 @Override 564 public void onCallSubstateChanged(Connection c, int callSubstate) { 565 String id = mIdByConnection.get(c); 566 Log.d(this, "Adapter set call substate %d", callSubstate); 567 mAdapter.setCallSubstate(id, callSubstate); 568 } 569 }; 570 571 /** {@inheritDoc} */ 572 @Override 573 public final IBinder onBind(Intent intent) { 574 return mBinder; 575 } 576 577 /** {@inheritDoc} */ 578 @Override 579 public boolean onUnbind(Intent intent) { 580 endAllConnections(); 581 return super.onUnbind(intent); 582 } 583 584 /** 585 * This can be used by telecom to either create a new outgoing call or attach to an existing 586 * incoming call. In either case, telecom will cycle through a set of services and call 587 * createConnection util a connection service cancels the process or completes it successfully. 588 */ 589 private void createConnection( 590 final PhoneAccountHandle callManagerAccount, 591 final String callId, 592 final ConnectionRequest request, 593 boolean isIncoming, 594 boolean isUnknown) { 595 Log.d(this, "createConnection, callManagerAccount: %s, callId: %s, request: %s, " + 596 "isIncoming: %b, isUnknown: %b", callManagerAccount, callId, request, isIncoming, 597 isUnknown); 598 599 Connection connection = isUnknown ? onCreateUnknownConnection(callManagerAccount, request) 600 : isIncoming ? onCreateIncomingConnection(callManagerAccount, request) 601 : onCreateOutgoingConnection(callManagerAccount, request); 602 Log.d(this, "createConnection, connection: %s", connection); 603 if (connection == null) { 604 connection = Connection.createFailedConnection( 605 new DisconnectCause(DisconnectCause.ERROR)); 606 } 607 608 if (connection.getState() != Connection.STATE_DISCONNECTED) { 609 addConnection(callId, connection); 610 } 611 612 Uri address = connection.getAddress(); 613 String number = address == null ? "null" : address.getSchemeSpecificPart(); 614 Log.v(this, "createConnection, number: %s, state: %s, capabilities: %s", 615 Connection.toLogSafePhoneNumber(number), 616 Connection.stateToString(connection.getState()), 617 Connection.capabilitiesToString(connection.getConnectionCapabilities())); 618 619 Log.d(this, "createConnection, calling handleCreateConnectionSuccessful %s", callId); 620 mAdapter.handleCreateConnectionComplete( 621 callId, 622 request, 623 new ParcelableConnection( 624 request.getAccountHandle(), 625 connection.getState(), 626 connection.getConnectionCapabilities(), 627 connection.getAddress(), 628 connection.getAddressPresentation(), 629 connection.getCallerDisplayName(), 630 connection.getCallerDisplayNamePresentation(), 631 connection.getVideoProvider() == null ? 632 null : connection.getVideoProvider().getInterface(), 633 connection.getVideoState(), 634 connection.isRingbackRequested(), 635 connection.getAudioModeIsVoip(), 636 connection.getStatusHints(), 637 connection.getDisconnectCause(), 638 createIdList(connection.getConferenceables()), 639 connection.getCallSubstate())); 640 } 641 642 private void abort(String callId) { 643 Log.d(this, "abort %s", callId); 644 findConnectionForAction(callId, "abort").onAbort(); 645 } 646 647 private void answerVideo(String callId, int videoState) { 648 Log.d(this, "answerVideo %s", callId); 649 findConnectionForAction(callId, "answer").onAnswer(videoState); 650 } 651 652 private void answer(String callId) { 653 Log.d(this, "answer %s", callId); 654 findConnectionForAction(callId, "answer").onAnswer(); 655 } 656 657 private void reject(String callId) { 658 Log.d(this, "reject %s", callId); 659 findConnectionForAction(callId, "reject").onReject(); 660 } 661 662 private void disconnect(String callId) { 663 Log.d(this, "disconnect %s", callId); 664 if (mConnectionById.containsKey(callId)) { 665 findConnectionForAction(callId, "disconnect").onDisconnect(); 666 } else { 667 findConferenceForAction(callId, "disconnect").onDisconnect(); 668 } 669 } 670 671 private void hold(String callId) { 672 Log.d(this, "hold %s", callId); 673 if (mConnectionById.containsKey(callId)) { 674 findConnectionForAction(callId, "hold").onHold(); 675 } else { 676 findConferenceForAction(callId, "hold").onHold(); 677 } 678 } 679 680 private void unhold(String callId) { 681 Log.d(this, "unhold %s", callId); 682 if (mConnectionById.containsKey(callId)) { 683 findConnectionForAction(callId, "unhold").onUnhold(); 684 } else { 685 findConferenceForAction(callId, "unhold").onUnhold(); 686 } 687 } 688 689 private void onAudioStateChanged(String callId, AudioState audioState) { 690 Log.d(this, "onAudioStateChanged %s %s", callId, audioState); 691 if (mConnectionById.containsKey(callId)) { 692 findConnectionForAction(callId, "onAudioStateChanged").setAudioState(audioState); 693 } else { 694 findConferenceForAction(callId, "onAudioStateChanged").setAudioState(audioState); 695 } 696 } 697 698 private void playDtmfTone(String callId, char digit) { 699 Log.d(this, "playDtmfTone %s %c", callId, digit); 700 if (mConnectionById.containsKey(callId)) { 701 findConnectionForAction(callId, "playDtmfTone").onPlayDtmfTone(digit); 702 } else { 703 findConferenceForAction(callId, "playDtmfTone").onPlayDtmfTone(digit); 704 } 705 } 706 707 private void stopDtmfTone(String callId) { 708 Log.d(this, "stopDtmfTone %s", callId); 709 if (mConnectionById.containsKey(callId)) { 710 findConnectionForAction(callId, "stopDtmfTone").onStopDtmfTone(); 711 } else { 712 findConferenceForAction(callId, "stopDtmfTone").onStopDtmfTone(); 713 } 714 } 715 716 private void conference(String callId1, String callId2) { 717 Log.d(this, "conference %s, %s", callId1, callId2); 718 719 // Attempt to get second connection or conference. 720 Connection connection2 = findConnectionForAction(callId2, "conference"); 721 Conference conference2 = getNullConference(); 722 if (connection2 == getNullConnection()) { 723 conference2 = findConferenceForAction(callId2, "conference"); 724 if (conference2 == getNullConference()) { 725 Log.w(this, "Connection2 or Conference2 missing in conference request %s.", 726 callId2); 727 return; 728 } 729 } 730 731 // Attempt to get first connection or conference and perform merge. 732 Connection connection1 = findConnectionForAction(callId1, "conference"); 733 if (connection1 == getNullConnection()) { 734 Conference conference1 = findConferenceForAction(callId1, "addConnection"); 735 if (conference1 == getNullConference()) { 736 Log.w(this, 737 "Connection1 or Conference1 missing in conference request %s.", 738 callId1); 739 } else { 740 // Call 1 is a conference. 741 if (connection2 != getNullConnection()) { 742 // Call 2 is a connection so merge via call 1 (conference). 743 conference1.onMerge(connection2); 744 } else { 745 // Call 2 is ALSO a conference; this should never happen. 746 Log.wtf(this, "There can only be one conference and an attempt was made to " + 747 "merge two conferences."); 748 return; 749 } 750 } 751 } else { 752 // Call 1 is a connection. 753 if (conference2 != getNullConference()) { 754 // Call 2 is a conference, so merge via call 2. 755 conference2.onMerge(connection1); 756 } else { 757 // Call 2 is a connection, so merge together. 758 onConference(connection1, connection2); 759 } 760 } 761 } 762 763 private void splitFromConference(String callId) { 764 Log.d(this, "splitFromConference(%s)", callId); 765 766 Connection connection = findConnectionForAction(callId, "splitFromConference"); 767 if (connection == getNullConnection()) { 768 Log.w(this, "Connection missing in conference request %s.", callId); 769 return; 770 } 771 772 Conference conference = connection.getConference(); 773 if (conference != null) { 774 conference.onSeparate(connection); 775 } 776 } 777 778 private void mergeConference(String callId) { 779 Log.d(this, "mergeConference(%s)", callId); 780 Conference conference = findConferenceForAction(callId, "mergeConference"); 781 if (conference != null) { 782 conference.onMerge(); 783 } 784 } 785 786 private void swapConference(String callId) { 787 Log.d(this, "swapConference(%s)", callId); 788 Conference conference = findConferenceForAction(callId, "swapConference"); 789 if (conference != null) { 790 conference.onSwap(); 791 } 792 } 793 794 private void onPostDialContinue(String callId, boolean proceed) { 795 Log.d(this, "onPostDialContinue(%s)", callId); 796 findConnectionForAction(callId, "stopDtmfTone").onPostDialContinue(proceed); 797 } 798 799 private void onAdapterAttached() { 800 if (mAreAccountsInitialized) { 801 // No need to query again if we already did it. 802 return; 803 } 804 805 mAdapter.queryRemoteConnectionServices(new RemoteServiceCallback.Stub() { 806 @Override 807 public void onResult( 808 final List<ComponentName> componentNames, 809 final List<IBinder> services) { 810 mHandler.post(new Runnable() { 811 @Override 812 public void run() { 813 for (int i = 0; i < componentNames.size() && i < services.size(); i++) { 814 mRemoteConnectionManager.addConnectionService( 815 componentNames.get(i), 816 IConnectionService.Stub.asInterface(services.get(i))); 817 } 818 onAccountsInitialized(); 819 Log.d(this, "remote connection services found: " + services); 820 } 821 }); 822 } 823 824 @Override 825 public void onError() { 826 mHandler.post(new Runnable() { 827 @Override 828 public void run() { 829 mAreAccountsInitialized = true; 830 } 831 }); 832 } 833 }); 834 } 835 836 /** 837 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an 838 * incoming request. This is used by {@code ConnectionService}s that are registered with 839 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to manage 840 * SIM-based incoming calls. 841 * 842 * @param connectionManagerPhoneAccount See description at 843 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}. 844 * @param request Details about the incoming call. 845 * @return The {@code Connection} object to satisfy this call, or {@code null} to 846 * not handle the call. 847 */ 848 public final RemoteConnection createRemoteIncomingConnection( 849 PhoneAccountHandle connectionManagerPhoneAccount, 850 ConnectionRequest request) { 851 return mRemoteConnectionManager.createRemoteConnection( 852 connectionManagerPhoneAccount, request, true); 853 } 854 855 /** 856 * Ask some other {@code ConnectionService} to create a {@code RemoteConnection} given an 857 * outgoing request. This is used by {@code ConnectionService}s that are registered with 858 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} and want to be able to use the 859 * SIM-based {@code ConnectionService} to place its outgoing calls. 860 * 861 * @param connectionManagerPhoneAccount See description at 862 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}. 863 * @param request Details about the incoming call. 864 * @return The {@code Connection} object to satisfy this call, or {@code null} to 865 * not handle the call. 866 */ 867 public final RemoteConnection createRemoteOutgoingConnection( 868 PhoneAccountHandle connectionManagerPhoneAccount, 869 ConnectionRequest request) { 870 return mRemoteConnectionManager.createRemoteConnection( 871 connectionManagerPhoneAccount, request, false); 872 } 873 874 /** 875 * Indicates to the relevant {@code RemoteConnectionService} that the specified 876 * {@link RemoteConnection}s should be merged into a conference call. 877 * <p> 878 * If the conference request is successful, the method {@link #onRemoteConferenceAdded} will 879 * be invoked. 880 * 881 * @param remoteConnection1 The first of the remote connections to conference. 882 * @param remoteConnection2 The second of the remote connections to conference. 883 */ 884 public final void conferenceRemoteConnections( 885 RemoteConnection remoteConnection1, 886 RemoteConnection remoteConnection2) { 887 mRemoteConnectionManager.conferenceRemoteConnections(remoteConnection1, remoteConnection2); 888 } 889 890 /** 891 * Adds a new conference call. When a conference call is created either as a result of an 892 * explicit request via {@link #onConference} or otherwise, the connection service should supply 893 * an instance of {@link Conference} by invoking this method. A conference call provided by this 894 * method will persist until {@link Conference#destroy} is invoked on the conference instance. 895 * 896 * @param conference The new conference object. 897 */ 898 public final void addConference(Conference conference) { 899 Log.d(this, "addConference: conference=%s", conference); 900 901 String id = addConferenceInternal(conference); 902 if (id != null) { 903 List<String> connectionIds = new ArrayList<>(2); 904 for (Connection connection : conference.getConnections()) { 905 if (mIdByConnection.containsKey(connection)) { 906 connectionIds.add(mIdByConnection.get(connection)); 907 } 908 } 909 ParcelableConference parcelableConference = new ParcelableConference( 910 conference.getPhoneAccountHandle(), 911 conference.getState(), 912 conference.getConnectionCapabilities(), 913 connectionIds, 914 conference.getVideoProvider() == null ? 915 null : conference.getVideoProvider().getInterface(), 916 conference.getVideoState(), 917 conference.getConnectTimeMillis() 918 ); 919 mAdapter.addConferenceCall(id, parcelableConference); 920 mAdapter.setVideoProvider(id, conference.getVideoProvider()); 921 mAdapter.setVideoState(id, conference.getVideoState()); 922 923 // Go through any child calls and set the parent. 924 for (Connection connection : conference.getConnections()) { 925 String connectionId = mIdByConnection.get(connection); 926 if (connectionId != null) { 927 mAdapter.setIsConferenced(connectionId, id); 928 } 929 } 930 } 931 } 932 933 /** 934 * Adds a connection created by the {@link ConnectionService} and informs telecom of the new 935 * connection. 936 * 937 * @param phoneAccountHandle The phone account handle for the connection. 938 * @param connection The connection to add. 939 */ 940 public final void addExistingConnection(PhoneAccountHandle phoneAccountHandle, 941 Connection connection) { 942 943 String id = addExistingConnectionInternal(connection); 944 if (id != null) { 945 List<String> emptyList = new ArrayList<>(0); 946 947 ParcelableConnection parcelableConnection = new ParcelableConnection( 948 phoneAccountHandle, 949 connection.getState(), 950 connection.getConnectionCapabilities(), 951 connection.getAddress(), 952 connection.getAddressPresentation(), 953 connection.getCallerDisplayName(), 954 connection.getCallerDisplayNamePresentation(), 955 connection.getVideoProvider() == null ? 956 null : connection.getVideoProvider().getInterface(), 957 connection.getVideoState(), 958 connection.isRingbackRequested(), 959 connection.getAudioModeIsVoip(), 960 connection.getStatusHints(), 961 connection.getDisconnectCause(), 962 emptyList, connection.getCallSubstate()); 963 mAdapter.addExistingConnection(id, parcelableConnection); 964 } 965 } 966 967 /** 968 * Returns all the active {@code Connection}s for which this {@code ConnectionService} 969 * has taken responsibility. 970 * 971 * @return A collection of {@code Connection}s created by this {@code ConnectionService}. 972 */ 973 public final Collection<Connection> getAllConnections() { 974 return mConnectionById.values(); 975 } 976 977 /** 978 * Create a {@code Connection} given an incoming request. This is used to attach to existing 979 * incoming calls. 980 * 981 * @param connectionManagerPhoneAccount See description at 982 * {@link #onCreateOutgoingConnection(PhoneAccountHandle, ConnectionRequest)}. 983 * @param request Details about the incoming call. 984 * @return The {@code Connection} object to satisfy this call, or {@code null} to 985 * not handle the call. 986 */ 987 public Connection onCreateIncomingConnection( 988 PhoneAccountHandle connectionManagerPhoneAccount, 989 ConnectionRequest request) { 990 return null; 991 } 992 993 /** 994 * Create a {@code Connection} given an outgoing request. This is used to initiate new 995 * outgoing calls. 996 * 997 * @param connectionManagerPhoneAccount The connection manager account to use for managing 998 * this call. 999 * <p> 1000 * If this parameter is not {@code null}, it means that this {@code ConnectionService} 1001 * has registered one or more {@code PhoneAccount}s having 1002 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. This parameter will contain 1003 * one of these {@code PhoneAccount}s, while the {@code request} will contain another 1004 * (usually but not always distinct) {@code PhoneAccount} to be used for actually 1005 * making the connection. 1006 * <p> 1007 * If this parameter is {@code null}, it means that this {@code ConnectionService} is 1008 * being asked to make a direct connection. The 1009 * {@link ConnectionRequest#getAccountHandle()} of parameter {@code request} will be 1010 * a {@code PhoneAccount} registered by this {@code ConnectionService} to use for 1011 * making the connection. 1012 * @param request Details about the outgoing call. 1013 * @return The {@code Connection} object to satisfy this call, or the result of an invocation 1014 * of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call. 1015 */ 1016 public Connection onCreateOutgoingConnection( 1017 PhoneAccountHandle connectionManagerPhoneAccount, 1018 ConnectionRequest request) { 1019 return null; 1020 } 1021 1022 /** 1023 * Create a {@code Connection} for a new unknown call. An unknown call is a call originating 1024 * from the ConnectionService that was neither a user-initiated outgoing call, nor an incoming 1025 * call created using 1026 * {@code TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}. 1027 * 1028 * @param connectionManagerPhoneAccount 1029 * @param request 1030 * @return 1031 * 1032 * @hide 1033 */ 1034 public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerPhoneAccount, 1035 ConnectionRequest request) { 1036 return null; 1037 } 1038 1039 /** 1040 * Conference two specified connections. Invoked when the user has made a request to merge the 1041 * specified connections into a conference call. In response, the connection service should 1042 * create an instance of {@link Conference} and pass it into {@link #addConference}. 1043 * 1044 * @param connection1 A connection to merge into a conference call. 1045 * @param connection2 A connection to merge into a conference call. 1046 */ 1047 public void onConference(Connection connection1, Connection connection2) {} 1048 1049 /** 1050 * Indicates that a remote conference has been created for existing {@link RemoteConnection}s. 1051 * When this method is invoked, this {@link ConnectionService} should create its own 1052 * representation of the conference call and send it to telecom using {@link #addConference}. 1053 * <p> 1054 * This is only relevant to {@link ConnectionService}s which are registered with 1055 * {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER}. 1056 * 1057 * @param conference The remote conference call. 1058 */ 1059 public void onRemoteConferenceAdded(RemoteConference conference) {} 1060 1061 /** 1062 * Called when an existing connection is added remotely. 1063 * @param connection The existing connection which was added. 1064 */ 1065 public void onRemoteExistingConnectionAdded(RemoteConnection connection) {} 1066 1067 /** 1068 * @hide 1069 */ 1070 public boolean containsConference(Conference conference) { 1071 return mIdByConference.containsKey(conference); 1072 } 1073 1074 /** {@hide} */ 1075 void addRemoteConference(RemoteConference remoteConference) { 1076 onRemoteConferenceAdded(remoteConference); 1077 } 1078 1079 /** {@hide} */ 1080 void addRemoteExistingConnection(RemoteConnection remoteConnection) { 1081 onRemoteExistingConnectionAdded(remoteConnection); 1082 } 1083 1084 private void onAccountsInitialized() { 1085 mAreAccountsInitialized = true; 1086 for (Runnable r : mPreInitializationConnectionRequests) { 1087 r.run(); 1088 } 1089 mPreInitializationConnectionRequests.clear(); 1090 } 1091 1092 /** 1093 * Adds an existing connection to the list of connections, identified by a new UUID. 1094 * 1095 * @param connection The connection. 1096 * @return The UUID of the connection (e.g. the call-id). 1097 */ 1098 private String addExistingConnectionInternal(Connection connection) { 1099 String id = UUID.randomUUID().toString(); 1100 addConnection(id, connection); 1101 return id; 1102 } 1103 1104 private void addConnection(String callId, Connection connection) { 1105 mConnectionById.put(callId, connection); 1106 mIdByConnection.put(connection, callId); 1107 connection.addConnectionListener(mConnectionListener); 1108 connection.setConnectionService(this); 1109 } 1110 1111 /** {@hide} */ 1112 protected void removeConnection(Connection connection) { 1113 String id = mIdByConnection.get(connection); 1114 connection.unsetConnectionService(this); 1115 connection.removeConnectionListener(mConnectionListener); 1116 mConnectionById.remove(mIdByConnection.get(connection)); 1117 mIdByConnection.remove(connection); 1118 mAdapter.removeCall(id); 1119 } 1120 1121 private String addConferenceInternal(Conference conference) { 1122 if (mIdByConference.containsKey(conference)) { 1123 Log.w(this, "Re-adding an existing conference: %s.", conference); 1124 } else if (conference != null) { 1125 String id = UUID.randomUUID().toString(); 1126 mConferenceById.put(id, conference); 1127 mIdByConference.put(conference, id); 1128 conference.addListener(mConferenceListener); 1129 return id; 1130 } 1131 1132 return null; 1133 } 1134 1135 private void removeConference(Conference conference) { 1136 if (mIdByConference.containsKey(conference)) { 1137 conference.removeListener(mConferenceListener); 1138 1139 String id = mIdByConference.get(conference); 1140 mConferenceById.remove(id); 1141 mIdByConference.remove(conference); 1142 mAdapter.removeCall(id); 1143 } 1144 } 1145 1146 private Connection findConnectionForAction(String callId, String action) { 1147 if (mConnectionById.containsKey(callId)) { 1148 return mConnectionById.get(callId); 1149 } 1150 Log.w(this, "%s - Cannot find Connection %s", action, callId); 1151 return getNullConnection(); 1152 } 1153 1154 static synchronized Connection getNullConnection() { 1155 if (sNullConnection == null) { 1156 sNullConnection = new Connection() {}; 1157 } 1158 return sNullConnection; 1159 } 1160 1161 private Conference findConferenceForAction(String conferenceId, String action) { 1162 if (mConferenceById.containsKey(conferenceId)) { 1163 return mConferenceById.get(conferenceId); 1164 } 1165 Log.w(this, "%s - Cannot find conference %s", action, conferenceId); 1166 return getNullConference(); 1167 } 1168 1169 private List<String> createConnectionIdList(List<Connection> connections) { 1170 List<String> ids = new ArrayList<>(); 1171 for (Connection c : connections) { 1172 if (mIdByConnection.containsKey(c)) { 1173 ids.add(mIdByConnection.get(c)); 1174 } 1175 } 1176 Collections.sort(ids); 1177 return ids; 1178 } 1179 1180 /** 1181 * Builds a list of {@link Connection} and {@link Conference} IDs based on the list of 1182 * {@link IConferenceable}s passed in. 1183 * 1184 * @param conferenceables The {@link IConferenceable} connections and conferences. 1185 * @return List of string conference and call Ids. 1186 */ 1187 private List<String> createIdList(List<IConferenceable> conferenceables) { 1188 List<String> ids = new ArrayList<>(); 1189 for (IConferenceable c : conferenceables) { 1190 // Only allow Connection and Conference conferenceables. 1191 if (c instanceof Connection) { 1192 Connection connection = (Connection) c; 1193 if (mIdByConnection.containsKey(connection)) { 1194 ids.add(mIdByConnection.get(connection)); 1195 } 1196 } else if (c instanceof Conference) { 1197 Conference conference = (Conference) c; 1198 if (mIdByConference.containsKey(conference)) { 1199 ids.add(mIdByConference.get(conference)); 1200 } 1201 } 1202 } 1203 Collections.sort(ids); 1204 return ids; 1205 } 1206 1207 private Conference getNullConference() { 1208 if (sNullConference == null) { 1209 sNullConference = new Conference(null) {}; 1210 } 1211 return sNullConference; 1212 } 1213 1214 private void endAllConnections() { 1215 // Unbound from telecomm. We should end all connections and conferences. 1216 for (Connection connection : mIdByConnection.keySet()) { 1217 // only operate on top-level calls. Conference calls will be removed on their own. 1218 if (connection.getConference() == null) { 1219 connection.onDisconnect(); 1220 } 1221 } 1222 for (Conference conference : mIdByConference.keySet()) { 1223 conference.onDisconnect(); 1224 } 1225 } 1226} 1227