Search in sources :

Example 71 with URLSpan

use of android.text.style.URLSpan in project android_frameworks_base by ParanoidAndroid.

the class Editor method selectCurrentWord.

/**
     * Adjusts selection to the word under last touch offset.
     * Return true if the operation was successfully performed.
     */
private boolean selectCurrentWord() {
    if (!canSelectText()) {
        return false;
    }
    if (hasPasswordTransformationMethod()) {
        // is however useful to delete or paste to replace the entire content.
        return mTextView.selectAllText();
    }
    int inputType = mTextView.getInputType();
    int klass = inputType & InputType.TYPE_MASK_CLASS;
    int variation = inputType & InputType.TYPE_MASK_VARIATION;
    // Specific text field types: select the entire text for these
    if (klass == InputType.TYPE_CLASS_NUMBER || klass == InputType.TYPE_CLASS_PHONE || klass == InputType.TYPE_CLASS_DATETIME || variation == InputType.TYPE_TEXT_VARIATION_URI || variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS || variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS || variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
        return mTextView.selectAllText();
    }
    long lastTouchOffsets = getLastTouchOffsets();
    final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets);
    final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets);
    // Safety check in case standard touch event handling has been bypassed
    if (minOffset < 0 || minOffset >= mTextView.getText().length())
        return false;
    if (maxOffset < 0 || maxOffset >= mTextView.getText().length())
        return false;
    int selectionStart, selectionEnd;
    // If a URLSpan (web address, email, phone...) is found at that position, select it.
    URLSpan[] urlSpans = ((Spanned) mTextView.getText()).getSpans(minOffset, maxOffset, URLSpan.class);
    if (urlSpans.length >= 1) {
        URLSpan urlSpan = urlSpans[0];
        selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan);
        selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan);
    } else {
        final WordIterator wordIterator = getWordIterator();
        wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset);
        selectionStart = wordIterator.getBeginning(minOffset);
        selectionEnd = wordIterator.getEnd(maxOffset);
        if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE || selectionStart == selectionEnd) {
            // Possible when the word iterator does not properly handle the text's language
            long range = getCharRange(minOffset);
            selectionStart = TextUtils.unpackRangeStartFromLong(range);
            selectionEnd = TextUtils.unpackRangeEndFromLong(range);
        }
    }
    Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
    return selectionEnd > selectionStart;
}
Also used : WordIterator(android.text.method.WordIterator) URLSpan(android.text.style.URLSpan) Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Example 72 with URLSpan

use of android.text.style.URLSpan in project android_frameworks_base by ParanoidAndroid.

the class HtmlTest method testMarkup.

