Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -402,6 +404,7 @@ object HandwritingLoader {
} catch (_: Exception) {}
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, false).apply()
activeRecognizer = null
cachedClassLoader = null
}
return false
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Loading