private void internalAdd(MediaModel media) throws IllegalStateException {
    if (media == null) {
      // Don't add null value into the list.
      return;
    }

    if (media.isText()) {
      String contentType = media.getContentType();
      if (TextUtils.isEmpty(contentType)
          || ContentType.TEXT_PLAIN.equals(contentType)
          || ContentType.TEXT_HTML.equals(contentType)) {
        internalAddOrReplace(mText, media);
        mText = media;
      } else {
        Log.w(
            TAG,
            "[SlideModel] content type " + media.getContentType() + " isn't supported (as text)");
      }
    } else if (media.isImage()) {
      if (mCanAddImage) {
        internalAddOrReplace(mImage, media);
        mImage = media;
        mCanAddVideo = false;
      } else {
        Log.w(
            TAG,
            "[SlideModel] content type "
                + media.getContentType()
                + " - can't add image in this state");
      }
    } else if (media.isAudio()) {
      if (mCanAddAudio) {
        internalAddOrReplace(mAudio, media);
        mAudio = media;
        mCanAddVideo = false;
      } else {
        Log.w(
            TAG,
            "[SlideModel] content type "
                + media.getContentType()
                + " - can't add audio in this state");
      }
    } else if (media.isVideo()) {
      if (mCanAddVideo) {
        internalAddOrReplace(mVideo, media);
        mVideo = media;
        mCanAddImage = false;
        mCanAddAudio = false;
      } else {
        Log.w(
            TAG,
            "[SlideModel] content type "
                + media.getContentType()
                + " - can't add video in this state");
      }
    }
  }
Example #2
0
  private CharSequence formatMessage(
      MessageItem msgItem,
      String contact,
      String body,
      String subject,
      Pattern highlight,
      String contentType) {
    SpannableStringBuilder buf = new SpannableStringBuilder();

    boolean hasSubject = !TextUtils.isEmpty(subject);
    SmileyParser parser = SmileyParser.getInstance();
    if (hasSubject) {
      CharSequence smilizedSubject = parser.addSmileySpans(subject);
      // Can't use the normal getString() with extra arguments for string replacement
      // because it doesn't preserve the SpannableText returned by addSmileySpans.
      // We have to manually replace the %s with our text.
      buf.append(
          TextUtils.replace(
              mContext.getResources().getString(R.string.inline_subject),
              new String[] {"%s"},
              new CharSequence[] {smilizedSubject}));
    }

    if (!TextUtils.isEmpty(body)) {
      // Converts html to spannable if ContentType is "text/html".
      if (contentType != null && ContentType.TEXT_HTML.equals(contentType)) {
        buf.append("\n");
        buf.append(Html.fromHtml(body));
      } else {
        if (hasSubject) {
          buf.append(" - ");
        }
        buf.append(parser.addSmileySpans(body));
      }
    }

    if (highlight != null) {
      Matcher m = highlight.matcher(buf.toString());
      while (m.find()) {
        buf.setSpan(new StyleSpan(Typeface.BOLD), m.start(), m.end(), 0);
      }
    }
    return buf;
  }