ImsPhoneConnection.java revision d9aa1a75304b1c04c352198b9269f40a2a059f74
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.internal.telephony.imsphone; 18 19import android.content.Context; 20import android.net.Uri; 21import android.os.AsyncResult; 22import android.os.Handler; 23import android.os.Looper; 24import android.os.Message; 25import android.os.PowerManager; 26import android.os.Registrant; 27import android.os.SystemClock; 28import android.telecom.Log; 29import android.telephony.DisconnectCause; 30import android.telephony.PhoneNumberUtils; 31import android.telephony.Rlog; 32 33import com.android.ims.ImsException; 34import com.android.ims.ImsStreamMediaProfile; 35import com.android.internal.telephony.CallStateException; 36import com.android.internal.telephony.Connection; 37import com.android.internal.telephony.Phone; 38import com.android.internal.telephony.PhoneConstants; 39import com.android.internal.telephony.UUSInfo; 40 41import com.android.ims.ImsCall; 42import com.android.ims.ImsCallProfile; 43 44/** 45 * {@hide} 46 */ 47public class ImsPhoneConnection extends Connection { 48 private static final String LOG_TAG = "ImsPhoneConnection"; 49 private static final boolean DBG = true; 50 51 //***** Instance Variables 52 53 private ImsPhoneCallTracker mOwner; 54 private ImsPhoneCall mParent; 55 private ImsCall mImsCall; 56 57 private String mPostDialString; // outgoing calls only 58 private boolean mDisconnected; 59 60 /* 61 int mIndex; // index in ImsPhoneCallTracker.connections[], -1 if unassigned 62 // The GSM index is 1 + this 63 */ 64 65 /* 66 * These time/timespan values are based on System.currentTimeMillis(), 67 * i.e., "wall clock" time. 68 */ 69 private long mDisconnectTime; 70 71 private int mNextPostDialChar; // index into postDialString 72 73 private int mCause = DisconnectCause.NOT_DISCONNECTED; 74 private PostDialState mPostDialState = PostDialState.NOT_STARTED; 75 private UUSInfo mUusInfo; 76 private Handler mHandler; 77 78 private PowerManager.WakeLock mPartialWakeLock; 79 80 //***** Event Constants 81 private static final int EVENT_DTMF_DONE = 1; 82 private static final int EVENT_PAUSE_DONE = 2; 83 private static final int EVENT_NEXT_POST_DIAL = 3; 84 private static final int EVENT_WAKE_LOCK_TIMEOUT = 4; 85 86 //***** Constants 87 private static final int PAUSE_DELAY_MILLIS = 3 * 1000; 88 private static final int WAKE_LOCK_TIMEOUT_MILLIS = 60*1000; 89 90 //***** Inner Classes 91 92 class MyHandler extends Handler { 93 MyHandler(Looper l) {super(l);} 94 95 @Override 96 public void 97 handleMessage(Message msg) { 98 99 switch (msg.what) { 100 case EVENT_NEXT_POST_DIAL: 101 case EVENT_DTMF_DONE: 102 case EVENT_PAUSE_DONE: 103 processNextPostDialChar(); 104 break; 105 case EVENT_WAKE_LOCK_TIMEOUT: 106 releaseWakeLock(); 107 break; 108 } 109 } 110 } 111 112 //***** Constructors 113 114 /** This is probably an MT call */ 115 /*package*/ 116 ImsPhoneConnection(Context context, ImsCall imsCall, ImsPhoneCallTracker ct, ImsPhoneCall parent) { 117 createWakeLock(context); 118 acquireWakeLock(); 119 120 mOwner = ct; 121 mHandler = new MyHandler(mOwner.getLooper()); 122 mImsCall = imsCall; 123 124 if ((imsCall != null) && (imsCall.getCallProfile() != null)) { 125 mAddress = imsCall.getCallProfile().getCallExtra(ImsCallProfile.EXTRA_OI); 126 mCnapName = imsCall.getCallProfile().getCallExtra(ImsCallProfile.EXTRA_CNA); 127 mNumberPresentation = ImsCallProfile.OIRToPresentation( 128 imsCall.getCallProfile().getCallExtraInt(ImsCallProfile.EXTRA_OIR)); 129 mCnapNamePresentation = ImsCallProfile.OIRToPresentation( 130 imsCall.getCallProfile().getCallExtraInt(ImsCallProfile.EXTRA_CNAP)); 131 132 updateMediaCapabilities(imsCall); 133 } else { 134 mNumberPresentation = PhoneConstants.PRESENTATION_UNKNOWN; 135 mCnapNamePresentation = PhoneConstants.PRESENTATION_UNKNOWN; 136 } 137 138 mIsIncoming = true; 139 mCreateTime = System.currentTimeMillis(); 140 mUusInfo = null; 141 142 //mIndex = index; 143 144 mParent = parent; 145 mParent.attach(this, ImsPhoneCall.State.INCOMING); 146 } 147 148 /** This is an MO call, created when dialing */ 149 /*package*/ 150 ImsPhoneConnection(Context context, String dialString, ImsPhoneCallTracker ct, ImsPhoneCall parent) { 151 createWakeLock(context); 152 acquireWakeLock(); 153 154 mOwner = ct; 155 mHandler = new MyHandler(mOwner.getLooper()); 156 157 mDialString = dialString; 158 159 mAddress = PhoneNumberUtils.extractNetworkPortionAlt(dialString); 160 mPostDialString = PhoneNumberUtils.extractPostDialPortion(dialString); 161 162 //mIndex = -1; 163 164 mIsIncoming = false; 165 mCnapName = null; 166 mCnapNamePresentation = PhoneConstants.PRESENTATION_ALLOWED; 167 mNumberPresentation = PhoneConstants.PRESENTATION_ALLOWED; 168 mCreateTime = System.currentTimeMillis(); 169 170 mParent = parent; 171 parent.attachFake(this, ImsPhoneCall.State.DIALING); 172 } 173 174 public void dispose() { 175 } 176 177 static boolean 178 equalsHandlesNulls (Object a, Object b) { 179 return (a == null) ? (b == null) : a.equals (b); 180 } 181 182 @Override 183 public String getOrigDialString(){ 184 return mDialString; 185 } 186 187 @Override 188 public ImsPhoneCall getCall() { 189 return mParent; 190 } 191 192 @Override 193 public long getDisconnectTime() { 194 return mDisconnectTime; 195 } 196 197 @Override 198 public long getHoldingStartTime() { 199 return mHoldingStartTime; 200 } 201 202 @Override 203 public long getHoldDurationMillis() { 204 if (getState() != ImsPhoneCall.State.HOLDING) { 205 // If not holding, return 0 206 return 0; 207 } else { 208 return SystemClock.elapsedRealtime() - mHoldingStartTime; 209 } 210 } 211 212 @Override 213 public int getDisconnectCause() { 214 return mCause; 215 } 216 217 public void setDisconnectCause(int cause) { 218 mCause = cause; 219 } 220 221 public ImsPhoneCallTracker getOwner () { 222 return mOwner; 223 } 224 225 @Override 226 public ImsPhoneCall.State getState() { 227 if (mDisconnected) { 228 return ImsPhoneCall.State.DISCONNECTED; 229 } else { 230 return super.getState(); 231 } 232 } 233 234 @Override 235 public void hangup() throws CallStateException { 236 if (!mDisconnected) { 237 mOwner.hangup(this); 238 } else { 239 throw new CallStateException ("disconnected"); 240 } 241 } 242 243 @Override 244 public void separate() throws CallStateException { 245 throw new CallStateException ("not supported"); 246 } 247 248 @Override 249 public PostDialState getPostDialState() { 250 return mPostDialState; 251 } 252 253 @Override 254 public void proceedAfterWaitChar() { 255 if (mPostDialState != PostDialState.WAIT) { 256 Rlog.w(LOG_TAG, "ImsPhoneConnection.proceedAfterWaitChar(): Expected " 257 + "getPostDialState() to be WAIT but was " + mPostDialState); 258 return; 259 } 260 261 setPostDialState(PostDialState.STARTED); 262 263 processNextPostDialChar(); 264 } 265 266 @Override 267 public void proceedAfterWildChar(String str) { 268 if (mPostDialState != PostDialState.WILD) { 269 Rlog.w(LOG_TAG, "ImsPhoneConnection.proceedAfterWaitChar(): Expected " 270 + "getPostDialState() to be WILD but was " + mPostDialState); 271 return; 272 } 273 274 setPostDialState(PostDialState.STARTED); 275 276 // make a new postDialString, with the wild char replacement string 277 // at the beginning, followed by the remaining postDialString. 278 279 StringBuilder buf = new StringBuilder(str); 280 buf.append(mPostDialString.substring(mNextPostDialChar)); 281 mPostDialString = buf.toString(); 282 mNextPostDialChar = 0; 283 if (Phone.DEBUG_PHONE) { 284 Rlog.d(LOG_TAG, "proceedAfterWildChar: new postDialString is " + 285 mPostDialString); 286 } 287 288 processNextPostDialChar(); 289 } 290 291 @Override 292 public void cancelPostDial() { 293 setPostDialState(PostDialState.CANCELLED); 294 } 295 296 /** 297 * Called when this Connection is being hung up locally (eg, user pressed "end") 298 */ 299 void 300 onHangupLocal() { 301 mCause = DisconnectCause.LOCAL; 302 } 303 304 /** Called when the connection has been disconnected */ 305 /*package*/ boolean 306 onDisconnect(int cause) { 307 Rlog.d(LOG_TAG, "onDisconnect: cause=" + cause); 308 if (mCause != DisconnectCause.LOCAL) mCause = cause; 309 return onDisconnect(); 310 } 311 312 /*package*/ boolean 313 onDisconnect() { 314 boolean changed = false; 315 316 if (!mDisconnected) { 317 //mIndex = -1; 318 319 mDisconnectTime = System.currentTimeMillis(); 320 mDuration = SystemClock.elapsedRealtime() - mConnectTimeReal; 321 mDisconnected = true; 322 323 mOwner.mPhone.notifyDisconnect(this); 324 325 if (mParent != null) { 326 changed = mParent.connectionDisconnected(this); 327 } else { 328 Rlog.d(LOG_TAG, "onDisconnect: no parent"); 329 } 330 if (mImsCall != null) mImsCall.close(); 331 mImsCall = null; 332 } 333 releaseWakeLock(); 334 return changed; 335 } 336 337 /** 338 * An incoming or outgoing call has connected 339 */ 340 void 341 onConnectedInOrOut() { 342 mConnectTime = System.currentTimeMillis(); 343 mConnectTimeReal = SystemClock.elapsedRealtime(); 344 mDuration = 0; 345 346 if (Phone.DEBUG_PHONE) { 347 Rlog.d(LOG_TAG, "onConnectedInOrOut: connectTime=" + mConnectTime); 348 } 349 350 if (!mIsIncoming) { 351 // outgoing calls only 352 processNextPostDialChar(); 353 } 354 releaseWakeLock(); 355 } 356 357 /*package*/ void 358 onStartedHolding() { 359 mHoldingStartTime = SystemClock.elapsedRealtime(); 360 } 361 /** 362 * Performs the appropriate action for a post-dial char, but does not 363 * notify application. returns false if the character is invalid and 364 * should be ignored 365 */ 366 private boolean 367 processPostDialChar(char c) { 368 if (PhoneNumberUtils.is12Key(c)) { 369 mOwner.sendDtmf(c, mHandler.obtainMessage(EVENT_DTMF_DONE)); 370 } else if (c == PhoneNumberUtils.PAUSE) { 371 // From TS 22.101: 372 // It continues... 373 // Upon the called party answering the UE shall send the DTMF digits 374 // automatically to the network after a delay of 3 seconds( 20 ). 375 // The digits shall be sent according to the procedures and timing 376 // specified in 3GPP TS 24.008 [13]. The first occurrence of the 377 // "DTMF Control Digits Separator" shall be used by the ME to 378 // distinguish between the addressing digits (i.e. the phone number) 379 // and the DTMF digits. Upon subsequent occurrences of the 380 // separator, 381 // the UE shall pause again for 3 seconds ( 20 ) before sending 382 // any further DTMF digits. 383 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_PAUSE_DONE), 384 PAUSE_DELAY_MILLIS); 385 } else if (c == PhoneNumberUtils.WAIT) { 386 setPostDialState(PostDialState.WAIT); 387 } else if (c == PhoneNumberUtils.WILD) { 388 setPostDialState(PostDialState.WILD); 389 } else { 390 return false; 391 } 392 393 return true; 394 } 395 396 @Override 397 public String 398 getRemainingPostDialString() { 399 if (mPostDialState == PostDialState.CANCELLED 400 || mPostDialState == PostDialState.COMPLETE 401 || mPostDialString == null 402 || mPostDialString.length() <= mNextPostDialChar 403 ) { 404 return ""; 405 } 406 407 return mPostDialString.substring(mNextPostDialChar); 408 } 409 410 @Override 411 protected void finalize() 412 { 413 releaseWakeLock(); 414 } 415 416 private void 417 processNextPostDialChar() { 418 char c = 0; 419 Registrant postDialHandler; 420 421 if (mPostDialState == PostDialState.CANCELLED) { 422 //Rlog.d(LOG_TAG, "##### processNextPostDialChar: postDialState == CANCELLED, bail"); 423 return; 424 } 425 426 if (mPostDialString == null || mPostDialString.length() <= mNextPostDialChar) { 427 setPostDialState(PostDialState.COMPLETE); 428 429 // notifyMessage.arg1 is 0 on complete 430 c = 0; 431 } else { 432 boolean isValid; 433 434 setPostDialState(PostDialState.STARTED); 435 436 c = mPostDialString.charAt(mNextPostDialChar++); 437 438 isValid = processPostDialChar(c); 439 440 if (!isValid) { 441 // Will call processNextPostDialChar 442 mHandler.obtainMessage(EVENT_NEXT_POST_DIAL).sendToTarget(); 443 // Don't notify application 444 Rlog.e(LOG_TAG, "processNextPostDialChar: c=" + c + " isn't valid!"); 445 return; 446 } 447 } 448 449 postDialHandler = mOwner.mPhone.mPostDialHandler; 450 451 Message notifyMessage; 452 453 if (postDialHandler != null 454 && (notifyMessage = postDialHandler.messageForRegistrant()) != null) { 455 // The AsyncResult.result is the Connection object 456 PostDialState state = mPostDialState; 457 AsyncResult ar = AsyncResult.forMessage(notifyMessage); 458 ar.result = this; 459 ar.userObj = state; 460 461 // arg1 is the character that was/is being processed 462 notifyMessage.arg1 = c; 463 464 //Rlog.v(LOG_TAG, "##### processNextPostDialChar: send msg to postDialHandler, arg1=" + c); 465 notifyMessage.sendToTarget(); 466 } 467 } 468 469 /** 470 * Set post dial state and acquire wake lock while switching to "started" 471 * state, the wake lock will be released if state switches out of "started" 472 * state or after WAKE_LOCK_TIMEOUT_MILLIS. 473 * @param s new PostDialState 474 */ 475 private void setPostDialState(PostDialState s) { 476 if (mPostDialState != PostDialState.STARTED 477 && s == PostDialState.STARTED) { 478 acquireWakeLock(); 479 Message msg = mHandler.obtainMessage(EVENT_WAKE_LOCK_TIMEOUT); 480 mHandler.sendMessageDelayed(msg, WAKE_LOCK_TIMEOUT_MILLIS); 481 } else if (mPostDialState == PostDialState.STARTED 482 && s != PostDialState.STARTED) { 483 mHandler.removeMessages(EVENT_WAKE_LOCK_TIMEOUT); 484 releaseWakeLock(); 485 } 486 mPostDialState = s; 487 notifyPostDialListeners(); 488 } 489 490 private void 491 createWakeLock(Context context) { 492 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 493 mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG); 494 } 495 496 private void 497 acquireWakeLock() { 498 Rlog.d(LOG_TAG, "acquireWakeLock"); 499 mPartialWakeLock.acquire(); 500 } 501 502 void 503 releaseWakeLock() { 504 synchronized(mPartialWakeLock) { 505 if (mPartialWakeLock.isHeld()) { 506 Rlog.d(LOG_TAG, "releaseWakeLock"); 507 mPartialWakeLock.release(); 508 } 509 } 510 } 511 512 @Override 513 public int getNumberPresentation() { 514 return mNumberPresentation; 515 } 516 517 @Override 518 public UUSInfo getUUSInfo() { 519 return mUusInfo; 520 } 521 522 @Override 523 public Connection getOrigConnection() { 524 return null; 525 } 526 527 @Override 528 public boolean isMultiparty() { 529 return mImsCall != null && mImsCall.isMultiparty(); 530 } 531 532 /*package*/ ImsCall getImsCall() { 533 return mImsCall; 534 } 535 536 /*package*/ void setImsCall(ImsCall imsCall) { 537 mImsCall = imsCall; 538 } 539 540 /*package*/ void changeParent(ImsPhoneCall parent) { 541 mParent = parent; 542 } 543 544 /** 545 * @return {@code true} if the {@link ImsPhoneConnection} or its media capabilities have been 546 * changed, and {@code false} otherwise. 547 */ 548 /*package*/ boolean update(ImsCall imsCall, ImsPhoneCall.State state) { 549 if (state == ImsPhoneCall.State.ACTIVE) { 550 if (mParent.getState().isRinging() || mParent.getState().isDialing()) { 551 onConnectedInOrOut(); 552 } 553 554 if (mParent.getState().isRinging() || mParent == mOwner.mBackgroundCall) { 555 //mForegroundCall should be IDLE 556 //when accepting WAITING call 557 //before accept WAITING call, 558 //the ACTIVE call should be held ahead 559 mParent.detach(this); 560 mParent = mOwner.mForegroundCall; 561 mParent.attach(this); 562 } 563 } else if (state == ImsPhoneCall.State.HOLDING) { 564 onStartedHolding(); 565 } 566 567 return mParent.update(this, imsCall, state) || updateMediaCapabilities(imsCall); 568 } 569 570 @Override 571 public int getPreciseDisconnectCause() { 572 return 0; 573 } 574 575 /** 576 * Notifies this Connection of a request to disconnect a participant of the conference managed 577 * by the connection. 578 * 579 * @param endpoint the {@link android.net.Uri} of the participant to disconnect. 580 */ 581 @Override 582 public void onDisconnectConferenceParticipant(Uri endpoint) { 583 ImsCall imsCall = getImsCall(); 584 if (imsCall == null) { 585 return; 586 } 587 try { 588 imsCall.removeParticipants(new String[]{endpoint.toString()}); 589 } catch (ImsException e) { 590 // No session in place -- no change 591 Rlog.e(LOG_TAG, "onDisconnectConferenceParticipant: no session in place. "+ 592 "Failed to disconnect endpoint = " + endpoint); 593 } 594 } 595 596 /** 597 * Check for a change in the video capabilities and audio quality for the {@link ImsCall}, and 598 * update the {@link ImsPhoneConnection} with this information. 599 * 600 * @param imsCall The call to check for changes in media capabilities. 601 * @return Whether the media capabilities have been changed. 602 */ 603 private boolean updateMediaCapabilities(ImsCall imsCall) { 604 if (imsCall == null) { 605 return false; 606 } 607 608 boolean changed = false; 609 610 try { 611 ImsCallProfile localCallProfile = imsCall.getLocalCallProfile(); 612 ImsCallProfile remoteCallProfile = imsCall.getRemoteCallProfile(); 613 614 if (localCallProfile != null) { 615 int callType = localCallProfile.mCallType; 616 617 boolean newLocalVideoCapable = callType == ImsCallProfile.CALL_TYPE_VT; 618 if (isLocalVideoCapable() != newLocalVideoCapable) { 619 setLocalVideoCapable(newLocalVideoCapable); 620 changed = true; 621 } 622 623 int newVideoState = ImsCallProfile.getVideoStateFromCallType(callType); 624 if (getVideoState() != newVideoState) { 625 setVideoState(newVideoState); 626 changed = true; 627 } 628 } 629 630 int newAudioQuality = 631 getAudioQualityFromCallProfile(localCallProfile, remoteCallProfile); 632 if (getAudioQuality() != newAudioQuality) { 633 setAudioQuality(newAudioQuality); 634 changed = true; 635 } 636 } catch (ImsException e) { 637 // No session in place -- no change 638 } 639 640 return changed; 641 } 642 643 /** 644 * Determines the {@link ImsPhoneConnection} audio quality based on the local and remote 645 * {@link ImsCallProfile}. If indicate a HQ audio call if the local stream profile 646 * indicates AMR_WB or EVRC_WB and there is no remote restrict cause. 647 * 648 * @param localCallProfile The local call profile. 649 * @param remoteCallProfile The remote call profile. 650 * @return The audio quality. 651 */ 652 private int getAudioQualityFromCallProfile( 653 ImsCallProfile localCallProfile, ImsCallProfile remoteCallProfile) { 654 if (localCallProfile == null || remoteCallProfile == null 655 || localCallProfile.mMediaProfile == null) { 656 return AUDIO_QUALITY_STANDARD; 657 } 658 659 boolean isHighDef = (localCallProfile.mMediaProfile.mAudioQuality 660 == ImsStreamMediaProfile.AUDIO_QUALITY_AMR_WB 661 || localCallProfile.mMediaProfile.mAudioQuality 662 == ImsStreamMediaProfile.AUDIO_QUALITY_EVRC_WB) 663 && remoteCallProfile.mRestrictCause == ImsCallProfile.CALL_RESTRICT_CAUSE_NONE; 664 return isHighDef ? AUDIO_QUALITY_HIGH_DEFINITION : AUDIO_QUALITY_STANDARD; 665 } 666 667 /** 668 * Provides a string representation of the {@link ImsPhoneConnection}. Primarily intended for 669 * use in log statements. 670 * 671 * @return String representation of call. 672 */ 673 @Override 674 public String toString() { 675 StringBuilder sb = new StringBuilder(); 676 sb.append("[ImsPhoneConnection objId: "); 677 sb.append(System.identityHashCode(this)); 678 sb.append(" address:"); 679 sb.append(Log.pii(getAddress())); 680 sb.append(" ImsCall:"); 681 if (mImsCall == null) { 682 sb.append("null"); 683 } else { 684 sb.append(mImsCall); 685 } 686 sb.append("]"); 687 return sb.toString(); 688 } 689} 690 691