Use built-in link movement method

pull/1472/head
Alex Baker 5 years ago
parent 7c00d20b2f
commit 1ac8ec0d97

@ -190,7 +190,6 @@ dependencies {
implementation("io.noties.markwon:ext-strikethrough:${Versions.markwon}") implementation("io.noties.markwon:ext-strikethrough:${Versions.markwon}")
implementation("io.noties.markwon:ext-tables:${Versions.markwon}") implementation("io.noties.markwon:ext-tables:${Versions.markwon}")
implementation("io.noties.markwon:linkify:${Versions.markwon}") implementation("io.noties.markwon:linkify:${Versions.markwon}")
implementation("me.saket:better-link-movement-method:2.2.0")
debugImplementation("com.facebook.flipper:flipper:${Versions.flipper}") debugImplementation("com.facebook.flipper:flipper:${Versions.flipper}")
debugImplementation("com.facebook.flipper:flipper-network-plugin:${Versions.flipper}") debugImplementation("com.facebook.flipper:flipper-network-plugin:${Versions.flipper}")

@ -938,12 +938,6 @@
name: commonmark-ext-gfm-strikethrough name: commonmark-ext-gfm-strikethrough
copyrightHolder: Atlassian and others copyrightHolder: Atlassian and others
license: BSD 2-Clause license: BSD 2-Clause
- artifact: me.saket:better-link-movement-method:+
name: better-link-movement-method
copyrightHolder: Saket Narayan
license: The Apache Software License, Version 2.0
licenseUrl: http://www.apache.org/licenses/LICENSE-2.0
url: https://github.com/Saketme/BetterLinkMovementMethod
- artifact: io.noties.markwon:linkify:+ - artifact: io.noties.markwon:linkify:+
name: linkify name: linkify
copyrightHolder: Dimitry Ivanov copyrightHolder: Dimitry Ivanov

@ -2230,20 +2230,6 @@
"normalizedLicense": "bsd_2_clauses", "normalizedLicense": "bsd_2_clauses",
"libraryName": "commonmark-ext-gfm-strikethrough" "libraryName": "commonmark-ext-gfm-strikethrough"
}, },
{
"artifactId": {
"name": "better-link-movement-method",
"group": "me.saket",
"version": "+"
},
"copyrightHolder": "Saket Narayan",
"copyrightStatement": "Copyright © Saket Narayan. All rights reserved.",
"license": "The Apache Software License, Version 2.0",
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0",
"normalizedLicense": "apache2",
"url": "https://github.com/Saketme/BetterLinkMovementMethod",
"libraryName": "better-link-movement-method"
},
{ {
"artifactId": { "artifactId": {
"name": "linkify", "name": "linkify",

@ -4,12 +4,14 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.text.Spannable import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.URLSpan
import android.text.util.Linkify import android.text.util.Linkify
import android.view.MotionEvent import android.view.View
import android.widget.TextView import android.widget.TextView
import androidx.core.text.util.LinkifyCompat import androidx.core.text.util.LinkifyCompat
import dagger.hilt.android.qualifiers.ActivityContext import dagger.hilt.android.qualifiers.ActivityContext
import me.saket.bettermovementmethod.BetterLinkMovementMethod
import org.tasks.R import org.tasks.R
import timber.log.Timber import timber.log.Timber
import java.io.UnsupportedEncodingException import java.io.UnsupportedEncodingException
@ -29,68 +31,78 @@ class Linkify @Inject constructor(
} }
fun setMovementMethod(tv: TextView, handle: (() -> Unit)? = null) { fun setMovementMethod(tv: TextView, handle: (() -> Unit)? = null) {
val blmm = BetterLinkMovementMethod.newInstance().apply { tv.setOnClickListener {
setOnLinkClickListener { _, url -> onClick(url, handle ?: {}) } if (!tv.hasSelection()) {
setOnLinkLongClickListener { _, _ -> tv.performLongClick() } handle?.invoke()
}
tv.movementMethod = blmm
tv.setOnTouchListener { _, event ->
val text = tv.text
when {
text is Spannable && blmm.onTouchEvent(tv, text, event) -> true
event.action == MotionEvent.ACTION_UP -> {
handle?.invoke()
false
}
else -> false
} }
} }
} val text = tv.text
if (text is SpannableStringBuilder || text is SpannableString) {
val spannable = text as Spannable
val spans = spannable.getSpans(0, text.length, URLSpan::class.java)
for (span in spans) {
val start = spannable.getSpanStart(span)
val end = spannable.getSpanEnd(span)
spannable.removeSpan(span)
spannable.setSpan(
ClickHandlingURLSpan(span.url, handle),
start,
end,
0
)
fun onClick(url: String, onEdit: () -> Unit): Boolean {
var title: String?
val edit = context.getString(R.string.TAd_actionEditTask)
val action: String
val uri = Uri.parse(url).let {
if (it.scheme.isNullOrBlank()) {
Uri.parse("https://$url")
} else {
it
} }
} }
when (uri.scheme) { }
"tel" -> {
title = uri.encodedSchemeSpecificPart private inner class ClickHandlingURLSpan constructor(
action = context.getString(R.string.action_call) url: String?,
} private val onEdit: (() -> Unit)? = null
"mailto" -> { ) : URLSpan(url) {
title = uri.encodedSchemeSpecificPart override fun onClick(widget: View) {
action = context.getString(R.string.action_open) var title: String?
} val edit = context.getString(R.string.TAd_actionEditTask)
"geo" -> { val action: String
title = uri.encodedQuery!!.replaceFirst("q=".toRegex(), "") val uri = Uri.parse(url).let {
try { if (it.scheme.isNullOrBlank()) {
title = URLDecoder.decode(title, "utf-8") Uri.parse("https://$url")
} catch (ignored: UnsupportedEncodingException) { } else {
it
} }
action = context.getString(R.string.action_open)
} }
else -> { when (uri.scheme) {
title = url "tel" -> {
action = context.getString(R.string.action_open) title = uri.encodedSchemeSpecificPart
} action = context.getString(R.string.action_call)
} }
dialogBuilder "mailto" -> {
.newDialog(title) title = uri.encodedSchemeSpecificPart
.setItems(listOf(action, edit)) { _, selected -> action = context.getString(R.string.action_open)
if (selected == 0) { }
context.startActivity(Intent(Intent.ACTION_VIEW, uri)) "geo" -> {
} else { title = uri.encodedQuery!!.replaceFirst("q=".toRegex(), "")
onEdit() try {
title = URLDecoder.decode(title, "utf-8")
} catch (ignored: UnsupportedEncodingException) {
}
action = context.getString(R.string.action_open)
}
else -> {
title = url
action = context.getString(R.string.action_open)
} }
} }
.show() dialogBuilder
return true .newDialog(title)
.setItems(listOf(action, edit)) { _, selected ->
if (selected == 0) {
context.startActivity(Intent(Intent.ACTION_VIEW, uri))
} else {
onEdit?.invoke()
}
}
.show()
}
} }
companion object { companion object {

@ -306,7 +306,6 @@
+| \--- com.atlassian.commonmark:commonmark:0.13.0 +| \--- com.atlassian.commonmark:commonmark:0.13.0
++--- io.noties.markwon:linkify:4.6.2 ++--- io.noties.markwon:linkify:4.6.2
+| \--- io.noties.markwon:core:4.6.2 (*) +| \--- io.noties.markwon:core:4.6.2 (*)
++--- me.saket:better-link-movement-method:2.2.0
++--- org.jetbrains.kotlin:kotlin-stdlib:1.4.32 (*) ++--- org.jetbrains.kotlin:kotlin-stdlib:1.4.32 (*)
++--- org.jetbrains.kotlinx:kotlinx-collections-immutable-jvm:0.3.4 ++--- org.jetbrains.kotlinx:kotlinx-collections-immutable-jvm:0.3.4
+| +--- org.jetbrains.kotlin:kotlin-stdlib:1.4.30 -> 1.4.32 (*) +| +--- org.jetbrains.kotlin:kotlin-stdlib:1.4.30 -> 1.4.32 (*)

@ -423,7 +423,6 @@
+| \--- com.atlassian.commonmark:commonmark:0.13.0 +| \--- com.atlassian.commonmark:commonmark:0.13.0
++--- io.noties.markwon:linkify:4.6.2 ++--- io.noties.markwon:linkify:4.6.2
+| \--- io.noties.markwon:core:4.6.2 (*) +| \--- io.noties.markwon:core:4.6.2 (*)
++--- me.saket:better-link-movement-method:2.2.0
++--- org.jetbrains.kotlin:kotlin-stdlib:1.4.32 (*) ++--- org.jetbrains.kotlin:kotlin-stdlib:1.4.32 (*)
++--- org.jetbrains.kotlinx:kotlinx-collections-immutable-jvm:0.3.4 ++--- org.jetbrains.kotlinx:kotlinx-collections-immutable-jvm:0.3.4
+| +--- org.jetbrains.kotlin:kotlin-stdlib:1.4.30 -> 1.4.32 (*) +| +--- org.jetbrains.kotlin:kotlin-stdlib:1.4.30 -> 1.4.32 (*)

Loading…
Cancel
Save