From ccb1857e7fa6307260f569676536e7c689514e91 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 16 Nov 2026 22:58:44 +0530 Subject: [PATCH 1/3] fix(handwriting): reuse plugin ClassLoader across keyboard re-opens (#851) Closing the keyboard releases the recognizer, and the next open created a new PluginClassLoader that tried to load libdigitalink.so again. ART only allows one ClassLoader per native library, so it failed with UnsatisfiedLinkError until a forced restart. Cache the loader per plugin APK (as OcrPluginLoader does) and share it with importPlugin(). --- .../latin/handwriting/HandwritingLoader.kt | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingLoader.kt b/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingLoader.kt index 913650a54..ca2514d22 100644 --- a/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingLoader.kt +++ b/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingLoader.kt @@ -20,6 +20,11 @@ object HandwritingLoader { private var activeRecognizer: HandwritingRecognizer? = null + // A native library can only be loaded by one ClassLoader. The loader must outlive the recognizer + // (release() drops it on every keyboard close), otherwise a re-open fails with UnsatisfiedLinkError. + private var cachedClassLoader: ClassLoader? = null + private var cachedApkModified: Long = 0L + fun resetRecognizer() { activeRecognizer = null displayNameCache = null @@ -232,6 +237,25 @@ object HandwritingLoader { return targetDir } + private fun getOrCreateClassLoader(context: Context, apkFile: File): ClassLoader { + val apkModified = apkFile.lastModified() + cachedClassLoader?.let { if (cachedApkModified == apkModified) return it } + val nativeLibDir = getNativeLibDir(context, apkFile) + extractNativeLibs(apkFile, nativeLibDir) + val nativeLoaderDex = getNativeLoaderDex(context) + val dexPaths = "${apkFile.absolutePath}${File.pathSeparator}${nativeLoaderDex.absolutePath}" + val classLoader = PluginClassLoader( + dexPaths, + context.codeCacheDir.absolutePath, + nativeLibDir.absolutePath, + context.classLoader + ) + loadNativeLibrariesInPlugin(classLoader, File(nativeLibDir, "libdigitalink.so")) + cachedClassLoader = classLoader + cachedApkModified = apkModified + return classLoader + } + fun getRecognizer(context: Context): HandwritingRecognizer? { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) return null if (activeRecognizer != null) return activeRecognizer @@ -252,18 +276,7 @@ object HandwritingLoader { try { ensureWorkManagerInitialized(context) - val nativeLibDir = getNativeLibDir(context, apkFile) - extractNativeLibs(apkFile, nativeLibDir) - val libFile = File(nativeLibDir, "libdigitalink.so") - val nativeLoaderDex = getNativeLoaderDex(context) - val dexPaths = "${apkFile.absolutePath}${File.pathSeparator}${nativeLoaderDex.absolutePath}" - val classLoader = PluginClassLoader( - dexPaths, - context.codeCacheDir.absolutePath, - nativeLibDir.absolutePath, - context.classLoader - ) - loadNativeLibrariesInPlugin(classLoader, libFile) + val classLoader = getOrCreateClassLoader(context, apkFile) val clazz = classLoader.loadClass(PLUGIN_CLASS_NAME) val recognizer = clazz.getDeclaredConstructor().newInstance() as HandwritingRecognizer val pluginContext = PluginContext(context.applicationContext, apkFile.absolutePath) @@ -363,18 +376,7 @@ object HandwritingLoader { // Verify the plugin loads successfully ensureWorkManagerInitialized(context) - val nativeLibDir = getNativeLibDir(context, apkFile) - extractNativeLibs(apkFile, nativeLibDir) - val libFile = File(nativeLibDir, "libdigitalink.so") - val nativeLoaderDex = getNativeLoaderDex(context) - val dexPaths = "${apkFile.absolutePath}${File.pathSeparator}${nativeLoaderDex.absolutePath}" - val classLoader = PluginClassLoader( - dexPaths, - context.codeCacheDir.absolutePath, - nativeLibDir.absolutePath, - context.classLoader - ) - loadNativeLibrariesInPlugin(classLoader, libFile) + val classLoader = getOrCreateClassLoader(context, apkFile) val clazz = classLoader.loadClass(PLUGIN_CLASS_NAME) val recognizer = clazz.getDeclaredConstructor().newInstance() as HandwritingRecognizer val pluginContext = PluginContext(context.applicationContext, apkFile.absolutePath) @@ -402,6 +404,7 @@ object HandwritingLoader { } catch (_: Exception) {} context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, false).apply() activeRecognizer = null + cachedClassLoader = null } return false } @@ -433,6 +436,7 @@ object HandwritingLoader { } catch (_: Exception) {} context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, false).apply() activeRecognizer = null + cachedClassLoader = null } private class PluginContext(base: Context, private val apkPath: String) : android.content.ContextWrapper(base), androidx.work.Configuration.Provider { From 02387c70dd8b84f0c12ab994cb03a95f05466232 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 16 Nov 2026 22:58:44 +0530 Subject: [PATCH 2/3] fix(handwriting): keep panel open when auto-caps changes after first word Committing the first handwritten word turns auto-caps off, and the shift update reloaded the alphabet keyboard, which hid the handwriting panel. Skip shift updates while handwriting is showing and apply the current auto-caps state when leaving handwriting. --- .../helium314/keyboard/keyboard/KeyboardSwitcher.kt | 3 +++ .../keyboard/latin/handwriting/HandwritingView.kt | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.kt b/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.kt index 28f5ccdf6..87f99b70e 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.kt @@ -991,6 +991,9 @@ class KeyboardSwitcher private constructor() : KeyboardState.SwitchActions { if (DEBUG_ACTION) { Log.d(TAG, "requestUpdatingShiftState: autoCapsFlags=" + CapsModeUtils.flagsToString(autoCapsFlags) + " recapitalizeMode=" + recapitalizeMode) } + // A shift change reloads the alphabet keyboard, which hides the handwriting panel (e.g. auto-caps + // turning off after the first handwritten word). The state is re-synced when handwriting is left. + if (isHandwritingShowing) return mState?.onUpdateShiftState(autoCapsFlags, recapitalizeMode) } diff --git a/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt b/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt index e89d46a3d..ef4a33a74 100644 --- a/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt +++ b/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt @@ -359,11 +359,19 @@ class HandwritingView @JvmOverloads constructor( } } + private fun exitToAlphabet() { + val switcher = KeyboardSwitcher.getInstance() + switcher.setAlphabetKeyboard() + // Shift updates are skipped while handwriting is showing, so apply the current auto-caps state now + val latinIME = switcher.latinIME ?: return + switcher.requestUpdatingShiftState(latinIME.currentAutoCapsState, latinIME.currentRecapitalizeState) + } + // Intercept KeyboardActionListener events for the bottom row override fun onCodeInput(primaryCode: Int, x: Int, y: Int, isKeyRepeat: Boolean) { if (primaryCode == KeyCode.ALPHA) { // Close handwriting mode - KeyboardSwitcher.getInstance().setAlphabetKeyboard() + exitToAlphabet() return } if (primaryCode == KeyCode.CLEAR_HANDWRITING) { @@ -399,7 +407,7 @@ class HandwritingView @JvmOverloads constructor( override fun onLongPressKey(primaryCode: Int) { if (primaryCode == KeyCode.CLEAR_HANDWRITING) { PointerTracker.cancelAllPointerTrackers() - KeyboardSwitcher.getInstance().setAlphabetKeyboard() + exitToAlphabet() return } keyboardActionListener?.onLongPressKey(primaryCode) From e9f57d4617fc9949ee06cfa105e27700ea0d481f Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 16 Nov 2026 22:58:44 +0530 Subject: [PATCH 3/3] fix(handwriting): retry model readiness check on cold start The plugin's isLanguageReady() waits about 1s for ML Kit's model manager and reports a timeout as "not downloaded", so an installed model showed "Tap to download" right after the keyboard started. Check imported models first and retry the ML Kit check before showing the download prompt. --- .../keyboard/latin/handwriting/HandwritingView.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt b/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt index ef4a33a74..70ab27319 100644 --- a/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt +++ b/app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt @@ -175,7 +175,10 @@ class HandwritingView @JvmOverloads constructor( if (recognizer != null) { recognizer.setLanguage(language) recognitionExecutor.execute { - val isReady = recognizer.isLanguageReady(language) + // The plugin reports false when ML Kit's model manager times out during cold-start init, + // so retry before claiming the model is missing + val isReady = HandwritingModelImporter.hasModelDirectly(context, language) || + (1..LANGUAGE_READY_ATTEMPTS).any { recognizer.isLanguageReady(language) } mainHandler.post { if (!isReady) { toolbar?.visibility = View.VISIBLE @@ -480,4 +483,9 @@ class HandwritingView @JvmOverloads constructor( } } } + + companion object { + // Each check waits about 1s inside the plugin; ML Kit's model manager can take a few seconds on cold start + private const val LANGUAGE_READY_ATTEMPTS = 3 + } }