private void getImsService() {
    if (DBG) log("getImsService");
    mImsManager = ImsManager.getInstance(mPhone.getContext(), mPhone.getPhoneId());
    try {
      mServiceId =
          mImsManager.open(
              ImsServiceClass.MMTEL,
              createIncomingCallPendingIntent(),
              mImsConnectionStateListener);

      // Get the ECBM interface and set IMSPhone's listener object for notifications
      getEcbmInterface().setEcbmStateListener(mPhone.mImsEcbmStateListener);
      if (mPhone.isInEcm()) {
        // Call exit ECBM which will invoke onECBMExited
        mPhone.exitEmergencyCallbackMode();
      }
      int mPreferredTtyMode =
          Settings.Secure.getInt(
              mPhone.getContext().getContentResolver(),
              Settings.Secure.PREFERRED_TTY_MODE,
              Phone.TTY_MODE_OFF);
      mImsManager.setUiTTYMode(mPhone.getContext(), mServiceId, mPreferredTtyMode, null);

    } catch (ImsException e) {
      loge("getImsService: " + e);
      // Leave mImsManager as null, then CallStateException will be thrown when dialing
      mImsManager = null;
    }
  }
 void setUiTTYMode(int uiTtyMode, Message onComplete) {
   try {
     mImsManager.setUiTTYMode(mPhone.getContext(), mServiceId, uiTtyMode, onComplete);
   } catch (ImsException e) {
     loge("setTTYMode : " + e);
     mPhone.sendErrorResponse(onComplete, e);
   }
 }
  public void dispose() {
    if (DBG) log("dispose");
    mRingingCall.dispose();
    mBackgroundCall.dispose();
    mForegroundCall.dispose();
    mHandoverCall.dispose();

    clearDisconnected();
    mPhone.getContext().unregisterReceiver(mReceiver);
  }
  ImsPhoneCallTracker(ImsPhone phone) {
    this.mPhone = phone;

    IntentFilter intentfilter = new IntentFilter();
    intentfilter.addAction(ImsManager.ACTION_IMS_INCOMING_CALL);
    mPhone.getContext().registerReceiver(mReceiver, intentfilter);

    Thread t =
        new Thread() {
          public void run() {
            getImsService();
          }
        };
    t.start();
  }
  /** oirMode is one of the CLIR_ constants */
  synchronized Connection dial(String dialString, int clirMode, int videoState)
      throws CallStateException {
    boolean isPhoneInEcmMode =
        SystemProperties.getBoolean(TelephonyProperties.PROPERTY_INECM_MODE, false);
    boolean isEmergencyNumber = PhoneNumberUtils.isEmergencyNumber(dialString);

    if (DBG) log("dial clirMode=" + clirMode);

    // note that this triggers call state changed notif
    clearDisconnected();

    if (mImsManager == null) {
      throw new CallStateException("service not available");
    }

    if (!canDial()) {
      throw new CallStateException("cannot dial in current state");
    }

    if (isPhoneInEcmMode && isEmergencyNumber) {
      handleEcmTimer(ImsPhone.CANCEL_ECM_TIMER);
    }

    boolean holdBeforeDial = false;

    // The new call must be assigned to the foreground call.
    // That call must be idle, so place anything that's
    // there on hold
    if (mForegroundCall.getState() == ImsPhoneCall.State.ACTIVE) {
      if (mBackgroundCall.getState() != ImsPhoneCall.State.IDLE) {
        // we should have failed in !canDial() above before we get here
        throw new CallStateException("cannot dial in current state");
      }
      // foreground call is empty for the newly dialed connection
      holdBeforeDial = true;
      switchWaitingOrHoldingAndActive();
    }

    ImsPhoneCall.State fgState = ImsPhoneCall.State.IDLE;
    ImsPhoneCall.State bgState = ImsPhoneCall.State.IDLE;

    mClirMode = clirMode;

    synchronized (mSyncHold) {
      if (holdBeforeDial) {
        fgState = mForegroundCall.getState();
        bgState = mBackgroundCall.getState();

        // holding foreground call failed
        if (fgState == ImsPhoneCall.State.ACTIVE) {
          throw new CallStateException("cannot dial in current state");
        }

        // holding foreground call succeeded
        if (bgState == ImsPhoneCall.State.HOLDING) {
          holdBeforeDial = false;
        }
      }

      mPendingMO =
          new ImsPhoneConnection(
              mPhone.getContext(), checkForTestEmergencyNumber(dialString), this, mForegroundCall);
    }
    addConnection(mPendingMO);

    if (!holdBeforeDial) {
      if ((!isPhoneInEcmMode) || (isPhoneInEcmMode && isEmergencyNumber)) {
        dialInternal(mPendingMO, clirMode, videoState);
      } else {
        try {
          getEcbmInterface().exitEmergencyCallbackMode();
        } catch (ImsException e) {
          e.printStackTrace();
          throw new CallStateException("service not available");
        }
        mPhone.setOnEcbModeExitResponse(this, EVENT_EXIT_ECM_RESPONSE_CDMA, null);
        pendingCallClirMode = clirMode;
        pendingCallVideoState = videoState;
        pendingCallInEcm = true;
      }
    }

    updatePhoneState();
    mPhone.notifyPreciseCallStateChanged();

    return mPendingMO;
  }
 Connection dial(String dialString, int videoState) throws CallStateException {
   SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
   int oirMode = sp.getInt(PhoneBase.CLIR_KEY, CommandsInterface.CLIR_DEFAULT);
   return dial(dialString, oirMode, videoState);
 }
 private PendingIntent createIncomingCallPendingIntent() {
   Intent intent = new Intent(ImsManager.ACTION_IMS_INCOMING_CALL);
   intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
   return PendingIntent.getBroadcast(
       mPhone.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 }