More crash fixes

This commit is contained in:
Blatzar 2022-11-06 20:16:48 +01:00
parent aef6f93efe
commit 1226426389
4 changed files with 25 additions and 10 deletions

View file

@ -61,8 +61,9 @@ object CommonActivity {
} }
} }
/** duration is Toast.LENGTH_SHORT if null*/
@MainThread @MainThread
fun showToast(act: Activity?, @StringRes message: Int, duration: Int) { fun showToast(act: Activity?, @StringRes message: Int, duration: Int? = null) {
if (act == null) return if (act == null) return
showToast(act, act.getString(message), duration) showToast(act, act.getString(message), duration)
} }

View file

@ -81,7 +81,8 @@ object APIHolder {
synchronized(allProviders) { synchronized(allProviders) {
initMap() initMap()
return apiMap?.get(apiName)?.let { apis.getOrNull(it) } 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<MainAPI> { fun Context.filterProviderByPreferredMedia(hasHomePageIsRequired: Boolean = true): List<MainAPI> {
// We are getting the weirdest crash ever done: // We are getting the weirdest crash ever done:
// java.lang.ClassCastException: com.lagradost.cloudstream3.TvType cannot be cast to com.lagradost.cloudstream3.TvType // java.lang.ClassCastException: com.lagradost.cloudstream3.TvType cannot be cast to com.lagradost.cloudstream3.TvType
// enumValues<TvType>() 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() val default = TvType.values()
.sorted() .sorted()
.filter { it != TvType.NSFW } .filter { it != TvType.NSFW }
.map { it.ordinal } .map { it.ordinal }
Thread.currentThread().contextClassLoader = oldLoader
val defaultSet = default.map { it.toString() }.toSet() val defaultSet = default.map { it.toString() }.toSet()
val currentPrefMedia = try { val currentPrefMedia = try {
PreferenceManager.getDefaultSharedPreferences(this) PreferenceManager.getDefaultSharedPreferences(this)

View file

@ -4,12 +4,14 @@ import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.os.TransactionTooLargeException
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import com.lagradost.cloudstream3.CommonActivity import com.lagradost.cloudstream3.CommonActivity
import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref
@ -81,12 +83,17 @@ class SettingsUpdates : PreferenceFragmentCompat() {
dialog.text1?.text = text dialog.text1?.text = text
dialog.copy_btt?.setOnClickListener { dialog.copy_btt?.setOnClickListener {
val serviceClipboard = // Can crash on too much text
(activity?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?) try {
?: return@setOnClickListener val serviceClipboard =
val clip = ClipData.newPlainText("logcat", text) (activity?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?)
serviceClipboard.setPrimaryClip(clip) ?: return@setOnClickListener
dialog.dismissSafe(activity) 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 { dialog.clear_btt?.setOnClickListener {
Runtime.getRuntime().exec("logcat -c") Runtime.getRuntime().exec("logcat -c")

View file

@ -652,5 +652,5 @@
<string name="clear_history">Clear history</string> <string name="clear_history">Clear history</string>
<string name="history">History</string> <string name="history">History</string>
<string name="enable_skip_op_from_database_des">Show skip popups for opening/ending</string> <string name="enable_skip_op_from_database_des">Show skip popups for opening/ending</string>
<string name="clipboard_too_large">Too much text. Unable to save to clipboard.</string>
</resources> </resources>