Example usage for android.text Spanned getSpanStart

List of usage examples for android.text Spanned getSpanStart

Introduction

In this page you can find the example usage for android.text Spanned getSpanStart.

Prototype

public int getSpanStart(Object tag);

Source Link

Document

Return the beginning 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.jecelyin.editor.v2.core.text.TextUtils.java

/**
 * Debugging tool to print the spans in a CharSequence.  The output will
 * be printed one span per line.  If the CharSequence is not a Spanned,
 * then the entire string will be printed on a single line.
 *///  w  w w  .  j  a  v a 2 s .  co  m
public static void dumpSpans(CharSequence cs, Printer printer, String prefix) {
    if (cs instanceof Spanned) {
        Spanned sp = (Spanned) cs;
        Object[] os = sp.getSpans(0, cs.length(), Object.class);

        for (int i = 0; i < os.length; i++) {
            Object o = os[i];
            printer.println(prefix + cs.subSequence(sp.getSpanStart(o), sp.getSpanEnd(o)) + ": "
                    + Integer.toHexString(System.identityHashCode(o)) + " " + o.getClass().getCanonicalName()
                    + " (" + sp.getSpanStart(o) + "-" + sp.getSpanEnd(o) + ") fl=#" + sp.getSpanFlags(o));
        }
    } else {
        printer.println(prefix + cs + ": (no spans)");
    }
}

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

/**
 * Copies the spans from the region <code>start...end</code> in
 * <code>source</code> to the region
 * <code>destoff...destoff+end-start</code> in <code>dest</code>.
 * Spans in <code>source</code> that begin before <code>start</code>
 * or end after <code>end</code> but overlap this range are trimmed
 * as if they began at <code>start</code> or ended at <code>end</code>.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 *//*w ww . java 2 s  . c  om*/
public static void copySpansFrom(Spanned source, int start, int end, Class kind, Spannable dest, int destoff) {
    if (kind == null) {
        kind = Object.class;
    }

    Object[] spans = source.getSpans(start, end, kind);

    for (int i = 0; i < spans.length; i++) {
        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);
        int fl = source.getSpanFlags(spans[i]);

        if (st < start)
            st = start;
        if (en > end)
            en = end;

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff, fl);
    }
}

From source file:com.googlecode.eyesfree.brailleback.BrailleIME.java

public boolean route(int position, DisplayManager.Content content) {
    InputConnection ic = getCurrentInputConnection();
    Spanned text = content.getSpanned();
    if (ic != null && text != null) {
        MarkingSpan[] spans = text.getSpans(position, position, MarkingSpan.class);
        if (spans.length == 1) {
            if (spans[0] == ACTION_LABEL_SPAN) {
                return emitFeedbackOnFailure(sendDefaultAction(), FeedbackManager.TYPE_COMMAND_FAILED);
            } else if (spans[0] == EDIT_TEXT_SPAN) {
                return emitFeedbackOnFailure(setCursor(ic, position - text.getSpanStart(EDIT_TEXT_SPAN)),
                        FeedbackManager.TYPE_COMMAND_FAILED);
            }//ww  w.j  a  va  2s.c  o  m
        } else if (spans.length == 0) {
            // Most likely, the user clicked on the label/hint part of the
            // content.
            emitFeedback(FeedbackManager.TYPE_NAVIGATE_OUT_OF_BOUNDS);
            return true;
        } else if (spans.length > 1) {
            LogUtils.log(this, Log.ERROR, "Conflicting spans in Braille IME");
        }
    }
    emitFeedback(FeedbackManager.TYPE_COMMAND_FAILED);
    return true;
}

From source file:tk.wasdennnoch.androidn_ify.utils.NotificationColorUtil.java

/**
 * Inverts all the grayscale colors set by {@link TextAppearanceSpan}s on
 * the text.//  w w w  . j  a va  2 s .  c om
 *
 * @param charSequence The text to process.
 * @return The color inverted text.
 */
