From e35c271720c1bde66b7c33d92457782ced33b60e Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 16 Nov 2026 23:53:22 +0530 Subject: [PATCH] fix(cjk): keep the engine when only Japanese is available A new engine starts in Chinese, so the loader judged it by Pinyin alone and rejected an engine whose Pinyin library failed although Japanese works. Accept a freshly loaded engine when either language is ready. Keep the cached engine until it is released or the plugin changes: isAvailable() only answers for the last language set by CjkInputManager, so re-checking it would reload a Japanese-only engine on every key while a Chinese layout is active. --- .../keyboard/latin/cjk/CjkPluginLoader.kt | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt b/app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt index 2c7ac63c6..357e40085 100644 --- a/app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt +++ b/app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt @@ -153,7 +153,10 @@ object CjkPluginLoader { val internalApk = File(context.filesDir, PLUGIN_FILENAME) val apkModified = if (internalApk.exists()) internalApk.lastModified() else 0L val cached = activeEngine - if (cached != null && cached.isAvailable() && (!internalApk.exists() || apkModified == lastApkModified)) { + // An accepted engine stays usable until it is released or the plugin changes. isAvailable() only answers for + // the language CjkInputManager set last, so checking it here would reload a Japanese-only engine on every key + // while a Chinese layout is active. + if (cached != null && (!internalApk.exists() || apkModified == lastApkModified)) { return cached } if (cached != null && internalApk.exists() && apkModified != lastApkModified) { @@ -166,7 +169,7 @@ object CjkPluginLoader { if (internalApk.exists() && internalApk.length() > 0L) { Log.i(TAG, "Attempting to load CJK engine from internal storage: ${internalApk.absolutePath}") val engine = loadEngineInternal(context, internalApk) - if (engine != null && engine.isAvailable()) { + if (engine != null && isUsable(engine)) { activeEngine = engine lastApkModified = apkModified return engine @@ -188,6 +191,14 @@ object CjkPluginLoader { return null } + /** + * Whether a freshly loaded engine supports Chinese or Japanese. A new engine starts in Chinese, so checking only + * [ICjkEngine.isAvailable] rejected an engine whose Pinyin library failed although Japanese works. Leaving it set to + * Japanese is fine: CjkInputManager sets the subtype's language on the first key for every new engine instance. + */ + private fun isUsable(engine: ICjkEngine): Boolean = + engine.isAvailable() || (engine.setLocale("ja") && engine.isAvailable()) + private fun loadEngineInternal(context: Context, apkFile: File): ICjkEngine? { return try { val sharedDir = File(context.filesDir, "cjk/shared").apply { mkdirs() } @@ -313,7 +324,7 @@ object CjkPluginLoader { activeEngine = null val engine = loadEngineInternal(context, targetFile) - val success = engine != null && engine.isAvailable() + val success = engine != null && isUsable(engine) if (success) { activeEngine = engine context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, true).apply() @@ -347,7 +358,7 @@ object CjkPluginLoader { activeEngine = null val engine = loadEngineInternal(context, targetFile) - val success = engine != null && engine.isAvailable() + val success = engine != null && isUsable(engine) if (success) { activeEngine = engine context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, true).apply()