From 6c4e71d7d409d3b8eb5fd3da56cffb10c6ec79cf Mon Sep 17 00:00:00 2001 From: onurays Date: Mon, 27 Apr 2020 12:26:35 +0300 Subject: [PATCH 1/2] Do not handle url if it is not valid. --- .../home/room/detail/timeline/tools/EventRenderingTools.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt index 097a95bb27..8d30ffbfba 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt @@ -31,6 +31,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import im.vector.riotx.core.utils.isValidUrl fun CharSequence.findPillsAndProcess(scope: CoroutineScope, processBlock: (PillImageSpan) -> Unit) { scope.launch(Dispatchers.Main) { @@ -59,14 +60,14 @@ fun CharSequence.linkify(callback: TimelineEventController.UrlClickCallback?): C fun createLinkMovementMethod(urlClickCallback: TimelineEventController.UrlClickCallback?): EvenBetterLinkMovementMethod { return EvenBetterLinkMovementMethod(object : EvenBetterLinkMovementMethod.OnLinkClickListener { override fun onLinkClicked(textView: TextView, span: ClickableSpan, url: String, actualText: String): Boolean { - return urlClickCallback?.onUrlClicked(url, actualText) == true + return url.isValidUrl() && urlClickCallback?.onUrlClicked(url, actualText) == true } }) .apply { // We need also to fix the case when long click on link will trigger long click on cell setOnLinkLongClickListener { tv, url -> // Long clicks are handled by parent, return true to block android to do something with url - if (urlClickCallback?.onUrlLongClicked(url) == true) { + if (url.isValidUrl() && urlClickCallback?.onUrlLongClicked(url) == true) { tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0)) true } else { From 626eb4d06b06900bb9e6347a78b4dc9301e52f44 Mon Sep 17 00:00:00 2001 From: onurays Date: Mon, 27 Apr 2020 13:44:16 +0300 Subject: [PATCH 2/2] Comments added to explain why we should check if the url is valid. --- .../home/room/detail/timeline/tools/EventRenderingTools.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt index 8d30ffbfba..d3d7260206 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt @@ -60,6 +60,7 @@ fun CharSequence.linkify(callback: TimelineEventController.UrlClickCallback?): C fun createLinkMovementMethod(urlClickCallback: TimelineEventController.UrlClickCallback?): EvenBetterLinkMovementMethod { return EvenBetterLinkMovementMethod(object : EvenBetterLinkMovementMethod.OnLinkClickListener { override fun onLinkClicked(textView: TextView, span: ClickableSpan, url: String, actualText: String): Boolean { + // Always return false if the url is not valid, so the EvenBetterLinkMovementMethod can fallback to default click listener. return url.isValidUrl() && urlClickCallback?.onUrlClicked(url, actualText) == true } }) @@ -67,6 +68,7 @@ fun createLinkMovementMethod(urlClickCallback: TimelineEventController.UrlClickC // We need also to fix the case when long click on link will trigger long click on cell setOnLinkLongClickListener { tv, url -> // Long clicks are handled by parent, return true to block android to do something with url + // Always return false if the url is not valid, so the EvenBetterLinkMovementMethod can fallback to default click listener. if (url.isValidUrl() && urlClickCallback?.onUrlLongClicked(url) == true) { tv.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0f, 0f, 0)) true