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