0
0
mirror of https://github.com/florisboard/florisboard.git synced 2024-09-19 19:42:20 +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.content.BroadcastReceiver
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
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.deleteContentsRecursively
import dev.patrickgold.florisboard.lib.io.subFile
import dev.patrickgold.florisboard.lib.kotlin.tryOrNull
import dev.patrickgold.florisboard.lib.toNativeStr
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")
class FlorisApplication : Application() {
@ -78,6 +87,7 @@ class FlorisApplication : Application() {
override fun onCreate() {
super.onCreate()
FlorisApplicationReference = WeakReference(this)
try {
JetPref.configure(saveIntervalMs = 500)
Flog.install(
@ -107,8 +117,8 @@ class FlorisApplication : Application() {
fun init() {
initICU(this)
cacheDir?.deleteContentsRecursively()
extensionManager.value.init()
prefs.initializeBlocking(this)
extensionManager.value.init()
clipboardManager.value.initializeForContext(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) {
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 }
init {
resources.anyChanged.observeForever {
updateActiveEvaluators {
keyboardCache.clear()
scope.launch(Dispatchers.Main.immediate) {
resources.anyChanged.observeForever {
updateActiveEvaluators {
keyboardCache.clear()
}
}
}
prefs.keyboard.numberRow.observeForever {
updateActiveEvaluators {
keyboardCache.clear(KeyboardMode.CHARACTERS)
prefs.keyboard.numberRow.observeForever {
updateActiveEvaluators {
keyboardCache.clear(KeyboardMode.CHARACTERS)
}
}
}
prefs.keyboard.hintedNumberRowEnabled.observeForever {
updateActiveEvaluators()
}
prefs.keyboard.hintedSymbolsEnabled.observeForever {
updateActiveEvaluators()
}
prefs.keyboard.utilityKeyEnabled.observeForever {
updateActiveEvaluators()
}
activeState.observeForever {
updateActiveEvaluators()
}
subtypeManager.activeSubtypeFlow.collectLatestIn(scope) {
reevaluateInputShiftState()
updateActiveEvaluators()
}
clipboardManager.primaryClipFlow.collectLatestIn(scope) {
updateActiveEvaluators()
}
editorInstance.activeContentFlow.collectIn(scope) { content ->
if (!activeState.isComposingEnabled) {
nlpManager.clearSuggestions()
return@collectIn
prefs.keyboard.hintedNumberRowEnabled.observeForever {
updateActiveEvaluators()
}
prefs.keyboard.hintedSymbolsEnabled.observeForever {
updateActiveEvaluators()
}
prefs.keyboard.utilityKeyEnabled.observeForever {
updateActiveEvaluators()
}
activeState.observeForever {
updateActiveEvaluators()
}
subtypeManager.activeSubtypeFlow.collectLatestIn(scope) {
reevaluateInputShiftState()
updateActiveEvaluators()
}
clipboardManager.primaryClipFlow.collectLatestIn(scope) {
updateActiveEvaluators()
}
editorInstance.activeContentFlow.collectIn(scope) { content ->
if (!activeState.isComposingEnabled) {
nlpManager.clearSuggestions()
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)
init {
extensionManager.keyboardExtensions.observeForever { keyboardExtensions ->
parseKeyboardExtensions(keyboardExtensions)
scope.launch(Dispatchers.Main.immediate) {
extensionManager.keyboardExtensions.observeForever { keyboardExtensions ->
parseKeyboardExtensions(keyboardExtensions)
}
}
}