-
-
Notifications
You must be signed in to change notification settings - Fork 23
ADFA-2594: Fix plugin fragment crash on configuration change #853
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
adf59dd
Fix plugin fragment crash on configuration change
Daniel-ADFA 2a7b1aa
Fix thread safety and classloader leak in PluginFragmentFactory
Daniel-ADFA 5feab84
Fix plugin tooltip display and documentation database initialization
Daniel-ADFA db11c9a
Merge branch 'stage' into ADFA-2594
Daniel-ADFA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...r/src/main/kotlin/com/itsaky/androidide/plugins/manager/fragment/PluginFragmentFactory.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package com.itsaky.androidide.plugins.manager.fragment | ||
|
|
||
| import android.util.Log | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.FragmentFactory | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| class PluginFragmentFactory( | ||
| private val defaultFactory: FragmentFactory | ||
| ) : FragmentFactory() { | ||
|
|
||
| companion object { | ||
| private const val TAG = "PluginFragmentFactory" | ||
|
|
||
| private val pluginClassLoaders = ConcurrentHashMap<String, ClassLoader>() | ||
| private val pluginFragmentClasses = ConcurrentHashMap<String, MutableSet<String>>() | ||
|
|
||
| @JvmStatic | ||
| fun registerPluginClassLoader(pluginId: String, classLoader: ClassLoader, fragmentClassNames: List<String>) { | ||
| val fragmentSet = pluginFragmentClasses.computeIfAbsent(pluginId) { | ||
| ConcurrentHashMap.newKeySet() | ||
| } | ||
| fragmentClassNames.forEach { className -> | ||
| pluginClassLoaders[className] = classLoader | ||
| fragmentSet.add(className) | ||
| Log.d(TAG, "Registered classloader for fragment: $className (plugin: $pluginId)") | ||
| } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun unregisterPluginClassLoader(pluginId: String, fragmentClassNames: List<String>) { | ||
| val fragmentSet = pluginFragmentClasses[pluginId] | ||
| fragmentClassNames.forEach { className -> | ||
| pluginClassLoaders.remove(className) | ||
| fragmentSet?.remove(className) | ||
| Log.d(TAG, "Unregistered classloader for fragment: $className (plugin: $pluginId)") | ||
| } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun unregisterAllClassLoadersForPlugin(pluginId: String) { | ||
| val fragmentClassNames = pluginFragmentClasses.remove(pluginId) ?: return | ||
| fragmentClassNames.forEach { className -> | ||
| pluginClassLoaders.remove(className) | ||
| Log.d(TAG, "Unregistered classloader for fragment: $className (plugin: $pluginId)") | ||
| } | ||
| Log.d(TAG, "Unregistered all classloaders for plugin: $pluginId (${fragmentClassNames.size} fragments)") | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun hasClassLoaderForFragment(className: String): Boolean { | ||
| return pluginClassLoaders.containsKey(className) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun getClassLoaderForFragment(className: String): ClassLoader? { | ||
| return pluginClassLoaders[className] | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun clearAllClassLoaders() { | ||
| pluginClassLoaders.clear() | ||
| pluginFragmentClasses.clear() | ||
| Log.d(TAG, "Cleared all plugin fragment classloaders") | ||
| } | ||
| } | ||
|
|
||
| override fun instantiate(classLoader: ClassLoader, className: String): Fragment { | ||
| val pluginClassLoader = pluginClassLoaders[className] | ||
|
|
||
| if (pluginClassLoader != null) { | ||
| Log.d(TAG, "Instantiating plugin fragment with plugin classloader: $className") | ||
| return try { | ||
| val fragmentClass = pluginClassLoader.loadClass(className) | ||
| val constructor = fragmentClass.getDeclaredConstructor() | ||
| constructor.isAccessible = true | ||
| constructor.newInstance() as Fragment | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Failed to instantiate plugin fragment: $className", e) | ||
| throw e | ||
| } | ||
| } | ||
|
|
||
| Log.d(TAG, "Using default factory for fragment: $className") | ||
| return defaultFactory.instantiate(classLoader, className) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.