Search in sources :

Example 1 with DividerSpan

use of de.pixart.messenger.ui.text.DividerSpan in project Pix-Art-Messenger by kriztan.

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(de.pixart.messenger.ui.text.DividerSpan) QuoteSpan(de.pixart.messenger.ui.text.QuoteSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 2 with DividerSpan

use of de.pixart.messenger.ui.text.DividerSpan in project Pix-Art-Messenger by kriztan.

the class MessageAdapter method applyQuoteSpan.

private int 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.bubble);
    DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    body.setSpan(new QuoteSpan(color, metrics), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return 0;
}
Also used : DividerSpan(de.pixart.messenger.ui.text.DividerSpan) QuoteSpan(de.pixart.messenger.ui.text.QuoteSpan) DisplayMetrics(android.util.DisplayMetrics)

Example 3 with DividerSpan

use of de.pixart.messenger.ui.text.DividerSpan in project Pix-Art-Messenger by kriztan.

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 ? 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 (mConversationFragment != null) {
            String searchQuery = mConversationFragment.binding.searchfieldInput.getText().toString().toLowerCase().trim();
            if (((!searchQuery.isEmpty() || !searchQuery.contains("")) && searchQuery.length() >= 3) && body.toString().toLowerCase().contains(searchQuery)) {
                int ofe = body.toString().toLowerCase().indexOf(searchQuery, 0);
                for (int ofs = 0; ofs < body.length() && ofe != -1; ofs = ofe + 1) {
                    ofe = body.toString().toLowerCase().indexOf(searchQuery, ofs);
                    if (ofe == -1) {
                        break;
                    } else {
                        body.setSpan(new StyleSpan(Typeface.BOLD), ofe, ofe + searchQuery.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        body.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + searchQuery.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }
        }
        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.getType() != Message.TYPE_PRIVATE) {
            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 {
                final String to;
                if (message.getCounterpart() != null) {
                    to = message.getCounterpart().getResourcepart();
                } else {
                    to = "";
                }
                privateMarker = activity.getString(R.string.private_message_to, to);
            }
            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) {
            Pattern pattern = NotificationService.generateNickHighlightPattern(message.getConversation().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.generatePattern(body).matcher(body);
        while (matcher.find()) {
            if (matcher.start() < matcher.end()) {
                body.setSpan(new RelativeSizeSpan(1.5f), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        StylingHelper.format(body, viewHolder.messageBody.getCurrentTextColor());
        Linkify.addLinks(body, XMPP_PATTERN, "xmpp", XMPPURI_MATCH_FILTER, null);
        Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
        Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
        FixedURLSpan.fix(body);
        viewHolder.messageBody.setAutoLinkMask(0);
        viewHolder.messageBody.setText(EmojiWrapper.transform(body));
        viewHolder.messageBody.setTextIsSelectable(true);
        viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance());
        listSelectionManager.onUpdate(viewHolder.messageBody, message);
    } else {
        viewHolder.messageBody.setText("");
        viewHolder.messageBody.setTextIsSelectable(false);
    }
}
Also used : Pattern(java.util.regex.Pattern) Message(de.pixart.messenger.entities.Message) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Matcher(java.util.regex.Matcher) SpannableString(android.text.SpannableString) RelativeSizeSpan(android.text.style.RelativeSizeSpan) StyleSpan(android.text.style.StyleSpan) DividerSpan(de.pixart.messenger.ui.text.DividerSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan)

Aggregations

DividerSpan (de.pixart.messenger.ui.text.DividerSpan)3 SpannableStringBuilder (android.text.SpannableStringBuilder)2 QuoteSpan (de.pixart.messenger.ui.text.QuoteSpan)2 SpannableString (android.text.SpannableString)1 BackgroundColorSpan (android.text.style.BackgroundColorSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 RelativeSizeSpan (android.text.style.RelativeSizeSpan)1 StyleSpan (android.text.style.StyleSpan)1 DisplayMetrics (android.util.DisplayMetrics)1 Message (de.pixart.messenger.entities.Message)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1