0
0
mirror of https://github.com/florisboard/florisboard.git synced 2024-09-20 03:52:18 +02:00

Fix KeyboardManager observer causing crash (#2020)

This commit is contained in:
Patrick Goldinger 2022-08-28 15:49:50 +02:00
parent e647e0d248
commit 90e60a5e03
No known key found for this signature in database
GPG Key ID: 533467C3DC7B9262
2 changed files with 72 additions and 54 deletions

View File

@ -19,6 +19,7 @@ package dev.patrickgold.florisboard
import android.app.Application import android.app.Application
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.ContextWrapper
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.Handler import android.os.Handler
@ -44,8 +45,16 @@ import dev.patrickgold.florisboard.lib.ext.ExtensionManager
import dev.patrickgold.florisboard.lib.io.AssetManager import dev.patrickgold.florisboard.lib.io.AssetManager
import dev.patrickgold.florisboard.lib.io.deleteContentsRecursively import dev.patrickgold.florisboard.lib.io.deleteContentsRecursively
import dev.patrickgold.florisboard.lib.io.subFile import dev.patrickgold.florisboard.lib.io.subFile
import dev.patrickgold.florisboard.lib.kotlin.tryOrNull
import dev.patrickgold.florisboard.lib.toNativeStr import dev.patrickgold.florisboard.lib.toNativeStr
import dev.patrickgold.jetpref.datastore.JetPref import dev.patrickgold.jetpref.datastore.JetPref
import java.lang.ref.WeakReference
/**
* Global weak reference for the [FlorisApplication] class. This is needed as in certain scenarios an application
* reference is needed, but the Android framework hasn't finished setting up
*/
private var FlorisApplicationReference = WeakReference<FlorisApplication?>(null)
@Suppress("unused") @Suppress("unused")
class FlorisApplication : Application() { class FlorisApplication : Application() {
@ -78,6 +87,7 @@ class FlorisApplication : Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
FlorisApplicationReference = WeakReference(this)
try { try {
JetPref.configure(saveIntervalMs = 500) JetPref.configure(saveIntervalMs = 500)
Flog.install( Flog.install(
@ -107,8 +117,8 @@ class FlorisApplication : Application() {
fun init() { fun init() {
initICU(this) initICU(this)
cacheDir?.deleteContentsRecursively() cacheDir?.deleteContentsRecursively()
extensionManager.value.init()
prefs.initializeBlocking(this) prefs.initializeBlocking(this)
extensionManager.value.init()
clipboardManager.value.initializeForContext(this) clipboardManager.value.initializeForContext(this)
DictionaryManager.init(this) DictionaryManager.init(this)
} }
@ -150,31 +160,35 @@ class FlorisApplication : Application() {
} }
} }
private fun Context.florisApplication(): FlorisApplication { private tailrec fun Context.florisApplication(): FlorisApplication {
return when (this) { return when (this) {
is FlorisApplication -> this is FlorisApplication -> this
else -> this.applicationContext as FlorisApplication is ContextWrapper -> when {
this.baseContext != null -> this.baseContext.florisApplication()
else -> FlorisApplicationReference.get()!!
}
else -> tryOrNull { this.applicationContext as FlorisApplication } ?: FlorisApplicationReference.get()!!
} }
} }
fun Context.appContext() = lazy { this.florisApplication() } fun Context.appContext() = lazyOf(this.florisApplication())
fun Context.assetManager() = lazy { this.florisApplication().assetManager.value } fun Context.assetManager() = this.florisApplication().assetManager
fun Context.cacheManager() = lazy { this.florisApplication().cacheManager.value } fun Context.cacheManager() = this.florisApplication().cacheManager
fun Context.clipboardManager() = lazy { this.florisApplication().clipboardManager.value } fun Context.clipboardManager() = this.florisApplication().clipboardManager
fun Context.editorInstance() = lazy { this.florisApplication().editorInstance.value } fun Context.editorInstance() = this.florisApplication().editorInstance
fun Context.extensionManager() = lazy { this.florisApplication().extensionManager.value } fun Context.extensionManager() = this.florisApplication().extensionManager
fun Context.glideTypingManager() = lazy { this.florisApplication().glideTypingManager.value } fun Context.glideTypingManager() = this.florisApplication().glideTypingManager
fun Context.keyboardManager() = lazy { this.florisApplication().keyboardManager.value } fun Context.keyboardManager() = this.florisApplication().keyboardManager
fun Context.nlpManager() = lazy { this.florisApplication().nlpManager.value } fun Context.nlpManager() = this.florisApplication().nlpManager
fun Context.subtypeManager() = lazy { this.florisApplication().subtypeManager.value } fun Context.subtypeManager() = this.florisApplication().subtypeManager
fun Context.themeManager() = lazy { this.florisApplication().themeManager.value } fun Context.themeManager() = this.florisApplication().themeManager

View File

@ -122,47 +122,49 @@ class KeyboardManager(context: Context) : InputKeyEventReceiver {
).also { it.keyEventReceiver = this } ).also { it.keyEventReceiver = this }
init { init {
resources.anyChanged.observeForever { scope.launch(Dispatchers.Main.immediate) {
updateActiveEvaluators { resources.anyChanged.observeForever {
keyboardCache.clear() updateActiveEvaluators {
keyboardCache.clear()
}
} }
} prefs.keyboard.numberRow.observeForever {
prefs.keyboard.numberRow.observeForever { updateActiveEvaluators {
updateActiveEvaluators { keyboardCache.clear(KeyboardMode.CHARACTERS)
keyboardCache.clear(KeyboardMode.CHARACTERS) }
} }
} prefs.keyboard.hintedNumberRowEnabled.observeForever {
prefs.keyboard.hintedNumberRowEnabled.observeForever { updateActiveEvaluators()
updateActiveEvaluators() }
} prefs.keyboard.hintedSymbolsEnabled.observeForever {
prefs.keyboard.hintedSymbolsEnabled.observeForever { updateActiveEvaluators()
updateActiveEvaluators() }
} prefs.keyboard.utilityKeyEnabled.observeForever {
prefs.keyboard.utilityKeyEnabled.observeForever { updateActiveEvaluators()
updateActiveEvaluators() }
} activeState.observeForever {
activeState.observeForever { updateActiveEvaluators()
updateActiveEvaluators() }
} subtypeManager.activeSubtypeFlow.collectLatestIn(scope) {
subtypeManager.activeSubtypeFlow.collectLatestIn(scope) { reevaluateInputShiftState()
reevaluateInputShiftState() updateActiveEvaluators()
updateActiveEvaluators() }
} clipboardManager.primaryClipFlow.collectLatestIn(scope) {
clipboardManager.primaryClipFlow.collectLatestIn(scope) { updateActiveEvaluators()
updateActiveEvaluators() }
} editorInstance.activeContentFlow.collectIn(scope) { content ->
editorInstance.activeContentFlow.collectIn(scope) { content -> if (!activeState.isComposingEnabled) {
if (!activeState.isComposingEnabled) { nlpManager.clearSuggestions()
nlpManager.clearSuggestions() return@collectIn
return@collectIn }
nlpManager.suggest(subtypeManager.activeSubtype, content)
}
prefs.devtools.enabled.observeForever {
reevaluateDebugFlags()
}
prefs.devtools.showDragAndDropHelpers.observeForever {
reevaluateDebugFlags()
} }
nlpManager.suggest(subtypeManager.activeSubtype, content)
}
prefs.devtools.enabled.observeForever {
reevaluateDebugFlags()
}
prefs.devtools.showDragAndDropHelpers.observeForever {
reevaluateDebugFlags()
} }
} }
@ -811,8 +813,10 @@ class KeyboardManager(context: Context) : InputKeyEventReceiver {
val anyChanged = MutableLiveData(Unit) val anyChanged = MutableLiveData(Unit)
init { init {
extensionManager.keyboardExtensions.observeForever { keyboardExtensions -> scope.launch(Dispatchers.Main.immediate) {
parseKeyboardExtensions(keyboardExtensions) extensionManager.keyboardExtensions.observeForever { keyboardExtensions ->
parseKeyboardExtensions(keyboardExtensions)
}
} }
} }