diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt index 87906740e0..62c93dc09b 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt @@ -23,231 +23,251 @@ import java.io.FileNotFoundException import java.io.IOException class PluginResourceContext( - baseContext: Context, - private val pluginId: String, - private var pluginResources: Resources, - private val pluginPackageInfo: PackageInfo? = null, - private val pluginClassLoader: ClassLoader? = null + baseContext: Context, + private val pluginId: String, + private var pluginResources: Resources, + private val pluginPackageInfo: PackageInfo? = null, + private val pluginClassLoader: ClassLoader? = null, ) : ContextThemeWrapper(baseContext, 0) { + companion object { + private const val TAG = "PluginResourceContext" + private const val PLUGIN_THEME_NAME = "PluginTheme" + private val ADD_ASSET_PATH_METHOD = AssetManager::class.java.getMethod("addAssetPath", String::class.java) + } - companion object { - private const val TAG = "PluginResourceContext" - private const val PLUGIN_THEME_NAME = "PluginTheme" - private val ADD_ASSET_PATH_METHOD = AssetManager::class.java.getMethod("addAssetPath", String::class.java) - } - - private var inflater: LayoutInflater? = null - private var pluginTheme: Theme? = null - private var lastActivityTheme: Theme? = null - private var lastNightMode: Int = -1 - private var usesCustomPackageId = false - private val pluginSourceDir: String? = pluginPackageInfo?.applicationInfo?.sourceDir - private val appThemeResId: Int = pluginPackageInfo?.applicationInfo?.theme ?: 0 - private var pluginThemeResId: Int = 0 - private var pluginThemeResIdResolved = false - - init { - usesCustomPackageId = detectCustomPackageId() - } - - private fun detectCustomPackageId(): Boolean { - val cl = pluginClassLoader ?: return false - val pkg = pluginPackageInfo?.packageName ?: return false - return try { - val rClass = cl.loadClass("$pkg.R") - rClass.declaredClasses.asSequence() - .flatMap { it.declaredFields.asSequence() } - .filter { it.type == Int::class.javaPrimitiveType } - .map { it.getInt(null) } - .filter { it != 0 } - .any { (it ushr 24) != 0x7F } - } catch (e: ReflectiveOperationException) { - Log.w(TAG, "Failed to detect custom package ID for $pkg", e) - false - } - } - - fun usesCustomPackageId(): Boolean = usesCustomPackageId - - private var recreatedAssetManager: AssetManager? = null - - private fun recreatePluginResources(newConfig: Configuration) { - val sourceDir = pluginSourceDir ?: return - val oldAssetManager = recreatedAssetManager - @Suppress("DEPRECATION") - val assetManager = AssetManager::class.java.getDeclaredConstructor().newInstance() - addAssetPathFallback(assetManager, sourceDir) - @Suppress("DEPRECATION") - pluginResources = Resources(assetManager, baseContext.resources.displayMetrics, newConfig) - recreatedAssetManager = assetManager - oldAssetManager?.close() - } - - private var patchedResources: Resources? = null - private var cachedLoader: ResourcesLoader? = null - private var cachedProvider: ResourcesProvider? = null - - @Suppress("NewApi") - private fun ensurePluginResourcesAdded(targetResources: Resources) { - if (patchedResources === targetResources) return - val sourceDir = pluginSourceDir ?: return - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - try { - if (cachedLoader == null) { - ParcelFileDescriptor.open(File(sourceDir), ParcelFileDescriptor.MODE_READ_ONLY).use { pfd -> - cachedProvider = ResourcesProvider.loadFromApk(pfd) - cachedLoader = ResourcesLoader().also { it.addProvider(cachedProvider!!) } - } - } - targetResources.addLoaders(cachedLoader!!) - patchedResources = targetResources - Log.d(TAG, "Added plugin resources via ResourcesLoader for ${pluginPackageInfo?.packageName}") - } catch (e: FileNotFoundException) { - Log.e(TAG, "ResourcesLoader failed, falling back to addAssetPath", e) - addAssetPathFallback(targetResources.assets, sourceDir) - patchedResources = targetResources - } catch (e: IOException) { - Log.e(TAG, "ResourcesLoader failed, falling back to addAssetPath", e) - addAssetPathFallback(targetResources.assets, sourceDir) - patchedResources = targetResources - } - } else { - addAssetPathFallback(targetResources.assets, sourceDir) - patchedResources = targetResources - } - } - - private fun addAssetPathFallback(assets: AssetManager, pluginSourceDir: String) { - try { - ADD_ASSET_PATH_METHOD.invoke(assets, pluginSourceDir) - } catch (e: Exception) { - Log.e(TAG, "Failed to add plugin asset path", e) - } - } - - private fun resolveActivityContextAndTheme(): Pair? { - val fragmentCtx = PluginFragmentHelper.getCurrentActivityContext(pluginId) - val fragmentTheme = PluginFragmentHelper.getCurrentActivityTheme(pluginId) - if (fragmentCtx != null && fragmentTheme != null) { - return fragmentCtx to fragmentTheme - } - val activity = PluginManager.getInstance()?.getCurrentActivity() ?: return null - return activity to activity.theme - } - - override fun getResources(): Resources { - if (!usesCustomPackageId) return pluginResources - - val (actCtx, _) = resolveActivityContextAndTheme() ?: return pluginResources - ensurePluginResourcesAdded(actCtx.resources) - return actCtx.resources - } - - override fun getAssets(): AssetManager = getResources().assets - - override fun getTheme(): Theme { - if (!usesCustomPackageId) { - val currentNightMode = baseContext.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - if (pluginTheme == null || lastNightMode != currentNightMode) { - lastNightMode = currentNightMode - inflater = null - recreatePluginResources(baseContext.resources.configuration) - if (!pluginThemeResIdResolved) { - pluginThemeResId = pluginResources.getIdentifier( - PLUGIN_THEME_NAME, "style", pluginPackageInfo?.packageName - ) - pluginThemeResIdResolved = true - } - val themeResId = when { - pluginThemeResId != 0 -> pluginThemeResId - appThemeResId != 0 -> appThemeResId - currentNightMode == Configuration.UI_MODE_NIGHT_YES -> android.R.style.Theme_Material - else -> android.R.style.Theme_Material_Light - } - pluginTheme = pluginResources.newTheme().apply { - applyStyle(themeResId, true) - } - } - return pluginTheme!! - } - - val (actCtx, actTheme) = resolveActivityContextAndTheme() ?: return pluginTheme ?: baseContext.theme - ensurePluginResourcesAdded(actCtx.resources) - if (pluginTheme == null || lastActivityTheme !== actTheme) { - lastActivityTheme = actTheme - inflater = null - - if (!pluginThemeResIdResolved) { - pluginThemeResId = actCtx.resources.getIdentifier( - PLUGIN_THEME_NAME, "style", pluginPackageInfo?.packageName - ) - pluginThemeResIdResolved = true - } - - pluginTheme = actCtx.resources.newTheme().apply { - setTo(actTheme) - - if (appThemeResId != 0) { - applyStyle(appThemeResId, false) - } - - if (pluginThemeResId != 0) { - applyStyle(pluginThemeResId, true) - } - } - } - return pluginTheme ?: baseContext.theme - } - - override fun getClassLoader(): ClassLoader { - return pluginClassLoader ?: baseContext.classLoader - } - - override fun getPackageName(): String { - return pluginPackageInfo?.packageName ?: super.getPackageName() - } - - override fun getApplicationInfo(): ApplicationInfo { - return pluginPackageInfo?.applicationInfo ?: super.getApplicationInfo() - } - - override fun getSystemService(name: String): Any? { - if (Context.LAYOUT_INFLATER_SERVICE == name) { - if (inflater == null) { - if (!usesCustomPackageId) { - val baseInflater = LayoutInflater.from(baseContext) - inflater = object : LayoutInflater(baseInflater, this@PluginResourceContext) { - override fun cloneInContext(newContext: Context): LayoutInflater { - return this@PluginResourceContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater - } - - override fun onCreateView(viewName: String, attrs: AttributeSet): View? { - if (viewName.indexOf('.') == -1) { - for (prefix in arrayOf("android.widget.", "android.view.", "android.webkit.")) { - try { return createView(viewName, prefix, attrs) } catch (_: ClassNotFoundException) {} - } - } - return super.onCreateView(viewName, attrs) - } - } - } else { - inflater = LayoutInflater.from(baseContext).cloneInContext(this) - } - } - return inflater - } - return super.getSystemService(name) - } - - fun getPluginPackageInfo(): PackageInfo? = pluginPackageInfo - - fun getResourceId(name: String, type: String): Int { - return getResources().getIdentifier(name, type, packageName) - } - - fun inflateLayout(layoutResId: Int, root: android.view.ViewGroup? = null, attachToRoot: Boolean = false): android.view.View { - val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater - return inflater.inflate(layoutResId, root, attachToRoot) - } -} \ No newline at end of file + private var inflater: LayoutInflater? = null + private var pluginTheme: Theme? = null + private var lastActivityTheme: Theme? = null + private var lastNightMode: Int = -1 + private var usesCustomPackageId = false + private val pluginSourceDir: String? = pluginPackageInfo?.applicationInfo?.sourceDir + private val appThemeResId: Int = pluginPackageInfo?.applicationInfo?.theme ?: 0 + private var pluginThemeResId: Int = 0 + private var pluginThemeResIdResolved = false + + init { + usesCustomPackageId = detectCustomPackageId() + } + + private fun detectCustomPackageId(): Boolean { + val cl = pluginClassLoader ?: return false + val pkg = pluginPackageInfo?.packageName ?: return false + return try { + val rClass = cl.loadClass("$pkg.R") + rClass.declaredClasses + .asSequence() + .flatMap { it.declaredFields.asSequence() } + .filter { it.type == Int::class.javaPrimitiveType } + .map { it.getInt(null) } + .filter { it != 0 } + .any { (it ushr 24) != 0x7F } + } catch (e: ReflectiveOperationException) { + Log.w(TAG, "Failed to detect custom package ID for $pkg", e) + false + } + } + + fun usesCustomPackageId(): Boolean = usesCustomPackageId + + private var recreatedAssetManager: AssetManager? = null + + private fun recreatePluginResources(newConfig: Configuration) { + val sourceDir = pluginSourceDir ?: return + val oldAssetManager = recreatedAssetManager + + @Suppress("DEPRECATION") + val assetManager = AssetManager::class.java.getDeclaredConstructor().newInstance() + addAssetPathFallback(assetManager, sourceDir) + @Suppress("DEPRECATION") + pluginResources = Resources(assetManager, baseContext.resources.displayMetrics, newConfig) + recreatedAssetManager = assetManager + oldAssetManager?.close() + } + + private var patchedResources: Resources? = null + private var cachedLoader: ResourcesLoader? = null + private var cachedProvider: ResourcesProvider? = null + + @Suppress("NewApi") + private fun ensurePluginResourcesAdded(targetResources: Resources) { + if (patchedResources === targetResources) return + val sourceDir = pluginSourceDir ?: return + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + try { + if (cachedLoader == null) { + ParcelFileDescriptor.open(File(sourceDir), ParcelFileDescriptor.MODE_READ_ONLY).use { pfd -> + cachedProvider = ResourcesProvider.loadFromApk(pfd) + cachedLoader = ResourcesLoader().also { it.addProvider(cachedProvider!!) } + } + } + targetResources.addLoaders(cachedLoader!!) + patchedResources = targetResources + Log.d(TAG, "Added plugin resources via ResourcesLoader for ${pluginPackageInfo?.packageName}") + } catch (e: FileNotFoundException) { + Log.e(TAG, "ResourcesLoader failed, falling back to addAssetPath", e) + addAssetPathFallback(targetResources.assets, sourceDir) + patchedResources = targetResources + } catch (e: IOException) { + Log.e(TAG, "ResourcesLoader failed, falling back to addAssetPath", e) + addAssetPathFallback(targetResources.assets, sourceDir) + patchedResources = targetResources + } + } else { + addAssetPathFallback(targetResources.assets, sourceDir) + patchedResources = targetResources + } + } + + private fun addAssetPathFallback( + assets: AssetManager, + pluginSourceDir: String, + ) { + try { + ADD_ASSET_PATH_METHOD.invoke(assets, pluginSourceDir) + } catch (e: Exception) { + Log.e(TAG, "Failed to add plugin asset path", e) + } + } + + private fun resolveActivityContextAndTheme(): Pair? { + val fragmentCtx = PluginFragmentHelper.getCurrentActivityContext(pluginId) + val fragmentTheme = PluginFragmentHelper.getCurrentActivityTheme(pluginId) + if (fragmentCtx != null && fragmentTheme != null) { + return fragmentCtx to fragmentTheme + } + val activity = PluginManager.getInstance()?.getCurrentActivity() ?: return null + return activity to activity.theme + } + + override fun getResources(): Resources { + if (!usesCustomPackageId) return pluginResources + + val (actCtx, _) = resolveActivityContextAndTheme() ?: return pluginResources + ensurePluginResourcesAdded(actCtx.resources) + return actCtx.resources + } + + // Never the activity's: every plugin with its own package id adds its APK to those, and asset + // paths carry no package id, so a path two plugins both ship opens whichever was added last. + override fun getAssets(): AssetManager = pluginResources.assets + + override fun getTheme(): Theme { + if (!usesCustomPackageId) { + val currentNightMode = baseContext.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + if (pluginTheme == null || lastNightMode != currentNightMode) { + lastNightMode = currentNightMode + inflater = null + recreatePluginResources(baseContext.resources.configuration) + if (!pluginThemeResIdResolved) { + pluginThemeResId = + pluginResources.getIdentifier( + PLUGIN_THEME_NAME, + "style", + pluginPackageInfo?.packageName, + ) + pluginThemeResIdResolved = true + } + val themeResId = + when { + pluginThemeResId != 0 -> pluginThemeResId + appThemeResId != 0 -> appThemeResId + currentNightMode == Configuration.UI_MODE_NIGHT_YES -> android.R.style.Theme_Material + else -> android.R.style.Theme_Material_Light + } + pluginTheme = + pluginResources.newTheme().apply { + applyStyle(themeResId, true) + } + } + return pluginTheme!! + } + + val (actCtx, actTheme) = resolveActivityContextAndTheme() ?: return pluginTheme ?: baseContext.theme + ensurePluginResourcesAdded(actCtx.resources) + if (pluginTheme == null || lastActivityTheme !== actTheme) { + lastActivityTheme = actTheme + inflater = null + + if (!pluginThemeResIdResolved) { + pluginThemeResId = + actCtx.resources.getIdentifier( + PLUGIN_THEME_NAME, + "style", + pluginPackageInfo?.packageName, + ) + pluginThemeResIdResolved = true + } + + pluginTheme = + actCtx.resources.newTheme().apply { + setTo(actTheme) + + if (appThemeResId != 0) { + applyStyle(appThemeResId, false) + } + + if (pluginThemeResId != 0) { + applyStyle(pluginThemeResId, true) + } + } + } + return pluginTheme ?: baseContext.theme + } + + override fun getClassLoader(): ClassLoader = pluginClassLoader ?: baseContext.classLoader + + override fun getPackageName(): String = pluginPackageInfo?.packageName ?: super.getPackageName() + + override fun getApplicationInfo(): ApplicationInfo = pluginPackageInfo?.applicationInfo ?: super.getApplicationInfo() + + override fun getSystemService(name: String): Any? { + if (Context.LAYOUT_INFLATER_SERVICE == name) { + if (inflater == null) { + if (!usesCustomPackageId) { + val baseInflater = LayoutInflater.from(baseContext) + inflater = + object : LayoutInflater(baseInflater, this@PluginResourceContext) { + override fun cloneInContext(newContext: Context): LayoutInflater = + this@PluginResourceContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + + override fun onCreateView( + viewName: String, + attrs: AttributeSet, + ): View? { + if (viewName.indexOf('.') == -1) { + for (prefix in arrayOf("android.widget.", "android.view.", "android.webkit.")) { + try { + return createView(viewName, prefix, attrs) + } catch (_: ClassNotFoundException) { + } + } + } + return super.onCreateView(viewName, attrs) + } + } + } else { + inflater = LayoutInflater.from(baseContext).cloneInContext(this) + } + } + return inflater + } + return super.getSystemService(name) + } + + fun getPluginPackageInfo(): PackageInfo? = pluginPackageInfo + + fun getResourceId( + name: String, + type: String, + ): Int = getResources().getIdentifier(name, type, packageName) + + fun inflateLayout( + layoutResId: Int, + root: android.view.ViewGroup? = null, + attachToRoot: Boolean = false, + ): android.view.View { + val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + return inflater.inflate(layoutResId, root, attachToRoot) + } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt new file mode 100644 index 0000000000..c6c49505d1 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt @@ -0,0 +1,71 @@ +package com.itsaky.androidide.plugins.manager.loaders + +import android.content.Context +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.content.res.AssetManager +import android.content.res.Resources +import android.view.LayoutInflater +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.plugins.base.PluginFragmentHelper +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +/** + * A plugin with its own resource package id resolves its resources through the activity's, which + * every such plugin adds its APK to. Asset paths carry no package id, so a path two plugins both + * ship must still open the reading plugin's own copy. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginResourceContextAssetsTest { + private val app: Context = RuntimeEnvironment.getApplication() + + /** Stands in for the Resources the loader builds from the plugin's own APK. */ + @Suppress("DEPRECATION") + private val ownResources = + Resources( + AssetManager::class.java.getDeclaredConstructor().newInstance(), + app.resources.displayMetrics, + app.resources.configuration, + ) + + private val pluginContext = + PluginResourceContext( + app, + PLUGIN_ID, + ownResources, + PackageInfo().apply { + packageName = PLUGIN_ID + applicationInfo = ApplicationInfo() + }, + javaClass.classLoader, + ) + + @Before + fun registerPlugin() { + assertThat(pluginContext.usesCustomPackageId()).isTrue() + PluginFragmentHelper.registerPluginContext(PLUGIN_ID, pluginContext) + PluginFragmentHelper.getPluginInflater(PLUGIN_ID, LayoutInflater.from(app)) + } + + @After + fun unregisterPlugin() { + PluginFragmentHelper.unregisterPluginContext(PLUGIN_ID) + } + + @Test + fun assetsComeFromThePluginsOwnApkWhileResourcesComeFromTheActivity() { + assertThat(pluginContext.resources).isSameInstanceAs(app.resources) + assertThat(pluginContext.assets).isSameInstanceAs(ownResources.assets) + } + + private companion object { + const val PLUGIN_ID = "com.itsaky.androidide.plugins.manager.loaders.fakeplugin" + } +}