From 1226426389ac327514f3189826b8712516dd9270 Mon Sep 17 00:00:00 2001 From: Blatzar <46196380+Blatzar@users.noreply.github.com> Date: Sun, 6 Nov 2022 20:16:48 +0100 Subject: [PATCH] More crash fixes --- .../lagradost/cloudstream3/CommonActivity.kt | 3 ++- .../com/lagradost/cloudstream3/MainAPI.kt | 11 +++++++++-- .../ui/settings/SettingsUpdates.kt | 19 +++++++++++++------ app/src/main/res/values/strings.xml | 2 +- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt index 342dc6f7..47a195d1 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt @@ -61,8 +61,9 @@ object CommonActivity { } } + /** duration is Toast.LENGTH_SHORT if null*/ @MainThread - fun showToast(act: Activity?, @StringRes message: Int, duration: Int) { + fun showToast(act: Activity?, @StringRes message: Int, duration: Int? = null) { if (act == null) return showToast(act, act.getString(message), duration) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt index 4b271192..5c9f3071 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt @@ -81,7 +81,8 @@ object APIHolder { synchronized(allProviders) { initMap() return apiMap?.get(apiName)?.let { apis.getOrNull(it) } - ?: allProviders.firstOrNull { it.name == apiName } + // Leave the ?. null check, it can crash regardless + ?: allProviders.firstOrNull { it?.name == apiName } } } @@ -244,11 +245,17 @@ object APIHolder { fun Context.filterProviderByPreferredMedia(hasHomePageIsRequired: Boolean = true): List { // We are getting the weirdest crash ever done: // java.lang.ClassCastException: com.lagradost.cloudstream3.TvType cannot be cast to com.lagradost.cloudstream3.TvType - // enumValues() might be the cause, hence I am trying TvType.values() + // Trying fixing using classloader fuckery + val oldLoader = Thread.currentThread().contextClassLoader + Thread.currentThread().contextClassLoader = TvType::class.java.classLoader + val default = TvType.values() .sorted() .filter { it != TvType.NSFW } .map { it.ordinal } + + Thread.currentThread().contextClassLoader = oldLoader + val defaultSet = default.map { it.toString() }.toSet() val currentPrefMedia = try { PreferenceManager.getDefaultSharedPreferences(this) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt index 6b19042a..78f6d628 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt @@ -4,12 +4,14 @@ import android.content.ClipData import android.content.ClipboardManager import android.content.Context import android.os.Bundle +import android.os.TransactionTooLargeException import android.view.View import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.navigation.fragment.findNavController import androidx.preference.PreferenceFragmentCompat import com.lagradost.cloudstream3.CommonActivity +import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref @@ -81,12 +83,17 @@ class SettingsUpdates : PreferenceFragmentCompat() { dialog.text1?.text = text dialog.copy_btt?.setOnClickListener { - val serviceClipboard = - (activity?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?) - ?: return@setOnClickListener - val clip = ClipData.newPlainText("logcat", text) - serviceClipboard.setPrimaryClip(clip) - dialog.dismissSafe(activity) + // Can crash on too much text + try { + val serviceClipboard = + (activity?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?) + ?: return@setOnClickListener + val clip = ClipData.newPlainText("logcat", text) + serviceClipboard.setPrimaryClip(clip) + dialog.dismissSafe(activity) + } catch (e: TransactionTooLargeException) { + showToast(activity, R.string.clipboard_too_large) + } } dialog.clear_btt?.setOnClickListener { Runtime.getRuntime().exec("logcat -c") diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e2ebf0ab..9381372c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -652,5 +652,5 @@ Clear history History Show skip popups for opening/ending - + Too much text. Unable to save to clipboard.