public CharSequence invertCharSequenceColors(CharSequence charSequence) {
    if (charSequence instanceof Spanned) {
        Spanned ss = (Spanned) charSequence;
        Object[] spans = ss.getSpans(0, ss.length(), Object.class);
        SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
        for (Object span : spans) {
            Object resultSpan = span;
            if (span instanceof TextAppearanceSpan) {
                resultSpan = processTextAppearanceSpan((TextAppearanceSpan) span);
            }
            builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span), ss.getSpanFlags(span));
        }
        return builder;
    }
    return charSequence;
}

From source file:com.googlecode.eyesfree.brailleback.SearchNavigationMode.java

/**
 * Formats some braille content from an {@link AccessibilityNodeInfoCompat}.
 * Marks the description with a cursor right after the last character
 * matching the search query. Returns content with keep pan strategy
 * by default, or reset pan strategy if there is no matched node stored yet.
 *//* www .j  ava2 s. c  o  m*/
private DisplayManager.Content formatNodeToBraille(AccessibilityNodeInfoCompat node) {
    if (node == null) {
        return null;
    }
    DisplayManager.Content content = mNodeBrailler.brailleNode(node);
    if (content == null) {
        return null;
    }

    Spanned spanned = content.getSpanned();
    if (spanned == null) {
        LogUtils.log(this, Log.ERROR, "No text for node");
        return null;
    }

    // Find index of match and the index corresponding to the
    // end of the matched text so we know where to place the cursor.
    String lowerCase = spanned.toString().toLowerCase();
    String matchText = mQueryText.toString().toLowerCase();
    AccessibilityNodeInfoCompat spanNode = (AccessibilityNodeInfoCompat) DisplaySpans.getEqualSpan(spanned,
            node);
    int cursorIndex = -1;
    if (spanNode != null) {
        int nodeIndex = spanned.getSpanStart(spanNode);
        if (nodeIndex >= 0 && nodeIndex < lowerCase.length()) {
            int matchIndex = lowerCase.indexOf(matchText, nodeIndex);
            if (matchIndex >= 0 && matchIndex <= lowerCase.length()) {
                cursorIndex = matchIndex + matchText.length();
            }
        }
    }

    // Add prefix to what's displayed to mark this as a search result.
    String prefix = mAccessibilityService.getString(R.string.search_result_prefix);
    int lengthDiff = prefix.length();
    SpannableStringBuilder sb = (SpannableStringBuilder) spanned;
    sb.insert(0, prefix);

    // If match in this node, add cursor at end of match.
    if (cursorIndex != -1) {
        DisplaySpans.addSelection(sb, cursorIndex + lengthDiff, cursorIndex + lengthDiff);
    }

    // No matched node stored, means we have just activated search mode,
    // so we want to reset the panning frame so we can see the search
    // prefix.
    int panStrategy = AccessibilityNodeInfoRef.isNull(mMatchedNode) ? DisplayManager.Content.PAN_RESET
            : DisplayManager.Content.PAN_KEEP;

    return content.setPanStrategy(panStrategy);
}

From source file:com.googlecode.eyesfree.brailleback.DisplayManager.java

private void markFocus(Spanned spanned) {
    DisplaySpans.FocusSpan[] spans = spanned.getSpans(0, spanned.length(), DisplaySpans.FocusSpan.class);
    for (DisplaySpans.FocusSpan span : spans) {
        int start = textToDisplayPosition(mTranslationResult, mCursorPosition, spanned.getSpanStart(span));
        if (start < mOverlaidBrailleContent.length) {
            copyOverlaidContent();/*  w  w w .j  a  v  a  2 s  .co m*/
            mOverlaidBrailleContent[start] |= FOCUS_DOTS;
            if (mDisplayPosition < 0) {
                mDisplayPosition = fixDisplayPosition(start);
            }
        }
    }
}

From source file:com.googlecode.eyesfree.brailleback.DisplayManager.java

