Search in sources :

Example 6 with TouchableUrlSpan

use of in.uncod.android.bypass.style.TouchableUrlSpan in project plaid by nickbutcher.

the class FabOverlapTextView method onTouchEvent.

/**
     * This is why you don't implement your own TextView kids; you have to handle everything!
     */
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!(text instanceof Spanned))
        return super.onTouchEvent(event);
    Spannable spannedText = (Spannable) text;
    boolean handled = false;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        pressedSpan = getPressedSpan(spannedText, event);
        if (pressedSpan != null) {
            pressedSpan.setPressed(true);
            Selection.setSelection(spannedText, spannedText.getSpanStart(pressedSpan), spannedText.getSpanEnd(pressedSpan));
            handled = true;
            postInvalidateOnAnimation();
        }
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        TouchableUrlSpan touchedSpan = getPressedSpan(spannedText, event);
        if (pressedSpan != null && touchedSpan != pressedSpan) {
            pressedSpan.setPressed(false);
            pressedSpan = null;
            Selection.removeSelection(spannedText);
            postInvalidateOnAnimation();
        }
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (pressedSpan != null) {
            pressedSpan.setPressed(false);
            pressedSpan.onClick(this);
            handled = true;
            postInvalidateOnAnimation();
        }
        pressedSpan = null;
        Selection.removeSelection(spannedText);
    } else {
        if (pressedSpan != null) {
            pressedSpan.setPressed(false);
            handled = true;
            postInvalidateOnAnimation();
        }
        pressedSpan = null;
        Selection.removeSelection(spannedText);
    }
    return handled;
}
Also used : TouchableUrlSpan(in.uncod.android.bypass.style.TouchableUrlSpan) Spanned(android.text.Spanned) Spannable(android.text.Spannable)

Example 7 with TouchableUrlSpan

use of in.uncod.android.bypass.style.TouchableUrlSpan in project plaid by nickbutcher.

the class DribbbleUtils method parseDribbbleHtml.

/**
     * An extension to {@link HtmlUtils#parseHtml(String, ColorStateList, int)} which adds Dribbble
     * specific behaviour.
     */
