do some inits on app create instead of in the services

This commit is contained in:
Helium314 2025-02-19 23:33:51 +01:00
parent 0899245ae7
commit 35083e6515
8 changed files with 21 additions and 50 deletions

View file

@ -12,6 +12,7 @@ import helium314.keyboard.latin.common.Constants.Separators
import helium314.keyboard.latin.common.Constants.Subtype.ExtraValue
import helium314.keyboard.latin.common.LocaleUtils.constructLocale
import helium314.keyboard.latin.common.encodeBase36
import helium314.keyboard.latin.define.DebugFlags
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.settings.USER_DICTIONARY_SUFFIX
@ -21,7 +22,6 @@ import helium314.keyboard.latin.utils.DictionaryInfoUtils
import helium314.keyboard.latin.utils.LayoutType
import helium314.keyboard.latin.utils.LayoutType.Companion.folder
import helium314.keyboard.latin.utils.LayoutUtilsCustom
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.ScriptUtils.SCRIPT_LATIN
import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SettingsSubtype
@ -43,6 +43,10 @@ import java.util.EnumMap
class App : Application() {
override fun onCreate() {
super.onCreate()
Settings.init(this)
DebugFlags.init(this)
SubtypeSettings.init(this)
checkVersionUpgrade(this)
app = this
Defaults.initDynamicDefaults(this)
@ -458,7 +462,6 @@ fun checkVersionUpgrade(context: Context) {
}
}
if (oldVersion <= 2308) {
SubtypeSettings.init(context) // not sure, but there may be cases where it's not initialized
SubtypeSettings.reloadEnabledSubtypes(context)
prefs.all.keys.toList().forEach { key ->
if (key.startsWith(Settings.PREF_POPUP_KEYS_ORDER+"_")) {

View file

@ -570,9 +570,6 @@ public class LatinIME extends InputMethodService implements
@Override
public void onCreate() {
Settings.init(this);
DebugFlags.init(this);
SubtypeSettings.INSTANCE.init(this);
KeyboardIconsSet.Companion.getInstance().loadIcons(this);
RichInputMethodManager.init(this);
mRichImm = RichInputMethodManager.getInstance();

View file

@ -23,7 +23,6 @@ object DebugFlags {
@JvmField
var DEBUG_ENABLED = false
@JvmStatic
fun init(context: Context) {
DEBUG_ENABLED = context.prefs().getBoolean(DebugSettings.PREF_DEBUG_MODE, Defaults.PREF_DEBUG_MODE)
if (DEBUG_ENABLED || BuildConfig.DEBUG)

View file

@ -74,11 +74,6 @@ public final class SettingsFragment extends PreferenceFragmentCompat {
}
}
// sometimes wrong languages are returned when not initializing on creation of LatinIME
// this might be a bug, at least it's not documented
// but anyway, here is really rare (LatinIme should be loaded when the settings are opened)
SubtypeSettings.INSTANCE.init(getActivity());
findPreference("screen_languages").setSummary(getEnabledSubtypesLabel());
if (BuildConfig.DEBUG || DebugFlags.DEBUG_ENABLED)
askAboutCrashReports();

View file

@ -85,7 +85,6 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
onSharedPreferenceChanged(prefs, Settings.PREF_USE_CONTACTS);
final boolean blockOffensive = prefs.getBoolean(Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE, Defaults.PREF_BLOCK_POTENTIALLY_OFFENSIVE);
mSettingsValuesForSuggestion = new SettingsValuesForSuggestion(blockOffensive, false);
SubtypeSettings.INSTANCE.init(this);
}
public float getRecommendedThreshold() {
@ -194,7 +193,6 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
// creating a keyboard reads SettingsValues from Settings instance
// maybe it would be "more correct" to create an instance of SettingsValues and use that one instead
// but creating a global one if not existing should be fine too
Settings.init(this);
final EditorInfo editorInfo = new EditorInfo();
editorInfo.inputType = InputType.TYPE_CLASS_TEXT;
Settings.getInstance().loadSettings(this, locale, new InputAttributes(editorInfo, false, getPackageName()));

View file

@ -153,7 +153,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
}
// localeString for this app is always empty, get it from settings if possible
// and we're sure this app is used
if (SubtypeSettings.INSTANCE.getInitialized() && "dummy".equals(currentInputMethodSubtype.getExtraValue())) {
if ("dummy".equals(currentInputMethodSubtype.getExtraValue())) {
final SharedPreferences prefs = KtxKt.prefs(mService);
return SubtypeSettings.INSTANCE.getSelectedSubtype(prefs).getLocale();
}

View file

@ -24,7 +24,6 @@ object SubtypeSettings {
/** @return enabled subtypes. If no subtypes are enabled, but a contextForFallback is provided,
* subtypes for system locales will be returned, or en-US if none found. */
fun getEnabledSubtypes(prefs: SharedPreferences, fallback: Boolean = false): List<InputMethodSubtype> {
require(initialized)
if (prefs.getBoolean(Settings.PREF_USE_SYSTEM_LOCALES, Defaults.PREF_USE_SYSTEM_LOCALES))
return getDefaultEnabledSubtypes()
if (fallback && enabledSubtypes.isEmpty())
@ -32,10 +31,8 @@ object SubtypeSettings {
return enabledSubtypes.toList()
}
fun getAllAvailableSubtypes(): List<InputMethodSubtype> {
require(initialized)
return resourceSubtypesByLocale.values.flatten() + additionalSubtypes
}
fun getAllAvailableSubtypes(): List<InputMethodSubtype> =
resourceSubtypesByLocale.values.flatten() + additionalSubtypes
fun getMatchingMainLayoutNameForLocale(locale: Locale): String {
val subtypes = resourceSubtypesByLocale.values.flatten()
@ -54,7 +51,6 @@ object SubtypeSettings {
}
fun addEnabledSubtype(prefs: SharedPreferences, newSubtype: InputMethodSubtype) {
require(initialized)
val subtypeString = newSubtype.toSettingsSubtype().toPref()
val oldSubtypeStrings = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!.split(Separators.SETS)
val newString = (oldSubtypeStrings + subtypeString).filter { it.isNotBlank() }.toSortedSet().joinToString(Separators.SETS)
@ -69,7 +65,6 @@ object SubtypeSettings {
/** @return whether subtype was actually removed */
fun removeEnabledSubtype(context: Context, subtype: InputMethodSubtype): Boolean {
require(initialized)
if (!removeEnabledSubtype(context.prefs(), subtype.toSettingsSubtype().toPref())) return false
if (!enabledSubtypes.remove(subtype)) reloadEnabledSubtypes(context)
else RichInputMethodManager.getInstance().refreshSubtypeCaches()
@ -77,7 +72,6 @@ object SubtypeSettings {
}
fun getSelectedSubtype(prefs: SharedPreferences): InputMethodSubtype {
require(initialized)
val selectedSubtype = prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)!!.toSettingsSubtype()
if (selectedSubtype.isAdditionalSubtype(prefs)) {
val selectedAdditionalSubtype = selectedSubtype.toAdditionalSubtype()
@ -109,14 +103,9 @@ object SubtypeSettings {
}
// todo: use this or the version in SubtypeUtilsAdditional?
fun isAdditionalSubtype(subtype: InputMethodSubtype): Boolean {
return subtype in additionalSubtypes
}
fun isAdditionalSubtype(subtype: InputMethodSubtype): Boolean = subtype in additionalSubtypes
fun getAdditionalSubtypes(): List<InputMethodSubtype> {
require(initialized)
return additionalSubtypes.toList()
}
fun getAdditionalSubtypes(): List<InputMethodSubtype> = additionalSubtypes.toList()
fun reloadSystemLocales(context: Context) {
systemLocales.clear()
@ -128,25 +117,15 @@ object SubtypeSettings {
systemSubtypes.clear()
}
fun getSystemLocales(): List<Locale> {
require(initialized)
return systemLocales.toList()
}
fun getSystemLocales(): List<Locale> = systemLocales.toList()
fun hasMatchingSubtypeForLocale(locale: Locale): Boolean {
require(initialized)
return !resourceSubtypesByLocale[locale].isNullOrEmpty()
}
fun hasMatchingSubtypeForLocale(locale: Locale): Boolean = !resourceSubtypesByLocale[locale].isNullOrEmpty()
fun getResourceSubtypesForLocale(locale: Locale): List<InputMethodSubtype> = resourceSubtypesByLocale[locale].orEmpty()
fun getAvailableSubtypeLocales(): List<Locale> {
require(initialized)
return resourceSubtypesByLocale.keys.toList()
}
fun getAvailableSubtypeLocales(): List<Locale> = resourceSubtypesByLocale.keys.toList()
fun reloadEnabledSubtypes(context: Context) {
require(initialized)
enabledSubtypes.clear()
removeInvalidCustomSubtypes(context)
loadAdditionalSubtypes(context.prefs())
@ -155,7 +134,6 @@ object SubtypeSettings {
}
fun init(context: Context) {
if (initialized) return
SubtypeLocaleUtils.init(context) // necessary to get the correct getKeyboardLayoutSetName
// necessary to set system locales at start, because for some weird reason (bug?)
@ -166,7 +144,6 @@ object SubtypeSettings {
removeInvalidCustomSubtypes(context)
loadAdditionalSubtypes(context.prefs())
loadEnabledSubtypes(context)
initialized = true
}
private fun getDefaultEnabledSubtypes(): List<InputMethodSubtype> {
@ -275,12 +252,10 @@ object SubtypeSettings {
return true
}
var initialized = false
private set
private val enabledSubtypes = mutableListOf<InputMethodSubtype>()
private val resourceSubtypesByLocale = LinkedHashMap<Locale, MutableList<InputMethodSubtype>>(100)
private val additionalSubtypes = mutableListOf<InputMethodSubtype>()
private val systemLocales = mutableListOf<Locale>()
private val systemSubtypes = mutableListOf<InputMethodSubtype>()
private const val TAG = "SubtypeSettings"
private val TAG = SubtypeSettings::class.simpleName
}

View file

@ -5,6 +5,7 @@ import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.widget.RelativeLayout
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
@ -15,7 +16,9 @@ import androidx.compose.material3.Surface
import androidx.compose.ui.platform.ComposeView
import androidx.core.view.ViewCompat
import androidx.core.view.isGone
import helium314.keyboard.compat.locale
import helium314.keyboard.latin.BuildConfig
import helium314.keyboard.latin.InputAttributes
import helium314.keyboard.latin.R
import helium314.keyboard.latin.common.FileUtils
import helium314.keyboard.latin.define.DebugFlags
@ -43,9 +46,10 @@ class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferen
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Settings.getInstance().current == null)
Settings.init(this)
SubtypeSettings.init(this)
if (Settings.getInstance().current == null) {
val inputAttributes = InputAttributes(EditorInfo(), false, packageName)
Settings.getInstance().loadSettings(this, resources.configuration.locale(), inputAttributes)
}
ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD).execute { cleanUnusedMainDicts(this) }
if (BuildConfig.DEBUG || DebugFlags.DEBUG_ENABLED)
askAboutCrashReports()