Search in sources :

Example 1 with DividerSpan

use of eu.siacs.conversations.ui.text.DividerSpan in project Conversations by siacs.

the class MessageAdapter method transformText.

private String transformText(CharSequence text, int start, int end, boolean forCopy) {
    SpannableStringBuilder builder = new SpannableStringBuilder(text);
    Object copySpan = new Object();
    builder.setSpan(copySpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    DividerSpan[] dividerSpans = builder.getSpans(0, builder.length(), DividerSpan.class);
    for (DividerSpan dividerSpan : dividerSpans) {
        builder.replace(builder.getSpanStart(dividerSpan), builder.getSpanEnd(dividerSpan), dividerSpan.isLarge() ? "\n\n" : "\n");
    }
    start = builder.getSpanStart(copySpan);
    end = builder.getSpanEnd(copySpan);
    if (start == -1 || end == -1)
        return "";
    builder = new SpannableStringBuilder(builder, start, end);
    if (forCopy) {
        QuoteSpan[] quoteSpans = builder.getSpans(0, builder.length(), QuoteSpan.class);
        for (QuoteSpan quoteSpan : quoteSpans) {
            builder.insert(builder.getSpanStart(quoteSpan), "> ");
        }
    }
    return builder.toString();
}
Also used : DividerSpan(eu.siacs.conversations.ui.text.DividerSpan) QuoteSpan(eu.siacs.conversations.ui.text.QuoteSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 2 with DividerSpan

use of eu.siacs.conversations.ui.text.DividerSpan in project Conversations by siacs.

the class MessageAdapter method displayTextMessage.

private void displayTextMessage(final ViewHolder viewHolder, final Message message, boolean darkBackground, int type) {
    viewHolder.download_button.setVisibility(View.GONE);
    viewHolder.image.setVisibility(View.GONE);
    viewHolder.audioPlayer.setVisibility(View.GONE);
    viewHolder.messageBody.setVisibility(View.VISIBLE);
    if (darkBackground) {
        viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1_OnDark);
    } else {
        viewHolder.messageBody.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Body1);
    }
    viewHolder.messageBody.setHighlightColor(ContextCompat.getColor(activity, darkBackground ? (type == SENT || !mUseGreenBackground ? R.color.black26 : R.color.grey800) : R.color.grey500));
    viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
    if (message.getBody() != null) {
        final String nick = UIHelper.getMessageDisplayName(message);
        SpannableStringBuilder body = message.getMergedBody();
        boolean hasMeCommand = message.hasMeCommand();
        if (hasMeCommand) {
            body = body.replace(0, Message.ME_COMMAND.length(), nick + " ");
        }
        if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
            body = new SpannableStringBuilder(body, 0, Config.MAX_DISPLAY_MESSAGE_CHARS);
            body.append("\u2026");
        }
        Message.MergeSeparator[] mergeSeparators = body.getSpans(0, body.length(), Message.MergeSeparator.class);
        for (Message.MergeSeparator mergeSeparator : mergeSeparators) {
            int start = body.getSpanStart(mergeSeparator);
            int end = body.getSpanEnd(mergeSeparator);
            body.setSpan(new DividerSpan(true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        boolean startsWithQuote = handleTextQuotes(body, darkBackground);
        if (!message.isPrivateMessage()) {
            if (hasMeCommand) {
                body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } else {
            String privateMarker;
            if (message.getStatus() <= Message.STATUS_RECEIVED) {
                privateMarker = activity.getString(R.string.private_message);
            } else {
                Jid cp = message.getCounterpart();
                privateMarker = activity.getString(R.string.private_message_to, Strings.nullToEmpty(cp == null ? null : cp.getResource()));
            }
            body.insert(0, privateMarker);
            int privateMarkerIndex = privateMarker.length();
            if (startsWithQuote) {
                body.insert(privateMarkerIndex, "\n\n");
                body.setSpan(new DividerSpan(false), privateMarkerIndex, privateMarkerIndex + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                body.insert(privateMarkerIndex, " ");
            }
            body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            body.setSpan(new StyleSpan(Typeface.BOLD), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if (hasMeCommand) {
                body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), privateMarkerIndex + 1, privateMarkerIndex + 1 + nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        if (message.getConversation().getMode() == Conversation.MODE_MULTI && message.getStatus() == Message.STATUS_RECEIVED) {
            if (message.getConversation() instanceof Conversation) {
                final Conversation conversation = (Conversation) message.getConversation();
                Pattern pattern = NotificationService.generateNickHighlightPattern(conversation.getMucOptions().getActualNick());
                Matcher matcher = pattern.matcher(body);
                while (matcher.find()) {
                    body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        Matcher matcher = Emoticons.getEmojiPattern(body).matcher(body);
        while (matcher.find()) {
            if (matcher.start() < matcher.end()) {
                body.setSpan(new RelativeSizeSpan(1.2f), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        StylingHelper.format(body, viewHolder.messageBody.getCurrentTextColor());
        if (highlightedTerm != null) {
            StylingHelper.highlight(activity, body, highlightedTerm, StylingHelper.isDarkText(viewHolder.messageBody));
        }
        MyLinkify.addLinks(body, true);
        viewHolder.messageBody.setAutoLinkMask(0);
        viewHolder.messageBody.setText(EmojiWrapper.transform(body));
        viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance());
    } else {
        viewHolder.messageBody.setText("");
        viewHolder.messageBody.setTextIsSelectable(false);
    }
}
Also used : Pattern(java.util.regex.Pattern) Message(eu.siacs.conversations.entities.Message) Jid(eu.siacs.conversations.xmpp.Jid) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Matcher(java.util.regex.Matcher) Conversation(eu.siacs.conversations.entities.Conversation) SpannableString(android.text.SpannableString) RelativeSizeSpan(android.text.style.RelativeSizeSpan) StyleSpan(android.text.style.StyleSpan) DividerSpan(eu.siacs.conversations.ui.text.DividerSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 3 with DividerSpan

use of eu.siacs.conversations.ui.text.DividerSpan in project Conversations by siacs.

the class MessageAdapter method applyQuoteSpan.

private void applyQuoteSpan(SpannableStringBuilder body, int start, int end, boolean darkBackground) {
    if (start > 1 && !"\n\n".equals(body.subSequence(start - 2, start).toString())) {
        body.insert(start++, "\n");
        body.setSpan(new DividerSpan(false), start - 2, start, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        end++;
    }
    if (end < body.length() - 1 && !"\n\n".equals(body.subSequence(end, end + 2).toString())) {
        body.insert(end, "\n");
        body.setSpan(new DividerSpan(false), end, end + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    int color = darkBackground ? this.getMessageTextColor(darkBackground, false) : ContextCompat.getColor(activity, R.color.green700_desaturated);
    DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    body.setSpan(new QuoteSpan(color, metrics), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Also used : DividerSpan(eu.siacs.conversations.ui.text.DividerSpan) QuoteSpan(eu.siacs.conversations.ui.text.QuoteSpan) DisplayMetrics(android.util.DisplayMetrics)

Aggregations

DividerSpan (eu.siacs.conversations.ui.text.DividerSpan)3 SpannableStringBuilder (android.text.SpannableStringBuilder)2 QuoteSpan (eu.siacs.conversations.ui.text.QuoteSpan)2 SpannableString (android.text.SpannableString)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 RelativeSizeSpan (android.text.style.RelativeSizeSpan)1 StyleSpan (android.text.style.StyleSpan)1 DisplayMetrics (android.util.DisplayMetrics)1 Conversation (eu.siacs.conversations.entities.Conversation)1 Message (eu.siacs.conversations.entities.Message)1 Jid (eu.siacs.conversations.xmpp.Jid)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1