ImsCall.java revision 87466c59b1980ec4e56bb17fae8b855efcc17fd8
1/* 2 * Copyright (c) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.ims; 18 19import com.android.internal.R; 20 21import java.util.ArrayList; 22import java.util.Iterator; 23import java.util.List; 24import java.util.Map.Entry; 25import java.util.Set; 26 27import android.content.Context; 28import android.net.Uri; 29import android.os.Bundle; 30import android.os.Message; 31import android.provider.Settings; 32import android.telecom.ConferenceParticipant; 33import android.telecom.TelecomManager; 34import android.telephony.Rlog; 35import android.util.Log; 36import android.widget.Toast; 37 38import com.android.ims.internal.ICall; 39import com.android.ims.internal.ImsCallSession; 40import com.android.ims.internal.ImsStreamMediaSession; 41import com.android.internal.annotations.VisibleForTesting; 42 43/** 44 * Handles an IMS voice / video call over LTE. You can instantiate this class with 45 * {@link ImsManager}. 46 * 47 * @hide 48 */ 49public class ImsCall implements ICall { 50 public static final int CALL_STATE_ACTIVE_TO_HOLD = 1; 51 public static final int CALL_STATE_HOLD_TO_ACTIVE = 2; 52 53 // Mode of USSD message 54 public static final int USSD_MODE_NOTIFY = 0; 55 public static final int USSD_MODE_REQUEST = 1; 56 57 private static final String TAG = "ImsCall"; 58 private static final boolean FORCE_DEBUG = false; /* STOPSHIP if true */ 59 private static final boolean DBG = FORCE_DEBUG || Rlog.isLoggable(TAG, Log.DEBUG); 60 private static final boolean VDBG = FORCE_DEBUG || Rlog.isLoggable(TAG, Log.VERBOSE); 61 62 /** 63 * Listener for events relating to an IMS call, such as when a call is being 64 * recieved ("on ringing") or a call is outgoing ("on calling"). 65 * <p>Many of these events are also received by {@link ImsCallSession.Listener}.</p> 66 */ 67 public static class Listener { 68 /** 69 * Called when a request is sent out to initiate a new call 70 * and 1xx response is received from the network. 71 * The default implementation calls {@link #onCallStateChanged}. 72 * 73 * @param call the call object that carries out the IMS call 74 */ 75 public void onCallProgressing(ImsCall call) { 76 onCallStateChanged(call); 77 } 78 79 /** 80 * Called when the call is established. 81 * The default implementation calls {@link #onCallStateChanged}. 82 * 83 * @param call the call object that carries out the IMS call 84 */ 85 public void onCallStarted(ImsCall call) { 86 onCallStateChanged(call); 87 } 88 89 /** 90 * Called when the call setup is failed. 91 * The default implementation calls {@link #onCallError}. 92 * 93 * @param call the call object that carries out the IMS call 94 * @param reasonInfo detailed reason of the call setup failure 95 */ 96 public void onCallStartFailed(ImsCall call, ImsReasonInfo reasonInfo) { 97 onCallError(call, reasonInfo); 98 } 99 100 /** 101 * Called when the call is terminated. 102 * The default implementation calls {@link #onCallStateChanged}. 103 * 104 * @param call the call object that carries out the IMS call 105 * @param reasonInfo detailed reason of the call termination 106 */ 107 public void onCallTerminated(ImsCall call, ImsReasonInfo reasonInfo) { 108 // Store the call termination reason 109 110 onCallStateChanged(call); 111 } 112 113 /** 114 * Called when the call is in hold. 115 * The default implementation calls {@link #onCallStateChanged}. 116 * 117 * @param call the call object that carries out the IMS call 118 */ 119 public void onCallHeld(ImsCall call) { 120 onCallStateChanged(call); 121 } 122 123 /** 124 * Called when the call hold is failed. 125 * The default implementation calls {@link #onCallError}. 126 * 127 * @param call the call object that carries out the IMS call 128 * @param reasonInfo detailed reason of the call hold failure 129 */ 130 public void onCallHoldFailed(ImsCall call, ImsReasonInfo reasonInfo) { 131 onCallError(call, reasonInfo); 132 } 133 134 /** 135 * Called when the call hold is received from the remote user. 136 * The default implementation calls {@link #onCallStateChanged}. 137 * 138 * @param call the call object that carries out the IMS call 139 */ 140 public void onCallHoldReceived(ImsCall call) { 141 onCallStateChanged(call); 142 } 143 144 /** 145 * Called when the call is in call. 146 * The default implementation calls {@link #onCallStateChanged}. 147 * 148 * @param call the call object that carries out the IMS call 149 */ 150 public void onCallResumed(ImsCall call) { 151 onCallStateChanged(call); 152 } 153 154 /** 155 * Called when the call resume is failed. 156 * The default implementation calls {@link #onCallError}. 157 * 158 * @param call the call object that carries out the IMS call 159 * @param reasonInfo detailed reason of the call resume failure 160 */ 161 public void onCallResumeFailed(ImsCall call, ImsReasonInfo reasonInfo) { 162 onCallError(call, reasonInfo); 163 } 164 165 /** 166 * Called when the call resume is received from the remote user. 167 * The default implementation calls {@link #onCallStateChanged}. 168 * 169 * @param call the call object that carries out the IMS call 170 */ 171 public void onCallResumeReceived(ImsCall call) { 172 onCallStateChanged(call); 173 } 174 175 /** 176 * Called when the call is in call. 177 * The default implementation calls {@link #onCallStateChanged}. 178 * 179 * @param call the call object that carries out the IMS call 180 * @param newCall the call object that is merged with an active & hold call 181 */ 182 public void onCallMerged(ImsCall call) { 183 onCallStateChanged(call); 184 } 185 186 /** 187 * Called when the call merge is failed. 188 * The default implementation calls {@link #onCallError}. 189 * 190 * @param call the call object that carries out the IMS call 191 * @param reasonInfo detailed reason of the call merge failure 192 */ 193 public void onCallMergeFailed(ImsCall call, ImsReasonInfo reasonInfo) { 194 onCallError(call, reasonInfo); 195 } 196 197 /** 198 * Called when the call is updated (except for hold/unhold). 199 * The default implementation calls {@link #onCallStateChanged}. 200 * 201 * @param call the call object that carries out the IMS call 202 */ 203 public void onCallUpdated(ImsCall call) { 204 onCallStateChanged(call); 205 } 206 207 /** 208 * Called when the call update is failed. 209 * The default implementation calls {@link #onCallError}. 210 * 211 * @param call the call object that carries out the IMS call 212 * @param reasonInfo detailed reason of the call update failure 213 */ 214 public void onCallUpdateFailed(ImsCall call, ImsReasonInfo reasonInfo) { 215 onCallError(call, reasonInfo); 216 } 217 218 /** 219 * Called when the call update is received from the remote user. 220 * 221 * @param call the call object that carries out the IMS call 222 */ 223 public void onCallUpdateReceived(ImsCall call) { 224 // no-op 225 } 226 227 /** 228 * Called when the call is extended to the conference call. 229 * The default implementation calls {@link #onCallStateChanged}. 230 * 231 * @param call the call object that carries out the IMS call 232 * @param newCall the call object that is extended to the conference from the active call 233 */ 234 public void onCallConferenceExtended(ImsCall call, ImsCall newCall) { 235 onCallStateChanged(call); 236 } 237 238 /** 239 * Called when the conference extension is failed. 240 * The default implementation calls {@link #onCallError}. 241 * 242 * @param call the call object that carries out the IMS call 243 * @param reasonInfo detailed reason of the conference extension failure 244 */ 245 public void onCallConferenceExtendFailed(ImsCall call, 246 ImsReasonInfo reasonInfo) { 247 onCallError(call, reasonInfo); 248 } 249 250 /** 251 * Called when the conference extension is received from the remote user. 252 * 253 * @param call the call object that carries out the IMS call 254 * @param newCall the call object that is extended to the conference from the active call 255 */ 256 public void onCallConferenceExtendReceived(ImsCall call, ImsCall newCall) { 257 onCallStateChanged(call); 258 } 259 260 /** 261 * Called when the invitation request of the participants is delivered to 262 * the conference server. 263 * 264 * @param call the call object that carries out the IMS call 265 */ 266 public void onCallInviteParticipantsRequestDelivered(ImsCall call) { 267 // no-op 268 } 269 270 /** 271 * Called when the invitation request of the participants is failed. 272 * 273 * @param call the call object that carries out the IMS call 274 * @param reasonInfo detailed reason of the conference invitation failure 275 */ 276 public void onCallInviteParticipantsRequestFailed(ImsCall call, 277 ImsReasonInfo reasonInfo) { 278 // no-op 279 } 280 281 /** 282 * Called when the removal request of the participants is delivered to 283 * the conference server. 284 * 285 * @param call the call object that carries out the IMS call 286 */ 287 public void onCallRemoveParticipantsRequestDelivered(ImsCall call) { 288 // no-op 289 } 290 291 /** 292 * Called when the removal request of the participants is failed. 293 * 294 * @param call the call object that carries out the IMS call 295 * @param reasonInfo detailed reason of the conference removal failure 296 */ 297 public void onCallRemoveParticipantsRequestFailed(ImsCall call, 298 ImsReasonInfo reasonInfo) { 299 // no-op 300 } 301 302 /** 303 * Called when the conference state is updated. 304 * 305 * @param call the call object that carries out the IMS call 306 * @param state state of the participant who is participated in the conference call 307 */ 308 public void onCallConferenceStateUpdated(ImsCall call, ImsConferenceState state) { 309 // no-op 310 } 311 312 /** 313 * Called when the state of IMS conference participant(s) has changed. 314 * 315 * @param call the call object that carries out the IMS call. 316 * @param participants the participant(s) and their new state information. 317 */ 318 public void onConferenceParticipantsStateChanged(ImsCall call, 319 List<ConferenceParticipant> participants) { 320 // no-op 321 } 322 323 /** 324 * Called when the USSD message is received from the network. 325 * 326 * @param mode mode of the USSD message (REQUEST / NOTIFY) 327 * @param ussdMessage USSD message 328 */ 329 public void onCallUssdMessageReceived(ImsCall call, 330 int mode, String ussdMessage) { 331 // no-op 332 } 333 334 /** 335 * Called when an error occurs. The default implementation is no op. 336 * overridden. The default implementation is no op. Error events are 337 * not re-directed to this callback and are handled in {@link #onCallError}. 338 * 339 * @param call the call object that carries out the IMS call 340 * @param reasonInfo detailed reason of this error 341 * @see ImsReasonInfo 342 */ 343 public void onCallError(ImsCall call, ImsReasonInfo reasonInfo) { 344 // no-op 345 } 346 347 /** 348 * Called when an event occurs and the corresponding callback is not 349 * overridden. The default implementation is no op. Error events are 350 * not re-directed to this callback and are handled in {@link #onCallError}. 351 * 352 * @param call the call object that carries out the IMS call 353 */ 354 public void onCallStateChanged(ImsCall call) { 355 // no-op 356 } 357 358 /** 359 * Called when the call moves the hold state to the conversation state. 360 * For example, when merging the active & hold call, the state of all the hold call 361 * will be changed from hold state to conversation state. 362 * This callback method can be invoked even though the application does not trigger 363 * any operations. 364 * 365 * @param call the call object that carries out the IMS call 366 * @param state the detailed state of call state changes; 367 * Refer to CALL_STATE_* in {@link ImsCall} 368 */ 369 public void onCallStateChanged(ImsCall call, int state) { 370 // no-op 371 } 372 } 373 374 375 376 // List of update operation for IMS call control 377 private static final int UPDATE_NONE = 0; 378 private static final int UPDATE_HOLD = 1; 379 private static final int UPDATE_HOLD_MERGE = 2; 380 private static final int UPDATE_RESUME = 3; 381 private static final int UPDATE_MERGE = 4; 382 private static final int UPDATE_EXTEND_TO_CONFERENCE = 5; 383 private static final int UPDATE_UNSPECIFIED = 6; 384 385 // For synchronization of private variables 386 private Object mLockObj = new Object(); 387 private Context mContext; 388 389 // true if the call is established & in the conversation state 390 private boolean mInCall = false; 391 // true if the call is on hold 392 // If it is triggered by the local, mute the call. Otherwise, play local hold tone 393 // or network generated media. 394 private boolean mHold = false; 395 // true if the call is on mute 396 private boolean mMute = false; 397 // It contains the exclusive call update request. Refer to UPDATE_*. 398 private int mUpdateRequest = UPDATE_NONE; 399 400 private ImsCall.Listener mListener = null; 401 402 // When merging two calls together, the "peer" call that will merge into this call. 403 private ImsCall mMergePeer = null; 404 // When merging two calls together, the "host" call we are merging into. 405 private ImsCall mMergeHost = null; 406 407 // Wrapper call session to interworking the IMS service (server). 408 private ImsCallSession mSession = null; 409 // Call profile of the current session. 410 // It can be changed at anytime when the call is updated. 411 private ImsCallProfile mCallProfile = null; 412 // Call profile to be updated after the application's action (accept/reject) 413 // to the call update. After the application's action (accept/reject) is done, 414 // it will be set to null. 415 private ImsCallProfile mProposedCallProfile = null; 416 private ImsReasonInfo mLastReasonInfo = null; 417 418 // Media session to control media (audio/video) operations for an IMS call 419 private ImsStreamMediaSession mMediaSession = null; 420 421 // The temporary ImsCallSession that could represent the merged call once 422 // we receive notification that the merge was successful. 423 private ImsCallSession mTransientConferenceSession = null; 424 // While a merge is progressing, we bury any session termination requests 425 // made on the original ImsCallSession until we have closure on the merge request 426 // If the request ultimately fails, we need to act on the termination request 427 // that we buried temporarily. We do this because we feel that timing issues could 428 // cause the termination request to occur just because the merge is succeeding. 429 private boolean mSessionEndDuringMerge = false; 430 // Just like mSessionEndDuringMerge, we need to keep track of the reason why the 431 // termination request was made on the original session in case we need to act 432 // on it in the case of a merge failure. 433 private ImsReasonInfo mSessionEndDuringMergeReasonInfo = null; 434 private boolean mIsMerged = false; 435 436 /** 437 * Create an IMS call object. 438 * 439 * @param context the context for accessing system services 440 * @param profile the call profile to make/take a call 441 */ 442 public ImsCall(Context context, ImsCallProfile profile) { 443 mContext = context; 444 mCallProfile = profile; 445 } 446 447 /** 448 * Closes this object. This object is not usable after being closed. 449 */ 450 @Override 451 public void close() { 452 synchronized(mLockObj) { 453 if (mSession != null) { 454 mSession.close(); 455 mSession = null; 456 } 457 458 mCallProfile = null; 459 mProposedCallProfile = null; 460 mLastReasonInfo = null; 461 mMediaSession = null; 462 } 463 } 464 465 /** 466 * Checks if the call has a same remote user identity or not. 467 * 468 * @param userId the remote user identity 469 * @return true if the remote user identity is equal; otherwise, false 470 */ 471 @Override 472 public boolean checkIfRemoteUserIsSame(String userId) { 473 if (userId == null) { 474 return false; 475 } 476 477 return userId.equals(mCallProfile.getCallExtra(ImsCallProfile.EXTRA_REMOTE_URI, "")); 478 } 479 480 /** 481 * Checks if the call is equal or not. 482 * 483 * @param call the call to be compared 484 * @return true if the call is equal; otherwise, false 485 */ 486 @Override 487 public boolean equalsTo(ICall call) { 488 if (call == null) { 489 return false; 490 } 491 492 if (call instanceof ImsCall) { 493 return this.equals(call); 494 } 495 496 return false; 497 } 498 499 /** 500 * Gets the negotiated (local & remote) call profile. 501 * 502 * @return a {@link ImsCallProfile} object that has the negotiated call profile 503 */ 504 public ImsCallProfile getCallProfile() { 505 synchronized(mLockObj) { 506 return mCallProfile; 507 } 508 } 509 510 /** 511 * Gets the local call profile (local capabilities). 512 * 513 * @return a {@link ImsCallProfile} object that has the local call profile 514 */ 515 public ImsCallProfile getLocalCallProfile() throws ImsException { 516 synchronized(mLockObj) { 517 if (mSession == null) { 518 throw new ImsException("No call session", 519 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 520 } 521 522 try { 523 return mSession.getLocalCallProfile(); 524 } catch (Throwable t) { 525 loge("getLocalCallProfile :: ", t); 526 throw new ImsException("getLocalCallProfile()", t, 0); 527 } 528 } 529 } 530 531 /** 532 * Gets the remote call profile (remote capabilities). 533 * 534 * @return a {@link ImsCallProfile} object that has the remote call profile 535 */ 536 public ImsCallProfile getRemoteCallProfile() throws ImsException { 537 synchronized(mLockObj) { 538 if (mSession == null) { 539 throw new ImsException("No call session", 540 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 541 } 542 543 try { 544 return mSession.getRemoteCallProfile(); 545 } catch (Throwable t) { 546 loge("getRemoteCallProfile :: ", t); 547 throw new ImsException("getRemoteCallProfile()", t, 0); 548 } 549 } 550 } 551 552 /** 553 * Gets the call profile proposed by the local/remote user. 554 * 555 * @return a {@link ImsCallProfile} object that has the proposed call profile 556 */ 557 public ImsCallProfile getProposedCallProfile() { 558 synchronized(mLockObj) { 559 if (!isInCall()) { 560 return null; 561 } 562 563 return mProposedCallProfile; 564 } 565 } 566 567 /** 568 * Gets the state of the {@link ImsCallSession} that carries this call. 569 * The value returned must be one of the states in {@link ImsCallSession#State}. 570 * 571 * @return the session state 572 */ 573 public int getState() { 574 synchronized(mLockObj) { 575 if (mSession == null) { 576 return ImsCallSession.State.IDLE; 577 } 578 579 return mSession.getState(); 580 } 581 } 582 583 /** 584 * Gets the {@link ImsCallSession} that carries this call. 585 * 586 * @return the session object that carries this call 587 * @hide 588 */ 589 public ImsCallSession getCallSession() { 590 synchronized(mLockObj) { 591 return mSession; 592 } 593 } 594 595 /** 596 * Gets the {@link ImsStreamMediaSession} that handles the media operation of this call. 597 * Almost interface APIs are for the VT (Video Telephony). 598 * 599 * @return the media session object that handles the media operation of this call 600 * @hide 601 */ 602 public ImsStreamMediaSession getMediaSession() { 603 synchronized(mLockObj) { 604 return mMediaSession; 605 } 606 } 607 608 /** 609 * Gets the specified property of this call. 610 * 611 * @param name key to get the extra call information defined in {@link ImsCallProfile} 612 * @return the extra call information as string 613 */ 614 public String getCallExtra(String name) throws ImsException { 615 // Lookup the cache 616 617 synchronized(mLockObj) { 618 // If not found, try to get the property from the remote 619 if (mSession == null) { 620 throw new ImsException("No call session", 621 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 622 } 623 624 try { 625 return mSession.getProperty(name); 626 } catch (Throwable t) { 627 loge("getCallExtra :: ", t); 628 throw new ImsException("getCallExtra()", t, 0); 629 } 630 } 631 } 632 633 /** 634 * Gets the last reason information when the call is not established, cancelled or terminated. 635 * 636 * @return the last reason information 637 */ 638 public ImsReasonInfo getLastReasonInfo() { 639 synchronized(mLockObj) { 640 return mLastReasonInfo; 641 } 642 } 643 644 /** 645 * Checks if the call has a pending update operation. 646 * 647 * @return true if the call has a pending update operation 648 */ 649 public boolean hasPendingUpdate() { 650 synchronized(mLockObj) { 651 return (mUpdateRequest != UPDATE_NONE); 652 } 653 } 654 655 /** 656 * Checks if the call is established. 657 * 658 * @return true if the call is established 659 */ 660 public boolean isInCall() { 661 synchronized(mLockObj) { 662 return mInCall; 663 } 664 } 665 666 /** 667 * Checks if the call is muted. 668 * 669 * @return true if the call is muted 670 */ 671 public boolean isMuted() { 672 synchronized(mLockObj) { 673 return mMute; 674 } 675 } 676 677 /** 678 * Checks if the call is on hold. 679 * 680 * @return true if the call is on hold 681 */ 682 public boolean isOnHold() { 683 synchronized(mLockObj) { 684 return mHold; 685 } 686 } 687 688 /** 689 * Determines if the call is a multiparty call. 690 * 691 * @return {@code True} if the call is a multiparty call. 692 */ 693 public boolean isMultiparty() { 694 synchronized(mLockObj) { 695 if (mSession == null) { 696 return false; 697 } 698 699 return mSession.isMultiparty(); 700 } 701 } 702 703 /** 704 * Marks whether an IMS call is merged. This should be set {@code true} when the call merges 705 * into a conference. 706 * 707 * @param isMerged Whether the call is merged. 708 */ 709 public void setIsMerged(boolean isMerged) { 710 mIsMerged = isMerged; 711 } 712 713 /** 714 * @return {@code true} if the call recently merged into a conference call. 715 */ 716 public boolean isMerged() { 717 return mIsMerged; 718 } 719 720 /** 721 * Sets the listener to listen to the IMS call events. 722 * The method calls {@link #setListener setListener(listener, false)}. 723 * 724 * @param listener to listen to the IMS call events of this object; null to remove listener 725 * @see #setListener(Listener, boolean) 726 */ 727 public void setListener(ImsCall.Listener listener) { 728 setListener(listener, false); 729 } 730 731 /** 732 * Sets the listener to listen to the IMS call events. 733 * A {@link ImsCall} can only hold one listener at a time. Subsequent calls 734 * to this method override the previous listener. 735 * 736 * @param listener to listen to the IMS call events of this object; null to remove listener 737 * @param callbackImmediately set to true if the caller wants to be called 738 * back immediately on the current state 739 */ 740 public void setListener(ImsCall.Listener listener, boolean callbackImmediately) { 741 boolean inCall; 742 boolean onHold; 743 int state; 744 ImsReasonInfo lastReasonInfo; 745 746 synchronized(mLockObj) { 747 mListener = listener; 748 749 if ((listener == null) || !callbackImmediately) { 750 return; 751 } 752 753 inCall = mInCall; 754 onHold = mHold; 755 state = getState(); 756 lastReasonInfo = mLastReasonInfo; 757 } 758 759 try { 760 if (lastReasonInfo != null) { 761 listener.onCallError(this, lastReasonInfo); 762 } else if (inCall) { 763 if (onHold) { 764 listener.onCallHeld(this); 765 } else { 766 listener.onCallStarted(this); 767 } 768 } else { 769 switch (state) { 770 case ImsCallSession.State.ESTABLISHING: 771 listener.onCallProgressing(this); 772 break; 773 case ImsCallSession.State.TERMINATED: 774 listener.onCallTerminated(this, lastReasonInfo); 775 break; 776 default: 777 // Ignore it. There is no action in the other state. 778 break; 779 } 780 } 781 } catch (Throwable t) { 782 loge("setListener()", t); 783 } 784 } 785 786 /** 787 * Mutes or unmutes the mic for the active call. 788 * 789 * @param muted true if the call is muted, false otherwise 790 */ 791 public void setMute(boolean muted) throws ImsException { 792 synchronized(mLockObj) { 793 if (mMute != muted) { 794 mMute = muted; 795 796 try { 797 mSession.setMute(muted); 798 } catch (Throwable t) { 799 loge("setMute :: ", t); 800 throwImsException(t, 0); 801 } 802 } 803 } 804 } 805 806 /** 807 * Attaches an incoming call to this call object. 808 * 809 * @param session the session that receives the incoming call 810 * @throws ImsException if the IMS service fails to attach this object to the session 811 */ 812 public void attachSession(ImsCallSession session) throws ImsException { 813 if (DBG) { 814 log("attachSession :: session=" + session); 815 } 816 817 synchronized(mLockObj) { 818 mSession = session; 819 820 try { 821 mSession.setListener(createCallSessionListener()); 822 } catch (Throwable t) { 823 loge("attachSession :: ", t); 824 throwImsException(t, 0); 825 } 826 } 827 } 828 829 /** 830 * Initiates an IMS call with the call profile which is provided 831 * when creating a {@link ImsCall}. 832 * 833 * @param session the {@link ImsCallSession} for carrying out the call 834 * @param callee callee information to initiate an IMS call 835 * @throws ImsException if the IMS service fails to initiate the call 836 */ 837 public void start(ImsCallSession session, String callee) 838 throws ImsException { 839 if (DBG) { 840 log("start(1) :: session=" + session + ", callee=" + callee); 841 } 842 843 synchronized(mLockObj) { 844 mSession = session; 845 846 try { 847 session.setListener(createCallSessionListener()); 848 session.start(callee, mCallProfile); 849 } catch (Throwable t) { 850 loge("start(1) :: ", t); 851 throw new ImsException("start(1)", t, 0); 852 } 853 } 854 } 855 856 /** 857 * Initiates an IMS conferenca call with the call profile which is provided 858 * when creating a {@link ImsCall}. 859 * 860 * @param session the {@link ImsCallSession} for carrying out the call 861 * @param participants participant list to initiate an IMS conference call 862 * @throws ImsException if the IMS service fails to initiate the call 863 */ 864 public void start(ImsCallSession session, String[] participants) 865 throws ImsException { 866 if (DBG) { 867 log("start(n) :: session=" + session + ", callee=" + participants); 868 } 869 870 synchronized(mLockObj) { 871 mSession = session; 872 873 try { 874 session.setListener(createCallSessionListener()); 875 session.start(participants, mCallProfile); 876 } catch (Throwable t) { 877 loge("start(n) :: ", t); 878 throw new ImsException("start(n)", t, 0); 879 } 880 } 881 } 882 883 /** 884 * Accepts a call. 885 * 886 * @see Listener#onCallStarted 887 * 888 * @param callType The call type the user agreed to for accepting the call. 889 * @throws ImsException if the IMS service fails to accept the call 890 */ 891 public void accept(int callType) throws ImsException { 892 if (VDBG) { 893 logv("accept ::"); 894 } 895 896 accept(callType, new ImsStreamMediaProfile()); 897 } 898 899 /** 900 * Accepts a call. 901 * 902 * @param callType call type to be answered in {@link ImsCallProfile} 903 * @param profile a media profile to be answered (audio/audio & video, direction, ...) 904 * @see Listener#onCallStarted 905 * @throws ImsException if the IMS service fails to accept the call 906 */ 907 public void accept(int callType, ImsStreamMediaProfile profile) throws ImsException { 908 if (VDBG) { 909 logv("accept :: callType=" + callType + ", profile=" + profile); 910 } 911 912 synchronized(mLockObj) { 913 if (mSession == null) { 914 throw new ImsException("No call to answer", 915 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 916 } 917 918 try { 919 mSession.accept(callType, profile); 920 } catch (Throwable t) { 921 loge("accept :: ", t); 922 throw new ImsException("accept()", t, 0); 923 } 924 925 if (mInCall && (mProposedCallProfile != null)) { 926 if (DBG) { 927 log("accept :: call profile will be updated"); 928 } 929 930 mCallProfile = mProposedCallProfile; 931 mProposedCallProfile = null; 932 } 933 934 // Other call update received 935 if (mInCall && (mUpdateRequest == UPDATE_UNSPECIFIED)) { 936 mUpdateRequest = UPDATE_NONE; 937 } 938 } 939 } 940 941 /** 942 * Rejects a call. 943 * 944 * @param reason reason code to reject an incoming call 945 * @see Listener#onCallStartFailed 946 * @throws ImsException if the IMS service fails to accept the call 947 */ 948 public void reject(int reason) throws ImsException { 949 if (VDBG) { 950 logv("reject :: reason=" + reason); 951 } 952 953 synchronized(mLockObj) { 954 if (mSession != null) { 955 mSession.reject(reason); 956 } 957 958 if (mInCall && (mProposedCallProfile != null)) { 959 if (DBG) { 960 log("reject :: call profile is not updated; destroy it..."); 961 } 962 963 mProposedCallProfile = null; 964 } 965 966 // Other call update received 967 if (mInCall && (mUpdateRequest == UPDATE_UNSPECIFIED)) { 968 mUpdateRequest = UPDATE_NONE; 969 } 970 } 971 } 972 973 /** 974 * Terminates an IMS call. 975 * 976 * @param reason reason code to terminate a call 977 * @throws ImsException if the IMS service fails to terminate the call 978 */ 979 public void terminate(int reason) throws ImsException { 980 if (VDBG) { 981 logv("terminate :: reason=" + reason); 982 } 983 984 synchronized(mLockObj) { 985 mHold = false; 986 mInCall = false; 987 988 if (mSession != null) { 989 mSession.terminate(reason); 990 } 991 } 992 } 993 994 995 /** 996 * Puts a call on hold. When succeeds, {@link Listener#onCallHeld} is called. 997 * 998 * @see Listener#onCallHeld, Listener#onCallHoldFailed 999 * @throws ImsException if the IMS service fails to hold the call 1000 */ 1001 public void hold() throws ImsException { 1002 if (VDBG) { 1003 logv("hold ::"); 1004 } 1005 1006 if (isOnHold()) { 1007 if (DBG) { 1008 log("hold :: call is already on hold"); 1009 } 1010 return; 1011 } 1012 1013 synchronized(mLockObj) { 1014 if (mUpdateRequest != UPDATE_NONE) { 1015 loge("hold :: update is in progress; request=" + 1016 updateRequestToString(mUpdateRequest)); 1017 throw new ImsException("Call update is in progress", 1018 ImsReasonInfo.CODE_LOCAL_ILLEGAL_STATE); 1019 } 1020 1021 if (mSession == null) { 1022 loge("hold :: "); 1023 throw new ImsException("No call session", 1024 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1025 } 1026 1027 mSession.hold(createHoldMediaProfile()); 1028 // FIXME: update the state on the callback? 1029 mHold = true; 1030 mUpdateRequest = UPDATE_HOLD; 1031 } 1032 } 1033 1034 /** 1035 * Continues a call that's on hold. When succeeds, {@link Listener#onCallResumed} is called. 1036 * 1037 * @see Listener#onCallResumed, Listener#onCallResumeFailed 1038 * @throws ImsException if the IMS service fails to resume the call 1039 */ 1040 public void resume() throws ImsException { 1041 if (VDBG) { 1042 logv("resume ::"); 1043 } 1044 1045 if (!isOnHold()) { 1046 if (DBG) { 1047 log("resume :: call is in conversation"); 1048 } 1049 return; 1050 } 1051 1052 synchronized(mLockObj) { 1053 if (mUpdateRequest != UPDATE_NONE) { 1054 loge("resume :: update is in progress; request=" + 1055 updateRequestToString(mUpdateRequest)); 1056 throw new ImsException("Call update is in progress", 1057 ImsReasonInfo.CODE_LOCAL_ILLEGAL_STATE); 1058 } 1059 1060 if (mSession == null) { 1061 loge("resume :: "); 1062 throw new ImsException("No call session", 1063 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1064 } 1065 1066 mSession.resume(createResumeMediaProfile()); 1067 // FIXME: update the state on the callback? 1068 mHold = false; 1069 mUpdateRequest = UPDATE_RESUME; 1070 } 1071 } 1072 1073 /** 1074 * Merges the active & hold call. 1075 * 1076 * @see Listener#onCallMerged, Listener#onCallMergeFailed 1077 * @throws ImsException if the IMS service fails to merge the call 1078 */ 1079 public void merge() throws ImsException { 1080 if (VDBG) { 1081 logv("merge ::"); 1082 } 1083 1084 synchronized(mLockObj) { 1085 if (mUpdateRequest != UPDATE_NONE) { 1086 loge("merge :: update is in progress; request=" + 1087 updateRequestToString(mUpdateRequest)); 1088 throw new ImsException("Call update is in progress", 1089 ImsReasonInfo.CODE_LOCAL_ILLEGAL_STATE); 1090 } 1091 1092 if (mSession == null) { 1093 loge("merge :: "); 1094 throw new ImsException("No call session", 1095 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1096 } 1097 1098 // if skipHoldBeforeMerge = true, IMS service implementation will 1099 // merge without explicitly holding the call. 1100 if (mHold || (mContext.getResources().getBoolean( 1101 com.android.internal.R.bool.skipHoldBeforeMerge))) { 1102 1103 if (mMergePeer != null && !mMergePeer.isMultiparty() && !isMultiparty()) { 1104 // We only set UPDATE_MERGE when we are adding the first 1105 // calls to the Conference. If there is already a conference 1106 // no special handling is needed.The existing conference 1107 // session will just go active and any other sessions will be terminated 1108 // if needed. There will be no merge failed callback. 1109 mUpdateRequest = UPDATE_MERGE; 1110 } 1111 1112 mSession.merge(); 1113 } else { 1114 // This code basically says, we need to explicitly hold before requesting a merge 1115 // when we get the callback that the hold was successful (or failed), we should 1116 // automatically request a merge. 1117 mSession.hold(createHoldMediaProfile()); 1118 mHold = true; 1119 mUpdateRequest = UPDATE_HOLD_MERGE; 1120 } 1121 } 1122 } 1123 1124 /** 1125 * Merges the active & hold call. 1126 * 1127 * @param bgCall the background (holding) call 1128 * @see Listener#onCallMerged, Listener#onCallMergeFailed 1129 * @throws ImsException if the IMS service fails to merge the call 1130 */ 1131 public void merge(ImsCall bgCall) throws ImsException { 1132 if (VDBG) { 1133 logv("merge(1) :: bgImsCall=" + bgCall); 1134 } 1135 1136 if (bgCall == null) { 1137 throw new ImsException("No background call", 1138 ImsReasonInfo.CODE_LOCAL_ILLEGAL_ARGUMENT); 1139 } 1140 1141 synchronized(mLockObj) { 1142 if ((!isMultiparty() && !bgCall.isMultiparty()) || isMultiparty()) { 1143 // If neither call is multiparty, the current call is the merge host and the bg call 1144 // is the merge peer (ie we're starting a new conference). 1145 // OR 1146 // If this call is multiparty, it is the merge host and the other call is the merge 1147 // peer. 1148 setMergePeer(bgCall); 1149 } else { 1150 // If the bg call is multiparty, it is the merge host. 1151 setMergeHost(bgCall); 1152 } 1153 } 1154 merge(); 1155 } 1156 1157 /** 1158 * Updates the current call's properties (ex. call mode change: video upgrade / downgrade). 1159 */ 1160 public void update(int callType, ImsStreamMediaProfile mediaProfile) throws ImsException { 1161 if (VDBG) { 1162 logv("update ::"); 1163 } 1164 1165 if (isOnHold()) { 1166 if (DBG) { 1167 log("update :: call is on hold"); 1168 } 1169 throw new ImsException("Not in a call to update call", 1170 ImsReasonInfo.CODE_LOCAL_ILLEGAL_STATE); 1171 } 1172 1173 synchronized(mLockObj) { 1174 if (mUpdateRequest != UPDATE_NONE) { 1175 if (DBG) { 1176 log("update :: update is in progress; request=" + 1177 updateRequestToString(mUpdateRequest)); 1178 } 1179 throw new ImsException("Call update is in progress", 1180 ImsReasonInfo.CODE_LOCAL_ILLEGAL_STATE); 1181 } 1182 1183 if (mSession == null) { 1184 loge("update :: "); 1185 throw new ImsException("No call session", 1186 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1187 } 1188 1189 mSession.update(callType, mediaProfile); 1190 mUpdateRequest = UPDATE_UNSPECIFIED; 1191 } 1192 } 1193 1194 /** 1195 * Extends this call (1-to-1 call) to the conference call 1196 * inviting the specified participants to. 1197 * 1198 */ 1199 public void extendToConference(String[] participants) throws ImsException { 1200 if (VDBG) { 1201 logv("extendToConference ::"); 1202 } 1203 1204 if (isOnHold()) { 1205 if (DBG) { 1206 log("extendToConference :: call is on hold"); 1207 } 1208 throw new ImsException("Not in a call to extend a call to conference", 1209 ImsReasonInfo.CODE_LOCAL_ILLEGAL_STATE); 1210 } 1211 1212 synchronized(mLockObj) { 1213 if (mUpdateRequest != UPDATE_NONE) { 1214 if (DBG) { 1215 log("extendToConference :: update is in progress; request=" + 1216 updateRequestToString(mUpdateRequest)); 1217 } 1218 throw new ImsException("Call update is in progress", 1219 ImsReasonInfo.CODE_LOCAL_ILLEGAL_STATE); 1220 } 1221 1222 if (mSession == null) { 1223 loge("extendToConference :: "); 1224 throw new ImsException("No call session", 1225 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1226 } 1227 1228 mSession.extendToConference(participants); 1229 mUpdateRequest = UPDATE_EXTEND_TO_CONFERENCE; 1230 } 1231 } 1232 1233 /** 1234 * Requests the conference server to invite an additional participants to the conference. 1235 * 1236 */ 1237 public void inviteParticipants(String[] participants) throws ImsException { 1238 if (VDBG) { 1239 logv("inviteParticipants ::"); 1240 } 1241 1242 synchronized(mLockObj) { 1243 if (mSession == null) { 1244 loge("inviteParticipants :: "); 1245 throw new ImsException("No call session", 1246 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1247 } 1248 1249 mSession.inviteParticipants(participants); 1250 } 1251 } 1252 1253 /** 1254 * Requests the conference server to remove the specified participants from the conference. 1255 * 1256 */ 1257 public void removeParticipants(String[] participants) throws ImsException { 1258 if (DBG) { 1259 log("removeParticipants ::"); 1260 } 1261 1262 synchronized(mLockObj) { 1263 if (mSession == null) { 1264 loge("removeParticipants :: "); 1265 throw new ImsException("No call session", 1266 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1267 } 1268 1269 mSession.removeParticipants(participants); 1270 } 1271 } 1272 1273 1274 /** 1275 * Sends a DTMF code. According to <a href="http://tools.ietf.org/html/rfc2833">RFC 2833</a>, 1276 * event 0 ~ 9 maps to decimal value 0 ~ 9, '*' to 10, '#' to 11, event 'A' ~ 'D' to 12 ~ 15, 1277 * and event flash to 16. Currently, event flash is not supported. 1278 * 1279 * @param char that represents the DTMF digit to send. 1280 */ 1281 public void sendDtmf(char c) { 1282 sendDtmf(c, null); 1283 } 1284 1285 /** 1286 * Sends a DTMF code. According to <a href="http://tools.ietf.org/html/rfc2833">RFC 2833</a>, 1287 * event 0 ~ 9 maps to decimal value 0 ~ 9, '*' to 10, '#' to 11, event 'A' ~ 'D' to 12 ~ 15, 1288 * and event flash to 16. Currently, event flash is not supported. 1289 * 1290 * @param c that represents the DTMF to send. '0' ~ '9', 'A' ~ 'D', '*', '#' are valid inputs. 1291 * @param result the result message to send when done. 1292 */ 1293 public void sendDtmf(char c, Message result) { 1294 if (VDBG) { 1295 logv("sendDtmf :: code=" + c); 1296 } 1297 1298 synchronized(mLockObj) { 1299 if (mSession != null) { 1300 mSession.sendDtmf(c); 1301 } 1302 } 1303 1304 if (result != null) { 1305 result.sendToTarget(); 1306 } 1307 } 1308 1309 /** 1310 * Sends an USSD message. 1311 * 1312 * @param ussdMessage USSD message to send 1313 */ 1314 public void sendUssd(String ussdMessage) throws ImsException { 1315 if (VDBG) { 1316 logv("sendUssd :: ussdMessage=" + ussdMessage); 1317 } 1318 1319 synchronized(mLockObj) { 1320 if (mSession == null) { 1321 loge("sendUssd :: "); 1322 throw new ImsException("No call session", 1323 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1324 } 1325 1326 mSession.sendUssd(ussdMessage); 1327 } 1328 } 1329 1330 private void clear(ImsReasonInfo lastReasonInfo) { 1331 mInCall = false; 1332 mHold = false; 1333 mUpdateRequest = UPDATE_NONE; 1334 mLastReasonInfo = lastReasonInfo; 1335 } 1336 1337 /** 1338 * Creates an IMS call session listener. 1339 */ 1340 private ImsCallSession.Listener createCallSessionListener() { 1341 return new ImsCallSessionListenerProxy(); 1342 } 1343 1344 private ImsCall createNewCall(ImsCallSession session, ImsCallProfile profile) { 1345 ImsCall call = new ImsCall(mContext, profile); 1346 1347 try { 1348 call.attachSession(session); 1349 } catch (ImsException e) { 1350 if (call != null) { 1351 call.close(); 1352 call = null; 1353 } 1354 } 1355 1356 // Do additional operations... 1357 1358 return call; 1359 } 1360 1361 private ImsStreamMediaProfile createHoldMediaProfile() { 1362 ImsStreamMediaProfile mediaProfile = new ImsStreamMediaProfile(); 1363 1364 if (mCallProfile == null) { 1365 return mediaProfile; 1366 } 1367 1368 mediaProfile.mAudioQuality = mCallProfile.mMediaProfile.mAudioQuality; 1369 mediaProfile.mVideoQuality = mCallProfile.mMediaProfile.mVideoQuality; 1370 mediaProfile.mAudioDirection = ImsStreamMediaProfile.DIRECTION_SEND; 1371 1372 if (mediaProfile.mVideoQuality != ImsStreamMediaProfile.VIDEO_QUALITY_NONE) { 1373 mediaProfile.mVideoDirection = ImsStreamMediaProfile.DIRECTION_SEND; 1374 } 1375 1376 return mediaProfile; 1377 } 1378 1379 private ImsStreamMediaProfile createResumeMediaProfile() { 1380 ImsStreamMediaProfile mediaProfile = new ImsStreamMediaProfile(); 1381 1382 if (mCallProfile == null) { 1383 return mediaProfile; 1384 } 1385 1386 mediaProfile.mAudioQuality = mCallProfile.mMediaProfile.mAudioQuality; 1387 mediaProfile.mVideoQuality = mCallProfile.mMediaProfile.mVideoQuality; 1388 mediaProfile.mAudioDirection = ImsStreamMediaProfile.DIRECTION_SEND_RECEIVE; 1389 1390 if (mediaProfile.mVideoQuality != ImsStreamMediaProfile.VIDEO_QUALITY_NONE) { 1391 mediaProfile.mVideoDirection = ImsStreamMediaProfile.DIRECTION_SEND_RECEIVE; 1392 } 1393 1394 return mediaProfile; 1395 } 1396 1397 private void enforceConversationMode() { 1398 if (mInCall) { 1399 mHold = false; 1400 mUpdateRequest = UPDATE_NONE; 1401 } 1402 } 1403 1404 private void mergeInternal() { 1405 if (VDBG) { 1406 logv("mergeInternal ::"); 1407 } 1408 1409 mSession.merge(); 1410 mUpdateRequest = UPDATE_MERGE; 1411 } 1412 1413 private void notifyConferenceSessionTerminated(ImsReasonInfo reasonInfo) { 1414 ImsCall.Listener listener = mListener; 1415 clear(reasonInfo); 1416 1417 if (listener != null) { 1418 try { 1419 listener.onCallTerminated(this, reasonInfo); 1420 } catch (Throwable t) { 1421 loge("notifyConferenceSessionTerminated :: ", t); 1422 } 1423 } 1424 } 1425 1426 private void notifyConferenceStateUpdated(ImsConferenceState state) { 1427 Set<Entry<String, Bundle>> participants = state.mParticipants.entrySet(); 1428 1429 if (participants == null) { 1430 return; 1431 } 1432 1433 Iterator<Entry<String, Bundle>> iterator = participants.iterator(); 1434 List<ConferenceParticipant> conferenceParticipants = new ArrayList<>(participants.size()); 1435 while (iterator.hasNext()) { 1436 Entry<String, Bundle> entry = iterator.next(); 1437 1438 String key = entry.getKey(); 1439 Bundle confInfo = entry.getValue(); 1440 String status = confInfo.getString(ImsConferenceState.STATUS); 1441 String user = confInfo.getString(ImsConferenceState.USER); 1442 String displayName = confInfo.getString(ImsConferenceState.DISPLAY_TEXT); 1443 String endpoint = confInfo.getString(ImsConferenceState.ENDPOINT); 1444 1445 if (DBG) { 1446 log("notifyConferenceStateUpdated :: key=" + key + 1447 ", status=" + status + 1448 ", user=" + user + 1449 ", displayName= " + displayName + 1450 ", endpoint=" + endpoint); 1451 } 1452 1453 Uri handle = Uri.parse(user); 1454 Uri endpointUri = Uri.parse(endpoint); 1455 int connectionState = ImsConferenceState.getConnectionStateForStatus(status); 1456 1457 ConferenceParticipant conferenceParticipant = new ConferenceParticipant(handle, 1458 displayName, endpointUri, connectionState); 1459 conferenceParticipants.add(conferenceParticipant); 1460 } 1461 1462 if (!conferenceParticipants.isEmpty() && mListener != null) { 1463 try { 1464 mListener.onConferenceParticipantsStateChanged(this, conferenceParticipants); 1465 } catch (Throwable t) { 1466 loge("notifyConferenceStateUpdated :: ", t); 1467 } 1468 } 1469 } 1470 1471 /** 1472 * Perform all cleanup and notification around the termination of a session. 1473 * Note that there are 2 distinct modes of operation. The first is when 1474 * we receive a session termination on the primary session when we are 1475 * in the processing of merging. The second is when we are not merging anything 1476 * and the call is terminated. 1477 * 1478 * @param reasonInfo The reason for the session termination 1479 */ 1480 private void processCallTerminated(ImsReasonInfo reasonInfo) { 1481 if (VDBG) { 1482 String reasonString = reasonInfo != null ? reasonInfo.toString() : "null"; 1483 logv("processCallTerminated :: reason=" + reasonString); 1484 } 1485 1486 ImsCall.Listener listener = null; 1487 1488 synchronized(ImsCall.this) { 1489 // Handle the case where the original pre-merge session is terminated due to the other 1490 // party disconnecting (or some other failure). 1491 if (mUpdateRequest == UPDATE_MERGE) { 1492 // Since we are in the process of a merge, this trigger means something 1493 // else because it is probably due to the merge happening vs. the 1494 // session is really terminated. Let's flag this and revisit if 1495 // the merge() ends up failing because we will need to take action on the 1496 // mSession in that case since the termination was not due to the merge 1497 // succeeding. 1498 if (DBG) { 1499 log("processCallTerminated :: burying termination during ongoing merge."); 1500 } 1501 mSessionEndDuringMerge = true; 1502 mSessionEndDuringMergeReasonInfo = reasonInfo; 1503 return; 1504 } 1505 1506 // If in the middle of a merge and call is not the merge host, then we are being 1507 // disconnected due to the merge and should suppress the disconnect tone. 1508 if (isMerging()) { 1509 if (isMergePeer()) { 1510 setIsMerged(true); 1511 } else { 1512 // We are the host and are being legitimately disconnected. Ensure neither call 1513 // will suppress the disconnect tone. 1514 mMergePeer.setIsMerged(false); 1515 } 1516 clearMergePeer(); 1517 } 1518 1519 // If we are terminating the conference call, notify using conference listeners. 1520 if (isMultiparty()) { 1521 notifyConferenceSessionTerminated(reasonInfo); 1522 return; 1523 } else { 1524 listener = mListener; 1525 clear(reasonInfo); 1526 } 1527 } 1528 1529 if (listener != null) { 1530 try { 1531 listener.onCallTerminated(ImsCall.this, reasonInfo); 1532 } catch (Throwable t) { 1533 loge("callSessionTerminated :: ", t); 1534 } 1535 } 1536 } 1537 1538 /** 1539 * This function determines if the ImsCallSession is our actual ImsCallSession or if is 1540 * the transient session used in the process of creating a conference. This function should only 1541 * be called within callbacks that are not directly related to conference merging but might 1542 * potentially still be called on the transient ImsCallSession sent to us from 1543 * callSessionMergeStarted() when we don't really care. In those situations, we probably don't 1544 * want to take any action so we need to know that we can return early. 1545 * 1546 * @param session - The {@link ImsCallSession} that the function needs to analyze 1547 * @return true if this is the transient {@link ImsCallSession}, false otherwise. 1548 */ 1549 private boolean isTransientConferenceSession(ImsCallSession session) { 1550 if (session != null && session != mSession && session == mTransientConferenceSession) { 1551 return true; 1552 } 1553 return false; 1554 } 1555 1556 /** 1557 * We received a callback from ImsCallSession that a merge was complete. Clean up all 1558 * internal state to represent this state change. This function will be called when 1559 * the transient conference session goes active or we get an explicit merge complete 1560 * callback on the transient session. 1561 * 1562 */ 1563 private void processMergeComplete() { 1564 if (VDBG) { 1565 logv("processMergeComplete ::"); 1566 } 1567 1568 ImsCall.Listener listener; 1569 synchronized(ImsCall.this) { 1570 listener = mListener; 1571 if (mTransientConferenceSession == null) { 1572 // This is an interesting state that needs to be logged since we 1573 // should only be going through this workflow for new conference calls 1574 // and not merges into existing conferences (which a null transient 1575 // session would imply) 1576 log("processMergeComplete :: ERROR no transient session"); 1577 return; 1578 } 1579 1580 // Swap out the underlying sessions after shutting down the existing session. 1581 mSession.setListener(null); 1582 mSession = mTransientConferenceSession; 1583 mTransientConferenceSession = null; 1584 listener = mListener; 1585 1586 // Mark the merge peer call as merged so that when it terminates, the disconnect tone is 1587 // suppressed. 1588 if (mMergePeer != null) { 1589 mMergePeer.setIsMerged(true); 1590 } 1591 clearMergePeer(); 1592 1593 // Clear some flags. If the merge eventually worked, we can safely 1594 // ignore the call terminated message for the old session since we closed it already. 1595 mSessionEndDuringMerge = false; 1596 mSessionEndDuringMergeReasonInfo = null; 1597 mUpdateRequest = UPDATE_NONE; 1598 } 1599 if (listener != null) { 1600 try { 1601 listener.onCallMerged(ImsCall.this); 1602 } catch (Throwable t) { 1603 loge("processMergeComplete :: ", t); 1604 } 1605 } 1606 1607 return; 1608 } 1609 1610 /** 1611 * We received a callback from ImsCallSession that a merge failed. Clean up all 1612 * internal state to represent this state change. 1613 * 1614 * @param reasonInfo The {@link ImsReasonInfo} why the merge failed. 1615 */ 1616 private void processMergeFailed(ImsReasonInfo reasonInfo) { 1617 if (VDBG) { 1618 String reasonString = reasonInfo != null ? reasonInfo.toString() : "null"; 1619 logv("processMergeFailed :: reason=" + reasonString); 1620 } 1621 1622 ImsCall.Listener listener; 1623 boolean notifyFailure = false; 1624 ImsReasonInfo notifyFailureReasonInfo = null; 1625 1626 synchronized(ImsCall.this) { 1627 listener = mListener; 1628 if (mTransientConferenceSession != null) { 1629 // Clean up any work that we performed on the transient session. 1630 mTransientConferenceSession.setListener(null); 1631 mTransientConferenceSession = null; 1632 listener = mListener; 1633 if (mSessionEndDuringMerge) { 1634 // Set some local variables that will send out a notification about a 1635 // previously buried termination callback for our primary session now that 1636 // we know that this is not due to the conference call merging succesfully. 1637 if (DBG) { 1638 log("processMergeFailed :: following up on a terminate during the merge"); 1639 } 1640 notifyFailure = true; 1641 notifyFailureReasonInfo = mSessionEndDuringMergeReasonInfo; 1642 } 1643 } else { 1644 // This is an interesting state that needs to be logged since we 1645 // should only be going through this workflow for new conference calls 1646 // and not merges into existing conferences (which a null transient 1647 // session would imply) 1648 log("processMergeFailed - ERROR no transient session"); 1649 } 1650 mSessionEndDuringMerge = false; 1651 mSessionEndDuringMergeReasonInfo = null; 1652 mUpdateRequest = UPDATE_NONE; 1653 1654 // Ensure the call being conferenced into the conference has isMerged = false. 1655 if (isMergeHost()) { 1656 mMergePeer.setIsMerged(false); 1657 } else { 1658 setIsMerged(false); 1659 } 1660 // Unlink the two calls since they are no longer being involved in an attempted merge. 1661 clearMergePeer(); 1662 } 1663 if (listener != null) { 1664 try { 1665 // TODO: are both of these callbacks necessary? 1666 listener.onCallMergeFailed(ImsCall.this, reasonInfo); 1667 if (notifyFailure) { 1668 processCallTerminated(notifyFailureReasonInfo); 1669 } 1670 } catch (Throwable t) { 1671 loge("processMergeFailed :: ", t); 1672 } 1673 } 1674 return; 1675 } 1676 1677 private void notifyError(int reason, int statusCode, String message) { 1678 } 1679 1680 private void throwImsException(Throwable t, int code) throws ImsException { 1681 if (t instanceof ImsException) { 1682 throw (ImsException) t; 1683 } else { 1684 throw new ImsException(String.valueOf(code), t, code); 1685 } 1686 } 1687 1688 private void log(String s) { 1689 Rlog.d(TAG, s); 1690 } 1691 1692 /** 1693 * Logs the specified message, as well as the current instance of {@link ImsCall}. 1694 * 1695 * @param s The message to log. 1696 */ 1697 private void logv(String s) { 1698 StringBuilder sb = new StringBuilder(); 1699 sb.append(s); 1700 sb.append(" imsCall="); 1701 sb.append(ImsCall.this); 1702 Rlog.v(TAG, sb.toString()); 1703 } 1704 1705 private void loge(String s) { 1706 Rlog.e(TAG, s); 1707 } 1708 1709 private void loge(String s, Throwable t) { 1710 Rlog.e(TAG, s, t); 1711 } 1712 1713 private class ImsCallSessionListenerProxy extends ImsCallSession.Listener { 1714 @Override 1715 public void callSessionProgressing(ImsCallSession session, ImsStreamMediaProfile profile) { 1716 if (isTransientConferenceSession(session)) { 1717 log("callSessionProgressing :: not supported for transient conference session=" + 1718 session); 1719 return; 1720 } 1721 1722 if (VDBG) { 1723 logv("callSessionProgressing :: profile=" + profile); 1724 } 1725 1726 ImsCall.Listener listener; 1727 1728 synchronized(ImsCall.this) { 1729 listener = mListener; 1730 mCallProfile.mMediaProfile.copyFrom(profile); 1731 } 1732 1733 if (listener != null) { 1734 try { 1735 listener.onCallProgressing(ImsCall.this); 1736 } catch (Throwable t) { 1737 loge("callSessionProgressing :: ", t); 1738 } 1739 } 1740 } 1741 1742 @Override 1743 public void callSessionStarted(ImsCallSession session, ImsCallProfile profile) { 1744 if (VDBG) { 1745 logv("callSessionStarted :: profile=" + profile); 1746 } 1747 1748 if (isTransientConferenceSession(session)) { 1749 log("callSessionStarted :: transient conference session resumed session=" + 1750 session); 1751 // If we get a resume on the transient session, this means that the merge 1752 // was completed, let's process it are skip the rest of the processing in 1753 // this callback. 1754 processMergeComplete(); 1755 return; 1756 } else if (isMerging() && isMergeHost()) { 1757 // Ensure peer is set to suppress the disconnect tone. 1758 mMergePeer.setIsMerged(true); 1759 } 1760 1761 ImsCall.Listener listener; 1762 1763 synchronized(ImsCall.this) { 1764 listener = mListener; 1765 mCallProfile = profile; 1766 } 1767 1768 if (listener != null) { 1769 try { 1770 listener.onCallStarted(ImsCall.this); 1771 } catch (Throwable t) { 1772 loge("callSessionStarted :: ", t); 1773 } 1774 } 1775 } 1776 1777 @Override 1778 public void callSessionStartFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 1779 if (isTransientConferenceSession(session)) { 1780 log("callSessionStartFailed :: not supported for transient conference session=" + 1781 session); 1782 return; 1783 } 1784 1785 if (VDBG) { 1786 logv("callSessionStartFailed :: reasonInfo=" + reasonInfo); 1787 } 1788 1789 ImsCall.Listener listener; 1790 1791 synchronized(ImsCall.this) { 1792 listener = mListener; 1793 mLastReasonInfo = reasonInfo; 1794 } 1795 1796 if (listener != null) { 1797 try { 1798 listener.onCallStartFailed(ImsCall.this, reasonInfo); 1799 } catch (Throwable t) { 1800 loge("callSessionStarted :: ", t); 1801 } 1802 } 1803 } 1804 1805 @Override 1806 public void callSessionTerminated(ImsCallSession session, ImsReasonInfo reasonInfo) { 1807 if (mSession != session) { 1808 log("callSessionTerminated :: not supported for conference session=" + session); 1809 return; 1810 } 1811 1812 if (VDBG) { 1813 logv("callSessionTerminated :: reasonInfo=" + reasonInfo); 1814 } 1815 1816 processCallTerminated(reasonInfo); 1817 } 1818 1819 @Override 1820 public void callSessionHeld(ImsCallSession session, ImsCallProfile profile) { 1821 if (isTransientConferenceSession(session)) { 1822 log("callSessionHeld :: not supported for transient conference session=" + session); 1823 return; 1824 } 1825 1826 if (VDBG) { 1827 logv("callSessionHeld :: profile=" + profile); 1828 } 1829 1830 ImsCall.Listener listener; 1831 1832 synchronized(ImsCall.this) { 1833 mCallProfile = profile; 1834 1835 if (mUpdateRequest == UPDATE_HOLD_MERGE) { 1836 mergeInternal(); 1837 return; 1838 } 1839 1840 mUpdateRequest = UPDATE_NONE; 1841 listener = mListener; 1842 } 1843 1844 if (listener != null) { 1845 try { 1846 listener.onCallHeld(ImsCall.this); 1847 } catch (Throwable t) { 1848 loge("callSessionHeld :: ", t); 1849 } 1850 } 1851 } 1852 1853 @Override 1854 public void callSessionHoldFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 1855 if (isTransientConferenceSession(session)) { 1856 log("callSessionHoldFailed :: not supported for transient conference session=" + 1857 session); 1858 return; 1859 } 1860 1861 if (VDBG) { 1862 logv("callSessionHoldFailed :: reasonInfo=" + reasonInfo); 1863 } 1864 1865 boolean isHoldForMerge = false; 1866 ImsCall.Listener listener; 1867 1868 synchronized(ImsCall.this) { 1869 if (mUpdateRequest == UPDATE_HOLD_MERGE) { 1870 isHoldForMerge = true; 1871 } 1872 1873 mUpdateRequest = UPDATE_NONE; 1874 listener = mListener; 1875 } 1876 1877 if (isHoldForMerge) { 1878 // Is hold for merge implemented/supported? If so we need to take a close look 1879 // at this workflow to make sure that we handle the case where 1880 // callSessionMergeFailed() does the right thing because we have not actually 1881 // started the merge yet. 1882 callSessionMergeFailed(session, reasonInfo); 1883 return; 1884 } 1885 1886 if (listener != null) { 1887 try { 1888 listener.onCallHoldFailed(ImsCall.this, reasonInfo); 1889 } catch (Throwable t) { 1890 loge("callSessionHoldFailed :: ", t); 1891 } 1892 } 1893 } 1894 1895 @Override 1896 public void callSessionHoldReceived(ImsCallSession session, ImsCallProfile profile) { 1897 if (isTransientConferenceSession(session)) { 1898 log("callSessionHoldReceived :: not supported for transient conference session=" + 1899 session); 1900 return; 1901 } 1902 1903 if (VDBG) { 1904 logv("callSessionHoldReceived :: profile=" + profile); 1905 } 1906 1907 ImsCall.Listener listener; 1908 1909 synchronized(ImsCall.this) { 1910 listener = mListener; 1911 mCallProfile = profile; 1912 } 1913 1914 if (listener != null) { 1915 try { 1916 listener.onCallHoldReceived(ImsCall.this); 1917 } catch (Throwable t) { 1918 loge("callSessionHoldReceived :: ", t); 1919 } 1920 } 1921 } 1922 1923 @Override 1924 public void callSessionResumed(ImsCallSession session, ImsCallProfile profile) { 1925 if (isTransientConferenceSession(session)) { 1926 log("callSessionResumed :: not supported for transient conference session=" + 1927 session); 1928 return; 1929 } 1930 1931 if (VDBG) { 1932 logv("callSessionResumed :: profile=" + profile); 1933 } 1934 1935 ImsCall.Listener listener; 1936 1937 synchronized(ImsCall.this) { 1938 listener = mListener; 1939 mCallProfile = profile; 1940 mUpdateRequest = UPDATE_NONE; 1941 mHold = false; 1942 } 1943 1944 if (listener != null) { 1945 try { 1946 listener.onCallResumed(ImsCall.this); 1947 } catch (Throwable t) { 1948 loge("callSessionResumed :: ", t); 1949 } 1950 } 1951 } 1952 1953 @Override 1954 public void callSessionResumeFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 1955 if (isTransientConferenceSession(session)) { 1956 log("callSessionResumeFailed :: not supported for transient conference session=" + 1957 session); 1958 return; 1959 } 1960 1961 if (VDBG) { 1962 logv("callSessionResumeFailed :: reasonInfo=" + reasonInfo); 1963 } 1964 1965 ImsCall.Listener listener; 1966 1967 synchronized(ImsCall.this) { 1968 listener = mListener; 1969 mUpdateRequest = UPDATE_NONE; 1970 } 1971 1972 if (listener != null) { 1973 try { 1974 listener.onCallResumeFailed(ImsCall.this, reasonInfo); 1975 } catch (Throwable t) { 1976 loge("callSessionResumeFailed :: ", t); 1977 } 1978 } 1979 } 1980 1981 @Override 1982 public void callSessionResumeReceived(ImsCallSession session, ImsCallProfile profile) { 1983 if (isTransientConferenceSession(session)) { 1984 log("callSessionResumeReceived :: not supported for transient conference session=" + 1985 session); 1986 return; 1987 } 1988 1989 if (VDBG) { 1990 logv("callSessionResumeReceived :: profile=" + profile); 1991 } 1992 1993 ImsCall.Listener listener; 1994 1995 synchronized(ImsCall.this) { 1996 listener = mListener; 1997 mCallProfile = profile; 1998 } 1999 2000 if (listener != null) { 2001 try { 2002 listener.onCallResumeReceived(ImsCall.this); 2003 } catch (Throwable t) { 2004 loge("callSessionResumeReceived :: ", t); 2005 } 2006 } 2007 } 2008 2009 @Override 2010 public void callSessionMergeStarted(ImsCallSession session, 2011 ImsCallSession newSession, ImsCallProfile profile) { 2012 if (VDBG) { 2013 String newSessionString = newSession == null ? "null" : newSession.toString(); 2014 logv("callSessionMergeStarted :: newSession=" + newSessionString + 2015 ", profile=" + profile); 2016 } 2017 2018 if (mUpdateRequest != UPDATE_MERGE) { 2019 // Odd, we are not in the midst of merging anything. 2020 log("callSessionMergeStarted :: no merge in progress."); 2021 return; 2022 } 2023 2024 // There are 2 ways that we can go here. If the session that supplied the params 2025 // is not null, then it is the new session that represents the new conference 2026 // if the merge succeeds. If it is null, the merge is happening on our current 2027 // ImsCallSession. 2028 if (session == null) { 2029 // Everything is already set up and we just need to make sure 2030 // that we properly respond to all the future callbacks about 2031 // this merge. 2032 if (DBG) { 2033 log("callSessionMergeStarted :: merging into existing ImsCallSession"); 2034 } 2035 return; 2036 } 2037 2038 if (DBG) { 2039 log("callSessionMergeStarted :: setting our transient ImsCallSession"); 2040 } 2041 2042 // If we are here, this means that we are creating a new conference and 2043 // we need to do some extra work around managing a new ImsCallSession that 2044 // could represent our new ImsCallSession if the merge succeeds. 2045 synchronized(ImsCall.this) { 2046 // Keep track of this session for future callbacks to indicate success 2047 // or failure of this merge. 2048 mTransientConferenceSession = newSession; 2049 mTransientConferenceSession.setListener(createCallSessionListener()); 2050 } 2051 2052 return; 2053 } 2054 2055 @Override 2056 public void callSessionMergeComplete(ImsCallSession session) { 2057 if (VDBG) { 2058 logv("callSessionMergeComplete ::"); 2059 } 2060 if (mUpdateRequest != UPDATE_MERGE) { 2061 // Odd, we are not in the midst of merging anything. 2062 log("callSessionMergeComplete :: no merge in progress."); 2063 return; 2064 } 2065 // Let's let our parent ImsCall now that we received notification that 2066 // the merge was completed so we can set up our internal state properly 2067 processMergeComplete(); 2068 } 2069 2070 @Override 2071 public void callSessionMergeFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 2072 if (VDBG) { 2073 String reasonInfoString = reasonInfo == null ? "null" : reasonInfo.toString(); 2074 logv("callSessionMergeFailed :: reasonInfo=" + reasonInfoString); 2075 } 2076 if (mUpdateRequest != UPDATE_MERGE && !isMerging()) { 2077 // Odd, we are not in the midst of merging anything. 2078 log("callSessionMergeFailed :: no merge in progress."); 2079 return; 2080 } 2081 // Let's tell our parent ImsCall that the merge has failed and we need to clean 2082 // up any temporary, transient state. 2083 processMergeFailed(reasonInfo); 2084 } 2085 2086 @Override 2087 public void callSessionUpdated(ImsCallSession session, ImsCallProfile profile) { 2088 if (isTransientConferenceSession(session)) { 2089 log("callSessionUpdated :: not supported for transient conference session=" + 2090 session); 2091 return; 2092 } 2093 2094 if (VDBG) { 2095 logv("callSessionUpdated :: profile=" + profile); 2096 } 2097 2098 ImsCall.Listener listener; 2099 2100 synchronized(ImsCall.this) { 2101 listener = mListener; 2102 mCallProfile = profile; 2103 mUpdateRequest = UPDATE_NONE; 2104 } 2105 2106 if (listener != null) { 2107 try { 2108 listener.onCallUpdated(ImsCall.this); 2109 } catch (Throwable t) { 2110 loge("callSessionUpdated :: ", t); 2111 } 2112 } 2113 } 2114 2115 @Override 2116 public void callSessionUpdateFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 2117 if (isTransientConferenceSession(session)) { 2118 log("callSessionUpdateFailed :: not supported for transient conference session=" + 2119 session); 2120 return; 2121 } 2122 2123 if (VDBG) { 2124 logv("callSessionUpdateFailed :: reasonInfo=" + reasonInfo); 2125 } 2126 2127 ImsCall.Listener listener; 2128 2129 synchronized(ImsCall.this) { 2130 listener = mListener; 2131 mUpdateRequest = UPDATE_NONE; 2132 } 2133 2134 if (listener != null) { 2135 try { 2136 listener.onCallUpdateFailed(ImsCall.this, reasonInfo); 2137 } catch (Throwable t) { 2138 loge("callSessionUpdateFailed :: ", t); 2139 } 2140 } 2141 } 2142 2143 @Override 2144 public void callSessionUpdateReceived(ImsCallSession session, ImsCallProfile profile) { 2145 if (isTransientConferenceSession(session)) { 2146 log("callSessionUpdateReceived :: not supported for transient conference " + 2147 "session=" + session); 2148 return; 2149 } 2150 2151 if (VDBG) { 2152 logv("callSessionUpdateReceived :: profile=" + profile); 2153 } 2154 2155 ImsCall.Listener listener; 2156 2157 synchronized(ImsCall.this) { 2158 listener = mListener; 2159 mProposedCallProfile = profile; 2160 mUpdateRequest = UPDATE_UNSPECIFIED; 2161 } 2162 2163 if (listener != null) { 2164 try { 2165 listener.onCallUpdateReceived(ImsCall.this); 2166 } catch (Throwable t) { 2167 loge("callSessionUpdateReceived :: ", t); 2168 } 2169 } 2170 } 2171 2172 @Override 2173 public void callSessionConferenceExtended(ImsCallSession session, ImsCallSession newSession, 2174 ImsCallProfile profile) { 2175 if (isTransientConferenceSession(session)) { 2176 log("callSessionConferenceExtended :: not supported for transient conference " + 2177 "session=" + session); 2178 return; 2179 } 2180 2181 if (VDBG) { 2182 logv("callSessionConferenceExtended :: newSession=" + newSession + ", profile=" 2183 + profile); 2184 } 2185 2186 ImsCall newCall = createNewCall(newSession, profile); 2187 2188 if (newCall == null) { 2189 callSessionConferenceExtendFailed(session, new ImsReasonInfo()); 2190 return; 2191 } 2192 2193 ImsCall.Listener listener; 2194 2195 synchronized(ImsCall.this) { 2196 listener = mListener; 2197 mUpdateRequest = UPDATE_NONE; 2198 } 2199 2200 if (listener != null) { 2201 try { 2202 listener.onCallConferenceExtended(ImsCall.this, newCall); 2203 } catch (Throwable t) { 2204 loge("callSessionConferenceExtended :: ", t); 2205 } 2206 } 2207 } 2208 2209 @Override 2210 public void callSessionConferenceExtendFailed(ImsCallSession session, 2211 ImsReasonInfo reasonInfo) { 2212 if (isTransientConferenceSession(session)) { 2213 log("callSessionConferenceExtendFailed :: not supported for transient " + 2214 "conference session=" + session); 2215 return; 2216 } 2217 2218 if (DBG) { 2219 log("callSessionConferenceExtendFailed :: imsCall=" + ImsCall.this + 2220 ", reasonInfo=" + reasonInfo); 2221 } 2222 2223 ImsCall.Listener listener; 2224 2225 synchronized(ImsCall.this) { 2226 listener = mListener; 2227 mUpdateRequest = UPDATE_NONE; 2228 } 2229 2230 if (listener != null) { 2231 try { 2232 listener.onCallConferenceExtendFailed(ImsCall.this, reasonInfo); 2233 } catch (Throwable t) { 2234 loge("callSessionConferenceExtendFailed :: ", t); 2235 } 2236 } 2237 } 2238 2239 @Override 2240 public void callSessionConferenceExtendReceived(ImsCallSession session, 2241 ImsCallSession newSession, ImsCallProfile profile) { 2242 if (isTransientConferenceSession(session)) { 2243 log("callSessionConferenceExtendReceived :: not supported for transient " + 2244 "conference session" + session); 2245 return; 2246 } 2247 2248 if (VDBG) { 2249 logv("callSessionConferenceExtendReceived :: newSession=" + newSession + 2250 ", profile=" + profile); 2251 } 2252 2253 ImsCall newCall = createNewCall(newSession, profile); 2254 2255 if (newCall == null) { 2256 // Should all the calls be terminated...??? 2257 return; 2258 } 2259 2260 ImsCall.Listener listener; 2261 2262 synchronized(ImsCall.this) { 2263 listener = mListener; 2264 } 2265 2266 if (listener != null) { 2267 try { 2268 listener.onCallConferenceExtendReceived(ImsCall.this, newCall); 2269 } catch (Throwable t) { 2270 loge("callSessionConferenceExtendReceived :: ", t); 2271 } 2272 } 2273 } 2274 2275 @Override 2276 public void callSessionInviteParticipantsRequestDelivered(ImsCallSession session) { 2277 if (isTransientConferenceSession(session)) { 2278 log("callSessionInviteParticipantsRequestDelivered :: not supported for " + 2279 "conference session=" + session); 2280 return; 2281 } 2282 2283 if (VDBG) { 2284 logv("callSessionInviteParticipantsRequestDelivered ::"); 2285 } 2286 2287 ImsCall.Listener listener; 2288 2289 synchronized(ImsCall.this) { 2290 listener = mListener; 2291 } 2292 2293 if (listener != null) { 2294 try { 2295 listener.onCallInviteParticipantsRequestDelivered(ImsCall.this); 2296 } catch (Throwable t) { 2297 loge("callSessionInviteParticipantsRequestDelivered :: ", t); 2298 } 2299 } 2300 } 2301 2302 @Override 2303 public void callSessionInviteParticipantsRequestFailed(ImsCallSession session, 2304 ImsReasonInfo reasonInfo) { 2305 if (isTransientConferenceSession(session)) { 2306 log("callSessionInviteParticipantsRequestFailed :: not supported for " + 2307 "conference session=" + session); 2308 return; 2309 } 2310 2311 if (VDBG) { 2312 logv("callSessionInviteParticipantsRequestFailed :: reasonInfo=" + reasonInfo); 2313 } 2314 2315 ImsCall.Listener listener; 2316 2317 synchronized(ImsCall.this) { 2318 listener = mListener; 2319 } 2320 2321 if (listener != null) { 2322 try { 2323 listener.onCallInviteParticipantsRequestFailed(ImsCall.this, reasonInfo); 2324 } catch (Throwable t) { 2325 loge("callSessionInviteParticipantsRequestFailed :: ", t); 2326 } 2327 } 2328 } 2329 2330 @Override 2331 public void callSessionRemoveParticipantsRequestDelivered(ImsCallSession session) { 2332 if (isTransientConferenceSession(session)) { 2333 log("callSessionRemoveParticipantsRequestDelivered :: not supported for " + 2334 "conference session=" + session); 2335 return; 2336 } 2337 2338 if (VDBG) { 2339 logv("callSessionRemoveParticipantsRequestDelivered ::"); 2340 } 2341 2342 ImsCall.Listener listener; 2343 2344 synchronized(ImsCall.this) { 2345 listener = mListener; 2346 } 2347 2348 if (listener != null) { 2349 try { 2350 listener.onCallRemoveParticipantsRequestDelivered(ImsCall.this); 2351 } catch (Throwable t) { 2352 loge("callSessionRemoveParticipantsRequestDelivered :: ", t); 2353 } 2354 } 2355 } 2356 2357 @Override 2358 public void callSessionRemoveParticipantsRequestFailed(ImsCallSession session, 2359 ImsReasonInfo reasonInfo) { 2360 if (isTransientConferenceSession(session)) { 2361 log("callSessionRemoveParticipantsRequestFailed :: not supported for " + 2362 "conference session=" + session); 2363 return; 2364 } 2365 2366 if (VDBG) { 2367 logv("callSessionRemoveParticipantsRequestFailed :: reasonInfo=" + reasonInfo); 2368 } 2369 2370 ImsCall.Listener listener; 2371 2372 synchronized(ImsCall.this) { 2373 listener = mListener; 2374 } 2375 2376 if (listener != null) { 2377 try { 2378 listener.onCallRemoveParticipantsRequestFailed(ImsCall.this, reasonInfo); 2379 } catch (Throwable t) { 2380 loge("callSessionRemoveParticipantsRequestFailed :: ", t); 2381 } 2382 } 2383 } 2384 2385 @Override 2386 public void callSessionConferenceStateUpdated(ImsCallSession session, 2387 ImsConferenceState state) { 2388 if (isTransientConferenceSession(session)) { 2389 log("callSessionConferenceStateUpdated :: not supported for transient " + 2390 "conference session=" + session); 2391 return; 2392 } 2393 2394 if (VDBG) { 2395 logv("callSessionConferenceStateUpdated :: state=" + state); 2396 } 2397 2398 conferenceStateUpdated(state); 2399 } 2400 2401 @Override 2402 public void callSessionUssdMessageReceived(ImsCallSession session, int mode, 2403 String ussdMessage) { 2404 if (isTransientConferenceSession(session)) { 2405 log("callSessionUssdMessageReceived :: not supported for transient " + 2406 "conference session=" + session); 2407 return; 2408 } 2409 2410 if (VDBG) { 2411 logv("callSessionUssdMessageReceived :: mode=" + mode + ", ussdMessage=" + 2412 ussdMessage); 2413 } 2414 2415 ImsCall.Listener listener; 2416 2417 synchronized(ImsCall.this) { 2418 listener = mListener; 2419 } 2420 2421 if (listener != null) { 2422 try { 2423 listener.onCallUssdMessageReceived(ImsCall.this, mode, ussdMessage); 2424 } catch (Throwable t) { 2425 loge("callSessionUssdMessageReceived :: ", t); 2426 } 2427 } 2428 } 2429 2430 @Override 2431 public void callSessionTtyModeReceived(ImsCallSession session, int mode) { 2432 if (VDBG) { 2433 logv("callSessionTtyModeReceived :: mode=" + mode); 2434 } 2435 2436 int settingsTtyMode = Settings.Secure.getInt( 2437 mContext.getContentResolver(), 2438 Settings.Secure.PREFERRED_TTY_MODE, 2439 TelecomManager.TTY_MODE_OFF); 2440 if (settingsTtyMode == TelecomManager.TTY_MODE_OFF) { 2441 // Notify the user that TTY mode changed in the far device 2442 int resId = 0; 2443 switch (mode) { 2444 case TelecomManager.TTY_MODE_FULL: 2445 resId = com.android.internal.R.string.peerTtyModeFull; 2446 break; 2447 case TelecomManager.TTY_MODE_HCO: 2448 resId = com.android.internal.R.string.peerTtyModeHco; 2449 break; 2450 case TelecomManager.TTY_MODE_VCO: 2451 resId = com.android.internal.R.string.peerTtyModeVco; 2452 break; 2453 case TelecomManager.TTY_MODE_OFF: 2454 resId = com.android.internal.R.string.peerTtyModeOff; 2455 break; 2456 } 2457 if (resId != 0) { 2458 Toast.makeText(mContext, resId, Toast.LENGTH_SHORT).show(); 2459 } 2460 } 2461 } 2462 } 2463 2464 /** 2465 * Report a new conference state to the current {@link ImsCall} and inform listeners of the 2466 * change. Marked as {@code VisibleForTesting} so that the 2467 * {@code com.android.internal.telephony.TelephonyTester} class can inject a test conference 2468 * event package into a regular ongoing IMS call. 2469 * 2470 * @param state The {@link ImsConferenceState}. 2471 */ 2472 @VisibleForTesting 2473 public void conferenceStateUpdated(ImsConferenceState state) { 2474 Listener listener; 2475 2476 synchronized(this) { 2477 notifyConferenceStateUpdated(state); 2478 listener = mListener; 2479 } 2480 2481 if (listener != null) { 2482 try { 2483 listener.onCallConferenceStateUpdated(this, state); 2484 } catch (Throwable t) { 2485 loge("callSessionConferenceStateUpdated :: ", t); 2486 } 2487 } 2488 } 2489 2490 /** 2491 * Provides a human-readable string representation of an update request. 2492 * 2493 * @param updateRequest The update request. 2494 * @return The string representation. 2495 */ 2496 private String updateRequestToString(int updateRequest) { 2497 switch (updateRequest) { 2498 case UPDATE_NONE: 2499 return "NONE"; 2500 case UPDATE_HOLD: 2501 return "HOLD"; 2502 case UPDATE_HOLD_MERGE: 2503 return "HOLD_MERGE"; 2504 case UPDATE_RESUME: 2505 return "RESUME"; 2506 case UPDATE_MERGE: 2507 return "MERGE"; 2508 case UPDATE_EXTEND_TO_CONFERENCE: 2509 return "EXTEND_TO_CONFERENCE"; 2510 case UPDATE_UNSPECIFIED: 2511 return "UNSPECIFIED"; 2512 default: 2513 return "UNKNOWN"; 2514 } 2515 } 2516 2517 /** 2518 * Clears the merge peer for this call, ensuring that the peer's connection to this call is also 2519 * severed at the same time. 2520 */ 2521 private void clearMergePeer() { 2522 if (mMergeHost != null) { 2523 mMergeHost.mMergePeer = null; 2524 mMergeHost = null; 2525 } 2526 2527 if (mMergePeer != null) { 2528 mMergePeer.mMergeHost = null; 2529 mMergePeer = null; 2530 } 2531 } 2532 2533 /** 2534 * Sets the merge peer for the current call. The merge peer is the background call that will be 2535 * merged into this call. On the merge peer, sets the merge host to be this call. 2536 * 2537 * @param mergePeer The peer call to be merged into this one. 2538 */ 2539 private void setMergePeer(ImsCall mergePeer) { 2540 mMergePeer = mergePeer; 2541 mMergeHost = null; 2542 2543 mergePeer.mMergeHost = ImsCall.this; 2544 mergePeer.mMergePeer = null; 2545 } 2546 2547 /** 2548 * Sets the merge hody for the current call. The merge host is the foreground call this call 2549 * will be merged into. On the merge host, sets the merge peer to be this call. 2550 * 2551 * @param mergeHost The merge host this call will be merged into. 2552 */ 2553 public void setMergeHost(ImsCall mergeHost) { 2554 mMergeHost = mergeHost; 2555 mMergePeer = null; 2556 2557 mergeHost.mMergeHost = null; 2558 mergeHost.mMergePeer = ImsCall.this; 2559 } 2560 2561 /** 2562 * Determines if the current call is in the process of merging with another call or conference. 2563 * 2564 * @return {@code true} if in the process of merging. 2565 */ 2566 private boolean isMerging() { 2567 return mMergePeer != null || mMergeHost != null; 2568 } 2569 2570 /** 2571 * Determines if the current call is the host of the merge. 2572 * 2573 * @return {@code true} if the call is the merge host. 2574 */ 2575 private boolean isMergeHost() { 2576 return mMergePeer != null && mMergeHost == null; 2577 } 2578 2579 /** 2580 * Determines if the current call is the peer of the merge. 2581 * 2582 * @return {@code true} if the call is the merge peer. 2583 */ 2584 private boolean isMergePeer() { 2585 return mMergePeer == null && mMergeHost != null; 2586 } 2587 2588 /** 2589 * Provides a string representation of the {@link ImsCall}. Primarily intended for use in log 2590 * statements. 2591 * 2592 * @return String representation of call. 2593 */ 2594 @Override 2595 public String toString() { 2596 StringBuilder sb = new StringBuilder(); 2597 sb.append("[ImsCall objId:"); 2598 sb.append(System.identityHashCode(this)); 2599 sb.append(" onHold:"); 2600 sb.append(isOnHold() ? "Y" : "N"); 2601 sb.append(" mute:"); 2602 sb.append(isMuted() ? "Y" : "N"); 2603 sb.append(" updateRequest:"); 2604 sb.append(updateRequestToString(mUpdateRequest)); 2605 sb.append(" merging:"); 2606 sb.append(isMerging() ? "Y" : "N"); 2607 if (isMerging()) { 2608 if (isMergePeer()) { 2609 sb.append("P"); 2610 } else { 2611 sb.append("H"); 2612 } 2613 } 2614 sb.append(" merged:"); 2615 sb.append(isMerged() ? "Y" : "N"); 2616 sb.append(" multiParty:"); 2617 sb.append(isMultiparty() ? "Y" : "N"); 2618 sb.append(" session:"); 2619 sb.append(mSession); 2620 sb.append(" transientSession:"); 2621 sb.append(mTransientConferenceSession); 2622 sb.append("]"); 2623 return sb.toString(); 2624 } 2625} 2626