private boolean markSelection(Spanned spanned) {
    DisplaySpans.SelectionSpan[] spans = spanned.getSpans(0, spanned.length(),
            DisplaySpans.SelectionSpan.class);
    for (DisplaySpans.SelectionSpan span : spans) {
        int start = textToDisplayPosition(mTranslationResult, mCursorPosition, spanned.getSpanStart(span));
        int end = textToDisplayPosition(mTranslationResult, mCursorPosition, spanned.getSpanEnd(span));
        if (start == end) {
            end = start + 1;//from  ww w .  ja v a2s.  c  om
        }
        if (end > mBrailleContent.length) {
            extendContentForCursor();
        }
        copyOverlaidContent();
        for (int i = start; i < end && i < mOverlaidBrailleContent.length; ++i) {
            mOverlaidBrailleContent[i] |= SELECTION_DOTS;
        }
        if (mDisplayPosition < 0) {
            mDisplayPosition = fixDisplayPosition(start);
        }
    }
    return spans.length > 0;
}

From source file:com.eleybourn.bookcatalogue.utils.Utils.java

/**
 * Linkify partial HTML. Linkify methods remove all spans before building links, this
 * method preserves them.//from  w  w  w. j a  v a  2s.c  o  m
 * 
 * See: http://stackoverflow.com/questions/14538113/using-linkify-addlinks-combine-with-html-fromhtml
 * 
 * @param html         Partial HTML
 * @param linkifyMask   Linkify mask to use in Linkify.addLinks
 * 
 * @return            Spannable with all links
 */
public static Spannable linkifyHtml(String html, int linkifyMask) {
    // Get the spannable HTML
    Spanned text = Html.fromHtml(html);
    // Save the span details for later restoration
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);

    // Build an empty spannable then add the links
    SpannableString buffer = new SpannableString(text);
    Linkify.addLinks(buffer, linkifyMask);

    // Add back the HTML spannables
    for (URLSpan span : currentSpans) {
        int end = text.getSpanEnd(span);
        int start = text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    return buffer;
}

From source file:com.googlecode.eyesfree.brailleback.DisplayManager.java

private int findMatchingPanPosition(Content oldContent, Content newContent,
        TranslationResult oldTranslationResult, TranslationResult newTranslationResult,
        int oldDisplayPosition) {
    Spanned oldSpanned = oldContent.getSpanned();
    Spanned newSpanned = newContent.getSpanned();
    if (oldSpanned == null || newSpanned == null) {
        return -1;
    }/*w  w  w .  j  a v  a2 s. com*/
    // Map the current display start and past-the-end positions
    // to the corresponding input positions.
    int oldTextStart = displayToTextPosition(oldTranslationResult, -1 /*cursorPosition*/, oldDisplayPosition);
    int oldTextEnd = displayToTextPosition(oldTranslationResult, -1 /*cursorPosition*/,
            oldDisplayPosition + getNumTextCells());
    // Find the nodes that overlap with the display.
    AccessibilityNodeInfoCompat[] displayedNodes = oldSpanned.getSpans(oldTextStart, oldTextEnd,
            AccessibilityNodeInfoCompat.class);
    Arrays.sort(displayedNodes, new ByDistanceComparator(oldSpanned, oldTextStart));
    // Find corresponding node in new content.
    for (AccessibilityNodeInfoCompat oldNode : displayedNodes) {
        AccessibilityNodeInfoCompat newNode = (AccessibilityNodeInfoCompat) DisplaySpans
                .getEqualSpan(newSpanned, oldNode);
        if (newNode == null) {
            continue;
        }
        int oldDisplayStart = textToDisplayPosition(oldTranslationResult, -1 /*cursorPosition*/,
                oldSpanned.getSpanStart(oldNode));
        int newDisplayStart = textToDisplayPosition(newTranslationResult, -1 /*cursorPosition*/,
                newSpanned.getSpanStart(newNode));
        // Offset position according to diff in node position.
        int newDisplayPosition = oldDisplayPosition + (newDisplayStart - oldDisplayStart);
        return newDisplayPosition;
    }
    return -1;
}