2022-08-06 15:48:00 +00:00
|
|
|
package com.lagradost.cloudstream3.plugins
|
|
|
|
|
2022-09-22 10:50:28 +00:00
|
|
|
import android.app.*
|
2022-08-06 15:48:00 +00:00
|
|
|
import dalvik.system.PathClassLoader
|
|
|
|
import com.google.gson.Gson
|
|
|
|
import android.content.res.AssetManager
|
|
|
|
import android.content.res.Resources
|
|
|
|
import android.os.Environment
|
2022-08-07 07:28:21 +00:00
|
|
|
import android.widget.Toast
|
2022-09-22 10:50:28 +00:00
|
|
|
import android.content.Context
|
|
|
|
import android.os.Build
|
2022-08-07 21:11:13 +00:00
|
|
|
import android.util.Log
|
2022-09-22 10:50:28 +00:00
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
import androidx.core.app.NotificationManagerCompat
|
2022-08-06 23:43:39 +00:00
|
|
|
import com.fasterxml.jackson.annotation.JsonProperty
|
2022-08-11 22:36:19 +00:00
|
|
|
import com.lagradost.cloudstream3.*
|
2022-08-06 23:43:39 +00:00
|
|
|
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.plugins.RepositoryManager.ONLINE_PLUGINS_FOLDER
|
|
|
|
import com.lagradost.cloudstream3.plugins.RepositoryManager.downloadPluginToFile
|
2022-08-07 07:28:21 +00:00
|
|
|
import com.lagradost.cloudstream3.CommonActivity.showToast
|
2022-08-07 16:01:32 +00:00
|
|
|
import com.lagradost.cloudstream3.plugins.RepositoryManager.getRepoPlugins
|
|
|
|
import com.lagradost.cloudstream3.ui.settings.extensions.REPOSITORIES_KEY
|
|
|
|
import com.lagradost.cloudstream3.ui.settings.extensions.RepositoryData
|
|
|
|
import com.lagradost.cloudstream3.utils.VideoDownloadManager.sanitizeFilename
|
2022-08-09 15:03:58 +00:00
|
|
|
import com.lagradost.cloudstream3.APIHolder.removePluginMapping
|
2022-08-23 19:28:42 +00:00
|
|
|
import com.lagradost.cloudstream3.MainActivity.Companion.afterPluginsLoadedEvent
|
2022-09-23 12:20:53 +00:00
|
|
|
import com.lagradost.cloudstream3.mvvm.debugPrint
|
2022-08-18 00:54:05 +00:00
|
|
|
import com.lagradost.cloudstream3.mvvm.logError
|
2022-10-07 12:53:51 +00:00
|
|
|
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
|
2022-08-12 01:51:06 +00:00
|
|
|
import com.lagradost.cloudstream3.plugins.RepositoryManager.PREBUILT_REPOSITORIES
|
2022-08-23 19:28:42 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
|
2022-09-22 10:50:28 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.Coroutines.main
|
2022-08-09 08:33:16 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.ExtractorApi
|
2022-09-22 10:50:28 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute
|
2022-08-09 08:33:16 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.extractorApis
|
2022-08-06 23:43:39 +00:00
|
|
|
import kotlinx.coroutines.sync.Mutex
|
|
|
|
import kotlinx.coroutines.sync.withLock
|
2022-08-06 15:48:00 +00:00
|
|
|
import java.io.File
|
|
|
|
import java.io.InputStreamReader
|
|
|
|
import java.util.*
|
|
|
|
|
2022-08-06 23:43:39 +00:00
|
|
|
// Different keys for local and not since local can be removed at any time without app knowing, hence the local are getting rebuilt on every app start
|
|
|
|
const val PLUGINS_KEY = "PLUGINS_KEY"
|
|
|
|
const val PLUGINS_KEY_LOCAL = "PLUGINS_KEY_LOCAL"
|
|
|
|
|
2022-09-22 10:50:28 +00:00
|
|
|
const val EXTENSIONS_CHANNEL_ID = "cloudstream3.extensions"
|
|
|
|
const val EXTENSIONS_CHANNEL_NAME = "Extensions"
|
|
|
|
const val EXTENSIONS_CHANNEL_DESCRIPT = "Extension notification channel"
|
2022-08-07 16:01:32 +00:00
|
|
|
|
|
|
|
// Data class for internal storage
|
2022-08-06 23:43:39 +00:00
|
|
|
data class PluginData(
|
2022-08-07 16:01:32 +00:00
|
|
|
@JsonProperty("internalName") val internalName: String,
|
2022-08-06 23:43:39 +00:00
|
|
|
@JsonProperty("url") val url: String?,
|
|
|
|
@JsonProperty("isOnline") val isOnline: Boolean,
|
|
|
|
@JsonProperty("filePath") val filePath: String,
|
2022-08-07 16:01:32 +00:00
|
|
|
@JsonProperty("version") val version: Int,
|
2022-08-11 22:36:19 +00:00
|
|
|
) {
|
|
|
|
fun toSitePlugin(): SitePlugin {
|
|
|
|
return SitePlugin(
|
|
|
|
this.filePath,
|
|
|
|
PROVIDER_STATUS_OK,
|
|
|
|
maxOf(1, version),
|
|
|
|
1,
|
|
|
|
internalName,
|
|
|
|
internalName,
|
|
|
|
emptyList(),
|
|
|
|
File(this.filePath).name,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
2022-08-19 08:59:11 +00:00
|
|
|
null,
|
|
|
|
File(this.filePath).length()
|
2022-08-11 22:36:19 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-08-06 23:43:39 +00:00
|
|
|
|
2022-08-07 16:01:32 +00:00
|
|
|
// This is used as a placeholder / not set version
|
|
|
|
const val PLUGIN_VERSION_NOT_SET = Int.MIN_VALUE
|
|
|
|
|
|
|
|
// This always updates
|
|
|
|
const val PLUGIN_VERSION_ALWAYS_UPDATE = -1
|
|
|
|
|
2022-08-06 15:48:00 +00:00
|
|
|
object PluginManager {
|
2022-08-06 23:43:39 +00:00
|
|
|
// Prevent multiple writes at once
|
|
|
|
val lock = Mutex()
|
|
|
|
|
2022-08-07 21:11:13 +00:00
|
|
|
const val TAG = "PluginManager"
|
|
|
|
|
2022-09-22 10:50:28 +00:00
|
|
|
private var hasCreatedNotChanel = false
|
|
|
|
|
2022-08-06 23:43:39 +00:00
|
|
|
/**
|
|
|
|
* Store data about the plugin for fetching later
|
|
|
|
* */
|
2022-08-07 21:11:13 +00:00
|
|
|
private suspend fun setPluginData(data: PluginData) {
|
|
|
|
lock.withLock {
|
|
|
|
if (data.isOnline) {
|
|
|
|
val plugins = getPluginsOnline()
|
2022-08-17 21:59:21 +00:00
|
|
|
val newPlugins = plugins.filter { it.filePath != data.filePath } + data
|
|
|
|
setKey(PLUGINS_KEY, newPlugins)
|
2022-08-07 21:11:13 +00:00
|
|
|
} else {
|
|
|
|
val plugins = getPluginsLocal()
|
2022-08-17 21:59:21 +00:00
|
|
|
setKey(PLUGINS_KEY_LOCAL, plugins.filter { it.filePath != data.filePath } + data)
|
2022-08-06 23:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 22:36:19 +00:00
|
|
|
private suspend fun deletePluginData(data: PluginData?) {
|
|
|
|
if (data == null) return
|
2022-08-07 21:11:13 +00:00
|
|
|
lock.withLock {
|
|
|
|
if (data.isOnline) {
|
|
|
|
val plugins = getPluginsOnline().filter { it.url != data.url }
|
|
|
|
setKey(PLUGINS_KEY, plugins)
|
|
|
|
} else {
|
|
|
|
val plugins = getPluginsLocal().filter { it.filePath != data.filePath }
|
2022-08-11 22:36:19 +00:00
|
|
|
setKey(PLUGINS_KEY_LOCAL, plugins)
|
2022-08-06 23:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 22:36:19 +00:00
|
|
|
suspend fun deleteRepositoryData(repositoryPath: String) {
|
|
|
|
lock.withLock {
|
|
|
|
val plugins = getPluginsOnline().filter {
|
|
|
|
!it.filePath.contains(repositoryPath)
|
|
|
|
}
|
2022-10-07 12:53:51 +00:00
|
|
|
val file = File(repositoryPath)
|
|
|
|
normalSafeApiCall {
|
|
|
|
if (file.exists()) file.deleteRecursively()
|
|
|
|
}
|
2022-08-11 22:36:19 +00:00
|
|
|
setKey(PLUGINS_KEY, plugins)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 23:43:39 +00:00
|
|
|
fun getPluginsOnline(): Array<PluginData> {
|
|
|
|
return getKey(PLUGINS_KEY) ?: emptyArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getPluginsLocal(): Array<PluginData> {
|
|
|
|
return getKey(PLUGINS_KEY_LOCAL) ?: emptyArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
private val LOCAL_PLUGINS_PATH =
|
2022-08-06 15:48:00 +00:00
|
|
|
Environment.getExternalStorageDirectory().absolutePath + "/Cloudstream3/plugins"
|
2022-08-06 23:43:39 +00:00
|
|
|
|
2022-09-01 12:13:54 +00:00
|
|
|
public var currentlyLoading: String? = null
|
|
|
|
|
2022-08-07 16:01:32 +00:00
|
|
|
// Maps filepath to plugin
|
2022-08-21 20:13:53 +00:00
|
|
|
val plugins: MutableMap<String, Plugin> =
|
2022-08-06 15:48:00 +00:00
|
|
|
LinkedHashMap<String, Plugin>()
|
2022-08-07 16:01:32 +00:00
|
|
|
|
2022-08-11 08:49:20 +00:00
|
|
|
// Maps urls to plugin
|
|
|
|
val urlPlugins: MutableMap<String, Plugin> =
|
|
|
|
LinkedHashMap<String, Plugin>()
|
|
|
|
|
2022-08-06 15:48:00 +00:00
|
|
|
private val classLoaders: MutableMap<PathClassLoader, Plugin> =
|
|
|
|
HashMap<PathClassLoader, Plugin>()
|
2022-08-11 17:39:34 +00:00
|
|
|
|
2022-08-07 16:01:32 +00:00
|
|
|
private var loadedLocalPlugins = false
|
2022-08-06 15:48:00 +00:00
|
|
|
private val gson = Gson()
|
2022-08-06 23:43:39 +00:00
|
|
|
|
2022-08-07 21:11:13 +00:00
|
|
|
private suspend fun maybeLoadPlugin(activity: Activity, file: File) {
|
2022-08-06 23:43:39 +00:00
|
|
|
val name = file.name
|
|
|
|
if (file.extension == "zip" || file.extension == "cs3") {
|
2022-08-07 16:01:32 +00:00
|
|
|
loadPlugin(
|
2022-08-07 21:11:13 +00:00
|
|
|
activity,
|
2022-08-07 16:01:32 +00:00
|
|
|
file,
|
|
|
|
PluginData(name, null, false, file.absolutePath, PLUGIN_VERSION_NOT_SET)
|
|
|
|
)
|
2022-08-09 08:33:16 +00:00
|
|
|
} else {
|
|
|
|
Log.i(TAG, "Skipping invalid plugin file: $file")
|
2022-08-07 16:01:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:52:03 +00:00
|
|
|
|
|
|
|
// Helper class for updateAllOnlinePluginsAndLoadThem
|
2022-08-11 17:39:34 +00:00
|
|
|
data class OnlinePluginData(
|
2022-08-08 18:52:03 +00:00
|
|
|
val savedData: PluginData,
|
|
|
|
val onlineData: Pair<String, SitePlugin>,
|
|
|
|
) {
|
|
|
|
val isOutdated =
|
2022-10-06 16:30:58 +00:00
|
|
|
onlineData.second.version > savedData.version || onlineData.second.version == PLUGIN_VERSION_ALWAYS_UPDATE
|
2022-08-08 18:52:03 +00:00
|
|
|
val isDisabled = onlineData.second.status == PROVIDER_STATUS_DOWN
|
2022-10-06 16:30:58 +00:00
|
|
|
|
|
|
|
fun validOnlineData(context: Context): Boolean {
|
2022-10-07 12:53:51 +00:00
|
|
|
return getPluginPath(
|
|
|
|
context,
|
|
|
|
savedData.internalName,
|
|
|
|
onlineData.first
|
|
|
|
).absolutePath == savedData.filePath
|
2022-10-06 16:30:58 +00:00
|
|
|
}
|
2022-08-08 18:52:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 19:28:42 +00:00
|
|
|
// var allCurrentOutDatedPlugins: Set<OnlinePluginData> = emptySet()
|
2022-08-11 17:39:34 +00:00
|
|
|
|
2022-08-21 02:22:24 +00:00
|
|
|
suspend fun loadSinglePlugin(activity: Activity, apiName: String): Boolean {
|
2022-08-24 02:21:46 +00:00
|
|
|
return (getPluginsOnline().firstOrNull {
|
|
|
|
// Most of the time the provider ends with Provider which isn't part of the api name
|
|
|
|
it.internalName.replace("provider", "", ignoreCase = true) == apiName
|
|
|
|
}
|
|
|
|
?: getPluginsLocal().firstOrNull {
|
|
|
|
it.internalName.replace("provider", "", ignoreCase = true) == apiName
|
|
|
|
})?.let { savedData ->
|
2022-08-21 01:44:49 +00:00
|
|
|
// OnlinePluginData(savedData, onlineData)
|
|
|
|
loadPlugin(
|
|
|
|
activity,
|
|
|
|
File(savedData.filePath),
|
|
|
|
savedData
|
|
|
|
)
|
|
|
|
} ?: false
|
|
|
|
}
|
|
|
|
|
2022-08-07 16:01:32 +00:00
|
|
|
/**
|
|
|
|
* Needs to be run before other plugin loading because plugin loading can not be overwritten
|
2022-08-08 18:52:03 +00:00
|
|
|
* 1. Gets all online data about the downloaded plugins
|
|
|
|
* 2. If disabled do nothing
|
|
|
|
* 3. If outdated download and load the plugin
|
|
|
|
* 4. Else load the plugin normally
|
2022-08-07 16:01:32 +00:00
|
|
|
**/
|
2022-10-31 00:16:15 +00:00
|
|
|
fun updateAllOnlinePluginsAndLoadThem(activity: Activity) = ioSafe {
|
2022-08-23 19:28:42 +00:00
|
|
|
// Load all plugins as fast as possible!
|
2022-08-25 11:49:24 +00:00
|
|
|
loadAllOnlinePlugins(activity)
|
2022-08-23 19:28:42 +00:00
|
|
|
|
|
|
|
afterPluginsLoadedEvent.invoke(true)
|
2022-10-31 00:16:15 +00:00
|
|
|
|
2022-08-23 19:28:42 +00:00
|
|
|
|
2022-08-11 17:39:34 +00:00
|
|
|
val urls = (getKey<Array<RepositoryData>>(REPOSITORIES_KEY)
|
|
|
|
?: emptyArray()) + PREBUILT_REPOSITORIES
|
2022-08-07 16:01:32 +00:00
|
|
|
|
2022-10-31 00:16:15 +00:00
|
|
|
val onlinePlugins = urls.toList().amap {
|
2022-08-07 16:01:32 +00:00
|
|
|
getRepoPlugins(it.url)?.toList() ?: emptyList()
|
2022-08-08 18:52:03 +00:00
|
|
|
}.flatten().distinctBy { it.second.url }
|
2022-08-07 16:01:32 +00:00
|
|
|
|
|
|
|
// Iterates over all offline plugins, compares to remote repo and returns the plugins which are outdated
|
|
|
|
val outdatedPlugins = getPluginsOnline().map { savedData ->
|
2022-09-23 12:20:53 +00:00
|
|
|
onlinePlugins
|
|
|
|
.filter { onlineData -> savedData.internalName == onlineData.second.internalName }
|
2022-08-08 18:52:03 +00:00
|
|
|
.map { onlineData ->
|
|
|
|
OnlinePluginData(savedData, onlineData)
|
2022-10-06 16:30:58 +00:00
|
|
|
}.filter {
|
|
|
|
it.validOnlineData(activity)
|
2022-08-07 16:01:32 +00:00
|
|
|
}
|
2022-08-08 18:52:03 +00:00
|
|
|
}.flatten().distinctBy { it.onlineData.second.url }
|
2022-08-07 16:01:32 +00:00
|
|
|
|
2022-09-23 12:20:53 +00:00
|
|
|
debugPrint {
|
2022-08-23 19:28:42 +00:00
|
|
|
"Outdated plugins: ${outdatedPlugins.filter { it.isOutdated }}"
|
|
|
|
}
|
2022-08-07 16:01:32 +00:00
|
|
|
|
2022-09-22 10:50:28 +00:00
|
|
|
val updatedPlugins = mutableListOf<String>()
|
|
|
|
|
2022-10-31 00:16:15 +00:00
|
|
|
outdatedPlugins.amap { pluginData ->
|
2022-08-23 19:28:42 +00:00
|
|
|
if (pluginData.isDisabled) {
|
2022-09-22 10:50:28 +00:00
|
|
|
//updatedPlugins.add(activity.getString(R.string.single_plugin_disabled, pluginData.onlineData.second.name))
|
2022-08-23 19:28:42 +00:00
|
|
|
unloadPlugin(pluginData.savedData.filePath)
|
|
|
|
} else if (pluginData.isOutdated) {
|
2022-08-08 18:52:03 +00:00
|
|
|
downloadAndLoadPlugin(
|
|
|
|
activity,
|
2022-08-23 19:28:42 +00:00
|
|
|
pluginData.onlineData.second.url,
|
|
|
|
pluginData.savedData.internalName,
|
2022-09-23 12:20:53 +00:00
|
|
|
File(pluginData.savedData.filePath)
|
2022-09-22 10:50:28 +00:00
|
|
|
).let { success ->
|
|
|
|
if (success)
|
|
|
|
updatedPlugins.add(pluginData.onlineData.second.name)
|
|
|
|
}
|
2022-08-08 18:52:03 +00:00
|
|
|
}
|
2022-08-06 23:43:39 +00:00
|
|
|
}
|
2022-08-07 16:01:32 +00:00
|
|
|
|
2022-09-22 10:50:28 +00:00
|
|
|
main {
|
|
|
|
createNotification(activity, updatedPlugins)
|
|
|
|
}
|
|
|
|
|
2022-10-31 00:16:15 +00:00
|
|
|
// ioSafe {
|
2022-08-23 19:28:42 +00:00
|
|
|
afterPluginsLoadedEvent.invoke(true)
|
2022-10-31 00:16:15 +00:00
|
|
|
// }
|
2022-08-23 19:28:42 +00:00
|
|
|
|
2022-08-07 21:11:13 +00:00
|
|
|
Log.i(TAG, "Plugin update done!")
|
2022-08-06 23:43:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 18:52:03 +00:00
|
|
|
/**
|
|
|
|
* Use updateAllOnlinePluginsAndLoadThem
|
|
|
|
* */
|
2022-10-31 00:16:15 +00:00
|
|
|
fun loadAllOnlinePlugins(activity: Activity) = ioSafe {
|
2022-08-25 11:48:55 +00:00
|
|
|
// Load all plugins as fast as possible!
|
2022-10-31 00:16:15 +00:00
|
|
|
(getPluginsOnline()).toList().amap { pluginData ->
|
2022-08-25 11:48:55 +00:00
|
|
|
loadPlugin(
|
|
|
|
activity,
|
|
|
|
File(pluginData.filePath),
|
|
|
|
pluginData
|
|
|
|
)
|
|
|
|
}
|
2022-08-06 23:43:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 00:16:15 +00:00
|
|
|
fun loadAllLocalPlugins(activity: Activity) = ioSafe {
|
2022-08-06 23:43:39 +00:00
|
|
|
val dir = File(LOCAL_PLUGINS_PATH)
|
|
|
|
removeKey(PLUGINS_KEY_LOCAL)
|
|
|
|
|
2022-08-04 10:51:11 +00:00
|
|
|
if (!dir.exists()) {
|
2022-08-06 15:48:00 +00:00
|
|
|
val res = dir.mkdirs()
|
2022-08-04 10:51:11 +00:00
|
|
|
if (!res) {
|
2022-08-08 20:02:02 +00:00
|
|
|
Log.w(TAG, "Failed to create local directories")
|
2022-10-31 00:16:15 +00:00
|
|
|
return@ioSafe
|
2022-08-04 10:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-06 23:43:39 +00:00
|
|
|
|
2022-08-06 15:48:00 +00:00
|
|
|
val sortedPlugins = dir.listFiles()
|
2022-08-04 10:51:11 +00:00
|
|
|
// Always sort plugins alphabetically for reproducible results
|
|
|
|
|
2022-08-14 16:09:19 +00:00
|
|
|
Log.d(TAG, "Files in '${LOCAL_PLUGINS_PATH}' folder: $sortedPlugins")
|
2022-08-09 08:33:16 +00:00
|
|
|
|
2022-08-07 21:11:13 +00:00
|
|
|
sortedPlugins?.sortedBy { it.name }?.apmap { file ->
|
|
|
|
maybeLoadPlugin(activity, file)
|
2022-08-04 10:51:11 +00:00
|
|
|
}
|
2022-08-06 15:48:00 +00:00
|
|
|
|
2022-08-06 23:43:39 +00:00
|
|
|
loadedLocalPlugins = true
|
2022-08-23 19:28:42 +00:00
|
|
|
afterPluginsLoadedEvent.invoke(true)
|
2022-08-04 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 23:43:39 +00:00
|
|
|
/**
|
|
|
|
* @return True if successful, false if not
|
|
|
|
* */
|
2022-08-07 21:11:13 +00:00
|
|
|
private suspend fun loadPlugin(activity: Activity, file: File, data: PluginData): Boolean {
|
2022-08-06 15:48:00 +00:00
|
|
|
val fileName = file.nameWithoutExtension
|
2022-08-07 16:01:32 +00:00
|
|
|
val filePath = file.absolutePath
|
2022-09-01 12:13:54 +00:00
|
|
|
currentlyLoading = fileName
|
2022-08-07 21:11:13 +00:00
|
|
|
Log.i(TAG, "Loading plugin: $data")
|
2022-08-06 23:43:39 +00:00
|
|
|
|
|
|
|
return try {
|
2022-08-07 21:11:13 +00:00
|
|
|
val loader = PathClassLoader(filePath, activity.classLoader)
|
2022-08-06 15:48:00 +00:00
|
|
|
var manifest: Plugin.Manifest
|
|
|
|
loader.getResourceAsStream("manifest.json").use { stream ->
|
2022-08-04 10:51:11 +00:00
|
|
|
if (stream == null) {
|
2022-08-08 20:02:02 +00:00
|
|
|
Log.e(TAG, "Failed to load plugin $fileName: No manifest found")
|
2022-08-06 23:43:39 +00:00
|
|
|
return false
|
2022-08-04 10:51:11 +00:00
|
|
|
}
|
2022-08-06 15:48:00 +00:00
|
|
|
InputStreamReader(stream).use { reader ->
|
|
|
|
manifest = gson.fromJson(
|
|
|
|
reader,
|
|
|
|
Plugin.Manifest::class.java
|
|
|
|
)
|
2022-08-04 10:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-07 16:01:32 +00:00
|
|
|
|
2022-08-17 21:59:21 +00:00
|
|
|
val name: String = manifest.name ?: "NO NAME".also {
|
|
|
|
Log.d(TAG, "No manifest name for ${data.internalName}")
|
|
|
|
}
|
|
|
|
val version: Int = manifest.version ?: PLUGIN_VERSION_NOT_SET.also {
|
|
|
|
Log.d(TAG, "No manifest version for ${data.internalName}")
|
|
|
|
}
|
2022-08-06 15:48:00 +00:00
|
|
|
val pluginClass: Class<*> =
|
|
|
|
loader.loadClass(manifest.pluginClassName) as Class<out Plugin?>
|
|
|
|
val pluginInstance: Plugin =
|
|
|
|
pluginClass.newInstance() as Plugin
|
2022-08-07 16:01:32 +00:00
|
|
|
|
|
|
|
// Sets with the proper version
|
|
|
|
setPluginData(data.copy(version = version))
|
|
|
|
|
2022-08-07 21:11:13 +00:00
|
|
|
if (plugins.containsKey(filePath)) {
|
|
|
|
Log.i(TAG, "Plugin with name $name already exists")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-08-06 15:48:00 +00:00
|
|
|
pluginInstance.__filename = fileName
|
2022-08-09 16:14:36 +00:00
|
|
|
if (manifest.requiresResources) {
|
|
|
|
Log.d(TAG, "Loading resources for ${data.internalName}")
|
2022-08-04 10:51:11 +00:00
|
|
|
// based on https://stackoverflow.com/questions/7483568/dynamic-resource-loading-from-other-apk
|
2022-08-06 15:48:00 +00:00
|
|
|
val assets = AssetManager::class.java.newInstance()
|
|
|
|
val addAssetPath =
|
|
|
|
AssetManager::class.java.getMethod("addAssetPath", String::class.java)
|
|
|
|
addAssetPath.invoke(assets, file.absolutePath)
|
|
|
|
pluginInstance.resources = Resources(
|
|
|
|
assets,
|
2022-08-07 21:11:13 +00:00
|
|
|
activity.resources.displayMetrics,
|
|
|
|
activity.resources.configuration
|
2022-08-06 15:48:00 +00:00
|
|
|
)
|
2022-08-04 10:51:11 +00:00
|
|
|
}
|
2022-08-07 16:01:32 +00:00
|
|
|
plugins[filePath] = pluginInstance
|
2022-08-06 15:48:00 +00:00
|
|
|
classLoaders[loader] = pluginInstance
|
2022-09-10 17:21:01 +00:00
|
|
|
urlPlugins[data.url ?: filePath] = pluginInstance
|
2022-08-07 21:11:13 +00:00
|
|
|
pluginInstance.load(activity)
|
|
|
|
Log.i(TAG, "Loaded plugin ${data.internalName} successfully")
|
2022-09-01 12:13:54 +00:00
|
|
|
currentlyLoading = null
|
2022-08-06 23:43:39 +00:00
|
|
|
true
|
2022-08-06 15:48:00 +00:00
|
|
|
} catch (e: Throwable) {
|
2022-08-08 20:02:02 +00:00
|
|
|
Log.e(TAG, "Failed to load $file: ${Log.getStackTraceString(e)}")
|
2022-08-07 07:28:21 +00:00
|
|
|
showToast(
|
2022-08-07 21:11:13 +00:00
|
|
|
activity,
|
|
|
|
activity.getString(R.string.plugin_load_fail).format(fileName),
|
2022-08-07 07:28:21 +00:00
|
|
|
Toast.LENGTH_LONG
|
|
|
|
)
|
2022-09-01 12:13:54 +00:00
|
|
|
currentlyLoading = null
|
2022-08-06 23:43:39 +00:00
|
|
|
false
|
2022-08-04 10:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-06 23:43:39 +00:00
|
|
|
|
2022-08-11 08:49:20 +00:00
|
|
|
private fun unloadPlugin(absolutePath: String) {
|
2022-08-09 12:17:29 +00:00
|
|
|
Log.i(TAG, "Unloading plugin: $absolutePath")
|
2022-08-11 08:49:20 +00:00
|
|
|
val plugin = plugins[absolutePath]
|
2022-08-09 08:33:16 +00:00
|
|
|
if (plugin == null) {
|
|
|
|
Log.w(TAG, "Couldn't find plugin $absolutePath")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
plugin.beforeUnload()
|
|
|
|
} catch (e: Throwable) {
|
|
|
|
Log.e(TAG, "Failed to run beforeUnload $absolutePath: ${Log.getStackTraceString(e)}")
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all registered apis
|
2022-08-18 00:54:05 +00:00
|
|
|
APIHolder.apis.filter { api -> api.sourcePlugin == plugin.__filename }.forEach {
|
2022-08-09 15:03:58 +00:00
|
|
|
removePluginMapping(it)
|
|
|
|
}
|
2022-08-09 15:19:26 +00:00
|
|
|
APIHolder.allProviders.removeIf { provider: MainAPI -> provider.sourcePlugin == plugin.__filename }
|
|
|
|
extractorApis.removeIf { provider: ExtractorApi -> provider.sourcePlugin == plugin.__filename }
|
2022-08-09 08:33:16 +00:00
|
|
|
|
2022-08-11 17:39:34 +00:00
|
|
|
classLoaders.values.removeIf { v -> v == plugin }
|
2022-08-09 15:03:58 +00:00
|
|
|
|
2022-08-09 08:33:16 +00:00
|
|
|
plugins.remove(absolutePath)
|
2022-09-10 17:21:01 +00:00
|
|
|
urlPlugins.values.removeIf { v -> v == plugin }
|
2022-08-09 08:33:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 18:14:57 +00:00
|
|
|
/**
|
|
|
|
* Spits out a unique and safe filename based on name.
|
|
|
|
* Used for repo folders (using repo url) and plugin file names (using internalName)
|
|
|
|
* */
|
|
|
|
fun getPluginSanitizedFileName(name: String): String {
|
|
|
|
return sanitizeFilename(
|
|
|
|
name,
|
|
|
|
true
|
|
|
|
) + "." + name.hashCode()
|
|
|
|
}
|
|
|
|
|
2022-10-06 16:30:58 +00:00
|
|
|
/**
|
|
|
|
* This should not be changed as it is used to also detect if a plugin is installed!
|
|
|
|
**/
|
|
|
|
fun getPluginPath(
|
|
|
|
context: Context,
|
|
|
|
internalName: String,
|
|
|
|
repositoryUrl: String
|
|
|
|
): File {
|
|
|
|
val folderName = getPluginSanitizedFileName(repositoryUrl) // Guaranteed unique
|
|
|
|
val fileName = getPluginSanitizedFileName(internalName)
|
|
|
|
return File("${context.filesDir}/${ONLINE_PLUGINS_FOLDER}/${folderName}/$fileName.cs3")
|
|
|
|
}
|
2022-09-23 12:20:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for fresh installs
|
|
|
|
* */
|
2022-08-07 16:01:32 +00:00
|
|
|
suspend fun downloadAndLoadPlugin(
|
2022-08-07 21:11:13 +00:00
|
|
|
activity: Activity,
|
2022-08-07 16:01:32 +00:00
|
|
|
pluginUrl: String,
|
|
|
|
internalName: String,
|
|
|
|
repositoryUrl: String
|
2022-09-23 12:20:53 +00:00
|
|
|
): Boolean {
|
2022-10-06 16:30:58 +00:00
|
|
|
val file = getPluginPath(activity, internalName, repositoryUrl)
|
2022-09-23 12:20:53 +00:00
|
|
|
downloadAndLoadPlugin(activity, pluginUrl, internalName, file)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for updates.
|
|
|
|
*
|
|
|
|
* Uses a file instead of repository url, as extensions can get moved it is better to directly
|
|
|
|
* update the files instead of getting the filepath from repo url.
|
|
|
|
* */
|
|
|
|
private suspend fun downloadAndLoadPlugin(
|
|
|
|
activity: Activity,
|
|
|
|
pluginUrl: String,
|
|
|
|
internalName: String,
|
|
|
|
file: File,
|
2022-08-07 16:01:32 +00:00
|
|
|
): Boolean {
|
2022-08-18 00:54:05 +00:00
|
|
|
try {
|
2022-09-23 12:20:53 +00:00
|
|
|
unloadPlugin(file.absolutePath)
|
2022-08-23 19:28:42 +00:00
|
|
|
|
2022-09-23 12:20:53 +00:00
|
|
|
Log.d(TAG, "Downloading plugin: $pluginUrl to ${file.absolutePath}")
|
2022-08-18 00:54:05 +00:00
|
|
|
// The plugin file needs to be salted with the repository url hash as to allow multiple repositories with the same internal plugin names
|
2022-09-23 12:20:53 +00:00
|
|
|
val newFile = downloadPluginToFile(pluginUrl, file)
|
2022-08-18 00:54:05 +00:00
|
|
|
return loadPlugin(
|
|
|
|
activity,
|
2022-09-23 12:20:53 +00:00
|
|
|
newFile ?: return false,
|
2022-10-06 16:30:58 +00:00
|
|
|
PluginData(
|
|
|
|
internalName,
|
|
|
|
pluginUrl,
|
|
|
|
true,
|
|
|
|
newFile.absolutePath,
|
|
|
|
PLUGIN_VERSION_NOT_SET
|
|
|
|
)
|
2022-08-18 00:54:05 +00:00
|
|
|
)
|
2022-08-21 01:44:49 +00:00
|
|
|
} catch (e: Exception) {
|
2022-08-18 00:54:05 +00:00
|
|
|
logError(e)
|
|
|
|
return false
|
|
|
|
}
|
2022-08-06 23:43:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 12:53:51 +00:00
|
|
|
suspend fun deletePlugin(file: File): Boolean {
|
|
|
|
val list = (getPluginsLocal() + getPluginsOnline()).filter { it.filePath == file.absolutePath }
|
2022-08-11 22:36:19 +00:00
|
|
|
|
2022-08-07 21:11:13 +00:00
|
|
|
return try {
|
2022-10-07 12:53:51 +00:00
|
|
|
if (File(file.absolutePath).delete()) {
|
|
|
|
unloadPlugin(file.absolutePath)
|
|
|
|
list.forEach { deletePluginData(it) }
|
2022-08-07 21:11:13 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
false
|
|
|
|
} catch (e: Exception) {
|
|
|
|
false
|
|
|
|
}
|
2022-08-06 23:43:39 +00:00
|
|
|
}
|
2022-09-22 10:50:28 +00:00
|
|
|
|
|
|
|
private fun Context.createNotificationChannel() {
|
|
|
|
hasCreatedNotChanel = true
|
|
|
|
// Create the NotificationChannel, but only on API 26+ because
|
|
|
|
// the NotificationChannel class is new and not in the support library
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
val name = EXTENSIONS_CHANNEL_NAME //getString(R.string.channel_name)
|
2022-09-23 12:20:53 +00:00
|
|
|
val descriptionText =
|
|
|
|
EXTENSIONS_CHANNEL_DESCRIPT//getString(R.string.channel_description)
|
2022-09-22 10:50:28 +00:00
|
|
|
val importance = NotificationManager.IMPORTANCE_LOW
|
|
|
|
val channel = NotificationChannel(EXTENSIONS_CHANNEL_ID, name, importance).apply {
|
|
|
|
description = descriptionText
|
|
|
|
}
|
|
|
|
// Register the channel with the system
|
|
|
|
val notificationManager: NotificationManager =
|
|
|
|
this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
notificationManager.createNotificationChannel(channel)
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 12:20:53 +00:00
|
|
|
|
2022-09-22 10:50:28 +00:00
|
|
|
private fun createNotification(
|
|
|
|
context: Context,
|
|
|
|
extensionNames: List<String>
|
2022-09-23 12:20:53 +00:00
|
|
|
): Notification? {
|
2022-09-22 10:50:28 +00:00
|
|
|
try {
|
|
|
|
if (extensionNames.isEmpty()) return null
|
|
|
|
|
|
|
|
val content = extensionNames.joinToString(", ")
|
|
|
|
// main { // DON'T WANT TO SLOW IT DOWN
|
|
|
|
val builder = NotificationCompat.Builder(context, EXTENSIONS_CHANNEL_ID)
|
|
|
|
.setAutoCancel(false)
|
|
|
|
.setColorized(true)
|
|
|
|
.setOnlyAlertOnce(true)
|
|
|
|
.setSilent(true)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
|
|
.setColor(context.colorFromAttribute(R.attr.colorPrimary))
|
|
|
|
.setContentTitle(context.getString(R.string.plugins_updated, extensionNames.size))
|
|
|
|
.setSmallIcon(R.drawable.ic_baseline_extension_24)
|
2022-09-23 12:20:53 +00:00
|
|
|
.setStyle(
|
|
|
|
NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(content)
|
|
|
|
)
|
2022-09-22 10:50:28 +00:00
|
|
|
.setContentText(content)
|
|
|
|
|
|
|
|
if (!hasCreatedNotChanel) {
|
|
|
|
context.createNotificationChannel()
|
|
|
|
}
|
|
|
|
|
|
|
|
val notification = builder.build()
|
|
|
|
with(NotificationManagerCompat.from(context)) {
|
|
|
|
// notificationId is a unique int for each notification that you must define
|
2022-09-23 12:20:53 +00:00
|
|
|
notify((System.currentTimeMillis() / 1000).toInt(), notification)
|
2022-09-22 10:50:28 +00:00
|
|
|
}
|
|
|
|
return notification
|
|
|
|
} catch (e: Exception) {
|
|
|
|
logError(e)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 10:51:11 +00:00
|
|
|
}
|