@SmallTest
public void testMarkup() throws Exception {
    SpannableString s;
    s = new SpannableString("Hello bold world");
    s.setSpan(new StyleSpan(Typeface.BOLD), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <b>bold</b> world</p>\n");
    s = new SpannableString("Hello italic world");
    s.setSpan(new StyleSpan(Typeface.ITALIC), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <i>italic</i> world</p>\n");
    s = new SpannableString("Hello monospace world");
    s.setSpan(new TypefaceSpan("monospace"), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <tt>monospace</tt> world</p>\n");
    s = new SpannableString("Hello superscript world");
    s.setSpan(new SuperscriptSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <sup>superscript</sup> world</p>\n");
    s = new SpannableString("Hello subscript world");
    s.setSpan(new SubscriptSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <sub>subscript</sub> world</p>\n");
    s = new SpannableString("Hello underline world");
    s.setSpan(new UnderlineSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <u>underline</u> world</p>\n");
    s = new SpannableString("Hello struck world");
    s.setSpan(new StrikethroughSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <strike>struck</strike> world</p>\n");
    s = new SpannableString("Hello linky world");
    s.setSpan(new URLSpan("http://www.google.com"), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <a href=\"http://www.google.com\">linky</a> world</p>\n");
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) URLSpan(android.text.style.URLSpan) UnderlineSpan(android.text.style.UnderlineSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 73 with URLSpan

use of android.text.style.URLSpan in project platform_frameworks_base by android.

the class HtmlToSpannedConverter method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
    int next;
    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()) {
                    Application application = ActivityThread.currentApplication();
                    sizeDip /= application.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>");
                }
            }
        }
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) Application(android.app.Application) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 74 with URLSpan

use of android.text.style.URLSpan in project XobotOS by xamarin.

the class HtmlToSpannedConverter method endA.

private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);
    if (where != len) {
        Href h = (Href) obj;
        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
Also used : URLSpan(android.text.style.URLSpan)

Example 75 with URLSpan

use of android.text.style.URLSpan in project XobotOS by xamarin.

the class TextView method selectCurrentWord.

/**
     * Adjusts selection to the word under last touch offset.
     * Return true if the operation was successfully performed.
     */
private boolean selectCurrentWord() {
    if (!canSelectText()) {
        return false;
    }
    if (hasPasswordTransformationMethod()) {
        // is however useful to delete or paste to replace the entire content.
        return selectAll();
    }
    int klass = mInputType & InputType.TYPE_MASK_CLASS;
    int variation = mInputType & InputType.TYPE_MASK_VARIATION;
    // Specific text field types: select the entire text for these
    if (klass == InputType.TYPE_CLASS_NUMBER || klass == InputType.TYPE_CLASS_PHONE || klass == InputType.TYPE_CLASS_DATETIME || variation == InputType.TYPE_TEXT_VARIATION_URI || variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS || variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS || variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
        return selectAll();
    }
    long lastTouchOffsets = getLastTouchOffsets();
    final int minOffset = extractRangeStartFromLong(lastTouchOffsets);
    final int maxOffset = extractRangeEndFromLong(lastTouchOffsets);
    // Safety check in case standard touch event handling has been bypassed
    if (minOffset < 0 || minOffset >= mText.length())
        return false;
    if (maxOffset < 0 || maxOffset >= mText.length())
        return false;
    int selectionStart, selectionEnd;
    // If a URLSpan (web address, email, phone...) is found at that position, select it.
    URLSpan[] urlSpans = ((Spanned) mText).getSpans(minOffset, maxOffset, URLSpan.class);
    if (urlSpans.length >= 1) {
        URLSpan urlSpan = urlSpans[0];
        selectionStart = ((Spanned) mText).getSpanStart(urlSpan);
        selectionEnd = ((Spanned) mText).getSpanEnd(urlSpan);
    } else {
        if (mWordIterator == null) {
            mWordIterator = new WordIterator();
        }
        mWordIterator.setCharSequence(mText, minOffset, maxOffset);
        selectionStart = mWordIterator.getBeginning(minOffset);
        if (selectionStart == BreakIterator.DONE)
            return false;
        selectionEnd = mWordIterator.getEnd(maxOffset);
        if (selectionEnd == BreakIterator.DONE)
            return false;
        if (selectionStart == selectionEnd) {
            // Possible when the word iterator does not properly handle the text's language
            long range = getCharRange(selectionStart);
            selectionStart = extractRangeStartFromLong(range);
            selectionEnd = extractRangeEndFromLong(range);
        }
    }
    Selection.setSelection((Spannable) mText, selectionStart, selectionEnd);
    return selectionEnd > selectionStart;
}
Also used : WordIterator(android.text.method.WordIterator) URLSpan(android.text.style.URLSpan) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Aggregations

URLSpan (android.text.style.URLSpan)134 SpannableString (android.text.SpannableString)36 Spannable (android.text.Spannable)22 SpannableStringBuilder (android.text.SpannableStringBuilder)21 TextPaint (android.text.TextPaint)21 Spanned (android.text.Spanned)20 TextView (android.widget.TextView)19 ForegroundColorSpan (android.text.style.ForegroundColorSpan)16 StyleSpan (android.text.style.StyleSpan)16 UnderlineSpan (android.text.style.UnderlineSpan)14 Paint (android.graphics.Paint)13 ImageSpan (android.text.style.ImageSpan)13 StrikethroughSpan (android.text.style.StrikethroughSpan)13 TypefaceSpan (android.text.style.TypefaceSpan)13 View (android.view.View)13 Intent (android.content.Intent)12 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)12 BackgroundColorSpan (android.text.style.BackgroundColorSpan)12 ClickableSpan (android.text.style.ClickableSpan)12 SubscriptSpan (android.text.style.SubscriptSpan)12