Example usage for android.text SpannableStringBuilder getSpans

List of usage examples for android.text SpannableStringBuilder getSpans

Introduction

In this page you can find the example usage for android.text SpannableStringBuilder getSpans.

Prototype

@SuppressWarnings("unchecked")
public <T> T[] getSpans(int queryStart, int queryEnd, @Nullable Class<T> kind) 

Source Link

Document

Return an array of the spans of the specified type that overlap the specified range of the buffer.

Usage

From source file:org.telegram.ui.Components.ChatActivityEnterView.java

public void setEditingMessageObject(MessageObject messageObject, boolean caption) {
    if (audioToSend != null || editingMessageObject == messageObject) {
        return;//  w w w  . j  av  a  2s .  c o m
    }
    if (editingMessageReqId != 0) {
        ConnectionsManager.getInstance().cancelRequest(editingMessageReqId, true);
        editingMessageReqId = 0;
    }
    editingMessageObject = messageObject;
    editingCaption = caption;
    if (editingMessageObject != null) {
        if (doneButtonAnimation != null) {
            doneButtonAnimation.cancel();
            doneButtonAnimation = null;
        }
        doneButtonContainer.setVisibility(View.VISIBLE);
        showEditDoneProgress(true, false);

        InputFilter[] inputFilters = new InputFilter[1];
        if (caption) {
            inputFilters[0] = new InputFilter.LengthFilter(200);
            if (editingMessageObject.caption != null) {
                setFieldText(Emoji.replaceEmoji(
                        new SpannableStringBuilder(editingMessageObject.caption.toString()),
                        messageEditText.getPaint().getFontMetricsInt(), AndroidUtilities.dp(20), false));
            } else {
                setFieldText("");
            }
        } else {
            inputFilters[0] = new InputFilter.LengthFilter(4096);
            if (editingMessageObject.messageText != null) {
                ArrayList<TLRPC.MessageEntity> entities = editingMessageObject.messageOwner.entities;//MessagesQuery.getEntities(message);
                MessagesQuery.sortEntities(entities);
                SpannableStringBuilder stringBuilder = new SpannableStringBuilder(
                        editingMessageObject.messageText);
                Object spansToRemove[] = stringBuilder.getSpans(0, stringBuilder.length(), Object.class);
                if (spansToRemove != null && spansToRemove.length > 0) {
                    for (int a = 0; a < spansToRemove.length; a++) {
                        stringBuilder.removeSpan(spansToRemove[a]);
                    }
                }
                if (entities != null) {
                    int addToOffset = 0;
                    try {
                        for (int a = 0; a < entities.size(); a++) {
                            TLRPC.MessageEntity entity = entities.get(a);
                            if (entity.offset + entity.length + addToOffset > stringBuilder.length()) {
                                continue;
                            }
                            if (entity instanceof TLRPC.TL_inputMessageEntityMentionName) {
                                if (entity.offset + entity.length + addToOffset < stringBuilder.length()
                                        && stringBuilder
                                                .charAt(entity.offset + entity.length + addToOffset) == ' ') {
                                    entity.length++;
                                }
                                stringBuilder.setSpan(new URLSpanUserMention(
                                        "" + ((TLRPC.TL_inputMessageEntityMentionName) entity).user_id.user_id,
                                        true), entity.offset + addToOffset,
                                        entity.offset + entity.length + addToOffset,
                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            } else if (entity instanceof TLRPC.TL_messageEntityCode) {
                                stringBuilder.insert(entity.offset + entity.length + addToOffset, "`");
                                stringBuilder.insert(entity.offset + addToOffset, "`");
                                addToOffset += 2;
                            } else if (entity instanceof TLRPC.TL_messageEntityPre) {
                                stringBuilder.insert(entity.offset + entity.length + addToOffset, "```");
                                stringBuilder.insert(entity.offset + addToOffset, "```");
                                addToOffset += 6;
                            } else if (entity instanceof TLRPC.TL_messageEntityBold) {
                                stringBuilder.setSpan(
                                        new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")),
                                        entity.offset + addToOffset,
                                        entity.offset + entity.length + addToOffset,
                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            } else if (entity instanceof TLRPC.TL_messageEntityItalic) {
                                stringBuilder.setSpan(
                                        new TypefaceSpan(AndroidUtilities.getTypeface("fonts/ritalic.ttf")),
                                        entity.offset + addToOffset,
                                        entity.offset + entity.length + addToOffset,
                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            }
                        }
                    } catch (Exception e) {
                        FileLog.e(e);
                    }
                }
                setFieldText(Emoji.replaceEmoji(stringBuilder, messageEditText.getPaint().getFontMetricsInt(),
                        AndroidUtilities.dp(20), false));
            } else {
                setFieldText("");
            }
        }
        messageEditText.setFilters(inputFilters);
        openKeyboard();
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) messageEditText.getLayoutParams();
        layoutParams.rightMargin = AndroidUtilities.dp(4);
        messageEditText.setLayoutParams(layoutParams);
        sendButton.setVisibility(GONE);
        cancelBotButton.setVisibility(GONE);
        audioVideoButtonContainer.setVisibility(GONE);
        attachLayout.setVisibility(GONE);
        sendButtonContainer.setVisibility(GONE);
    } else {
        doneButtonContainer.setVisibility(View.GONE);
        messageEditText.setFilters(new InputFilter[0]);
        delegate.onMessageEditEnd(false);
        audioVideoButtonContainer.setVisibility(VISIBLE);
        attachLayout.setVisibility(VISIBLE);
        sendButtonContainer.setVisibility(VISIBLE);
        attachLayout.setScaleX(1.0f);
        attachLayout.setAlpha(1.0f);
        sendButton.setScaleX(0.1f);
        sendButton.setScaleY(0.1f);
        sendButton.setAlpha(0.0f);
        cancelBotButton.setScaleX(0.1f);
        cancelBotButton.setScaleY(0.1f);
        cancelBotButton.setAlpha(0.0f);
        audioVideoButtonContainer.setScaleX(1.0f);
        audioVideoButtonContainer.setScaleY(1.0f);
        audioVideoButtonContainer.setAlpha(1.0f);
        sendButton.setVisibility(GONE);
        cancelBotButton.setVisibility(GONE);
        messageEditText.setText("");
        if (getVisibility() == VISIBLE) {
            delegate.onAttachButtonShow();
        }
        updateFieldRight(1);
    }
    updateFieldHint();
}

From source file:com.tct.mail.browse.ConversationItemView.java

private void layoutParticipantText(SpannableStringBuilder participantText) {
    if (participantText != null) {
        if (isActivated() && showActivatedText()) {
            participantText.setSpan(sActivatedTextSpan, 0, mHeader.styledMessageInfoStringOffset,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {//  ww w.  j  av  a2  s  . c om
            participantText.removeSpan(sActivatedTextSpan);
        }

        //TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD_S
        //Note sender width should use define in xml
        final int w = mCoordinates.sendersWidth;
        //TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD_E
        final int h = mCoordinates.sendersHeight;
        mSendersTextView.setLayoutParams(new ViewGroup.LayoutParams(w, h));
        mSendersTextView.setMaxLines(mCoordinates.sendersLineCount);
        mSendersTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mCoordinates.sendersFontSize);
        layoutViewExactly(mSendersTextView, w, h);

        /// TCT: add for search term highlight @{
        boolean hasFilter = (mSearchParams != null && !TextUtils.isEmpty(mSearchParams.mFilter));
        boolean fieldMatched = (mSearchParams != null
                && (SearchParams.SEARCH_FIELD_FROM.equals(mSearchParams.mField)
                        || SearchParams.SEARCH_FIELD_ALL.equals(mSearchParams.mField)
                        || SearchParams.SEARCH_FIELD_TO.equals(mSearchParams.mField))); //porting from PR937141
        if (hasFilter && fieldMatched) {
            CharacterStyle[] spans = participantText.getSpans(0, participantText.length(),
                    CharacterStyle.class);
            String senderToHightlight = participantText.toString();
            CharSequence highlightedSender = TextUtilities.highlightTermsInText(senderToHightlight,
                    mSearchParams.mFilter);
            highlightedSender = copyStyles(spans, highlightedSender);
            mSendersTextView.setText(highlightedSender);
        } else {
            mSendersTextView.setText(participantText);
        }
        /// @}
    }
}

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

private SpannableStringBuilder getClickableText(CharSequence sequence) {
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }/* w  w  w.  ja va  2  s . c  o  m*/
    return strBuilder;
}

From source file:org.telegram.ui.ArticleViewer.java

private CharSequence getText(TLRPC.RichText parentRichText, TLRPC.RichText richText,
        TLRPC.PageBlock parentBlock) {//w w w . jav a 2  s.co  m
    if (richText instanceof TLRPC.TL_textFixed) {
        return getText(parentRichText, ((TLRPC.TL_textFixed) richText).text, parentBlock);
    } else if (richText instanceof TLRPC.TL_textItalic) {
        return getText(parentRichText, ((TLRPC.TL_textItalic) richText).text, parentBlock);
    } else if (richText instanceof TLRPC.TL_textBold) {
        return getText(parentRichText, ((TLRPC.TL_textBold) richText).text, parentBlock);
    } else if (richText instanceof TLRPC.TL_textUnderline) {
        return getText(parentRichText, ((TLRPC.TL_textUnderline) richText).text, parentBlock);
    } else if (richText instanceof TLRPC.TL_textStrike) {
        return getText(parentRichText, ((TLRPC.TL_textStrike) richText).text, parentBlock);
    } else if (richText instanceof TLRPC.TL_textEmail) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(
                getText(parentRichText, ((TLRPC.TL_textEmail) richText).text, parentBlock));
        MetricAffectingSpan innerSpans[] = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(),
                MetricAffectingSpan.class);
        spannableStringBuilder.setSpan(
                new TextPaintUrlSpan(innerSpans == null || innerSpans.length == 0
                        ? getTextPaint(parentRichText, richText, parentBlock)
                        : null, getUrl(richText)),
                0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textUrl) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(
                getText(parentRichText, ((TLRPC.TL_textUrl) richText).text, parentBlock));
        MetricAffectingSpan innerSpans[] = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(),
                MetricAffectingSpan.class);
        spannableStringBuilder.setSpan(
                new TextPaintUrlSpan(innerSpans == null || innerSpans.length == 0
                        ? getTextPaint(parentRichText, richText, parentBlock)
                        : null, getUrl(richText)),
                0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textPlain) {
        return ((TLRPC.TL_textPlain) richText).text;
    } else if (richText instanceof TLRPC.TL_textEmpty) {
        return "";
    } else if (richText instanceof TLRPC.TL_textConcat) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        int count = richText.texts.size();
        for (int a = 0; a < count; a++) {
            TLRPC.RichText innerRichText = richText.texts.get(a);
            CharSequence innerText = getText(parentRichText, innerRichText, parentBlock);
            int flags = getTextFlags(innerRichText);
            int startLength = spannableStringBuilder.length();
            spannableStringBuilder.append(innerText);
            if (flags != 0 && !(innerText instanceof SpannableStringBuilder)) {
                if ((flags & TEXT_FLAG_URL) != 0) {
                    String url = getUrl(innerRichText);
                    if (url == null) {
                        url = getUrl(parentRichText);
                    }
                    spannableStringBuilder.setSpan(
                            new TextPaintUrlSpan(getTextPaint(parentRichText, innerRichText, parentBlock), url),
                            startLength, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    spannableStringBuilder.setSpan(
                            new TextPaintSpan(getTextPaint(parentRichText, innerRichText, parentBlock)),
                            startLength, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return spannableStringBuilder;
    }
    return "not supported " + richText;
}