From 949bcc418ac130624f1ea7d24384948ee1dffb3d Mon Sep 17 00:00:00 2001 From: rebelonion <87634197+rebelonion@users.noreply.github.com> Date: Sat, 18 May 2024 11:43:30 -0500 Subject: [PATCH] fix: some error checking --- .../java/ani/dantotsu/addons/AddonLoader.kt | 63 ++++++++++++++----- .../extension/util/ExtensionInstaller.kt | 14 ++++- app/src/main/res/values/strings.xml | 1 + 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/addons/AddonLoader.kt b/app/src/main/java/ani/dantotsu/addons/AddonLoader.kt index 25681b94..f2dc4488 100644 --- a/app/src/main/java/ani/dantotsu/addons/AddonLoader.kt +++ b/app/src/main/java/ani/dantotsu/addons/AddonLoader.kt @@ -21,6 +21,20 @@ import eu.kanade.tachiyomi.util.system.getApplicationIcon class AddonLoader { companion object { + + /** + * Load an extension from a package name with a specific class name + * @param context the context + * @param packageName the package name of the extension + * @param type the type of extension + * @return the loaded extension + * @throws IllegalStateException if the extension is not of the correct type + * @throws ClassNotFoundException if the extension class is not found + * @throws NoClassDefFoundError if the extension class is not found + * @throws Exception if any other error occurs + * @throws PackageManager.NameNotFoundException if the package is not found + * @throws IllegalStateException if the extension is not found + */ fun loadExtension( context: Context, packageName: String, @@ -70,11 +84,11 @@ class AddonLoader { val loadedClass = try { Class.forName(className, false, classLoader) } catch (e: ClassNotFoundException) { - Logger.log("Extension load error: $extName ($className)") + Logger.log("ClassNotFoundException load error: $extName ($className)") Logger.log(e) throw e } catch (e: NoClassDefFoundError) { - Logger.log("Extension load error: $extName ($className)") + Logger.log("NoClassDefFoundError load error: $extName ($className)") Logger.log(e) throw e } catch (e: Exception) { @@ -117,24 +131,43 @@ class AddonLoader { } } + /** + * Load an extension from a package name (class is determined by type) + * @param context the context + * @param packageName the package name of the extension + * @param type the type of extension + * @return the loaded extension + */ fun loadFromPkgName(context: Context, packageName: String, type: AddonType): LoadResult? { - return when (type) { - AddonType.TORRENT -> loadExtension( - context, - packageName, - TorrentAddonManager.TORRENT_CLASS, - type - ) + return try { + when (type) { + AddonType.TORRENT -> loadExtension( + context, + packageName, + TorrentAddonManager.TORRENT_CLASS, + type + ) - AddonType.DOWNLOAD -> loadExtension( - context, - packageName, - DownloadAddonManager.DOWNLOAD_CLASS, - type - ) + AddonType.DOWNLOAD -> loadExtension( + context, + packageName, + DownloadAddonManager.DOWNLOAD_CLASS, + type + ) + } + } catch (e: Exception) { + Logger.log("Error loading extension from package name: $packageName") + Logger.log(e) + null } } + /** + * Check if a package is an extension by comparing the package name + * @param type the type of extension + * @param pkgInfo the package info + * @return true if the package is an extension + */ private fun isPackageAnExtension(type: String, pkgInfo: PackageInfo): Boolean { return pkgInfo.packageName.equals(type) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt index a2f9e83d..13106131 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.extension.util import android.app.DownloadManager +import android.app.ForegroundServiceStartNotAllowedException import android.content.BroadcastReceiver import android.content.Context import android.content.Intent @@ -11,9 +12,11 @@ import android.os.Environment import androidx.core.content.ContextCompat import androidx.core.content.getSystemService import androidx.core.net.toUri +import ani.dantotsu.R import ani.dantotsu.media.AddonType import ani.dantotsu.media.MediaType import ani.dantotsu.media.Type +import ani.dantotsu.toast import ani.dantotsu.util.Logger import com.jakewharton.rxrelay.PublishRelay import eu.kanade.domain.base.BasePreferences @@ -160,7 +163,16 @@ class ExtensionInstaller(private val context: Context) { else -> { val intent = ExtensionInstallService.getIntent(context, type, downloadId, uri, installer) - ContextCompat.startForegroundService(context, intent) + try { + ContextCompat.startForegroundService(context, intent) + } catch (e: RuntimeException) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) { + toast(context.getString(R.string.error_msg, context.getString(R.string.foreground_service_not_allowed))) + } else { + toast(context.getString(R.string.error_msg, e.message)) + } + Logger.log(e) + } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 14e4e115..5640e828 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -995,4 +995,5 @@ Non quae tempore quo provident laudantium qui illo dolor vel quia dolor et exerc View and edit all your subscriptions Subscriptions Subscription Deleted + Cannot install when app is in background