/** Handle receiving a MMS message */
  private void handleMmsReceived(Intent intent) {
    if (Log.DEBUG) Log.v("MMS received!");
    SmsMmsMessage mmsMessage = null;
    int count = 0;

    // Ok this is super hacky, but fixes the case where this code
    // runs before the system MMS transaction service (that stores
    // the MMS details in the database). This should really be
    // a content listener that waits for a while then gives up...
    while (mmsMessage == null && count < MESSAGE_RETRY) {

      mmsMessage = SmsPopupUtils.getMmsDetails(context);

      if (mmsMessage != null) {
        if (Log.DEBUG) Log.v("MMS found in content provider");
        notifyMessageReceived(mmsMessage);
      } else {
        if (Log.DEBUG) Log.v("MMS not found, sleeping (count is " + count + ")");
        count++;
        try {
          Thread.sleep(MESSAGE_RETRY_PAUSE);
        } catch (InterruptedException e) {
          // e.printStackTrace();
        }
      }
    }
  }
  /** Handle receiving a SMS message */
  private void handleSmsReceived(Intent intent) {
    if (Log.DEBUG) Log.v("SMSReceiver: Intercept SMS");

    Bundle bundle = intent.getExtras();
    if (bundle != null) {
      SmsMessage[] messages = SmsPopupUtils.getMessagesFromIntent(intent);
      if (messages != null) {
        notifyMessageReceived(new SmsMmsMessage(context, messages, System.currentTimeMillis()));
      }
    }
  }
  private void notifyMessageReceived(SmsMmsMessage message) {

    // Class 0 SMS, let the system handle this
    if (message.getMessageType() == SmsMmsMessage.MESSAGE_TYPE_SMS
        && message.getMessageClass() == MessageClass.CLASS_0) {
      return;
    }

    if (message.isSprintVisualVoicemail()) {
      return;
    }

    // Fetch preferences
    ManagePreferences mPrefs = new ManagePreferences(context, message.getContactId());

    // Whether or not the popup should only show when keyguard is on
    boolean onlyShowOnKeyguard =
        mPrefs.getBoolean(
            R.string.pref_onlyShowOnKeyguard_key, Defaults.PREFS_ONLY_SHOW_ON_KEYGUARD);

    // check if popup is enabled for this contact
    boolean showPopup =
        mPrefs.getBoolean(
            R.string.pref_popup_enabled_key,
            Defaults.PREFS_SHOW_POPUP,
            SmsPopupDbAdapter.KEY_POPUP_ENABLED_NUM);

    // check if notifications are on for this contact
    boolean notifEnabled =
        mPrefs.getBoolean(
            R.string.pref_notif_enabled_key,
            Defaults.PREFS_NOTIF_ENABLED,
            SmsPopupDbAdapter.KEY_ENABLED_NUM);

    // get docked state of phone
    int docked_state = mPrefs.getInt(R.string.pref_docked_key, 0);

    boolean docked = docked_state == ExternalEventReceiver.EXTRA_DOCK_STATE_DESK;

    mPrefs.close();

    // Fetch call state, if the user is in a call or the phone is ringing we don't want to show the
    // popup
    TelephonyManager mTM = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    boolean callStateIdle = mTM.getCallState() == TelephonyManager.CALL_STATE_IDLE;

    // Init keyguard manager
    ManageKeyguard.initialize(context);

    /*
     * If popup is enabled for this user -AND- the user is not in a call -AND-
     * -AND- phone is not docked -AND-
     * (screen is locked -OR- (setting is OFF to only show on keyguard -AND- user is not in messaging app:
     * then show the popup activity, otherwise check if notifications are on and just use the standard
     * notification))
     */
    if (showPopup
        && callStateIdle
        && !docked
        && (ManageKeyguard.inKeyguardRestrictedInputMode()
            || (!onlyShowOnKeyguard && !SmsPopupUtils.inMessagingApp(context)))) {

      if (Log.DEBUG) Log.v("^^^^^^Showing SMS Popup");
      ManageWakeLock.acquireFull(context);
      context.startActivity(message.getPopupIntent());

    } else if (notifEnabled) {

      if (Log.DEBUG) Log.v("^^^^^^Not showing SMS Popup, using notifications");
      ManageNotification.show(context, message);
      ReminderReceiver.scheduleReminder(context, message);
    }
  }
 @Override
 protected Bitmap doInBackground(String... params) {
   if (Log.DEBUG) Log.v("Loading contact photo in background...");
   // try { Thread.sleep(2000); } catch (InterruptedException e) {}
   return SmsPopupUtils.getPersonPhoto(SmsPopupActivity.this.getApplicationContext(), params[0]);
 }