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 17 package android.telecom; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.annotation.TestApi; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.os.SystemClock; 26 import android.telecom.Connection.VideoProvider; 27 import android.telephony.ServiceState; 28 import android.telephony.TelephonyManager; 29 import android.util.ArraySet; 30 31 import java.util.ArrayList; 32 import java.util.Arrays; 33 import java.util.Collections; 34 import java.util.List; 35 import java.util.Locale; 36 import java.util.Set; 37 import java.util.concurrent.CopyOnWriteArrayList; 38 import java.util.concurrent.CopyOnWriteArraySet; 39 40 /** 41 * Represents a conference call which can contain any number of {@link Connection} objects. 42 */ 43 public abstract class Conference extends Conferenceable { 44 45 /** 46 * Used to indicate that the conference connection time is not specified. If not specified, 47 * Telecom will set the connect time. 48 */ 49 public static final long CONNECT_TIME_NOT_SPECIFIED = 0; 50 51 /** @hide */ 52 public abstract static class Listener { onStateChanged(Conference conference, int oldState, int newState)53 public void onStateChanged(Conference conference, int oldState, int newState) {} onDisconnected(Conference conference, DisconnectCause disconnectCause)54 public void onDisconnected(Conference conference, DisconnectCause disconnectCause) {} onConnectionAdded(Conference conference, Connection connection)55 public void onConnectionAdded(Conference conference, Connection connection) {} onConnectionRemoved(Conference conference, Connection connection)56 public void onConnectionRemoved(Conference conference, Connection connection) {} onConferenceableConnectionsChanged( Conference conference, List<Connection> conferenceableConnections)57 public void onConferenceableConnectionsChanged( 58 Conference conference, List<Connection> conferenceableConnections) {} onDestroyed(Conference conference)59 public void onDestroyed(Conference conference) {} onConnectionCapabilitiesChanged( Conference conference, int connectionCapabilities)60 public void onConnectionCapabilitiesChanged( 61 Conference conference, int connectionCapabilities) {} onConnectionPropertiesChanged( Conference conference, int connectionProperties)62 public void onConnectionPropertiesChanged( 63 Conference conference, int connectionProperties) {} onVideoStateChanged(Conference c, int videoState)64 public void onVideoStateChanged(Conference c, int videoState) { } onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider)65 public void onVideoProviderChanged(Conference c, Connection.VideoProvider videoProvider) {} onStatusHintsChanged(Conference conference, StatusHints statusHints)66 public void onStatusHintsChanged(Conference conference, StatusHints statusHints) {} onExtrasChanged(Conference c, Bundle extras)67 public void onExtrasChanged(Conference c, Bundle extras) {} onExtrasRemoved(Conference c, List<String> keys)68 public void onExtrasRemoved(Conference c, List<String> keys) {} onConferenceStateChanged(Conference c, boolean isConference)69 public void onConferenceStateChanged(Conference c, boolean isConference) {} onAddressChanged(Conference c, Uri newAddress, int presentation)70 public void onAddressChanged(Conference c, Uri newAddress, int presentation) {} onConnectionEvent(Conference c, String event, Bundle extras)71 public void onConnectionEvent(Conference c, String event, Bundle extras) {} onCallerDisplayNameChanged( Conference c, String callerDisplayName, int presentation)72 public void onCallerDisplayNameChanged( 73 Conference c, String callerDisplayName, int presentation) {} 74 } 75 76 private final Set<Listener> mListeners = new CopyOnWriteArraySet<>(); 77 private final List<Connection> mChildConnections = new CopyOnWriteArrayList<>(); 78 private final List<Connection> mUnmodifiableChildConnections = 79 Collections.unmodifiableList(mChildConnections); 80 private final List<Connection> mConferenceableConnections = new ArrayList<>(); 81 private final List<Connection> mUnmodifiableConferenceableConnections = 82 Collections.unmodifiableList(mConferenceableConnections); 83 84 private String mTelecomCallId; 85 private PhoneAccountHandle mPhoneAccount; 86 private CallAudioState mCallAudioState; 87 private int mState = Connection.STATE_NEW; 88 private DisconnectCause mDisconnectCause; 89 private int mConnectionCapabilities; 90 private int mConnectionProperties; 91 private String mDisconnectMessage; 92 private long mConnectTimeMillis = CONNECT_TIME_NOT_SPECIFIED; 93 private long mConnectionStartElapsedRealTime = CONNECT_TIME_NOT_SPECIFIED; 94 private StatusHints mStatusHints; 95 private Bundle mExtras; 96 private Set<String> mPreviousExtraKeys; 97 private final Object mExtrasLock = new Object(); 98 private Uri mAddress; 99 private int mAddressPresentation; 100 private String mCallerDisplayName; 101 private int mCallerDisplayNamePresentation; 102 103 private final Connection.Listener mConnectionDeathListener = new Connection.Listener() { 104 @Override 105 public void onDestroyed(Connection c) { 106 if (mConferenceableConnections.remove(c)) { 107 fireOnConferenceableConnectionsChanged(); 108 } 109 } 110 }; 111 112 /** 113 * Constructs a new Conference with a mandatory {@link PhoneAccountHandle} 114 * 115 * @param phoneAccount The {@code PhoneAccountHandle} associated with the conference. 116 */ Conference(PhoneAccountHandle phoneAccount)117 public Conference(PhoneAccountHandle phoneAccount) { 118 mPhoneAccount = phoneAccount; 119 } 120 121 /** 122 * Returns the telecom internal call ID associated with this conference. 123 * 124 * @return The telecom call ID. 125 * @hide 126 */ getTelecomCallId()127 public final String getTelecomCallId() { 128 return mTelecomCallId; 129 } 130 131 /** 132 * Sets the telecom internal call ID associated with this conference. 133 * 134 * @param telecomCallId The telecom call ID. 135 * @hide 136 */ setTelecomCallId(String telecomCallId)137 public final void setTelecomCallId(String telecomCallId) { 138 mTelecomCallId = telecomCallId; 139 } 140 141 /** 142 * Returns the {@link PhoneAccountHandle} the conference call is being placed through. 143 * 144 * @return A {@code PhoneAccountHandle} object representing the PhoneAccount of the conference. 145 */ getPhoneAccountHandle()146 public final PhoneAccountHandle getPhoneAccountHandle() { 147 return mPhoneAccount; 148 } 149 150 /** 151 * Returns the list of connections currently associated with the conference call. 152 * 153 * @return A list of {@code Connection} objects which represent the children of the conference. 154 */ getConnections()155 public final List<Connection> getConnections() { 156 return mUnmodifiableChildConnections; 157 } 158 159 /** 160 * Gets the state of the conference call. See {@link Connection} for valid values. 161 * 162 * @return A constant representing the state the conference call is currently in. 163 */ getState()164 public final int getState() { 165 return mState; 166 } 167 168 /** 169 * Returns the capabilities of the conference. See {@code CAPABILITY_*} constants in class 170 * {@link Connection} for valid values. 171 * 172 * @return A bitmask of the capabilities of the conference call. 173 */ getConnectionCapabilities()174 public final int getConnectionCapabilities() { 175 return mConnectionCapabilities; 176 } 177 178 /** 179 * Returns the properties of the conference. See {@code PROPERTY_*} constants in class 180 * {@link Connection} for valid values. 181 * 182 * @return A bitmask of the properties of the conference call. 183 */ getConnectionProperties()184 public final int getConnectionProperties() { 185 return mConnectionProperties; 186 } 187 188 /** 189 * Whether the given capabilities support the specified capability. 190 * 191 * @param capabilities A capability bit field. 192 * @param capability The capability to check capabilities for. 193 * @return Whether the specified capability is supported. 194 * @hide 195 */ can(int capabilities, int capability)196 public static boolean can(int capabilities, int capability) { 197 return (capabilities & capability) != 0; 198 } 199 200 /** 201 * Whether the capabilities of this {@code Connection} supports the specified capability. 202 * 203 * @param capability The capability to check capabilities for. 204 * @return Whether the specified capability is supported. 205 * @hide 206 */ can(int capability)207 public boolean can(int capability) { 208 return can(mConnectionCapabilities, capability); 209 } 210 211 /** 212 * Removes the specified capability from the set of capabilities of this {@code Conference}. 213 * 214 * @param capability The capability to remove from the set. 215 * @hide 216 */ removeCapability(int capability)217 public void removeCapability(int capability) { 218 int newCapabilities = mConnectionCapabilities; 219 newCapabilities &= ~capability; 220 221 setConnectionCapabilities(newCapabilities); 222 } 223 224 /** 225 * Adds the specified capability to the set of capabilities of this {@code Conference}. 226 * 227 * @param capability The capability to add to the set. 228 * @hide 229 */ addCapability(int capability)230 public void addCapability(int capability) { 231 int newCapabilities = mConnectionCapabilities; 232 newCapabilities |= capability; 233 234 setConnectionCapabilities(newCapabilities); 235 } 236 237 /** 238 * @return The audio state of the conference, describing how its audio is currently 239 * being routed by the system. This is {@code null} if this Conference 240 * does not directly know about its audio state. 241 * @deprecated Use {@link #getCallAudioState()} instead. 242 * @hide 243 */ 244 @Deprecated 245 @SystemApi getAudioState()246 public final AudioState getAudioState() { 247 return new AudioState(mCallAudioState); 248 } 249 250 /** 251 * @return The audio state of the conference, describing how its audio is currently 252 * being routed by the system. This is {@code null} if this Conference 253 * does not directly know about its audio state. 254 */ getCallAudioState()255 public final CallAudioState getCallAudioState() { 256 return mCallAudioState; 257 } 258 259 /** 260 * Returns VideoProvider of the primary call. This can be null. 261 */ getVideoProvider()262 public VideoProvider getVideoProvider() { 263 return null; 264 } 265 266 /** 267 * Returns video state of the primary call. 268 */ getVideoState()269 public int getVideoState() { 270 return VideoProfile.STATE_AUDIO_ONLY; 271 } 272 273 /** 274 * Notifies the {@link Conference} when the Conference and all it's {@link Connection}s should 275 * be disconnected. 276 */ onDisconnect()277 public void onDisconnect() {} 278 279 /** 280 * Notifies the {@link Conference} when the specified {@link Connection} should be separated 281 * from the conference call. 282 * 283 * @param connection The connection to separate. 284 */ onSeparate(Connection connection)285 public void onSeparate(Connection connection) {} 286 287 /** 288 * Notifies the {@link Conference} when the specified {@link Connection} should merged with the 289 * conference call. 290 * 291 * @param connection The {@code Connection} to merge. 292 */ onMerge(Connection connection)293 public void onMerge(Connection connection) {} 294 295 /** 296 * Notifies the {@link Conference} when it should be put on hold. 297 */ onHold()298 public void onHold() {} 299 300 /** 301 * Notifies the {@link Conference} when it should be moved from a held to active state. 302 */ onUnhold()303 public void onUnhold() {} 304 305 /** 306 * Notifies the {@link Conference} when the child calls should be merged. Only invoked if the 307 * conference contains the capability {@link Connection#CAPABILITY_MERGE_CONFERENCE}. 308 */ onMerge()309 public void onMerge() {} 310 311 /** 312 * Notifies the {@link Conference} when the child calls should be swapped. Only invoked if the 313 * conference contains the capability {@link Connection#CAPABILITY_SWAP_CONFERENCE}. 314 */ onSwap()315 public void onSwap() {} 316 317 /** 318 * Notifies the {@link Conference} of a request to play a DTMF tone. 319 * 320 * @param c A DTMF character. 321 */ onPlayDtmfTone(char c)322 public void onPlayDtmfTone(char c) {} 323 324 /** 325 * Notifies the {@link Conference} of a request to stop any currently playing DTMF tones. 326 */ onStopDtmfTone()327 public void onStopDtmfTone() {} 328 329 /** 330 * Notifies the {@link Conference} that the {@link #getAudioState()} property has a new value. 331 * 332 * @param state The new call audio state. 333 * @deprecated Use {@link #onCallAudioStateChanged(CallAudioState)} instead. 334 * @hide 335 */ 336 @SystemApi 337 @Deprecated onAudioStateChanged(AudioState state)338 public void onAudioStateChanged(AudioState state) {} 339 340 /** 341 * Notifies the {@link Conference} that the {@link #getCallAudioState()} property has a new 342 * value. 343 * 344 * @param state The new call audio state. 345 */ onCallAudioStateChanged(CallAudioState state)346 public void onCallAudioStateChanged(CallAudioState state) {} 347 348 /** 349 * Notifies the {@link Conference} that a {@link Connection} has been added to it. 350 * 351 * @param connection The newly added connection. 352 */ onConnectionAdded(Connection connection)353 public void onConnectionAdded(Connection connection) {} 354 355 /** 356 * Sets state to be on hold. 357 */ setOnHold()358 public final void setOnHold() { 359 setState(Connection.STATE_HOLDING); 360 } 361 362 /** 363 * Sets state to be dialing. 364 */ setDialing()365 public final void setDialing() { 366 setState(Connection.STATE_DIALING); 367 } 368 369 /** 370 * Sets state to be active. 371 */ setActive()372 public final void setActive() { 373 setState(Connection.STATE_ACTIVE); 374 } 375 376 /** 377 * Sets state to disconnected. 378 * 379 * @param disconnectCause The reason for the disconnection, as described by 380 * {@link android.telecom.DisconnectCause}. 381 */ setDisconnected(DisconnectCause disconnectCause)382 public final void setDisconnected(DisconnectCause disconnectCause) { 383 mDisconnectCause = disconnectCause;; 384 setState(Connection.STATE_DISCONNECTED); 385 for (Listener l : mListeners) { 386 l.onDisconnected(this, mDisconnectCause); 387 } 388 } 389 390 /** 391 * @return The {@link DisconnectCause} for this connection. 392 */ getDisconnectCause()393 public final DisconnectCause getDisconnectCause() { 394 return mDisconnectCause; 395 } 396 397 /** 398 * Sets the capabilities of a conference. See {@code CAPABILITY_*} constants of class 399 * {@link Connection} for valid values. 400 * 401 * @param connectionCapabilities A bitmask of the {@code Capabilities} of the conference call. 402 */ setConnectionCapabilities(int connectionCapabilities)403 public final void setConnectionCapabilities(int connectionCapabilities) { 404 if (connectionCapabilities != mConnectionCapabilities) { 405 mConnectionCapabilities = connectionCapabilities; 406 407 for (Listener l : mListeners) { 408 l.onConnectionCapabilitiesChanged(this, mConnectionCapabilities); 409 } 410 } 411 } 412 413 /** 414 * Sets the properties of a conference. See {@code PROPERTY_*} constants of class 415 * {@link Connection} for valid values. 416 * 417 * @param connectionProperties A bitmask of the {@code Properties} of the conference call. 418 */ setConnectionProperties(int connectionProperties)419 public final void setConnectionProperties(int connectionProperties) { 420 if (connectionProperties != mConnectionProperties) { 421 mConnectionProperties = connectionProperties; 422 423 for (Listener l : mListeners) { 424 l.onConnectionPropertiesChanged(this, mConnectionProperties); 425 } 426 } 427 } 428 429 /** 430 * Adds the specified connection as a child of this conference. 431 * 432 * @param connection The connection to add. 433 * @return True if the connection was successfully added. 434 */ addConnection(Connection connection)435 public final boolean addConnection(Connection connection) { 436 Log.d(this, "Connection=%s, connection=", connection); 437 if (connection != null && !mChildConnections.contains(connection)) { 438 if (connection.setConference(this)) { 439 mChildConnections.add(connection); 440 onConnectionAdded(connection); 441 for (Listener l : mListeners) { 442 l.onConnectionAdded(this, connection); 443 } 444 return true; 445 } 446 } 447 return false; 448 } 449 450 /** 451 * Removes the specified connection as a child of this conference. 452 * 453 * @param connection The connection to remove. 454 */ removeConnection(Connection connection)455 public final void removeConnection(Connection connection) { 456 Log.d(this, "removing %s from %s", connection, mChildConnections); 457 if (connection != null && mChildConnections.remove(connection)) { 458 connection.resetConference(); 459 for (Listener l : mListeners) { 460 l.onConnectionRemoved(this, connection); 461 } 462 } 463 } 464 465 /** 466 * Sets the connections with which this connection can be conferenced. 467 * 468 * @param conferenceableConnections The set of connections this connection can conference with. 469 */ setConferenceableConnections(List<Connection> conferenceableConnections)470 public final void setConferenceableConnections(List<Connection> conferenceableConnections) { 471 clearConferenceableList(); 472 for (Connection c : conferenceableConnections) { 473 // If statement checks for duplicates in input. It makes it N^2 but we're dealing with a 474 // small amount of items here. 475 if (!mConferenceableConnections.contains(c)) { 476 c.addConnectionListener(mConnectionDeathListener); 477 mConferenceableConnections.add(c); 478 } 479 } 480 fireOnConferenceableConnectionsChanged(); 481 } 482 483 /** 484 * Set the video state for the conference. 485 * Valid values: {@link VideoProfile#STATE_AUDIO_ONLY}, 486 * {@link VideoProfile#STATE_BIDIRECTIONAL}, 487 * {@link VideoProfile#STATE_TX_ENABLED}, 488 * {@link VideoProfile#STATE_RX_ENABLED}. 489 * 490 * @param videoState The new video state. 491 */ setVideoState(Connection c, int videoState)492 public final void setVideoState(Connection c, int videoState) { 493 Log.d(this, "setVideoState Conference: %s Connection: %s VideoState: %s", 494 this, c, videoState); 495 for (Listener l : mListeners) { 496 l.onVideoStateChanged(this, videoState); 497 } 498 } 499 500 /** 501 * Sets the video connection provider. 502 * 503 * @param videoProvider The video provider. 504 */ setVideoProvider(Connection c, Connection.VideoProvider videoProvider)505 public final void setVideoProvider(Connection c, Connection.VideoProvider videoProvider) { 506 Log.d(this, "setVideoProvider Conference: %s Connection: %s VideoState: %s", 507 this, c, videoProvider); 508 for (Listener l : mListeners) { 509 l.onVideoProviderChanged(this, videoProvider); 510 } 511 } 512 fireOnConferenceableConnectionsChanged()513 private final void fireOnConferenceableConnectionsChanged() { 514 for (Listener l : mListeners) { 515 l.onConferenceableConnectionsChanged(this, getConferenceableConnections()); 516 } 517 } 518 519 /** 520 * Returns the connections with which this connection can be conferenced. 521 */ getConferenceableConnections()522 public final List<Connection> getConferenceableConnections() { 523 return mUnmodifiableConferenceableConnections; 524 } 525 526 /** 527 * Tears down the conference object and any of its current connections. 528 */ destroy()529 public final void destroy() { 530 Log.d(this, "destroying conference : %s", this); 531 // Tear down the children. 532 for (Connection connection : mChildConnections) { 533 Log.d(this, "removing connection %s", connection); 534 removeConnection(connection); 535 } 536 537 // If not yet disconnected, set the conference call as disconnected first. 538 if (mState != Connection.STATE_DISCONNECTED) { 539 Log.d(this, "setting to disconnected"); 540 setDisconnected(new DisconnectCause(DisconnectCause.LOCAL)); 541 } 542 543 // ...and notify. 544 for (Listener l : mListeners) { 545 l.onDestroyed(this); 546 } 547 } 548 549 /** 550 * Add a listener to be notified of a state change. 551 * 552 * @param listener The new listener. 553 * @return This conference. 554 * @hide 555 */ addListener(Listener listener)556 public final Conference addListener(Listener listener) { 557 mListeners.add(listener); 558 return this; 559 } 560 561 /** 562 * Removes the specified listener. 563 * 564 * @param listener The listener to remove. 565 * @return This conference. 566 * @hide 567 */ removeListener(Listener listener)568 public final Conference removeListener(Listener listener) { 569 mListeners.remove(listener); 570 return this; 571 } 572 573 /** 574 * Retrieves the primary connection associated with the conference. The primary connection is 575 * the connection from which the conference will retrieve its current state. 576 * 577 * @return The primary connection. 578 * @hide 579 */ 580 @TestApi 581 @SystemApi getPrimaryConnection()582 public Connection getPrimaryConnection() { 583 if (mUnmodifiableChildConnections == null || mUnmodifiableChildConnections.isEmpty()) { 584 return null; 585 } 586 return mUnmodifiableChildConnections.get(0); 587 } 588 589 /** 590 * Updates RIL voice radio technology used for current conference after its creation. 591 * 592 * @hide 593 */ updateCallRadioTechAfterCreation()594 public void updateCallRadioTechAfterCreation() { 595 final Connection primaryConnection = getPrimaryConnection(); 596 if (primaryConnection != null) { 597 setCallRadioTech(primaryConnection.getCallRadioTech()); 598 } else { 599 Log.w(this, "No primary connection found while updateCallRadioTechAfterCreation"); 600 } 601 } 602 603 /** 604 * @hide 605 * @deprecated Use {@link #setConnectionTime}. 606 */ 607 @Deprecated 608 @SystemApi setConnectTimeMillis(long connectTimeMillis)609 public final void setConnectTimeMillis(long connectTimeMillis) { 610 setConnectionTime(connectTimeMillis); 611 } 612 613 /** 614 * Sets the connection start time of the {@code Conference}. This is used in the call log to 615 * indicate the date and time when the conference took place. 616 * <p> 617 * Should be specified in wall-clock time returned by {@link System#currentTimeMillis()}. 618 * <p> 619 * When setting the connection time, you should always set the connection elapsed time via 620 * {@link #setConnectionStartElapsedRealTime(long)} to ensure the duration is reflected. 621 * 622 * @param connectionTimeMillis The connection time, in milliseconds, as returned by 623 * {@link System#currentTimeMillis()}. 624 */ setConnectionTime(long connectionTimeMillis)625 public final void setConnectionTime(long connectionTimeMillis) { 626 mConnectTimeMillis = connectionTimeMillis; 627 } 628 629 /** 630 * Sets the start time of the {@link Conference} which is the basis for the determining the 631 * duration of the {@link Conference}. 632 * <p> 633 * You should use a value returned by {@link SystemClock#elapsedRealtime()} to ensure that time 634 * zone changes do not impact the conference duration. 635 * <p> 636 * When setting this, you should also set the connection time via 637 * {@link #setConnectionTime(long)}. 638 * 639 * @param connectionStartElapsedRealTime The connection time, as measured by 640 * {@link SystemClock#elapsedRealtime()}. 641 */ setConnectionStartElapsedRealTime(long connectionStartElapsedRealTime)642 public final void setConnectionStartElapsedRealTime(long connectionStartElapsedRealTime) { 643 mConnectionStartElapsedRealTime = connectionStartElapsedRealTime; 644 } 645 646 /** 647 * @hide 648 * @deprecated Use {@link #getConnectionTime}. 649 */ 650 @Deprecated 651 @SystemApi getConnectTimeMillis()652 public final long getConnectTimeMillis() { 653 return getConnectionTime(); 654 } 655 656 /** 657 * Retrieves the connection start time of the {@code Conference}, if specified. A value of 658 * {@link #CONNECT_TIME_NOT_SPECIFIED} indicates that Telecom should determine the start time 659 * of the conference. 660 * 661 * @return The time at which the {@code Conference} was connected. 662 */ getConnectionTime()663 public final long getConnectionTime() { 664 return mConnectTimeMillis; 665 } 666 667 /** 668 * Retrieves the connection start time of the {@link Conference}, if specified. A value of 669 * {@link #CONNECT_TIME_NOT_SPECIFIED} indicates that Telecom should determine the start time 670 * of the conference. 671 * 672 * This is based on the value of {@link SystemClock#elapsedRealtime()} to ensure that it is not 673 * impacted by wall clock changes (user initiated, network initiated, time zone change, etc). 674 * 675 * @return The elapsed time at which the {@link Conference} was connected. 676 * @hide 677 */ getConnectionStartElapsedRealTime()678 public final long getConnectionStartElapsedRealTime() { 679 return mConnectionStartElapsedRealTime; 680 } 681 682 /** 683 * Sets RIL voice radio technology used for current conference. 684 * 685 * @param vrat the RIL voice radio technology used for current conference, 686 * see {@code RIL_RADIO_TECHNOLOGY_*} in {@link android.telephony.ServiceState}. 687 * 688 * @hide 689 */ setCallRadioTech(@erviceState.RilRadioTechnology int vrat)690 public final void setCallRadioTech(@ServiceState.RilRadioTechnology int vrat) { 691 putExtra(TelecomManager.EXTRA_CALL_NETWORK_TYPE, 692 ServiceState.rilRadioTechnologyToNetworkType(vrat)); 693 } 694 695 /** 696 * Returns RIL voice radio technology used for current conference. 697 * 698 * @return the RIL voice radio technology used for current conference, 699 * see {@code RIL_RADIO_TECHNOLOGY_*} in {@link android.telephony.ServiceState}. 700 * 701 * @hide 702 */ getCallRadioTech()703 public final @ServiceState.RilRadioTechnology int getCallRadioTech() { 704 int voiceNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN; 705 Bundle extras = getExtras(); 706 if (extras != null) { 707 voiceNetworkType = extras.getInt(TelecomManager.EXTRA_CALL_NETWORK_TYPE, 708 TelephonyManager.NETWORK_TYPE_UNKNOWN); 709 } 710 return ServiceState.networkTypeToRilRadioTechnology(voiceNetworkType); 711 } 712 713 /** 714 * Inform this Conference that the state of its audio output has been changed externally. 715 * 716 * @param state The new audio state. 717 * @hide 718 */ setCallAudioState(CallAudioState state)719 final void setCallAudioState(CallAudioState state) { 720 Log.d(this, "setCallAudioState %s", state); 721 mCallAudioState = state; 722 onAudioStateChanged(getAudioState()); 723 onCallAudioStateChanged(state); 724 } 725 setState(int newState)726 private void setState(int newState) { 727 if (newState != Connection.STATE_ACTIVE && 728 newState != Connection.STATE_HOLDING && 729 newState != Connection.STATE_DISCONNECTED) { 730 Log.w(this, "Unsupported state transition for Conference call.", 731 Connection.stateToString(newState)); 732 return; 733 } 734 735 if (mState != newState) { 736 int oldState = mState; 737 mState = newState; 738 for (Listener l : mListeners) { 739 l.onStateChanged(this, oldState, newState); 740 } 741 } 742 } 743 clearConferenceableList()744 private final void clearConferenceableList() { 745 for (Connection c : mConferenceableConnections) { 746 c.removeConnectionListener(mConnectionDeathListener); 747 } 748 mConferenceableConnections.clear(); 749 } 750 751 @Override toString()752 public String toString() { 753 return String.format(Locale.US, 754 "[State: %s,Capabilites: %s, VideoState: %s, VideoProvider: %s, ThisObject %s]", 755 Connection.stateToString(mState), 756 Call.Details.capabilitiesToString(mConnectionCapabilities), 757 getVideoState(), 758 getVideoProvider(), 759 super.toString()); 760 } 761 762 /** 763 * Sets the label and icon status to display in the InCall UI. 764 * 765 * @param statusHints The status label and icon to set. 766 */ setStatusHints(StatusHints statusHints)767 public final void setStatusHints(StatusHints statusHints) { 768 mStatusHints = statusHints; 769 for (Listener l : mListeners) { 770 l.onStatusHintsChanged(this, statusHints); 771 } 772 } 773 774 /** 775 * @return The status hints for this conference. 776 */ getStatusHints()777 public final StatusHints getStatusHints() { 778 return mStatusHints; 779 } 780 781 /** 782 * Replaces all the extras associated with this {@code Conference}. 783 * <p> 784 * New or existing keys are replaced in the {@code Conference} extras. Keys which are no longer 785 * in the new extras, but were present the last time {@code setExtras} was called are removed. 786 * <p> 787 * Alternatively you may use the {@link #putExtras(Bundle)}, and 788 * {@link #removeExtras(String...)} methods to modify the extras. 789 * <p> 790 * No assumptions should be made as to how an In-Call UI or service will handle these extras. 791 * Keys should be fully qualified (e.g., com.example.extras.MY_EXTRA) to avoid conflicts. 792 * 793 * @param extras The extras associated with this {@code Conference}. 794 */ setExtras(@ullable Bundle extras)795 public final void setExtras(@Nullable Bundle extras) { 796 // Keeping putExtras and removeExtras in the same lock so that this operation happens as a 797 // block instead of letting other threads put/remove while this method is running. 798 synchronized (mExtrasLock) { 799 // Add/replace any new or changed extras values. 800 putExtras(extras); 801 // If we have used "setExtras" in the past, compare the key set from the last invocation 802 // to the current one and remove any keys that went away. 803 if (mPreviousExtraKeys != null) { 804 List<String> toRemove = new ArrayList<String>(); 805 for (String oldKey : mPreviousExtraKeys) { 806 if (extras == null || !extras.containsKey(oldKey)) { 807 toRemove.add(oldKey); 808 } 809 } 810 811 if (!toRemove.isEmpty()) { 812 removeExtras(toRemove); 813 } 814 } 815 816 // Track the keys the last time set called setExtras. This way, the next time setExtras 817 // is called we can see if the caller has removed any extras values. 818 if (mPreviousExtraKeys == null) { 819 mPreviousExtraKeys = new ArraySet<String>(); 820 } 821 mPreviousExtraKeys.clear(); 822 if (extras != null) { 823 mPreviousExtraKeys.addAll(extras.keySet()); 824 } 825 } 826 } 827 828 /** 829 * Adds some extras to this {@link Conference}. Existing keys are replaced and new ones are 830 * added. 831 * <p> 832 * No assumptions should be made as to how an In-Call UI or service will handle these extras. 833 * Keys should be fully qualified (e.g., com.example.MY_EXTRA) to avoid conflicts. 834 * 835 * @param extras The extras to add. 836 */ putExtras(@onNull Bundle extras)837 public final void putExtras(@NonNull Bundle extras) { 838 if (extras == null) { 839 return; 840 } 841 842 // Creating a Bundle clone so we don't have to synchronize on mExtrasLock while calling 843 // onExtrasChanged. 844 Bundle listenersBundle; 845 synchronized (mExtrasLock) { 846 if (mExtras == null) { 847 mExtras = new Bundle(); 848 } 849 mExtras.putAll(extras); 850 listenersBundle = new Bundle(mExtras); 851 } 852 853 for (Listener l : mListeners) { 854 l.onExtrasChanged(this, new Bundle(listenersBundle)); 855 } 856 } 857 858 /** 859 * Adds a boolean extra to this {@link Conference}. 860 * 861 * @param key The extra key. 862 * @param value The value. 863 * @hide 864 */ putExtra(String key, boolean value)865 public final void putExtra(String key, boolean value) { 866 Bundle newExtras = new Bundle(); 867 newExtras.putBoolean(key, value); 868 putExtras(newExtras); 869 } 870 871 /** 872 * Adds an integer extra to this {@link Conference}. 873 * 874 * @param key The extra key. 875 * @param value The value. 876 * @hide 877 */ putExtra(String key, int value)878 public final void putExtra(String key, int value) { 879 Bundle newExtras = new Bundle(); 880 newExtras.putInt(key, value); 881 putExtras(newExtras); 882 } 883 884 /** 885 * Adds a string extra to this {@link Conference}. 886 * 887 * @param key The extra key. 888 * @param value The value. 889 * @hide 890 */ putExtra(String key, String value)891 public final void putExtra(String key, String value) { 892 Bundle newExtras = new Bundle(); 893 newExtras.putString(key, value); 894 putExtras(newExtras); 895 } 896 897 /** 898 * Removes extras from this {@link Conference}. 899 * 900 * @param keys The keys of the extras to remove. 901 */ removeExtras(List<String> keys)902 public final void removeExtras(List<String> keys) { 903 if (keys == null || keys.isEmpty()) { 904 return; 905 } 906 907 synchronized (mExtrasLock) { 908 if (mExtras != null) { 909 for (String key : keys) { 910 mExtras.remove(key); 911 } 912 } 913 } 914 915 List<String> unmodifiableKeys = Collections.unmodifiableList(keys); 916 for (Listener l : mListeners) { 917 l.onExtrasRemoved(this, unmodifiableKeys); 918 } 919 } 920 921 /** 922 * Removes extras from this {@link Conference}. 923 * 924 * @param keys The keys of the extras to remove. 925 */ removeExtras(String .... keys)926 public final void removeExtras(String ... keys) { 927 removeExtras(Arrays.asList(keys)); 928 } 929 930 /** 931 * Returns the extras associated with this conference. 932 * <p> 933 * Extras should be updated using {@link #putExtras(Bundle)} and {@link #removeExtras(List)}. 934 * <p> 935 * Telecom or an {@link InCallService} can also update the extras via 936 * {@link android.telecom.Call#putExtras(Bundle)}, and 937 * {@link Call#removeExtras(List)}. 938 * <p> 939 * The conference is notified of changes to the extras made by Telecom or an 940 * {@link InCallService} by {@link #onExtrasChanged(Bundle)}. 941 * 942 * @return The extras associated with this connection. 943 */ getExtras()944 public final Bundle getExtras() { 945 return mExtras; 946 } 947 948 /** 949 * Notifies this {@link Conference} of a change to the extras made outside the 950 * {@link ConnectionService}. 951 * <p> 952 * These extras changes can originate from Telecom itself, or from an {@link InCallService} via 953 * {@link android.telecom.Call#putExtras(Bundle)}, and 954 * {@link Call#removeExtras(List)}. 955 * 956 * @param extras The new extras bundle. 957 */ onExtrasChanged(Bundle extras)958 public void onExtrasChanged(Bundle extras) {} 959 960 /** 961 * Set whether Telecom should treat this {@link Conference} as a conference call or if it 962 * should treat it as a single-party call. 963 * This method is used as part of a workaround regarding IMS conference calls and user 964 * expectation. In IMS, once a conference is formed, the UE is connected to an IMS conference 965 * server. If all participants of the conference drop out of the conference except for one, the 966 * UE is still connected to the IMS conference server. At this point, the user logically 967 * assumes they're no longer in a conference, yet the underlying network actually is. 968 * To help provide a better user experiece, IMS conference calls can pretend to actually be a 969 * single-party call when the participant count drops to 1. Although the dialer/phone app 970 * could perform this trickery, it makes sense to do this in Telephony since a fix there will 971 * ensure that bluetooth head units, auto and wearable apps all behave consistently. 972 * 973 * @param isConference {@code true} if this {@link Conference} should be treated like a 974 * conference call, {@code false} if it should be treated like a single-party call. 975 * @hide 976 */ setConferenceState(boolean isConference)977 public void setConferenceState(boolean isConference) { 978 for (Listener l : mListeners) { 979 l.onConferenceStateChanged(this, isConference); 980 } 981 } 982 983 /** 984 * Sets the address of this {@link Conference}. Used when {@link #setConferenceState(boolean)} 985 * is called to mark a conference temporarily as NOT a conference. 986 * 987 * @param address The new address. 988 * @param presentation The presentation requirements for the address. 989 * See {@link TelecomManager} for valid values. 990 * @hide 991 */ setAddress(Uri address, int presentation)992 public final void setAddress(Uri address, int presentation) { 993 Log.d(this, "setAddress %s", address); 994 mAddress = address; 995 mAddressPresentation = presentation; 996 for (Listener l : mListeners) { 997 l.onAddressChanged(this, address, presentation); 998 } 999 } 1000 1001 /** 1002 * Returns the "address" associated with the conference. This is applicable in two cases: 1003 * <ol> 1004 * <li>When {@link #setConferenceState(boolean)} is used to mark a conference as 1005 * temporarily "not a conference"; we need to present the correct address in the in-call 1006 * UI.</li> 1007 * <li>When the conference is not hosted on the current device, we need to know the address 1008 * information for the purpose of showing the original address to the user, as well as for 1009 * logging to the call log.</li> 1010 * </ol> 1011 * @return The address of the conference, or {@code null} if not applicable. 1012 * @hide 1013 */ getAddress()1014 public final Uri getAddress() { 1015 return mAddress; 1016 } 1017 1018 /** 1019 * Returns the address presentation associated with the conference. 1020 * <p> 1021 * This is applicable in two cases: 1022 * <ol> 1023 * <li>When {@link #setConferenceState(boolean)} is used to mark a conference as 1024 * temporarily "not a conference"; we need to present the correct address in the in-call 1025 * UI.</li> 1026 * <li>When the conference is not hosted on the current device, we need to know the address 1027 * information for the purpose of showing the original address to the user, as well as for 1028 * logging to the call log.</li> 1029 * </ol> 1030 * @return The address of the conference, or {@code null} if not applicable. 1031 * @hide 1032 */ getAddressPresentation()1033 public final int getAddressPresentation() { 1034 return mAddressPresentation; 1035 } 1036 1037 /** 1038 * @return The caller display name (CNAP). 1039 * @hide 1040 */ getCallerDisplayName()1041 public final String getCallerDisplayName() { 1042 return mCallerDisplayName; 1043 } 1044 1045 /** 1046 * @return The presentation requirements for the handle. 1047 * See {@link TelecomManager} for valid values. 1048 * @hide 1049 */ getCallerDisplayNamePresentation()1050 public final int getCallerDisplayNamePresentation() { 1051 return mCallerDisplayNamePresentation; 1052 } 1053 1054 /** 1055 * Sets the caller display name (CNAP) of this {@link Conference}. Used when 1056 * {@link #setConferenceState(boolean)} is called to mark a conference temporarily as NOT a 1057 * conference. 1058 * 1059 * @param callerDisplayName The new display name. 1060 * @param presentation The presentation requirements for the handle. 1061 * See {@link TelecomManager} for valid values. 1062 * @hide 1063 */ setCallerDisplayName(String callerDisplayName, int presentation)1064 public final void setCallerDisplayName(String callerDisplayName, int presentation) { 1065 Log.d(this, "setCallerDisplayName %s", callerDisplayName); 1066 mCallerDisplayName = callerDisplayName; 1067 mCallerDisplayNamePresentation = presentation; 1068 for (Listener l : mListeners) { 1069 l.onCallerDisplayNameChanged(this, callerDisplayName, presentation); 1070 } 1071 } 1072 1073 /** 1074 * Handles a change to extras received from Telecom. 1075 * 1076 * @param extras The new extras. 1077 * @hide 1078 */ handleExtrasChanged(Bundle extras)1079 final void handleExtrasChanged(Bundle extras) { 1080 Bundle b = null; 1081 synchronized (mExtrasLock) { 1082 mExtras = extras; 1083 if (mExtras != null) { 1084 b = new Bundle(mExtras); 1085 } 1086 } 1087 onExtrasChanged(b); 1088 } 1089 1090 /** 1091 * See {@link Connection#sendConnectionEvent(String, Bundle)} 1092 * @hide 1093 */ sendConnectionEvent(String event, Bundle extras)1094 public void sendConnectionEvent(String event, Bundle extras) { 1095 for (Listener l : mListeners) { 1096 l.onConnectionEvent(this, event, extras); 1097 } 1098 } 1099 } 1100