private void startUi(InCallState inCallState) {
    final Call incomingCall = mCallList.getIncomingCall();
    final boolean isCallWaiting =
        (incomingCall != null && incomingCall.getState() == Call.State.CALL_WAITING);

    // If the screen is off, we need to make sure it gets turned on for incoming calls.
    // This normally works just fine thanks to FLAG_TURN_SCREEN_ON but that only works
    // when the activity is first created. Therefore, to ensure the screen is turned on
    // for the call waiting case, we finish() the current activity and start a new one.
    // There should be no jank from this since the screen is already off and will remain so
    // until our new activity is up.
    if (mProximitySensor.isScreenReallyOff() && isCallWaiting) {
      if (isActivityStarted()) {
        mInCallActivity.finish();
      }
      mInCallActivity = null;
    }

    final PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
    // If the screen is on, we'll prefer to not interrupt the user too much and slide in a card
    if (pm.isScreenOn()) {
      Intent intent = new Intent(mContext, InCallCardActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      mContext.startActivity(intent);
    } else {
      mStatusBarNotifier.updateNotificationAndLaunchIncomingCallUi(inCallState, mCallList);
    }
  }
  /** Called when the activity goes in/out of the foreground. */
  public void onUiShowing(boolean showing) {
    // We need to update the notification bar when we leave the UI because that
    // could trigger it to show again.
    if (mStatusBarNotifier != null) {
      mStatusBarNotifier.updateNotification(mInCallState, mCallList);
    }

    if (mProximitySensor != null) {
      mProximitySensor.onInCallShowing(showing);
    }

    if (showing) {
      mIsActivityPreviouslyStarted = true;
    }
  }
  /**
   * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
   */
  private void attemptCleanup() {
    boolean shouldCleanup =
        (mInCallActivity == null && !mServiceConnected && mInCallState == InCallState.NO_CALLS);
    Log.i(this, "attemptCleanup? " + shouldCleanup);

    if (shouldCleanup) {
      mIsActivityPreviouslyStarted = false;

      // blow away stale contact info so that we get fresh data on
      // the next set of calls
      if (mContactInfoCache != null) {
        mContactInfoCache.clearCache();
      }
      mContactInfoCache = null;

      if (mProximitySensor != null) {
        removeListener(mProximitySensor);
        mProximitySensor.tearDown();
      }
      mProximitySensor = null;

      mAccelerometerListener = null;

      mAudioModeProvider = null;

      if (mStatusBarNotifier != null) {
        removeListener(mStatusBarNotifier);
      }
      mStatusBarNotifier = null;

      if (mCallList != null) {
        mCallList.removeListener(this);
      }
      mCallList = null;

      mContext = null;
      mInCallActivity = null;

      mListeners.clear();
      mIncomingCallListeners.clear();

      Log.d(this, "Finished InCallPresenter.CleanUp");
    }
  }