From bd139e1cec23dfccdeccc8d3ee68ed26366bac45 Mon Sep 17 00:00:00 2001 From: - <-> Date: Sun, 15 Dec 2024 14:49:39 +0100 Subject: [PATCH] fix: accept mimetype text/html when looking for clipboard text --- .../java/net/mynero/wallet/util/Helper.kt | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/util/Helper.kt b/app/src/main/java/net/mynero/wallet/util/Helper.kt index ad54c03..e109096 100644 --- a/app/src/main/java/net/mynero/wallet/util/Helper.kt +++ b/app/src/main/java/net/mynero/wallet/util/Helper.kt @@ -254,19 +254,17 @@ object Helper { } fun getClipBoardText(context: Context): String? { - val clipboardManager = - context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager - try { - if (clipboardManager.hasPrimaryClip() - && clipboardManager.primaryClipDescription?.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) == true - ) { - val item = clipboardManager.primaryClip?.getItemAt(0) - return item?.text.toString() + val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + val primaryClip = clipboardManager.primaryClip + return primaryClip?.let { + val description = it.description + // some apps for some reason store clipboard text as html + if (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) || description.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML)) { + // getItemAt may throw IndexOutOfBoundsException and there is no safe version of it + kotlin.runCatching { it.getItemAt(0) }.getOrNull()?.text?.toString() + } else { + null } - } catch (ex: NullPointerException) { - // if we have don't find a text in the clipboard - return null } - return null } } \ No newline at end of file