Example usage for android.text Spanned getSpans

List of usage examples for android.text Spanned getSpans

Introduction

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

Prototype

public <T> T[] getSpans(int start, int end, Class<T> type);

Source Link

Document

Return an array of the markup objects attached to the specified slice of this CharSequence and whose type is the specified type or a subclass of it.

Usage

From source file:org.miaowo.miaowo.util.Html.java

private static void withinBlockquoteIndividual(StringBuilder out, Spanned text, int start, int end,
        Context context) {//from w  w  w  . jav a2  s  .co m
    boolean isInList = false;
    int next;
    for (int i = start; i <= end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        boolean isListItem = false;
        ParagraphStyle[] paragraphStyles = text.getSpans(i, next, ParagraphStyle.class);
        for (ParagraphStyle paragraphStyle : paragraphStyles) {
            final int spanFlags = text.getSpanFlags(paragraphStyle);
            if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH
                    && paragraphStyle instanceof BulletSpan) {
                isListItem = true;
                break;
            }
        }

        if (isListItem && !isInList) {
            // Current paragraph is the first item in a list
            isInList = true;
            out.append("<ul>\n");
        }

        if (isInList && !isListItem) {
            // Current paragraph is no longer a list item; close the previously opened list
            isInList = false;
            out.append("</ul>\n");
        }

        String tagType = isListItem ? "li" : "p";
        out.append("<").append(tagType).append(getTextDirection(text, start, next))
                .append(getTextStyles(text, start, next)).append(">");

        if (next - i == 0) {
            out.append("<br>");
        } else {
            withinParagraph(out, text, i, next, context);
        }

        out.append("</");
        out.append(tagType);
        out.append(">\n");

        if (next == end && isInList) {
            isInList = false;
            out.append("</ul>\n");
        }

        next++;
    }
}

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);
            }/*from  w w w. j a  v  a  2s.  com*/
        } 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: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.
 *//*from w  w w  . j  ava2  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

/**
 * Flatten a CharSequence and whatever styles can be copied across processes
 * into the parcel.//  ww w.  j a  va  2  s  .  com
 */
public static void writeToParcel(CharSequence cs, Parcel p, int parcelableFlags) {
    if (cs instanceof Spanned) {
        p.writeInt(0);
        p.writeString(cs.toString());

        Spanned sp = (Spanned) cs;
        Object[] os = sp.getSpans(0, cs.length(), Object.class);

        // note to people adding to this: check more specific types
        // before more generic types.  also notice that it uses
        // "if" instead of "else if" where there are interfaces
        // so one object can be several.

        for (int i = 0; i < os.length; i++) {
            Object o = os[i];
            Object prop = os[i];

            if (prop instanceof CharacterStyle) {
                prop = ((CharacterStyle) prop).getUnderlying();
            }

            if (prop instanceof ParcelableSpan) {
                ParcelableSpan ps = (ParcelableSpan) prop;
                int spanTypeId = ps.getSpanTypeId();
                if (spanTypeId < FIRST_SPAN || spanTypeId > LAST_SPAN) {
                    Log.e(TAG, "external class \"" + ps.getClass().getSimpleName()
                            + "\" is attempting to use the frameworks-only ParcelableSpan" + " interface");
                } else {
                    p.writeInt(spanTypeId);
                    ps.writeToParcel(p, parcelableFlags);
                    writeWhere(p, sp, o);
                }
            }
        }

        p.writeInt(0);
    } else {
        p.writeInt(1);
        if (cs != null) {
            p.writeString(cs.toString());
        } else {
            p.writeString(null);
        }
    }
}

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>.
 *//*from  w  w  w .j av  a 2s .co  m*/
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.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();//  ww  w  .  j  a v  a  2  s . com
            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  w  ww  . j av  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.googlecode.eyesfree.brailleback.DisplayManager.java

private boolean allowContractedBraille(Content content) {
    if (content.getContractionMode() == Content.CONTRACT_ALWAYS_ALLOW) {
        return true;
    }//ww  w . jav a 2  s.c o  m
    Spanned spanned = content.getSpanned();
    if (spanned == null) {
        return true;
    }
    DisplaySpans.SelectionSpan[] selectionSpans = spanned.getSpans(0, spanned.length(),
            DisplaySpans.SelectionSpan.class);
    return selectionSpans.length == 0;
}

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

private static float setPara(MeasuredText mt, TextPaint paint, CharSequence text, int start, int end,
        TextDirectionHeuristic textDir) {

    //        mt.setPara(text, start, end, textDir); //jec- ????

    float width;//from w ww . ja  v a2 s .  co  m
    Spanned sp = text instanceof Spanned ? (Spanned) text : null;
    int len = end - start;
    if (sp == null) {
        width = mt.addStyleRun(paint, len, null);
    } else {
        width = 0;
        int spanEnd;
        for (int spanStart = 0; spanStart < len; spanStart = spanEnd) {
            spanEnd = sp.nextSpanTransition(spanStart, len, MetricAffectingSpan.class);
            MetricAffectingSpan[] spans = sp.getSpans(spanStart, spanEnd, MetricAffectingSpan.class);
            spans = TextUtils.removeEmptySpans(spans, sp, MetricAffectingSpan.class);
            width += mt.addStyleRun(paint, spans, spanEnd - spanStart, null);
        }
    }

    return width;
}

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.  ja  v  a 2  s .  co 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;
}