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) + } }