From 8f8cdb0879a331bc1e3e52475800a4b528ed3e40 Mon Sep 17 00:00:00 2001 From: Nick Butcher Date: Mon, 25 Jul 2016 17:48:52 +0100 Subject: [PATCH] Temp workaround for blank link issue. --- .../java/io/plaidapp/util/DribbbleUtils.java | 7 +-- .../main/java/io/plaidapp/util/HtmlUtils.java | 49 +++++++++++++------ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/io/plaidapp/util/DribbbleUtils.java b/app/src/main/java/io/plaidapp/util/DribbbleUtils.java index e9999c47c..865516a72 100644 --- a/app/src/main/java/io/plaidapp/util/DribbbleUtils.java +++ b/app/src/main/java/io/plaidapp/util/DribbbleUtils.java @@ -19,9 +19,10 @@ private DribbbleUtils() { } * 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) { + 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); diff --git a/app/src/main/java/io/plaidapp/util/HtmlUtils.java b/app/src/main/java/io/plaidapp/util/HtmlUtils.java index 14a516c29..99f9ddff1 100644 --- a/app/src/main/java/io/plaidapp/util/HtmlUtils.java +++ b/app/src/main/java/io/plaidapp/util/HtmlUtils.java @@ -17,6 +17,7 @@ package io.plaidapp.util; import android.content.res.ColorStateList; +import android.os.Build; import android.support.annotation.ColorInt; import android.text.Html; import android.text.SpannableString; @@ -57,10 +58,11 @@ public static void setTextWithNiceLinks(TextView textView, CharSequence input) { * Parse the given input using {@link TouchableUrlSpan}s rather than vanilla {@link URLSpan}s * so that they respond to touch. */ - public static SpannableStringBuilder parseHtml(String input, - ColorStateList linkTextColor, - @ColorInt int linkHighlightColor) { - SpannableStringBuilder spanned = (SpannableStringBuilder) Html.fromHtml(input); + public static SpannableStringBuilder parseHtml( + String input, + ColorStateList linkTextColor, + @ColorInt int linkHighlightColor) { + SpannableStringBuilder spanned = fromHtml(input); // strip any trailing newlines while (spanned.charAt(spanned.length() - 1) == '\n') { @@ -76,17 +78,22 @@ public static void parseAndSetText(TextView textView, String input) { textView.getHighlightColor())); } - private static SpannableStringBuilder linkifyPlainLinks(CharSequence input, - ColorStateList linkTextColor, - @ColorInt int linkHighlightColor) { + private static SpannableStringBuilder linkifyPlainLinks( + CharSequence input, + ColorStateList linkTextColor, + @ColorInt int linkHighlightColor) { final SpannableString plainLinks = new SpannableString(input); // copy of input - Linkify.addLinks(plainLinks, Linkify.WEB_URLS); + + // 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), @@ -95,6 +102,15 @@ private static SpannableStringBuilder linkifyPlainLinks(CharSequence input, return ssb; } + + private static SpannableStringBuilder fromHtml(String input) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return (SpannableStringBuilder) Html.fromHtml(input, Html.FROM_HTML_MODE_LEGACY); + } else { + return (SpannableStringBuilder) Html.fromHtml(input); + } + } + /** * Parse Markdown and plain-text links. *

@@ -107,10 +123,10 @@ private static SpannableStringBuilder linkifyPlainLinks(CharSequence input, * Best of both worlds. */ public static CharSequence parseMarkdownAndPlainLinks( - TextView textView, - String input, - Bypass markdown, - Bypass.LoadImageCallback loadImageCallback) { + TextView textView, + String input, + Bypass markdown, + Bypass.LoadImageCallback loadImageCallback) { CharSequence markedUp = markdown.markdownToSpannable(input, textView, loadImageCallback); return linkifyPlainLinks( markedUp, textView.getLinkTextColors(), textView.getHighlightColor()); @@ -120,10 +136,11 @@ public static CharSequence parseMarkdownAndPlainLinks( * Parse Markdown and plain-text links and set on the {@link TextView} with proper clickable * spans. */ - public static void parseMarkdownAndSetText(TextView textView, - String input, - Bypass markdown, - Bypass.LoadImageCallback loadImageCallback) { + public static void parseMarkdownAndSetText( + TextView textView, + String input, + Bypass markdown, + Bypass.LoadImageCallback loadImageCallback) { if (TextUtils.isEmpty(input)) return; setTextWithNiceLinks(textView, parseMarkdownAndPlainLinks(textView, input, markdown, loadImageCallback));