ImsCall.java revision a4710d5d926d8112179acece620f49cb79257e98
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 * Sends a DTMF code. According to <a href="http://tools.ietf.org/html/rfc2833">RFC 2833</a>, 1275 * event 0 ~ 9 maps to decimal value 0 ~ 9, '*' to 10, '#' to 11, event 'A' ~ 'D' to 12 ~ 15, 1276 * and event flash to 16. Currently, event flash is not supported. 1277 * 1278 * @param c that represents the DTMF to send. '0' ~ '9', 'A' ~ 'D', '*', '#' are valid inputs. 1279 * @param result the result message to send when done. 1280 */ 1281 public void sendDtmf(char c, Message result) { 1282 if (VDBG) { 1283 logv("sendDtmf :: code=" + c); 1284 } 1285 1286 synchronized(mLockObj) { 1287 if (mSession != null) { 1288 mSession.sendDtmf(c, result); 1289 } 1290 } 1291 } 1292 1293 /** 1294 * Start a DTMF code. According to <a href="http://tools.ietf.org/html/rfc2833">RFC 2833</a>, 1295 * event 0 ~ 9 maps to decimal value 0 ~ 9, '*' to 10, '#' to 11, event 'A' ~ 'D' to 12 ~ 15, 1296 * and event flash to 16. Currently, event flash is not supported. 1297 * 1298 * @param c that represents the DTMF to send. '0' ~ '9', 'A' ~ 'D', '*', '#' are valid inputs. 1299 */ 1300 public void startDtmf(char c) { 1301 if (DBG) { 1302 log("startDtmf :: session=" + mSession + ", code=" + c); 1303 } 1304 1305 synchronized(mLockObj) { 1306 if (mSession != null) { 1307 mSession.startDtmf(c); 1308 } 1309 } 1310 } 1311 1312 /** 1313 * Stop a DTMF code. 1314 */ 1315 public void stopDtmf() { 1316 if (DBG) { 1317 log("stopDtmf :: session=" + mSession); 1318 } 1319 1320 synchronized(mLockObj) { 1321 if (mSession != null) { 1322 mSession.stopDtmf(); 1323 } 1324 } 1325 } 1326 1327 /** 1328 * Sends an USSD message. 1329 * 1330 * @param ussdMessage USSD message to send 1331 */ 1332 public void sendUssd(String ussdMessage) throws ImsException { 1333 if (VDBG) { 1334 logv("sendUssd :: ussdMessage=" + ussdMessage); 1335 } 1336 1337 synchronized(mLockObj) { 1338 if (mSession == null) { 1339 loge("sendUssd :: "); 1340 throw new ImsException("No call session", 1341 ImsReasonInfo.CODE_LOCAL_CALL_TERMINATED); 1342 } 1343 1344 mSession.sendUssd(ussdMessage); 1345 } 1346 } 1347 1348 private void clear(ImsReasonInfo lastReasonInfo) { 1349 mInCall = false; 1350 mHold = false; 1351 mUpdateRequest = UPDATE_NONE; 1352 mLastReasonInfo = lastReasonInfo; 1353 } 1354 1355 /** 1356 * Creates an IMS call session listener. 1357 */ 1358 private ImsCallSession.Listener createCallSessionListener() { 1359 return new ImsCallSessionListenerProxy(); 1360 } 1361 1362 private ImsCall createNewCall(ImsCallSession session, ImsCallProfile profile) { 1363 ImsCall call = new ImsCall(mContext, profile); 1364 1365 try { 1366 call.attachSession(session); 1367 } catch (ImsException e) { 1368 if (call != null) { 1369 call.close(); 1370 call = null; 1371 } 1372 } 1373 1374 // Do additional operations... 1375 1376 return call; 1377 } 1378 1379 private ImsStreamMediaProfile createHoldMediaProfile() { 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; 1389 1390 if (mediaProfile.mVideoQuality != ImsStreamMediaProfile.VIDEO_QUALITY_NONE) { 1391 mediaProfile.mVideoDirection = ImsStreamMediaProfile.DIRECTION_SEND; 1392 } 1393 1394 return mediaProfile; 1395 } 1396 1397 private ImsStreamMediaProfile createResumeMediaProfile() { 1398 ImsStreamMediaProfile mediaProfile = new ImsStreamMediaProfile(); 1399 1400 if (mCallProfile == null) { 1401 return mediaProfile; 1402 } 1403 1404 mediaProfile.mAudioQuality = mCallProfile.mMediaProfile.mAudioQuality; 1405 mediaProfile.mVideoQuality = mCallProfile.mMediaProfile.mVideoQuality; 1406 mediaProfile.mAudioDirection = ImsStreamMediaProfile.DIRECTION_SEND_RECEIVE; 1407 1408 if (mediaProfile.mVideoQuality != ImsStreamMediaProfile.VIDEO_QUALITY_NONE) { 1409 mediaProfile.mVideoDirection = ImsStreamMediaProfile.DIRECTION_SEND_RECEIVE; 1410 } 1411 1412 return mediaProfile; 1413 } 1414 1415 private void enforceConversationMode() { 1416 if (mInCall) { 1417 mHold = false; 1418 mUpdateRequest = UPDATE_NONE; 1419 } 1420 } 1421 1422 private void mergeInternal() { 1423 if (VDBG) { 1424 logv("mergeInternal ::"); 1425 } 1426 1427 mSession.merge(); 1428 mUpdateRequest = UPDATE_MERGE; 1429 } 1430 1431 private void notifyConferenceSessionTerminated(ImsReasonInfo reasonInfo) { 1432 ImsCall.Listener listener = mListener; 1433 clear(reasonInfo); 1434 1435 if (listener != null) { 1436 try { 1437 listener.onCallTerminated(this, reasonInfo); 1438 } catch (Throwable t) { 1439 loge("notifyConferenceSessionTerminated :: ", t); 1440 } 1441 } 1442 } 1443 1444 private void notifyConferenceStateUpdated(ImsConferenceState state) { 1445 Set<Entry<String, Bundle>> participants = state.mParticipants.entrySet(); 1446 1447 if (participants == null) { 1448 return; 1449 } 1450 1451 Iterator<Entry<String, Bundle>> iterator = participants.iterator(); 1452 List<ConferenceParticipant> conferenceParticipants = new ArrayList<>(participants.size()); 1453 while (iterator.hasNext()) { 1454 Entry<String, Bundle> entry = iterator.next(); 1455 1456 String key = entry.getKey(); 1457 Bundle confInfo = entry.getValue(); 1458 String status = confInfo.getString(ImsConferenceState.STATUS); 1459 String user = confInfo.getString(ImsConferenceState.USER); 1460 String displayName = confInfo.getString(ImsConferenceState.DISPLAY_TEXT); 1461 String endpoint = confInfo.getString(ImsConferenceState.ENDPOINT); 1462 1463 if (DBG) { 1464 log("notifyConferenceStateUpdated :: key=" + key + 1465 ", status=" + status + 1466 ", user=" + user + 1467 ", displayName= " + displayName + 1468 ", endpoint=" + endpoint); 1469 } 1470 1471 Uri handle = Uri.parse(user); 1472 Uri endpointUri = Uri.parse(endpoint); 1473 int connectionState = ImsConferenceState.getConnectionStateForStatus(status); 1474 1475 ConferenceParticipant conferenceParticipant = new ConferenceParticipant(handle, 1476 displayName, endpointUri, connectionState); 1477 conferenceParticipants.add(conferenceParticipant); 1478 } 1479 1480 if (!conferenceParticipants.isEmpty() && mListener != null) { 1481 try { 1482 mListener.onConferenceParticipantsStateChanged(this, conferenceParticipants); 1483 } catch (Throwable t) { 1484 loge("notifyConferenceStateUpdated :: ", t); 1485 } 1486 } 1487 } 1488 1489 /** 1490 * Perform all cleanup and notification around the termination of a session. 1491 * Note that there are 2 distinct modes of operation. The first is when 1492 * we receive a session termination on the primary session when we are 1493 * in the processing of merging. The second is when we are not merging anything 1494 * and the call is terminated. 1495 * 1496 * @param reasonInfo The reason for the session termination 1497 */ 1498 private void processCallTerminated(ImsReasonInfo reasonInfo) { 1499 if (VDBG) { 1500 String reasonString = reasonInfo != null ? reasonInfo.toString() : "null"; 1501 logv("processCallTerminated :: reason=" + reasonString); 1502 } 1503 1504 ImsCall.Listener listener = null; 1505 1506 synchronized(ImsCall.this) { 1507 // Handle the case where the original pre-merge session is terminated due to the other 1508 // party disconnecting (or some other failure). 1509 if (mUpdateRequest == UPDATE_MERGE) { 1510 // Since we are in the process of a merge, this trigger means something 1511 // else because it is probably due to the merge happening vs. the 1512 // session is really terminated. Let's flag this and revisit if 1513 // the merge() ends up failing because we will need to take action on the 1514 // mSession in that case since the termination was not due to the merge 1515 // succeeding. 1516 if (DBG) { 1517 log("processCallTerminated :: burying termination during ongoing merge."); 1518 } 1519 mSessionEndDuringMerge = true; 1520 mSessionEndDuringMergeReasonInfo = reasonInfo; 1521 return; 1522 } 1523 1524 // If in the middle of a merge and call is not the merge host, then we are being 1525 // disconnected due to the merge and should suppress the disconnect tone. 1526 if (isMerging()) { 1527 if (isMergePeer()) { 1528 setIsMerged(true); 1529 } else { 1530 // We are the host and are being legitimately disconnected. Ensure neither call 1531 // will suppress the disconnect tone. 1532 mMergePeer.setIsMerged(false); 1533 } 1534 clearMergePeer(); 1535 } 1536 1537 // If we are terminating the conference call, notify using conference listeners. 1538 if (isMultiparty()) { 1539 notifyConferenceSessionTerminated(reasonInfo); 1540 return; 1541 } else { 1542 listener = mListener; 1543 clear(reasonInfo); 1544 } 1545 } 1546 1547 if (listener != null) { 1548 try { 1549 listener.onCallTerminated(ImsCall.this, reasonInfo); 1550 } catch (Throwable t) { 1551 loge("callSessionTerminated :: ", t); 1552 } 1553 } 1554 } 1555 1556 /** 1557 * This function determines if the ImsCallSession is our actual ImsCallSession or if is 1558 * the transient session used in the process of creating a conference. This function should only 1559 * be called within callbacks that are not directly related to conference merging but might 1560 * potentially still be called on the transient ImsCallSession sent to us from 1561 * callSessionMergeStarted() when we don't really care. In those situations, we probably don't 1562 * want to take any action so we need to know that we can return early. 1563 * 1564 * @param session - The {@link ImsCallSession} that the function needs to analyze 1565 * @return true if this is the transient {@link ImsCallSession}, false otherwise. 1566 */ 1567 private boolean isTransientConferenceSession(ImsCallSession session) { 1568 if (session != null && session != mSession && session == mTransientConferenceSession) { 1569 return true; 1570 } 1571 return false; 1572 } 1573 1574 /** 1575 * We received a callback from ImsCallSession that a merge was complete. Clean up all 1576 * internal state to represent this state change. This function will be called when 1577 * the transient conference session goes active or we get an explicit merge complete 1578 * callback on the transient session. 1579 * 1580 */ 1581 private void processMergeComplete() { 1582 if (VDBG) { 1583 logv("processMergeComplete ::"); 1584 } 1585 1586 ImsCall.Listener listener; 1587 synchronized(ImsCall.this) { 1588 listener = mListener; 1589 if (mTransientConferenceSession == null) { 1590 // This is an interesting state that needs to be logged since we 1591 // should only be going through this workflow for new conference calls 1592 // and not merges into existing conferences (which a null transient 1593 // session would imply) 1594 log("processMergeComplete :: ERROR no transient session"); 1595 return; 1596 } 1597 1598 // Swap out the underlying sessions after shutting down the existing session. 1599 mSession.setListener(null); 1600 mSession = mTransientConferenceSession; 1601 mTransientConferenceSession = null; 1602 listener = mListener; 1603 1604 // Mark the merge peer call as merged so that when it terminates, the disconnect tone is 1605 // suppressed. 1606 if (mMergePeer != null) { 1607 mMergePeer.setIsMerged(true); 1608 } 1609 clearMergePeer(); 1610 1611 // Clear some flags. If the merge eventually worked, we can safely 1612 // ignore the call terminated message for the old session since we closed it already. 1613 mSessionEndDuringMerge = false; 1614 mSessionEndDuringMergeReasonInfo = null; 1615 mUpdateRequest = UPDATE_NONE; 1616 } 1617 if (listener != null) { 1618 try { 1619 listener.onCallMerged(ImsCall.this); 1620 } catch (Throwable t) { 1621 loge("processMergeComplete :: ", t); 1622 } 1623 } 1624 1625 return; 1626 } 1627 1628 /** 1629 * We received a callback from ImsCallSession that a merge failed. Clean up all 1630 * internal state to represent this state change. 1631 * 1632 * @param reasonInfo The {@link ImsReasonInfo} why the merge failed. 1633 */ 1634 private void processMergeFailed(ImsReasonInfo reasonInfo) { 1635 if (VDBG) { 1636 String reasonString = reasonInfo != null ? reasonInfo.toString() : "null"; 1637 logv("processMergeFailed :: reason=" + reasonString); 1638 } 1639 1640 ImsCall.Listener listener; 1641 boolean notifyFailure = false; 1642 ImsReasonInfo notifyFailureReasonInfo = null; 1643 1644 synchronized(ImsCall.this) { 1645 listener = mListener; 1646 if (mTransientConferenceSession != null) { 1647 // Clean up any work that we performed on the transient session. 1648 mTransientConferenceSession.setListener(null); 1649 mTransientConferenceSession = null; 1650 listener = mListener; 1651 if (mSessionEndDuringMerge) { 1652 // Set some local variables that will send out a notification about a 1653 // previously buried termination callback for our primary session now that 1654 // we know that this is not due to the conference call merging succesfully. 1655 if (DBG) { 1656 log("processMergeFailed :: following up on a terminate during the merge"); 1657 } 1658 notifyFailure = true; 1659 notifyFailureReasonInfo = mSessionEndDuringMergeReasonInfo; 1660 } 1661 } else { 1662 // This is an interesting state that needs to be logged since we 1663 // should only be going through this workflow for new conference calls 1664 // and not merges into existing conferences (which a null transient 1665 // session would imply) 1666 log("processMergeFailed - ERROR no transient session"); 1667 } 1668 mSessionEndDuringMerge = false; 1669 mSessionEndDuringMergeReasonInfo = null; 1670 mUpdateRequest = UPDATE_NONE; 1671 1672 // Ensure the call being conferenced into the conference has isMerged = false. 1673 if (isMergeHost()) { 1674 mMergePeer.setIsMerged(false); 1675 } else { 1676 setIsMerged(false); 1677 } 1678 // Unlink the two calls since they are no longer being involved in an attempted merge. 1679 clearMergePeer(); 1680 } 1681 if (listener != null) { 1682 try { 1683 // TODO: are both of these callbacks necessary? 1684 listener.onCallMergeFailed(ImsCall.this, reasonInfo); 1685 if (notifyFailure) { 1686 processCallTerminated(notifyFailureReasonInfo); 1687 } 1688 } catch (Throwable t) { 1689 loge("processMergeFailed :: ", t); 1690 } 1691 } 1692 return; 1693 } 1694 1695 private void notifyError(int reason, int statusCode, String message) { 1696 } 1697 1698 private void throwImsException(Throwable t, int code) throws ImsException { 1699 if (t instanceof ImsException) { 1700 throw (ImsException) t; 1701 } else { 1702 throw new ImsException(String.valueOf(code), t, code); 1703 } 1704 } 1705 1706 private void log(String s) { 1707 Rlog.d(TAG, s); 1708 } 1709 1710 /** 1711 * Logs the specified message, as well as the current instance of {@link ImsCall}. 1712 * 1713 * @param s The message to log. 1714 */ 1715 private void logv(String s) { 1716 StringBuilder sb = new StringBuilder(); 1717 sb.append(s); 1718 sb.append(" imsCall="); 1719 sb.append(ImsCall.this); 1720 Rlog.v(TAG, sb.toString()); 1721 } 1722 1723 private void loge(String s) { 1724 Rlog.e(TAG, s); 1725 } 1726 1727 private void loge(String s, Throwable t) { 1728 Rlog.e(TAG, s, t); 1729 } 1730 1731 private class ImsCallSessionListenerProxy extends ImsCallSession.Listener { 1732 @Override 1733 public void callSessionProgressing(ImsCallSession session, ImsStreamMediaProfile profile) { 1734 if (isTransientConferenceSession(session)) { 1735 log("callSessionProgressing :: not supported for transient conference session=" + 1736 session); 1737 return; 1738 } 1739 1740 if (VDBG) { 1741 logv("callSessionProgressing :: profile=" + profile); 1742 } 1743 1744 ImsCall.Listener listener; 1745 1746 synchronized(ImsCall.this) { 1747 listener = mListener; 1748 mCallProfile.mMediaProfile.copyFrom(profile); 1749 } 1750 1751 if (listener != null) { 1752 try { 1753 listener.onCallProgressing(ImsCall.this); 1754 } catch (Throwable t) { 1755 loge("callSessionProgressing :: ", t); 1756 } 1757 } 1758 } 1759 1760 @Override 1761 public void callSessionStarted(ImsCallSession session, ImsCallProfile profile) { 1762 if (VDBG) { 1763 logv("callSessionStarted :: profile=" + profile); 1764 } 1765 1766 if (isTransientConferenceSession(session)) { 1767 log("callSessionStarted :: transient conference session resumed session=" + 1768 session); 1769 // If we get a resume on the transient session, this means that the merge 1770 // was completed, let's process it are skip the rest of the processing in 1771 // this callback. 1772 processMergeComplete(); 1773 return; 1774 } else if (isMerging() && isMergeHost()) { 1775 // Ensure peer is set to suppress the disconnect tone. 1776 mMergePeer.setIsMerged(true); 1777 } 1778 1779 ImsCall.Listener listener; 1780 1781 synchronized(ImsCall.this) { 1782 listener = mListener; 1783 mCallProfile = profile; 1784 } 1785 1786 if (listener != null) { 1787 try { 1788 listener.onCallStarted(ImsCall.this); 1789 } catch (Throwable t) { 1790 loge("callSessionStarted :: ", t); 1791 } 1792 } 1793 } 1794 1795 @Override 1796 public void callSessionStartFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 1797 if (isTransientConferenceSession(session)) { 1798 log("callSessionStartFailed :: not supported for transient conference session=" + 1799 session); 1800 return; 1801 } 1802 1803 if (VDBG) { 1804 logv("callSessionStartFailed :: reasonInfo=" + reasonInfo); 1805 } 1806 1807 ImsCall.Listener listener; 1808 1809 synchronized(ImsCall.this) { 1810 listener = mListener; 1811 mLastReasonInfo = reasonInfo; 1812 } 1813 1814 if (listener != null) { 1815 try { 1816 listener.onCallStartFailed(ImsCall.this, reasonInfo); 1817 } catch (Throwable t) { 1818 loge("callSessionStarted :: ", t); 1819 } 1820 } 1821 } 1822 1823 @Override 1824 public void callSessionTerminated(ImsCallSession session, ImsReasonInfo reasonInfo) { 1825 if (mSession != session) { 1826 log("callSessionTerminated :: not supported for conference session=" + session); 1827 return; 1828 } 1829 1830 if (VDBG) { 1831 logv("callSessionTerminated :: reasonInfo=" + reasonInfo); 1832 } 1833 1834 processCallTerminated(reasonInfo); 1835 } 1836 1837 @Override 1838 public void callSessionHeld(ImsCallSession session, ImsCallProfile profile) { 1839 if (isTransientConferenceSession(session)) { 1840 log("callSessionHeld :: not supported for transient conference session=" + session); 1841 return; 1842 } 1843 1844 if (VDBG) { 1845 logv("callSessionHeld :: profile=" + profile); 1846 } 1847 1848 ImsCall.Listener listener; 1849 1850 synchronized(ImsCall.this) { 1851 mCallProfile = profile; 1852 1853 if (mUpdateRequest == UPDATE_HOLD_MERGE) { 1854 mergeInternal(); 1855 return; 1856 } 1857 1858 mUpdateRequest = UPDATE_NONE; 1859 listener = mListener; 1860 } 1861 1862 if (listener != null) { 1863 try { 1864 listener.onCallHeld(ImsCall.this); 1865 } catch (Throwable t) { 1866 loge("callSessionHeld :: ", t); 1867 } 1868 } 1869 } 1870 1871 @Override 1872 public void callSessionHoldFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 1873 if (isTransientConferenceSession(session)) { 1874 log("callSessionHoldFailed :: not supported for transient conference session=" + 1875 session); 1876 return; 1877 } 1878 1879 if (VDBG) { 1880 logv("callSessionHoldFailed :: reasonInfo=" + reasonInfo); 1881 } 1882 1883 boolean isHoldForMerge = false; 1884 ImsCall.Listener listener; 1885 1886 synchronized(ImsCall.this) { 1887 if (mUpdateRequest == UPDATE_HOLD_MERGE) { 1888 isHoldForMerge = true; 1889 } 1890 1891 mUpdateRequest = UPDATE_NONE; 1892 listener = mListener; 1893 } 1894 1895 if (isHoldForMerge) { 1896 // Is hold for merge implemented/supported? If so we need to take a close look 1897 // at this workflow to make sure that we handle the case where 1898 // callSessionMergeFailed() does the right thing because we have not actually 1899 // started the merge yet. 1900 callSessionMergeFailed(session, reasonInfo); 1901 return; 1902 } 1903 1904 if (listener != null) { 1905 try { 1906 listener.onCallHoldFailed(ImsCall.this, reasonInfo); 1907 } catch (Throwable t) { 1908 loge("callSessionHoldFailed :: ", t); 1909 } 1910 } 1911 } 1912 1913 @Override 1914 public void callSessionHoldReceived(ImsCallSession session, ImsCallProfile profile) { 1915 if (isTransientConferenceSession(session)) { 1916 log("callSessionHoldReceived :: not supported for transient conference session=" + 1917 session); 1918 return; 1919 } 1920 1921 if (VDBG) { 1922 logv("callSessionHoldReceived :: profile=" + profile); 1923 } 1924 1925 ImsCall.Listener listener; 1926 1927 synchronized(ImsCall.this) { 1928 listener = mListener; 1929 mCallProfile = profile; 1930 } 1931 1932 if (listener != null) { 1933 try { 1934 listener.onCallHoldReceived(ImsCall.this); 1935 } catch (Throwable t) { 1936 loge("callSessionHoldReceived :: ", t); 1937 } 1938 } 1939 } 1940 1941 @Override 1942 public void callSessionResumed(ImsCallSession session, ImsCallProfile profile) { 1943 if (isTransientConferenceSession(session)) { 1944 log("callSessionResumed :: not supported for transient conference session=" + 1945 session); 1946 return; 1947 } 1948 1949 if (VDBG) { 1950 logv("callSessionResumed :: profile=" + profile); 1951 } 1952 1953 ImsCall.Listener listener; 1954 1955 synchronized(ImsCall.this) { 1956 listener = mListener; 1957 mCallProfile = profile; 1958 mUpdateRequest = UPDATE_NONE; 1959 mHold = false; 1960 } 1961 1962 if (listener != null) { 1963 try { 1964 listener.onCallResumed(ImsCall.this); 1965 } catch (Throwable t) { 1966 loge("callSessionResumed :: ", t); 1967 } 1968 } 1969 } 1970 1971 @Override 1972 public void callSessionResumeFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 1973 if (isTransientConferenceSession(session)) { 1974 log("callSessionResumeFailed :: not supported for transient conference session=" + 1975 session); 1976 return; 1977 } 1978 1979 if (VDBG) { 1980 logv("callSessionResumeFailed :: reasonInfo=" + reasonInfo); 1981 } 1982 1983 ImsCall.Listener listener; 1984 1985 synchronized(ImsCall.this) { 1986 listener = mListener; 1987 mUpdateRequest = UPDATE_NONE; 1988 } 1989 1990 if (listener != null) { 1991 try { 1992 listener.onCallResumeFailed(ImsCall.this, reasonInfo); 1993 } catch (Throwable t) { 1994 loge("callSessionResumeFailed :: ", t); 1995 } 1996 } 1997 } 1998 1999 @Override 2000 public void callSessionResumeReceived(ImsCallSession session, ImsCallProfile profile) { 2001 if (isTransientConferenceSession(session)) { 2002 log("callSessionResumeReceived :: not supported for transient conference session=" + 2003 session); 2004 return; 2005 } 2006 2007 if (VDBG) { 2008 logv("callSessionResumeReceived :: profile=" + profile); 2009 } 2010 2011 ImsCall.Listener listener; 2012 2013 synchronized(ImsCall.this) { 2014 listener = mListener; 2015 mCallProfile = profile; 2016 } 2017 2018 if (listener != null) { 2019 try { 2020 listener.onCallResumeReceived(ImsCall.this); 2021 } catch (Throwable t) { 2022 loge("callSessionResumeReceived :: ", t); 2023 } 2024 } 2025 } 2026 2027 @Override 2028 public void callSessionMergeStarted(ImsCallSession session, 2029 ImsCallSession newSession, ImsCallProfile profile) { 2030 if (VDBG) { 2031 String newSessionString = newSession == null ? "null" : newSession.toString(); 2032 logv("callSessionMergeStarted :: newSession=" + newSessionString + 2033 ", profile=" + profile); 2034 } 2035 2036 if (mUpdateRequest != UPDATE_MERGE) { 2037 // Odd, we are not in the midst of merging anything. 2038 log("callSessionMergeStarted :: no merge in progress."); 2039 return; 2040 } 2041 2042 // There are 2 ways that we can go here. If the session that supplied the params 2043 // is not null, then it is the new session that represents the new conference 2044 // if the merge succeeds. If it is null, the merge is happening on our current 2045 // ImsCallSession. 2046 if (session == null) { 2047 // Everything is already set up and we just need to make sure 2048 // that we properly respond to all the future callbacks about 2049 // this merge. 2050 if (DBG) { 2051 log("callSessionMergeStarted :: merging into existing ImsCallSession"); 2052 } 2053 return; 2054 } 2055 2056 if (DBG) { 2057 log("callSessionMergeStarted :: setting our transient ImsCallSession"); 2058 } 2059 2060 // If we are here, this means that we are creating a new conference and 2061 // we need to do some extra work around managing a new ImsCallSession that 2062 // could represent our new ImsCallSession if the merge succeeds. 2063 synchronized(ImsCall.this) { 2064 // Keep track of this session for future callbacks to indicate success 2065 // or failure of this merge. 2066 mTransientConferenceSession = newSession; 2067 mTransientConferenceSession.setListener(createCallSessionListener()); 2068 } 2069 2070 return; 2071 } 2072 2073 @Override 2074 public void callSessionMergeComplete(ImsCallSession session) { 2075 if (VDBG) { 2076 logv("callSessionMergeComplete ::"); 2077 } 2078 if (mUpdateRequest != UPDATE_MERGE) { 2079 // Odd, we are not in the midst of merging anything. 2080 log("callSessionMergeComplete :: no merge in progress."); 2081 return; 2082 } 2083 // Let's let our parent ImsCall now that we received notification that 2084 // the merge was completed so we can set up our internal state properly 2085 processMergeComplete(); 2086 } 2087 2088 @Override 2089 public void callSessionMergeFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 2090 if (VDBG) { 2091 String reasonInfoString = reasonInfo == null ? "null" : reasonInfo.toString(); 2092 logv("callSessionMergeFailed :: reasonInfo=" + reasonInfoString); 2093 } 2094 if (mUpdateRequest != UPDATE_MERGE && !isMerging()) { 2095 // Odd, we are not in the midst of merging anything. 2096 log("callSessionMergeFailed :: no merge in progress."); 2097 return; 2098 } 2099 // Let's tell our parent ImsCall that the merge has failed and we need to clean 2100 // up any temporary, transient state. 2101 processMergeFailed(reasonInfo); 2102 } 2103 2104 @Override 2105 public void callSessionUpdated(ImsCallSession session, ImsCallProfile profile) { 2106 if (isTransientConferenceSession(session)) { 2107 log("callSessionUpdated :: not supported for transient conference session=" + 2108 session); 2109 return; 2110 } 2111 2112 if (VDBG) { 2113 logv("callSessionUpdated :: profile=" + profile); 2114 } 2115 2116 ImsCall.Listener listener; 2117 2118 synchronized(ImsCall.this) { 2119 listener = mListener; 2120 mCallProfile = profile; 2121 mUpdateRequest = UPDATE_NONE; 2122 } 2123 2124 if (listener != null) { 2125 try { 2126 listener.onCallUpdated(ImsCall.this); 2127 } catch (Throwable t) { 2128 loge("callSessionUpdated :: ", t); 2129 } 2130 } 2131 } 2132 2133 @Override 2134 public void callSessionUpdateFailed(ImsCallSession session, ImsReasonInfo reasonInfo) { 2135 if (isTransientConferenceSession(session)) { 2136 log("callSessionUpdateFailed :: not supported for transient conference session=" + 2137 session); 2138 return; 2139 } 2140 2141 if (VDBG) { 2142 logv("callSessionUpdateFailed :: reasonInfo=" + reasonInfo); 2143 } 2144 2145 ImsCall.Listener listener; 2146 2147 synchronized(ImsCall.this) { 2148 listener = mListener; 2149 mUpdateRequest = UPDATE_NONE; 2150 } 2151 2152 if (listener != null) { 2153 try { 2154 listener.onCallUpdateFailed(ImsCall.this, reasonInfo); 2155 } catch (Throwable t) { 2156 loge("callSessionUpdateFailed :: ", t); 2157 } 2158 } 2159 } 2160 2161 @Override 2162 public void callSessionUpdateReceived(ImsCallSession session, ImsCallProfile profile) { 2163 if (isTransientConferenceSession(session)) { 2164 log("callSessionUpdateReceived :: not supported for transient conference " + 2165 "session=" + session); 2166 return; 2167 } 2168 2169 if (VDBG) { 2170 logv("callSessionUpdateReceived :: profile=" + profile); 2171 } 2172 2173 ImsCall.Listener listener; 2174 2175 synchronized(ImsCall.this) { 2176 listener = mListener; 2177 mProposedCallProfile = profile; 2178 mUpdateRequest = UPDATE_UNSPECIFIED; 2179 } 2180 2181 if (listener != null) { 2182 try { 2183 listener.onCallUpdateReceived(ImsCall.this); 2184 } catch (Throwable t) { 2185 loge("callSessionUpdateReceived :: ", t); 2186 } 2187 } 2188 } 2189 2190 @Override 2191 public void callSessionConferenceExtended(ImsCallSession session, ImsCallSession newSession, 2192 ImsCallProfile profile) { 2193 if (isTransientConferenceSession(session)) { 2194 log("callSessionConferenceExtended :: not supported for transient conference " + 2195 "session=" + session); 2196 return; 2197 } 2198 2199 if (VDBG) { 2200 logv("callSessionConferenceExtended :: newSession=" + newSession + ", profile=" 2201 + profile); 2202 } 2203 2204 ImsCall newCall = createNewCall(newSession, profile); 2205 2206 if (newCall == null) { 2207 callSessionConferenceExtendFailed(session, new ImsReasonInfo()); 2208 return; 2209 } 2210 2211 ImsCall.Listener listener; 2212 2213 synchronized(ImsCall.this) { 2214 listener = mListener; 2215 mUpdateRequest = UPDATE_NONE; 2216 } 2217 2218 if (listener != null) { 2219 try { 2220 listener.onCallConferenceExtended(ImsCall.this, newCall); 2221 } catch (Throwable t) { 2222 loge("callSessionConferenceExtended :: ", t); 2223 } 2224 } 2225 } 2226 2227 @Override 2228 public void callSessionConferenceExtendFailed(ImsCallSession session, 2229 ImsReasonInfo reasonInfo) { 2230 if (isTransientConferenceSession(session)) { 2231 log("callSessionConferenceExtendFailed :: not supported for transient " + 2232 "conference session=" + session); 2233 return; 2234 } 2235 2236 if (DBG) { 2237 log("callSessionConferenceExtendFailed :: imsCall=" + ImsCall.this + 2238 ", reasonInfo=" + reasonInfo); 2239 } 2240 2241 ImsCall.Listener listener; 2242 2243 synchronized(ImsCall.this) { 2244 listener = mListener; 2245 mUpdateRequest = UPDATE_NONE; 2246 } 2247 2248 if (listener != null) { 2249 try { 2250 listener.onCallConferenceExtendFailed(ImsCall.this, reasonInfo); 2251 } catch (Throwable t) { 2252 loge("callSessionConferenceExtendFailed :: ", t); 2253 } 2254 } 2255 } 2256 2257 @Override 2258 public void callSessionConferenceExtendReceived(ImsCallSession session, 2259 ImsCallSession newSession, ImsCallProfile profile) { 2260 if (isTransientConferenceSession(session)) { 2261 log("callSessionConferenceExtendReceived :: not supported for transient " + 2262 "conference session" + session); 2263 return; 2264 } 2265 2266 if (VDBG) { 2267 logv("callSessionConferenceExtendReceived :: newSession=" + newSession + 2268 ", profile=" + profile); 2269 } 2270 2271 ImsCall newCall = createNewCall(newSession, profile); 2272 2273 if (newCall == null) { 2274 // Should all the calls be terminated...??? 2275 return; 2276 } 2277 2278 ImsCall.Listener listener; 2279 2280 synchronized(ImsCall.this) { 2281 listener = mListener; 2282 } 2283 2284 if (listener != null) { 2285 try { 2286 listener.onCallConferenceExtendReceived(ImsCall.this, newCall); 2287 } catch (Throwable t) { 2288 loge("callSessionConferenceExtendReceived :: ", t); 2289 } 2290 } 2291 } 2292 2293 @Override 2294 public void callSessionInviteParticipantsRequestDelivered(ImsCallSession session) { 2295 if (isTransientConferenceSession(session)) { 2296 log("callSessionInviteParticipantsRequestDelivered :: not supported for " + 2297 "conference session=" + session); 2298 return; 2299 } 2300 2301 if (VDBG) { 2302 logv("callSessionInviteParticipantsRequestDelivered ::"); 2303 } 2304 2305 ImsCall.Listener listener; 2306 2307 synchronized(ImsCall.this) { 2308 listener = mListener; 2309 } 2310 2311 if (listener != null) { 2312 try { 2313 listener.onCallInviteParticipantsRequestDelivered(ImsCall.this); 2314 } catch (Throwable t) { 2315 loge("callSessionInviteParticipantsRequestDelivered :: ", t); 2316 } 2317 } 2318 } 2319 2320 @Override 2321 public void callSessionInviteParticipantsRequestFailed(ImsCallSession session, 2322 ImsReasonInfo reasonInfo) { 2323 if (isTransientConferenceSession(session)) { 2324 log("callSessionInviteParticipantsRequestFailed :: not supported for " + 2325 "conference session=" + session); 2326 return; 2327 } 2328 2329 if (VDBG) { 2330 logv("callSessionInviteParticipantsRequestFailed :: reasonInfo=" + reasonInfo); 2331 } 2332 2333 ImsCall.Listener listener; 2334 2335 synchronized(ImsCall.this) { 2336 listener = mListener; 2337 } 2338 2339 if (listener != null) { 2340 try { 2341 listener.onCallInviteParticipantsRequestFailed(ImsCall.this, reasonInfo); 2342 } catch (Throwable t) { 2343 loge("callSessionInviteParticipantsRequestFailed :: ", t); 2344 } 2345 } 2346 } 2347 2348 @Override 2349 public void callSessionRemoveParticipantsRequestDelivered(ImsCallSession session) { 2350 if (isTransientConferenceSession(session)) { 2351 log("callSessionRemoveParticipantsRequestDelivered :: not supported for " + 2352 "conference session=" + session); 2353 return; 2354 } 2355 2356 if (VDBG) { 2357 logv("callSessionRemoveParticipantsRequestDelivered ::"); 2358 } 2359 2360 ImsCall.Listener listener; 2361 2362 synchronized(ImsCall.this) { 2363 listener = mListener; 2364 } 2365 2366 if (listener != null) { 2367 try { 2368 listener.onCallRemoveParticipantsRequestDelivered(ImsCall.this); 2369 } catch (Throwable t) { 2370 loge("callSessionRemoveParticipantsRequestDelivered :: ", t); 2371 } 2372 } 2373 } 2374 2375 @Override 2376 public void callSessionRemoveParticipantsRequestFailed(ImsCallSession session, 2377 ImsReasonInfo reasonInfo) { 2378 if (isTransientConferenceSession(session)) { 2379 log("callSessionRemoveParticipantsRequestFailed :: not supported for " + 2380 "conference session=" + session); 2381 return; 2382 } 2383 2384 if (VDBG) { 2385 logv("callSessionRemoveParticipantsRequestFailed :: reasonInfo=" + reasonInfo); 2386 } 2387 2388 ImsCall.Listener listener; 2389 2390 synchronized(ImsCall.this) { 2391 listener = mListener; 2392 } 2393 2394 if (listener != null) { 2395 try { 2396 listener.onCallRemoveParticipantsRequestFailed(ImsCall.this, reasonInfo); 2397 } catch (Throwable t) { 2398 loge("callSessionRemoveParticipantsRequestFailed :: ", t); 2399 } 2400 } 2401 } 2402 2403 @Override 2404 public void callSessionConferenceStateUpdated(ImsCallSession session, 2405 ImsConferenceState state) { 2406 if (isTransientConferenceSession(session)) { 2407 log("callSessionConferenceStateUpdated :: not supported for transient " + 2408 "conference session=" + session); 2409 return; 2410 } 2411 2412 if (VDBG) { 2413 logv("callSessionConferenceStateUpdated :: state=" + state); 2414 } 2415 2416 conferenceStateUpdated(state); 2417 } 2418 2419 @Override 2420 public void callSessionUssdMessageReceived(ImsCallSession session, int mode, 2421 String ussdMessage) { 2422 if (isTransientConferenceSession(session)) { 2423 log("callSessionUssdMessageReceived :: not supported for transient " + 2424 "conference session=" + session); 2425 return; 2426 } 2427 2428 if (VDBG) { 2429 logv("callSessionUssdMessageReceived :: mode=" + mode + ", ussdMessage=" + 2430 ussdMessage); 2431 } 2432 2433 ImsCall.Listener listener; 2434 2435 synchronized(ImsCall.this) { 2436 listener = mListener; 2437 } 2438 2439 if (listener != null) { 2440 try { 2441 listener.onCallUssdMessageReceived(ImsCall.this, mode, ussdMessage); 2442 } catch (Throwable t) { 2443 loge("callSessionUssdMessageReceived :: ", t); 2444 } 2445 } 2446 } 2447 2448 @Override 2449 public void callSessionTtyModeReceived(ImsCallSession session, int mode) { 2450 if (VDBG) { 2451 logv("callSessionTtyModeReceived :: mode=" + mode); 2452 } 2453 2454 int settingsTtyMode = Settings.Secure.getInt( 2455 mContext.getContentResolver(), 2456 Settings.Secure.PREFERRED_TTY_MODE, 2457 TelecomManager.TTY_MODE_OFF); 2458 if (settingsTtyMode == TelecomManager.TTY_MODE_OFF) { 2459 // Notify the user that TTY mode changed in the far device 2460 int resId = 0; 2461 switch (mode) { 2462 case TelecomManager.TTY_MODE_FULL: 2463 resId = com.android.internal.R.string.peerTtyModeFull; 2464 break; 2465 case TelecomManager.TTY_MODE_HCO: 2466 resId = com.android.internal.R.string.peerTtyModeHco; 2467 break; 2468 case TelecomManager.TTY_MODE_VCO: 2469 resId = com.android.internal.R.string.peerTtyModeVco; 2470 break; 2471 case TelecomManager.TTY_MODE_OFF: 2472 resId = com.android.internal.R.string.peerTtyModeOff; 2473 break; 2474 } 2475 if (resId != 0) { 2476 Toast.makeText(mContext, resId, Toast.LENGTH_SHORT).show(); 2477 } 2478 } 2479 } 2480 } 2481 2482 /** 2483 * Report a new conference state to the current {@link ImsCall} and inform listeners of the 2484 * change. Marked as {@code VisibleForTesting} so that the 2485 * {@code com.android.internal.telephony.TelephonyTester} class can inject a test conference 2486 * event package into a regular ongoing IMS call. 2487 * 2488 * @param state The {@link ImsConferenceState}. 2489 */ 2490 @VisibleForTesting 2491 public void conferenceStateUpdated(ImsConferenceState state) { 2492 Listener listener; 2493 2494 synchronized(this) { 2495 notifyConferenceStateUpdated(state); 2496 listener = mListener; 2497 } 2498 2499 if (listener != null) { 2500 try { 2501 listener.onCallConferenceStateUpdated(this, state); 2502 } catch (Throwable t) { 2503 loge("callSessionConferenceStateUpdated :: ", t); 2504 } 2505 } 2506 } 2507 2508 /** 2509 * Provides a human-readable string representation of an update request. 2510 * 2511 * @param updateRequest The update request. 2512 * @return The string representation. 2513 */ 2514 private String updateRequestToString(int updateRequest) { 2515 switch (updateRequest) { 2516 case UPDATE_NONE: 2517 return "NONE"; 2518 case UPDATE_HOLD: 2519 return "HOLD"; 2520 case UPDATE_HOLD_MERGE: 2521 return "HOLD_MERGE"; 2522 case UPDATE_RESUME: 2523 return "RESUME"; 2524 case UPDATE_MERGE: 2525 return "MERGE"; 2526 case UPDATE_EXTEND_TO_CONFERENCE: 2527 return "EXTEND_TO_CONFERENCE"; 2528 case UPDATE_UNSPECIFIED: 2529 return "UNSPECIFIED"; 2530 default: 2531 return "UNKNOWN"; 2532 } 2533 } 2534 2535 /** 2536 * Clears the merge peer for this call, ensuring that the peer's connection to this call is also 2537 * severed at the same time. 2538 */ 2539 private void clearMergePeer() { 2540 if (mMergeHost != null) { 2541 mMergeHost.mMergePeer = null; 2542 mMergeHost = null; 2543 } 2544 2545 if (mMergePeer != null) { 2546 mMergePeer.mMergeHost = null; 2547 mMergePeer = null; 2548 } 2549 } 2550 2551 /** 2552 * Sets the merge peer for the current call. The merge peer is the background call that will be 2553 * merged into this call. On the merge peer, sets the merge host to be this call. 2554 * 2555 * @param mergePeer The peer call to be merged into this one. 2556 */ 2557 private void setMergePeer(ImsCall mergePeer) { 2558 mMergePeer = mergePeer; 2559 mMergeHost = null; 2560 2561 mergePeer.mMergeHost = ImsCall.this; 2562 mergePeer.mMergePeer = null; 2563 } 2564 2565 /** 2566 * Sets the merge hody for the current call. The merge host is the foreground call this call 2567 * will be merged into. On the merge host, sets the merge peer to be this call. 2568 * 2569 * @param mergeHost The merge host this call will be merged into. 2570 */ 2571 public void setMergeHost(ImsCall mergeHost) { 2572 mMergeHost = mergeHost; 2573 mMergePeer = null; 2574 2575 mergeHost.mMergeHost = null; 2576 mergeHost.mMergePeer = ImsCall.this; 2577 } 2578 2579 /** 2580 * Determines if the current call is in the process of merging with another call or conference. 2581 * 2582 * @return {@code true} if in the process of merging. 2583 */ 2584 private boolean isMerging() { 2585 return mMergePeer != null || mMergeHost != null; 2586 } 2587 2588 /** 2589 * Determines if the current call is the host of the merge. 2590 * 2591 * @return {@code true} if the call is the merge host. 2592 */ 2593 private boolean isMergeHost() { 2594 return mMergePeer != null && mMergeHost == null; 2595 } 2596 2597 /** 2598 * Determines if the current call is the peer of the merge. 2599 * 2600 * @return {@code true} if the call is the merge peer. 2601 */ 2602 private boolean isMergePeer() { 2603 return mMergePeer == null && mMergeHost != null; 2604 } 2605 2606 /** 2607 * Provides a string representation of the {@link ImsCall}. Primarily intended for use in log 2608 * statements. 2609 * 2610 * @return String representation of call. 2611 */ 2612 @Override 2613 public String toString() { 2614 StringBuilder sb = new StringBuilder(); 2615 sb.append("[ImsCall objId:"); 2616 sb.append(System.identityHashCode(this)); 2617 sb.append(" onHold:"); 2618 sb.append(isOnHold() ? "Y" : "N"); 2619 sb.append(" mute:"); 2620 sb.append(isMuted() ? "Y" : "N"); 2621 sb.append(" updateRequest:"); 2622 sb.append(updateRequestToString(mUpdateRequest)); 2623 sb.append(" merging:"); 2624 sb.append(isMerging() ? "Y" : "N"); 2625 if (isMerging()) { 2626 if (isMergePeer()) { 2627 sb.append("P"); 2628 } else { 2629 sb.append("H"); 2630 } 2631 } 2632 sb.append(" merged:"); 2633 sb.append(isMerged() ? "Y" : "N"); 2634 sb.append(" multiParty:"); 2635 sb.append(isMultiparty() ? "Y" : "N"); 2636 sb.append(" session:"); 2637 sb.append(mSession); 2638 sb.append(" transientSession:"); 2639 sb.append(mTransientConferenceSession); 2640 sb.append("]"); 2641 return sb.toString(); 2642 } 2643} 2644