From 38610d8a2432fb2c44498e3717668b62ea00e2a5 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 17 Apr 2021 12:29:22 -0400 Subject: [PATCH] Avoid crash when users copying to clipboard fails because they have apps that are listening to their clipboards but also denied permissions See https://commonsware.com/blog/2013/08/08/developer-psa-please-fix-your-clipboard-handling.html --- .../tachiyomi/util/system/ContextExtensions.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt index 936ec57ecb..3344e98179 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt @@ -34,6 +34,7 @@ import androidx.core.net.toUri import androidx.localbroadcastmanager.content.LocalBroadcastManager import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.lang.truncateCenter +import timber.log.Timber import java.io.File import kotlin.math.roundToInt @@ -69,10 +70,15 @@ fun Context.toast(text: String?, duration: Int = Toast.LENGTH_SHORT, block: (Toa fun Context.copyToClipboard(label: String, content: String) { if (content.isBlank()) return - val clipboard = getSystemService()!! - clipboard.setPrimaryClip(ClipData.newPlainText(label, content)) + try { + val clipboard = getSystemService()!! + clipboard.setPrimaryClip(ClipData.newPlainText(label, content)) - toast(getString(R.string.copied_to_clipboard, content.truncateCenter(50))) + toast(getString(R.string.copied_to_clipboard, content.truncateCenter(50))) + } catch (e: Throwable) { + Timber.e(e) + toast(R.string.clipboard_copy_error) + } } /**