Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 55
ADFA-3694: add day/night icon support for plugins#1213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,11 +8,14 @@ import android.widget.PopupMenu | ||
| import androidx.recyclerview.widget.DiffUtil | ||
| import androidx.recyclerview.widget.ListAdapter | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import com.bumptech.glide.Glide | ||
| import com.itsaky.androidide.R | ||
| import com.itsaky.androidide.databinding.ItemPluginBinding | ||
| import com.itsaky.androidide.idetooltips.TooltipManager | ||
| import com.itsaky.androidide.idetooltips.TooltipTag | ||
| import com.itsaky.androidide.plugins.PluginInfo | ||
| import com.itsaky.androidide.utils.isSystemInDarkMode | ||
| import java.io.File | ||
| class PluginListAdapter( | ||
| private val onActionClick: (PluginInfo, Action) -> Unit | ||
| @@ -54,7 +57,27 @@ class PluginListAdapter( | ||
| "v$version" | ||
| } | ||
| pluginAuthor.text = "by ${plugin.metadata.author}" | ||
| val iconPath = if (itemView.context.isSystemInDarkMode()) { | ||
| plugin.metadata.iconNightPath | ||
| } else { | ||
| plugin.metadata.iconDayPath | ||
| } | ||
| pluginIcon.background = null | ||
| pluginIcon.imageTintList = null | ||
| val iconFile = iconPath?.let(::File)?.takeIf { it.exists() } | ||
| if (iconFile != null) { | ||
| Glide.with(pluginIcon) | ||
| .load(iconFile) | ||
| .placeholder(R.drawable.ic_extension) | ||
| .error(R.drawable.ic_extension) | ||
| .into(pluginIcon) | ||
| } else { | ||
| Glide.with(pluginIcon).clear(pluginIcon) | ||
| pluginIcon.setImageResource(R.drawable.ic_extension) | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // Set status | ||
| val statusText = when { | ||
| !plugin.isLoaded -> "Not Loaded" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -224,19 +224,28 @@ class PluginManager private constructor( | ||
| verifyDocumentationForLoadedPlugins() | ||
| } | ||
| fun getPluginMetadataOnly(pluginFile: File): Result<PluginManifest> { | ||
| if (!pluginFile.exists()) { | ||
| return Result.failure(IllegalArgumentException("Plugin file does not exist: ${pluginFile.absolutePath}")) | ||
| } | ||
| if (!pluginFile.canRead()) { | ||
| return Result.failure(IllegalArgumentException("Cannot read plugin file: ${pluginFile.absolutePath}")) | ||
| } | ||
| val pluginLoader = PluginLoader(context, pluginFile) | ||
| val manifest = pluginLoader.getPluginMetadata() | ||
| private fun loadAndValidate(pluginFile: File): Result<Pair<PluginManifest, PluginLoader>> { | ||
| if (!pluginFile.exists()) return Result.failure(IllegalArgumentException("Plugin file does not exist: ${pluginFile.absolutePath}")) | ||
| if (!pluginFile.canRead()) return Result.failure(IllegalArgumentException("Cannot read plugin file: ${pluginFile.absolutePath}")) | ||
| val loader = PluginLoader(context, pluginFile) | ||
| val manifest = loader.getPluginMetadata() | ||
| ?: return Result.failure(IllegalArgumentException("Plugin manifest not found in: ${pluginFile.name}")) | ||
| return Result.success(manifest) | ||
| return Result.success(manifest to loader) | ||
| } | ||
| fun getPluginMetadataOnly(pluginFile: File): Result<PluginManifest> = | ||
| loadAndValidate(pluginFile).map { it.first } | ||
| fun getPluginValidation(pluginFile: File): Result<PluginValidation> = | ||
| loadAndValidate(pluginFile).map { (manifest, loader) -> | ||
| PluginValidation( | ||
| manifest = manifest, | ||
| isDebug = loader.isDebuggable(), | ||
| iconDayEntryExists = manifest.iconDay?.let(loader::hasEntry) ?: false, | ||
| iconNightEntryExists = manifest.iconNight?.let(loader::hasEntry) ?: false | ||
| ) | ||
| } | ||
| /** | ||
| * Load plugin and return both the plugin instance and its metadata | ||
| */ | ||
| @@ -344,6 +353,13 @@ class PluginManager private constructor( | ||
| null | ||
| } | ||
| val (iconDayPath, iconNightPath) = try { | ||
| pluginLoader.extractPluginIcons(manifest.id, manifest) | ||
| } catch (e: Exception) { | ||
| logger.warn("Failed to extract icons for plugin: ${manifest.id}", e) | ||
| null to null | ||
| } | ||
| if (nativeLibPath != null && !permissions.contains(PluginPermission.NATIVE_CODE)) { | ||
| File(nativeLibPath).deleteRecursively() | ||
| if (manifest.sidebarItems > 0) { | ||
| @@ -415,7 +431,11 @@ class PluginManager private constructor( | ||
| logger.debug("Registered service registry for plugin: ${manifest.id}") | ||
| val isEnabled = getPluginState(manifest.id) | ||
| val loadedPlugin = LoadedPlugin(plugin, manifest, classLoader, pluginContext, isEnabled) | ||
| val loadedPlugin = LoadedPlugin( | ||
| plugin, manifest, classLoader, pluginContext, isEnabled, | ||
| iconDayPath = iconDayPath, | ||
| iconNightPath = iconNightPath | ||
| ) | ||
| loadedPlugins[manifest.id] = loadedPlugin | ||
| if (isEnabled) { | ||
| try { | ||
| @@ -614,7 +634,9 @@ class PluginManager private constructor( | ||
| author = loadedPlugin.manifest.author, | ||
| minIdeVersion = loadedPlugin.manifest.minIdeVersion, | ||
| dependencies = loadedPlugin.manifest.dependencies, | ||
| permissions = loadedPlugin.manifest.permissions | ||
| permissions = loadedPlugin.manifest.permissions, | ||
| iconDayPath = loadedPlugin.iconDayPath, | ||
| iconNightPath = loadedPlugin.iconNightPath | ||
| ), | ||
| isEnabled = loadedPlugin.isEnabled, | ||
| isLoaded = true | ||
| @@ -1178,6 +1200,10 @@ class PluginManager private constructor( | ||
| if (dir.exists()) dir.deleteRecursively() | ||
| } | ||
| File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId).let { dir -> | ||
| if (dir.exists()) dir.deleteRecursively() | ||
| } | ||
Daniel-ADFA marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // Clean up ART cache files in oat directory | ||
| try { | ||
| val oatDir = File(pluginsDir, "oat") | ||
| @@ -1238,10 +1264,19 @@ class PluginManager private constructor( | ||
| } | ||
| data class PluginValidation( | ||
| val manifest: PluginManifest, | ||
| val isDebug: Boolean, | ||
| val iconDayEntryExists: Boolean, | ||
| val iconNightEntryExists: Boolean | ||
| ) | ||
| data class LoadedPlugin( | ||
| val plugin: IPlugin, | ||
| val manifest: PluginManifest, | ||
| val classLoader: ClassLoader, | ||
| val context: PluginContext, | ||
| var isEnabled: Boolean = true | ||
| var isEnabled: Boolean = true, | ||
| val iconDayPath: String? = null, | ||
| val iconNightPath: String? = null | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package com.itsaky.androidide.plugins.manager.loaders | ||
| import android.content.Context | ||
| import android.content.pm.ApplicationInfo | ||
| import android.content.pm.PackageInfo | ||
| import android.content.pm.PackageManager | ||
| import android.content.res.AssetManager | ||
| @@ -178,6 +179,51 @@ class PluginLoader( | ||
| return pluginNativeDir | ||
| } | ||
| fun isDebuggable(): Boolean { | ||
| val packageInfo = pluginPackageInfo | ||
| ?: context.packageManager.getPackageArchiveInfo( | ||
| pluginApk.absolutePath, | ||
| PackageManager.GET_META_DATA | ||
| ) ?: return false | ||
| val appInfo = packageInfo.applicationInfo ?: return false | ||
| return (appInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0 | ||
| } | ||
| fun hasEntry(entryPath: String): Boolean = | ||
| try { | ||
| ZipFile(pluginApk).use { it.getEntry(entryPath) != null } | ||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Failed to inspect plugin APK for entry '$entryPath'", e) | ||
| false | ||
| } | ||
| fun extractPluginIcons(pluginId: String, manifest: PluginManifest): Pair<String?, String?> { | ||
| if (manifest.iconDay == null && manifest.iconNight == null) return null to null | ||
| val iconDir = File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId) | ||
| iconDir.deleteRecursively() | ||
| iconDir.mkdirs() | ||
| val targetPath = iconDir.toPath().toAbsolutePath().normalize() | ||
| return try { | ||
| ZipFile(pluginApk).use { zip -> | ||
| fun extractEntry(role: String, entryPath: String?): String? { | ||
| entryPath ?: return null | ||
| val entry = zip.getEntry(entryPath) ?: return null | ||
| val ext = entryPath.substringAfterLast('.', "").ifEmpty { "png" } | ||
| val outPath = targetPath.resolve("$role.$ext").normalize() | ||
| if (!outPath.startsWith(targetPath)) return null | ||
Daniel-ADFA marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| zip.getInputStream(entry).use { input -> | ||
| outPath.toFile().outputStream().use { output -> input.copyTo(output) } | ||
| } | ||
| return outPath.toFile().absolutePath | ||
| } | ||
| extractEntry("icon_day", manifest.iconDay) to extractEntry("icon_night", manifest.iconNight) | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Failed to extract plugin icons for $pluginId", e) | ||
| null to null | ||
| } | ||
| } | ||
Daniel-ADFA marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| fun getPluginMetadata(): PluginManifest? { | ||
| try { | ||
| val packageInfo = pluginPackageInfo ?: context.packageManager.getPackageArchiveInfo( | ||
| @@ -206,6 +252,9 @@ class PluginLoader( | ||
| // Parse sidebar items count | ||
| val sidebarItems = metaData.getInt("plugin.sidebar_items", 0) | ||
| val iconDay = metaData.getString("plugin.icon_day") | ||
| val iconNight = metaData.getString("plugin.icon_night") | ||
| return PluginManifest( | ||
| id = pluginId, | ||
| name = pluginName, | ||
| @@ -218,7 +267,9 @@ class PluginLoader( | ||
| permissions = permissions, | ||
| dependencies = dependencies, | ||
| extensions = emptyList(), | ||
| sidebarItems = sidebarItems | ||
| sidebarItems = sidebarItems, | ||
| iconDay = iconDay, | ||
| iconNight = iconNight | ||
| ) | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Failed to extract plugin metadata: ${e.message}", e) | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.