diff --git a/app/src/main/java/helium314/keyboard/latin/App.kt b/app/src/main/java/helium314/keyboard/latin/App.kt index 39eae07ed..b79e6dd3b 100644 --- a/app/src/main/java/helium314/keyboard/latin/App.kt +++ b/app/src/main/java/helium314/keyboard/latin/App.kt @@ -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+"_")) { diff --git a/app/src/main/java/helium314/keyboard/latin/LatinIME.java b/app/src/main/java/helium314/keyboard/latin/LatinIME.java index 159a7b1a8..142767a67 100644 --- a/app/src/main/java/helium314/keyboard/latin/LatinIME.java +++ b/app/src/main/java/helium314/keyboard/latin/LatinIME.java @@ -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(); diff --git a/app/src/main/java/helium314/keyboard/latin/define/DebugFlags.kt b/app/src/main/java/helium314/keyboard/latin/define/DebugFlags.kt index 8beede3e4..55148d091 100644 --- a/app/src/main/java/helium314/keyboard/latin/define/DebugFlags.kt +++ b/app/src/main/java/helium314/keyboard/latin/define/DebugFlags.kt @@ -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) diff --git a/app/src/main/java/helium314/keyboard/latin/settings/SettingsFragment.java b/app/src/main/java/helium314/keyboard/latin/settings/SettingsFragment.java index f5c1f49e9..859e8de10 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/SettingsFragment.java +++ b/app/src/main/java/helium314/keyboard/latin/settings/SettingsFragment.java @@ -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(); diff --git a/app/src/main/java/helium314/keyboard/latin/spellcheck/AndroidSpellCheckerService.java b/app/src/main/java/helium314/keyboard/latin/spellcheck/AndroidSpellCheckerService.java index d7f99a201..94269b5ef 100644 --- a/app/src/main/java/helium314/keyboard/latin/spellcheck/AndroidSpellCheckerService.java +++ b/app/src/main/java/helium314/keyboard/latin/spellcheck/AndroidSpellCheckerService.java @@ -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())); diff --git a/app/src/main/java/helium314/keyboard/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/app/src/main/java/helium314/keyboard/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java index 1487ddd4e..264fe4e69 100644 --- a/app/src/main/java/helium314/keyboard/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java +++ b/app/src/main/java/helium314/keyboard/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java @@ -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(); } diff --git a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt index d985e1863..f941f997c 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt @@ -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 { - 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 { - require(initialized) - return resourceSubtypesByLocale.values.flatten() + additionalSubtypes - } + fun getAllAvailableSubtypes(): List = + 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 { - require(initialized) - return additionalSubtypes.toList() - } + fun getAdditionalSubtypes(): List = additionalSubtypes.toList() fun reloadSystemLocales(context: Context) { systemLocales.clear() @@ -128,25 +117,15 @@ object SubtypeSettings { systemSubtypes.clear() } - fun getSystemLocales(): List { - require(initialized) - return systemLocales.toList() - } + fun getSystemLocales(): List = 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 = resourceSubtypesByLocale[locale].orEmpty() - fun getAvailableSubtypeLocales(): List { - require(initialized) - return resourceSubtypesByLocale.keys.toList() - } + fun getAvailableSubtypeLocales(): List = 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 { @@ -275,12 +252,10 @@ object SubtypeSettings { return true } - var initialized = false - private set private val enabledSubtypes = mutableListOf() private val resourceSubtypesByLocale = LinkedHashMap>(100) private val additionalSubtypes = mutableListOf() private val systemLocales = mutableListOf() private val systemSubtypes = mutableListOf() - private const val TAG = "SubtypeSettings" + private val TAG = SubtypeSettings::class.simpleName } diff --git a/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt b/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt index 8359a0b8f..2d9d927b6 100644 --- a/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt +++ b/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt @@ -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()