From 5eb5cdf3612b1094fb161a8caaf882f22afb8a04 Mon Sep 17 00:00:00 2001 From: Patrick Martin Date: Fri, 15 Nov 2019 12:23:52 -0800 Subject: [PATCH] For #1695 - Detect HTML mime type for clipboard URL (#6612) * For #1695 - Detect HTML mime type for clipboard URL --- .../mozilla/fenix/utils/ClipboardHandler.kt | 24 +++++++++++-------- .../fenix/utils/ClipboardHandlerTest.kt | 8 +++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/utils/ClipboardHandler.kt b/app/src/main/java/org/mozilla/fenix/utils/ClipboardHandler.kt index 9a6c8ce74de2..5ff3321cec37 100644 --- a/app/src/main/java/org/mozilla/fenix/utils/ClipboardHandler.kt +++ b/app/src/main/java/org/mozilla/fenix/utils/ClipboardHandler.kt @@ -10,6 +10,7 @@ import android.content.Context import mozilla.components.support.utils.WebURLFinder private const val MIME_TYPE_TEXT_PLAIN = "text/plain" +private const val MIME_TYPE_TEXT_HTML = "text/html" /** * A clipboard utility class that allows copying and pasting links/text to & from the clipboard @@ -19,29 +20,32 @@ class ClipboardHandler(context: Context) { var text: String? get() { - if (clipboard.isPrimaryClipEmpty() || - !clipboard.isPrimaryClipPlainText()) { - return null + if (!clipboard.isPrimaryClipEmpty() && + (clipboard.isPrimaryClipPlainText() || + clipboard.isPrimaryClipHtmlText()) + ) { + return clipboard.firstPrimaryClipItem?.text.toString() } - - return clipboard.firstPrimaryClipItem?.text.toString() + return null } - set(value) { clipboard.primaryClip = ClipData.newPlainText("Text", value) } val url: String? get() { - if (text == null) { return null } - - val finder = WebURLFinder(text) - return finder.bestWebURL() + return text?.let { + val finder = WebURLFinder(it) + finder.bestWebURL() + } } private fun ClipboardManager.isPrimaryClipPlainText() = primaryClipDescription?.hasMimeType(MIME_TYPE_TEXT_PLAIN) ?: false + private fun ClipboardManager.isPrimaryClipHtmlText() = + primaryClipDescription?.hasMimeType(MIME_TYPE_TEXT_HTML) ?: false + private fun ClipboardManager.isPrimaryClipEmpty() = primaryClip?.itemCount == 0 private val ClipboardManager.firstPrimaryClipItem: ClipData.Item? diff --git a/app/src/test/java/org/mozilla/fenix/utils/ClipboardHandlerTest.kt b/app/src/test/java/org/mozilla/fenix/utils/ClipboardHandlerTest.kt index 7f2f92111775..267212063389 100644 --- a/app/src/test/java/org/mozilla/fenix/utils/ClipboardHandlerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/utils/ClipboardHandlerTest.kt @@ -54,4 +54,12 @@ class ClipboardHandlerTest { clipboard.primaryClip = ClipData.newPlainText("Text", clipboardUrl) assertEquals(clipboardUrl, clipboardHandler.url) } + + @Test + fun getUrlfromTextUrlMIME() { + assertEquals(null, clipboardHandler.url) + + clipboard.primaryClip = ClipData.newHtmlText("Html", clipboardUrl, clipboardUrl) + assertEquals(clipboardUrl, clipboardHandler.url) + } }