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/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 { 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..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 @@ -359,11 +362,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 +410,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) @@ -472,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 + } }