Example usage for android.text Spannable getSpanEnd

List of usage examples for android.text Spannable getSpanEnd

Introduction

In this page you can find the example usage for android.text Spannable getSpanEnd.

Prototype

public int getSpanEnd(Object tag);

Source Link

Document

Return the end of the range of text to which the specified markup object is attached, or -1 if the object is not attached.

Usage

From source file:com.android.ex.chips.RecipientEditTextView.java

/**
 * Create the more chip. The more chip is text that replaces any chips that do not fit in the pre-defined available
 * space when the RecipientEditTextView loses focus.
 *///from   www.  ja v a  2 s . c  o m
// Visible for testing.
/* package */void createMoreChip() {
    if (mNoChips) {
        createMoreChipPlainText();
        return;
    }
    if (!mShouldShrink)
        return;
    final ImageSpan[] tempMore = getSpannable().getSpans(0, getText().length(), MoreImageSpan.class);
    if (tempMore.length > 0)
        getSpannable().removeSpan(tempMore[0]);
    final DrawableRecipientChip[] recipients = getSortedRecipients();
    if (recipients == null || recipients.length <= CHIP_LIMIT) {
        mMoreChip = null;
        return;
    }
    final Spannable spannable = getSpannable();
    final int numRecipients = recipients.length;
    final int overage = numRecipients - CHIP_LIMIT;
    final MoreImageSpan moreSpan = createMoreSpan(overage);
    mRemovedSpans = new ArrayList<DrawableRecipientChip>();
    int totalReplaceStart = 0;
    int totalReplaceEnd = 0;
    final Editable text = getText();
    for (int i = numRecipients - overage; i < recipients.length; i++) {
        mRemovedSpans.add(recipients[i]);
        if (i == numRecipients - overage)
            totalReplaceStart = spannable.getSpanStart(recipients[i]);
        if (i == recipients.length - 1)
            totalReplaceEnd = spannable.getSpanEnd(recipients[i]);
        if (mTemporaryRecipients == null || !mTemporaryRecipients.contains(recipients[i])) {
            final int spanStart = spannable.getSpanStart(recipients[i]);
            final int spanEnd = spannable.getSpanEnd(recipients[i]);
            recipients[i].setOriginalText(text.toString().substring(spanStart, spanEnd));
        }
        spannable.removeSpan(recipients[i]);
    }
    if (totalReplaceEnd < text.length())
        totalReplaceEnd = text.length();
    final int end = Math.max(totalReplaceStart, totalReplaceEnd);
    final int start = Math.min(totalReplaceStart, totalReplaceEnd);
    final SpannableString chipText = new SpannableString(text.subSequence(start, end));
    chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    text.replace(start, end, chipText);
    mMoreChip = moreSpan;
    // If adding the +more chip goes over the limit, resize accordingly.
    if (!isPhoneQuery() && getLineCount() > mMaxLines)
        setMaxLines(getLineCount());
}

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

private boolean checkLayoutForLinks(MotionEvent event, View parentView, StaticLayout layout, int layoutX,
        int layoutY) {
    if (parentView == null || layout == null) {
        return false;
    }/* www  . j  a  v a2  s  . c  om*/
    CharSequence text = layout.getText();
    if (!(text instanceof Spannable)) {
        return false;
    }
    int x = (int) event.getX();
    int y = (int) event.getY();
    boolean removeLink = false;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (x >= layoutX && x <= layoutX + layout.getWidth() && y >= layoutY
                && y <= layoutY + layout.getHeight()) {
            try {
                int checkX = x - layoutX;
                int checkY = y - layoutY;
                final int line = layout.getLineForVertical(checkY);
                final int off = layout.getOffsetForHorizontal(line, checkX);
                final float left = layout.getLineLeft(line);
                if (left <= checkX && left + layout.getLineWidth(line) >= checkX) {
                    Spannable buffer = (Spannable) layout.getText();
                    TextPaintUrlSpan[] link = buffer.getSpans(off, off, TextPaintUrlSpan.class);
                    if (link != null && link.length > 0) {
                        pressedLink = link[0];
                        int pressedStart = buffer.getSpanStart(pressedLink);
                        int pressedEnd = buffer.getSpanEnd(pressedLink);
                        for (int a = 1; a < link.length; a++) {
                            TextPaintUrlSpan span = link[a];
                            int start = buffer.getSpanStart(span);
                            int end = buffer.getSpanEnd(span);
                            if (pressedStart > start || end > pressedEnd) {
                                pressedLink = span;
                                pressedStart = start;
                                pressedEnd = end;
                            }
                        }
                        pressedLinkOwnerLayout = layout;
                        pressedLinkOwnerView = parentView;
                        try {
                            urlPath.setCurrentLayout(layout, pressedStart, 0);
                            layout.getSelectionPath(pressedStart, pressedEnd, urlPath);
                            parentView.invalidate();
                        } catch (Exception e) {
                            FileLog.e(e);
                        }
                    }
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (pressedLink != null) {
            removeLink = true;
            String url = pressedLink.getUrl();
            if (url != null) {
                int index;
                boolean isAnchor = false;
                final String anchor;
                if ((index = url.lastIndexOf('#')) != -1) {
                    anchor = url.substring(index + 1);
                    if (url.toLowerCase().contains(currentPage.url.toLowerCase())) {
                        Integer row = anchors.get(anchor);
                        if (row != null) {
                            layoutManager.scrollToPositionWithOffset(row, 0);
                            isAnchor = true;
                        }
                    }
                } else {
                    anchor = null;
                }
                if (!isAnchor) {
                    if (openUrlReqId == 0) {
                        showProgressView(true);
                        final TLRPC.TL_messages_getWebPage req = new TLRPC.TL_messages_getWebPage();
                        req.url = pressedLink.getUrl();
                        req.hash = 0;
                        openUrlReqId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                            @Override
                            public void run(final TLObject response, TLRPC.TL_error error) {
                                AndroidUtilities.runOnUIThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (openUrlReqId == 0) {
                                            return;
                                        }
                                        openUrlReqId = 0;
                                        showProgressView(false);
                                        if (isVisible) {
                                            if (response instanceof TLRPC.TL_webPage
                                                    && ((TLRPC.TL_webPage) response).cached_page instanceof TLRPC.TL_pageFull) {
                                                addPageToStack((TLRPC.TL_webPage) response, anchor);
                                            } else {
                                                Browser.openUrl(parentActivity, req.url);
                                            }
                                        }
                                    }
                                });
                            }
                        });
                    }
                }
            }
        }
    } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
        removeLink = true;
    }
    if (removeLink && pressedLink != null) {
        pressedLink = null;
        pressedLinkOwnerLayout = null;
        pressedLinkOwnerView = null;
        parentView.invalidate();
    }
    if (pressedLink != null && event.getAction() == MotionEvent.ACTION_DOWN) {
        startCheckLongPress();
    }
    if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) {
        cancelCheckLongPress();
    }
    return pressedLink != null;
}