@Override
 public void onPause() {
   super.onPause();
   if (!Sipdroid.release) Log.i("SipUA:", "on pause");
   switch (Receiver.call_state) {
     case UserAgent.UA_STATE_INCOMING_CALL:
       if (!RtpStreamReceiver.isBluetoothAvailable()) Receiver.moveTop();
       break;
     case UserAgent.UA_STATE_IDLE:
       if (Receiver.ccCall != null) mCallCard.displayMainCallStatus(ccPhone, Receiver.ccCall);
       mHandler.sendEmptyMessageDelayed(MSG_BACK, Receiver.call_end_reason == -1 ? 2000 : 5000);
       break;
   }
   if (t != null) {
     running = false;
     t.interrupt();
   }
   screenOff(false);
   if (mCallCard.mElapsedTime != null) mCallCard.mElapsedTime.stop();
 }
  @Override
  public void onResume() {
    super.onResume();
    if (!Sipdroid.release) Log.i("SipUA:", "on resume");
    switch (Receiver.call_state) {
      case UserAgent.UA_STATE_INCOMING_CALL:
        if (Receiver.pstn_state == null || Receiver.pstn_state.equals("IDLE"))
          if (PreferenceManager.getDefaultSharedPreferences(mContext)
                  .getBoolean(
                      org.sipdroid.sipua.ui.Settings.PREF_AUTO_ON,
                      org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ON)
              && !mKeyguardManager.inKeyguardRestrictedInputMode())
            mHandler.sendEmptyMessageDelayed(MSG_ANSWER, 1000);
          else if ((PreferenceManager.getDefaultSharedPreferences(mContext)
                      .getBoolean(
                          org.sipdroid.sipua.ui.Settings.PREF_AUTO_ONDEMAND,
                          org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ONDEMAND)
                  && PreferenceManager.getDefaultSharedPreferences(mContext)
                      .getBoolean(
                          org.sipdroid.sipua.ui.Settings.PREF_AUTO_DEMAND,
                          org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_DEMAND))
              || (PreferenceManager.getDefaultSharedPreferences(mContext)
                      .getBoolean(
                          org.sipdroid.sipua.ui.Settings.PREF_AUTO_HEADSET,
                          org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_HEADSET)
                  && Receiver.headset > 0))
            mHandler.sendEmptyMessageDelayed(MSG_ANSWER_SPEAKER, 10000);
        break;
      case UserAgent.UA_STATE_INCALL:
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
          mDialerDrawer.close();
          mDialerDrawer.setVisibility(View.GONE);
        } else mDialerDrawer.setVisibility(View.VISIBLE);
        if (Receiver.docked <= 0) screenOff(true);
        break;
      case UserAgent.UA_STATE_IDLE:
        if (!mHandler.hasMessages(MSG_BACK)) moveBack();
        break;
    }
    if (Receiver.call_state != UserAgent.UA_STATE_INCALL) {
      mDialerDrawer.close();
      mDialerDrawer.setVisibility(View.GONE);
    }
    if (Receiver.ccCall != null) mCallCard.displayMainCallStatus(ccPhone, Receiver.ccCall);
    if (mSlidingCardManager != null) mSlidingCardManager.showPopup();
    mHandler.sendEmptyMessage(MSG_TICK);
    if (t == null && Receiver.call_state != UserAgent.UA_STATE_IDLE) {
      mDigits.setText("");
      running = true;
      (t =
              new Thread() {
                public void run() {
                  int len = 0;
                  long time;
                  ToneGenerator tg = null;

                  if (Settings.System.getInt(
                          getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING, 1)
                      == 1)
                    tg =
                        new ToneGenerator(
                            AudioManager.STREAM_VOICE_CALL,
                            (int)
                                (ToneGenerator.MAX_VOLUME
                                    * 2
                                    * org.sipdroid.sipua.ui.Settings.getEarGain()));
                  for (; ; ) {
                    if (!running) {
                      t = null;
                      break;
                    }
                    if (len != mDigits.getText().length()) {
                      time = SystemClock.elapsedRealtime();
                      if (tg != null) tg.startTone(mToneMap.get(mDigits.getText().charAt(len)));
                      Receiver.engine(Receiver.mContext).info(mDigits.getText().charAt(len++), 250);
                      time = 250 - (SystemClock.elapsedRealtime() - time);
                      try {
                        if (time > 0) sleep(time);
                      } catch (InterruptedException e) {
                      }
                      if (tg != null) tg.stopTone();
                      try {
                        if (running) sleep(250);
                      } catch (InterruptedException e) {
                      }
                      continue;
                    }
                    mHandler.sendEmptyMessage(MSG_TICK);
                    try {
                      sleep(1000);
                    } catch (InterruptedException e) {
                    }
                  }
                  if (tg != null) tg.release();
                }
              })
          .start();
    }
  }