RocketChatMessageLayout.java 2.63 KB
Newer Older
1 2 3 4 5
package chat.rocket.android.widget.message;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
6
import android.text.TextUtils;
7 8 9 10
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
11

Yusuke Iwaki's avatar
Yusuke Iwaki committed
12 13
import com.emojione.Emojione;

14
import chat.rocket.android.widget.R;
15
import chat.rocket.android.widget.helper.InlineHightlighter;
16
import chat.rocket.android.widget.helper.Linkify;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
17
import chat.rocket.android.widget.helper.MarkDown;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/**
 */
public class RocketChatMessageLayout extends LinearLayout {
  private LayoutInflater inflater;

  public RocketChatMessageLayout(Context context) {
    super(context);
    initialize(context, null);
  }

  public RocketChatMessageLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context, attrs);
  }

  public RocketChatMessageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize(context, attrs);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public RocketChatMessageLayout(Context context, AttributeSet attrs, int defStyleAttr,
Yusuke Iwaki's avatar
Yusuke Iwaki committed
41
                                 int defStyleRes) {
42 43 44 45 46 47 48 49 50 51 52
    super(context, attrs, defStyleAttr, defStyleRes);
    initialize(context, attrs);
  }

  private void initialize(Context context, AttributeSet attrs) {
    inflater = LayoutInflater.from(context);
    setOrientation(VERTICAL);
  }

  public void setText(String messageBody) {
    removeAllViews();
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    if (messageBody.contains("```")) {
      boolean highlight = false;
      for (final String chunk : TextUtils.split(messageBody.replace("\r\n", "\n"), "```")) {
        final String trimmedChunk = chunk.replaceFirst("^\n", "").replaceFirst("\n$", "");
        if (highlight) {
          appendHighlightTextView(trimmedChunk);
        } else if (trimmedChunk.length() > 0) {
          appendTextView(trimmedChunk);
        }
        highlight = !highlight;
      }
    } else {
      appendTextView(messageBody);
    }
  }

  private void appendHighlightTextView(String text) {
    TextView textView = (TextView) inflater.inflate(R.layout.message_body_highlight, this, false);
    textView.setText(text);

    Linkify.markup(textView);

    addView(textView);
76 77 78
  }

  private void appendTextView(String text) {
Tiago Cunha's avatar
Tiago Cunha committed
79 80 81 82
    if (TextUtils.isEmpty(text)) {
      return;
    }

83
    TextView textView = (TextView) inflater.inflate(R.layout.message_body, this, false);
Yusuke Iwaki's avatar
Yusuke Iwaki committed
84
    textView.setText(Emojione.shortnameToUnicode(text, false));
85

Yusuke Iwaki's avatar
Yusuke Iwaki committed
86
    MarkDown.apply(textView);
87 88 89 90 91 92
    Linkify.markup(textView);
    InlineHightlighter.highlight(textView);

    addView(textView);
  }
}