From 723c554bc86c3d6f45fa6d9abd05f0ef2874297b Mon Sep 17 00:00:00 2001 From: Jace <54625750+Jacekun@users.noreply.github.com> Date: Wed, 30 Nov 2022 03:46:31 +0800 Subject: [PATCH] [Feature] Automatically download plugin, based on language setting (#172) --- .../lagradost/cloudstream3/MainActivity.kt | 12 +- .../cloudstream3/plugins/PluginManager.kt | 107 ++++++++++++++++-- app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/settings_updates.xml | 5 + 4 files changed, 115 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt index b999199f..d7351dc7 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt @@ -45,6 +45,7 @@ import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.normalSafeApiCall import com.lagradost.cloudstream3.network.initClient import com.lagradost.cloudstream3.plugins.PluginManager +import com.lagradost.cloudstream3.plugins.PluginManager.loadAllOnlinePlugins import com.lagradost.cloudstream3.plugins.PluginManager.loadSinglePlugin import com.lagradost.cloudstream3.receivers.VideoDownloadRestartReceiver import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.OAuth2Apis @@ -568,7 +569,16 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { ) { PluginManager.updateAllOnlinePluginsAndLoadThem(this@MainActivity) } else { - PluginManager.loadAllOnlinePlugins(this@MainActivity) + loadAllOnlinePlugins(this@MainActivity) + } + + //Automatically download not existing plugins + if (settingsManager.getBoolean( + getString(R.string.auto_download_plugins_key), + false + ) + ) { + PluginManager.downloadNotExistingPluginsAndLoad(this@MainActivity) } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt b/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt index b9c775c0..f2dbb02f 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt @@ -13,11 +13,13 @@ import androidx.core.app.NotificationManagerCompat import com.fasterxml.jackson.annotation.JsonProperty import com.google.gson.Gson import com.lagradost.cloudstream3.* +import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings import com.lagradost.cloudstream3.APIHolder.removePluginMapping import com.lagradost.cloudstream3.AcraApplication.Companion.getKey import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey import com.lagradost.cloudstream3.AcraApplication.Companion.setKey import com.lagradost.cloudstream3.CommonActivity.showToast +import com.lagradost.cloudstream3.MainAPI.Companion.settingsForProvider import com.lagradost.cloudstream3.MainActivity.Companion.afterPluginsLoadedEvent import com.lagradost.cloudstream3.mvvm.debugPrint import com.lagradost.cloudstream3.mvvm.logError @@ -26,6 +28,8 @@ import com.lagradost.cloudstream3.plugins.RepositoryManager.ONLINE_PLUGINS_FOLDE import com.lagradost.cloudstream3.plugins.RepositoryManager.PREBUILT_REPOSITORIES import com.lagradost.cloudstream3.plugins.RepositoryManager.downloadPluginToFile import com.lagradost.cloudstream3.plugins.RepositoryManager.getRepoPlugins +import com.lagradost.cloudstream3.ui.result.UiText +import com.lagradost.cloudstream3.ui.result.txt import com.lagradost.cloudstream3.ui.settings.extensions.REPOSITORIES_KEY import com.lagradost.cloudstream3.ui.settings.extensions.RepositoryData import com.lagradost.cloudstream3.utils.Coroutines.main @@ -219,9 +223,7 @@ object PluginManager { fun updateAllOnlinePluginsAndLoadThem(activity: Activity) { // Load all plugins as fast as possible! loadAllOnlinePlugins(activity) - - afterPluginsLoadedEvent.invoke(true) - + afterPluginsLoadedEvent.invoke(true) val urls = (getKey>(REPOSITORIES_KEY) ?: emptyArray()) + PREBUILT_REPOSITORIES @@ -265,16 +267,98 @@ object PluginManager { } main { - createNotification(activity, updatedPlugins) + val uitext = txt(R.string.plugins_updated, updatedPlugins.size) + createNotification(activity, uitext, updatedPlugins) } - // ioSafe { + // ioSafe { afterPluginsLoadedEvent.invoke(true) - // } + // } Log.i(TAG, "Plugin update done!") } + /** + * Automatically download plugins not yet existing on local + * 1. Gets all online data from online plugins repo + * 2. Fetch all not downloaded plugins + * 3. Download them and reload plugins + **/ + fun downloadNotExistingPluginsAndLoad(activity: Activity) { + val newDownloadPlugins = mutableListOf() + val urls = (getKey>(REPOSITORIES_KEY) + ?: emptyArray()) + PREBUILT_REPOSITORIES + val onlinePlugins = urls.toList().apmap { + getRepoPlugins(it.url)?.toList() ?: emptyList() + }.flatten().distinctBy { it.second.url } + + val providerLang = activity.getApiProviderLangSettings() + //Log.i(TAG, "providerLang => ${providerLang.toJson()}") + + // Iterate online repos and returns not downloaded plugins + val notDownloadedPlugins = onlinePlugins.mapNotNull { onlineData -> + val sitePlugin = onlineData.second + //Don't include empty urls + if (sitePlugin.url.isBlank()) { return@mapNotNull null } + if (sitePlugin.repositoryUrl.isNullOrBlank()) { return@mapNotNull null } + + //Omit already existing plugins + if (getPluginPath(activity, sitePlugin.internalName, onlineData.first).exists()) { + Log.i(TAG, "Skip > ${sitePlugin.internalName}") + return@mapNotNull null + } + + //Omit lang not selected on language setting + val lang = sitePlugin.language ?: return@mapNotNull null + //If set to 'universal', don't skip any language + if (!providerLang.contains(AllLanguagesName) && !providerLang.contains(lang)) { + return@mapNotNull null + } + //Log.i(TAG, "sitePlugin lang => $lang") + + //Omit NSFW, if disabled + sitePlugin.tvTypes?.let { tvtypes -> + if (!settingsForProvider.enableAdult) { + if (tvtypes.contains(TvType.NSFW.name)) { + return@mapNotNull null + } + } + } + val savedData = PluginData( + url = sitePlugin.url, + internalName = sitePlugin.internalName, + isOnline = true, + filePath = "", + version = sitePlugin.version + ) + OnlinePluginData(savedData, onlineData) + } + //Log.i(TAG, "notDownloadedPlugins => ${notDownloadedPlugins.toJson()}") + + notDownloadedPlugins.apmap { pluginData -> + downloadAndLoadPlugin( + activity, + pluginData.onlineData.second.url, + pluginData.savedData.internalName, + pluginData.onlineData.first + ).let { success -> + if (success) + newDownloadPlugins.add(pluginData.onlineData.second.name) + } + } + + main { + val uitext = txt(R.string.plugins_downloaded, newDownloadPlugins.size) + createNotification(activity, uitext, newDownloadPlugins) + } + + // ioSafe { + afterPluginsLoadedEvent.invoke(true) + // } + + Log.i(TAG, "Plugin download done!") + } + /** * Use updateAllOnlinePluginsAndLoadThem * */ @@ -527,12 +611,14 @@ object PluginManager { private fun createNotification( context: Context, - extensionNames: List + uitext: UiText, + extensions: List ): Notification? { try { - if (extensionNames.isEmpty()) return null - val content = extensionNames.joinToString(", ") + if (extensions.isEmpty()) return null + + val content = extensions.joinToString(", ") // main { // DON'T WANT TO SLOW IT DOWN val builder = NotificationCompat.Builder(context, EXTENSIONS_CHANNEL_ID) .setAutoCancel(false) @@ -541,7 +627,8 @@ object PluginManager { .setSilent(true) .setPriority(NotificationCompat.PRIORITY_LOW) .setColor(context.colorFromAttribute(R.attr.colorPrimary)) - .setContentTitle(context.getString(R.string.plugins_updated, extensionNames.size)) + .setContentTitle(uitext.asString(context)) + //.setContentTitle(context.getString(title, extensionNames.size)) .setSmallIcon(R.drawable.ic_baseline_extension_24) .setStyle( NotificationCompat.BigTextStyle() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9381372c..db042b95 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,6 +6,7 @@ search_type_list auto_update auto_update_plugins + auto_download_plugins_key skip_update_key prerelease_update manual_check_update @@ -269,6 +270,7 @@ Hide selected video quality on Search results Automatic plugin updates + Automatically download plugins Show app updates Automatically search for new updates on start Update to prereleases diff --git a/app/src/main/res/xml/settings_updates.xml b/app/src/main/res/xml/settings_updates.xml index eaceb785..3a17f393 100644 --- a/app/src/main/res/xml/settings_updates.xml +++ b/app/src/main/res/xml/settings_updates.xml @@ -33,6 +33,11 @@ android:icon="@drawable/ic_baseline_extension_24" android:key="@string/auto_update_plugins_key" android:title="@string/automatic_plugin_updates" /> +