public static Spanned parseDribbbleHtml(String input, ColorStateList linkTextColor, @ColorInt int linkHighlightColor) {
    SpannableStringBuilder ssb = HtmlUtils.parseHtml(input, linkTextColor, linkHighlightColor);
    TouchableUrlSpan[] urlSpans = ssb.getSpans(0, ssb.length(), TouchableUrlSpan.class);
    for (TouchableUrlSpan urlSpan : urlSpans) {
        int start = ssb.getSpanStart(urlSpan);
        if (ssb.subSequence(start, start + 1).toString().equals("@")) {
            int end = ssb.getSpanEnd(urlSpan);
            ssb.removeSpan(urlSpan);
            HttpUrl url = HttpUrl.parse(urlSpan.getURL());
            long playerId = -1l;
            String playerUsername = null;
            try {
                playerId = Long.parseLong(url.pathSegments().get(0));
            } catch (NumberFormatException nfe) {
                playerUsername = url.pathSegments().get(0);
            }
            ssb.setSpan(new PlayerSpan(urlSpan.getURL(), ssb.subSequence(start + 1, end).toString(), playerId, playerUsername, linkTextColor, linkHighlightColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return ssb;
}
Also used : TouchableUrlSpan(in.uncod.android.bypass.style.TouchableUrlSpan) PlayerSpan(io.plaidapp.ui.span.PlayerSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) HttpUrl(okhttp3.HttpUrl)

Example 8 with TouchableUrlSpan

use of in.uncod.android.bypass.style.TouchableUrlSpan in project plaid by nickbutcher.

the class HtmlUtils method linkifyPlainLinks.

private static SpannableStringBuilder linkifyPlainLinks(CharSequence input, ColorStateList linkTextColor, @ColorInt int linkHighlightColor) {
    // copy of input
    final SpannableString plainLinks = new SpannableString(input);
    // Linkify doesn't seem to work as expected on M+
    // TODO: figure out why
    //Linkify.addLinks(plainLinks, Linkify.WEB_URLS);
    final URLSpan[] urlSpans = plainLinks.getSpans(0, plainLinks.length(), URLSpan.class);
    // add any plain links to the output
    final SpannableStringBuilder ssb = new SpannableStringBuilder(input);
    for (URLSpan urlSpan : urlSpans) {
        ssb.removeSpan(urlSpan);
        ssb.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor), plainLinks.getSpanStart(urlSpan), plainLinks.getSpanEnd(urlSpan), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return ssb;
}
Also used : SpannableString(android.text.SpannableString) TouchableUrlSpan(in.uncod.android.bypass.style.TouchableUrlSpan) URLSpan(android.text.style.URLSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 9 with TouchableUrlSpan

use of in.uncod.android.bypass.style.TouchableUrlSpan in project sbt-android by scala-android.

the class HtmlUtils method parseHtml.

/**
 * Parse the given input using {@link TouchableUrlSpan}s
 * rather than vanilla {@link android.text.style.URLSpan}s so that they respond to touch.
 *
 * @param input
 * @param linkTextColor
 * @param linkHighlightColor
 * @return
 */
public static Spanned parseHtml(String input, ColorStateList linkTextColor, int linkHighlightColor) {
    SpannableStringBuilder spanned = (SpannableStringBuilder) Html.fromHtml(input);
    // strip any trailing newlines
    while (spanned.charAt(spanned.length() - 1) == '\n') {
        spanned = spanned.delete(spanned.length() - 1, spanned.length());
    }
    URLSpan[] urlSpans = spanned.getSpans(0, spanned.length(), URLSpan.class);
    for (URLSpan urlSpan : urlSpans) {
        int start = spanned.getSpanStart(urlSpan);
        int end = spanned.getSpanEnd(urlSpan);
        spanned.removeSpan(urlSpan);
        // spanned.subSequence(start, start + 1) == "@" TODO send to our own user activity...
        // when i've written it
        spanned.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return spanned;
}
Also used : TouchableUrlSpan(in.uncod.android.bypass.style.TouchableUrlSpan) URLSpan(android.text.style.URLSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 10 with TouchableUrlSpan

use of in.uncod.android.bypass.style.TouchableUrlSpan in project sbt-android by scala-android.

the class LinkTouchMovementMethod method getPressedSpan.

private TouchableUrlSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    x -= textView.getTotalPaddingLeft();
    y -= textView.getTotalPaddingTop();
    x += textView.getScrollX();
    y += textView.getScrollY();
    Layout layout = textView.getLayout();
    int line = layout.getLineForVertical(y);
    int off = layout.getOffsetForHorizontal(line, x);
    TouchableUrlSpan[] link = spannable.getSpans(off, off, TouchableUrlSpan.class);
    TouchableUrlSpan touchedSpan = null;
    if (link.length > 0) {
        touchedSpan = link[0];
    }
    return touchedSpan;
}
Also used : TouchableUrlSpan(in.uncod.android.bypass.style.TouchableUrlSpan) Layout(android.text.Layout)

Aggregations

TouchableUrlSpan (in.uncod.android.bypass.style.TouchableUrlSpan)12 SpannableStringBuilder (android.text.SpannableStringBuilder)7 Layout (android.text.Layout)3 ForegroundColorSpan (android.text.style.ForegroundColorSpan)3 LeadingMarginSpan (android.text.style.LeadingMarginSpan)3 RelativeSizeSpan (android.text.style.RelativeSizeSpan)3 StrikethroughSpan (android.text.style.StrikethroughSpan)3 StyleSpan (android.text.style.StyleSpan)3 TypefaceSpan (android.text.style.TypefaceSpan)3 URLSpan (android.text.style.URLSpan)3 Type (in.uncod.android.bypass.Element.Type)3 FancyQuoteSpan (in.uncod.android.bypass.style.FancyQuoteSpan)3 HorizontalLineSpan (in.uncod.android.bypass.style.HorizontalLineSpan)3 ImageLoadingSpan (in.uncod.android.bypass.style.ImageLoadingSpan)3 SpannableString (android.text.SpannableString)2 Paint (android.graphics.Paint)1 Spannable (android.text.Spannable)1 Spanned (android.text.Spanned)1 TextPaint (android.text.TextPaint)1 PlayerSpan (io.plaidapp.ui.span.PlayerSpan)1