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:com.googlecode.eyesfree.brailleback.DisplayManager.java

private static int findCursorPosition(Content content) {
    Spanned spanned = content.getSpanned();
    if (spanned == null) {
        return -1;
    }/* www .  j a v a 2  s  .c  o  m*/
    DisplaySpans.SelectionSpan[] selectionSpans = spanned.getSpans(0, spanned.length(),
            DisplaySpans.SelectionSpan.class);
    if (selectionSpans.length > 0) {
        return spanned.getSpanStart(selectionSpans[0]);
    }
    DisplaySpans.FocusSpan[] focusSpans = spanned.getSpans(0, spanned.length(), DisplaySpans.FocusSpan.class);
    if (focusSpans.length > 0) {
        return spanned.getSpanStart(focusSpans[0]);
    }
    return -1;
}

From source file:com.google.android.apps.common.testing.accessibility.framework.ClickableSpanInfoCheck.java

@Override
public List<AccessibilityInfoCheckResult> runCheckOnInfo(AccessibilityNodeInfo info, Context context,
        Bundle metadata) {/* w  ww.  j  a  v  a  2  s.c o  m*/
    List<AccessibilityInfoCheckResult> results = new ArrayList<AccessibilityInfoCheckResult>(1);
    AccessibilityNodeInfoCompat compatInfo = new AccessibilityNodeInfoCompat(info);
    if (AccessibilityNodeInfoUtils.nodeMatchesAnyClassByType(context, compatInfo, TextView.class)) {
        if (info.getText() instanceof Spanned) {
            Spanned text = (Spanned) info.getText();
            ClickableSpan[] clickableSpans = text.getSpans(0, text.length(), ClickableSpan.class);
            for (ClickableSpan clickableSpan : clickableSpans) {
                if (clickableSpan instanceof URLSpan) {
                    String url = ((URLSpan) clickableSpan).getURL();
                    if (url == null) {
                        results.add(new AccessibilityInfoCheckResult(this.getClass(),
                                AccessibilityCheckResultType.ERROR, "URLSpan has null URL", info));
                    } else {
                        Uri uri = Uri.parse(url);
                        if (uri.isRelative()) {
                            // Relative URIs cannot be resolved.
                            results.add(new AccessibilityInfoCheckResult(this.getClass(),
                                    AccessibilityCheckResultType.ERROR,
                                    "URLSpan should not contain relative links", info));
                        }
                    }
                } else { // Non-URLSpan ClickableSpan
                    results.add(new AccessibilityInfoCheckResult(this.getClass(),
                            AccessibilityCheckResultType.ERROR,
                            "URLSpan should be used in place of ClickableSpan for improved accessibility",
                            info));
                }
            }
        }
    } else {
        results.add(new AccessibilityInfoCheckResult(this.getClass(), AccessibilityCheckResultType.NOT_RUN,
                "View must be a TextView", info));
    }
    return results;
}

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static Object getLast(Spanned text, Class<?> kind) {
    /*//  w w  w. java 2s .com
     * This knows that the last returned object from getSpans()
     * will be the most recently added.
     */
    Object[] objs = text.getSpans(0, text.length(), kind);

    if (objs.length == 0) {
        return null;
    } else {
        return objs[objs.length - 1];
    }
}

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

private static <T> T getLast(Spanned text, Class<T> kind) {
    /*/*from www.jav  a  2s  .  c o m*/
     * This knows that the last returned object from getSpans()
     * will be the most recently added.
     */
    T[] objs = text.getSpans(0, text.length(), kind);

    if (objs.length == 0) {
        return null;
    } else {
        return objs[objs.length - 1];
    }
}

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

private static String getTextStyles(Spanned text, int start, int end) {
    final StringBuilder style = new StringBuilder(" style=\"margin-top:0; margin-bottom:0;");

    final AlignmentSpan[] alignmentSpans = text.getSpans(start, end, AlignmentSpan.class);
    final int len = alignmentSpans.length;
    if (len > 0) {
        final Layout.Alignment alignment = alignmentSpans[len - 1].getAlignment();
        if (alignment == Layout.Alignment.ALIGN_NORMAL) {
            style.append(" text-align:start;");
        } else if (alignment == Layout.Alignment.ALIGN_CENTER) {
            style.append(" text-align:center;");
        } else if (alignment == Layout.Alignment.ALIGN_OPPOSITE) {
            style.append(" text-align:end;");
        }/*from w  w w .  j  a va 2  s.c  o  m*/
    }

    style.append("\"");
    return style.toString();
}

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

private static void withinDiv(StringBuilder out, Spanned text, int start, int end, int option,
        Context context) {/*  www.  j  a  v  a  2  s . c  om*/
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, QuoteSpan.class);
        QuoteSpan[] quotes = text.getSpans(i, next, QuoteSpan.class);

        for (QuoteSpan quote : quotes) {
            out.append("<blockquote>");
        }

        withinBlockquote(out, text, i, next, option, context);

        for (QuoteSpan quote : quotes) {
            out.append("</blockquote>\n");
        }
    }
}

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

private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option, Context context) {
    int len = text.length();

    int next;/* ww  w .  ja  v  a  2 s.com*/
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align = ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next, option, context);

        if (needDiv) {
            out.append("</div>");
        }
    }
}

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

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end, Context context) {
    int next;//from  w  w w  . j a  va2  s . c  om
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);

        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();

                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();

                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");

                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    sizeDip /= context.getResources().getDisplayMetrics().density;
                }

                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }

        withinStyle(out, text, i, next);

        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();

                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();

                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}

From source file:com.taobao.weex.dom.WXTextDomObject.java

/**
 * Adjust span range after truncate due to the wrong span range during span copy and slicing.
 * @param beforeTruncate The span before truncate
 * @param afterTruncate The span after truncate
 *//* w  w w.  j a  va  2 s .co  m*/
private void adjustSpansRange(@NonNull Spanned beforeTruncate, @NonNull Spannable afterTruncate) {
    Object[] spans = beforeTruncate.getSpans(0, beforeTruncate.length(), Object.class);
    for (Object span : spans) {
        int start = beforeTruncate.getSpanStart(span);
        int end = beforeTruncate.getSpanEnd(span);
        if (start == 0 && end == beforeTruncate.length()) {
            afterTruncate.removeSpan(span);
            afterTruncate.setSpan(span, 0, afterTruncate.length(), beforeTruncate.getSpanFlags(span));
        }
    }
}

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

/**
 * Inverts all the grayscale colors set by {@link TextAppearanceSpan}s on
 * the text./*www  . j  a  va  2s  .  c o  m*/
 *
 * @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;
}