move settings.kt and settings subtype

This commit is contained in:
Helium314 2025-03-01 07:32:09 +01:00
parent e55b375f90
commit c431f7043f
12 changed files with 129 additions and 110 deletions

View file

@ -6,7 +6,7 @@ import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import helium314.keyboard.keyboard.KeyboardTheme
import helium314.keyboard.latin.R
import helium314.keyboard.latin.customIconIds
import helium314.keyboard.latin.settings.customIconIds
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.Log

View file

@ -15,6 +15,8 @@ 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.SettingsSubtype
import helium314.keyboard.latin.settings.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.settings.USER_DICTIONARY_SUFFIX
import helium314.keyboard.latin.settings.colorPrefsAndResIds
import helium314.keyboard.latin.utils.DeviceProtectedUtils
@ -24,8 +26,6 @@ import helium314.keyboard.latin.utils.LayoutType.Companion.folder
import helium314.keyboard.latin.utils.LayoutUtilsCustom
import helium314.keyboard.latin.utils.ScriptUtils.SCRIPT_LATIN
import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SettingsSubtype
import helium314.keyboard.latin.utils.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.utils.SubtypeSettings
import helium314.keyboard.latin.utils.SubtypeUtilsAdditional
import helium314.keyboard.latin.utils.ToolbarKey

View file

@ -35,7 +35,6 @@ import helium314.keyboard.keyboard.KeyboardTheme
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.latin.R
import helium314.keyboard.latin.common.FileUtils
import helium314.keyboard.latin.customIconNames
import helium314.keyboard.latin.databinding.ReorderDialogItemBinding
import helium314.keyboard.latin.utils.DeviceProtectedUtils
import helium314.keyboard.latin.utils.ResourceUtils

View file

@ -1,10 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin
package helium314.keyboard.latin.settings
import android.content.Context
import android.content.SharedPreferences
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import kotlinx.serialization.json.Json
fun customIconNames(prefs: SharedPreferences) = runCatching {

View file

@ -0,0 +1,117 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin.settings
import android.content.SharedPreferences
import android.os.Build
import android.view.inputmethod.InputMethodSubtype
import helium314.keyboard.latin.common.Constants.Separators
import helium314.keyboard.latin.common.Constants.Subtype.ExtraValue
import helium314.keyboard.latin.common.Constants.Subtype.ExtraValue.KEYBOARD_LAYOUT_SET
import helium314.keyboard.latin.common.LocaleUtils.constructLocale
import helium314.keyboard.latin.define.DebugFlags
import helium314.keyboard.latin.utils.LayoutType
import helium314.keyboard.latin.utils.LayoutType.Companion.toExtraValue
import helium314.keyboard.latin.utils.LayoutUtilsCustom
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.ScriptUtils
import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SubtypeLocaleUtils
import helium314.keyboard.latin.utils.SubtypeUtilsAdditional
import helium314.keyboard.latin.utils.locale
import java.util.Locale
// some kind of intermediate between the string stored in preferences and an InputMethodSubtype
data class SettingsSubtype(val locale: Locale, val extraValues: String) {
fun toPref() = locale.toLanguageTag() + Separators.SET + extraValues
/** Creates an additional subtype from the SettingsSubtype.
* Resulting InputMethodSubtypes are equal if SettingsSubtypes are equal */
fun toAdditionalSubtype(): InputMethodSubtype? {
val asciiCapable = locale.script() == ScriptUtils.SCRIPT_LATIN
val subtype = SubtypeUtilsAdditional.createAdditionalSubtype(locale, extraValues, asciiCapable, true)
if (subtype.nameResId == SubtypeLocaleUtils.UNKNOWN_KEYBOARD_LAYOUT
&& mainLayoutName()?.endsWith("+") != true // "+" layouts and custom layouts are always "unknown"
&& !LayoutUtilsCustom.isCustomLayout(mainLayoutName() ?: SubtypeLocaleUtils.QWERTY)
) {
// Skip unknown keyboard layout subtype. This may happen when predefined keyboard
// layout has been removed.
Log.w(SettingsSubtype::class.simpleName, "unknown additional subtype $this")
return null
}
return subtype
}
fun mainLayoutName() = LayoutType.getMainLayoutFromExtraValue(extraValues)
fun layoutName(type: LayoutType) = LayoutType.getLayoutMap(getExtraValueOf(KEYBOARD_LAYOUT_SET) ?: "")[type]
fun with(extraValueKey: String, extraValue: String? = null): SettingsSubtype {
val newList = extraValues.split(",")
.filterNot { it.isBlank() || it.startsWith("$extraValueKey=") || it == extraValueKey }
val newValue = if (extraValue == null) extraValueKey else "$extraValueKey=$extraValue"
val newValues = (newList + newValue).joinToString(",")
return copy(extraValues = newValues)
}
fun without(extraValueKey: String): SettingsSubtype {
val newValues = extraValues.split(",")
.filterNot { it.isBlank() || it.startsWith("$extraValueKey=") || it == extraValueKey }
.joinToString(",")
return copy(extraValues = newValues)
}
fun getExtraValueOf(extraValueKey: String): String? = extraValues.getExtraValueOf(extraValueKey)
fun hasExtraValueOf(extraValueKey: String): Boolean = extraValues.hasExtraValueOf(extraValueKey)
fun withLayout(type: LayoutType, name: String): SettingsSubtype {
val map = LayoutType.getLayoutMap(getExtraValueOf(KEYBOARD_LAYOUT_SET) ?: "")
map[type] = name
return with(KEYBOARD_LAYOUT_SET, map.toExtraValue())
}
fun withoutLayout(type: LayoutType): SettingsSubtype {
val map = LayoutType.getLayoutMap(getExtraValueOf(KEYBOARD_LAYOUT_SET) ?: "")
map.remove(type)
return if (map.isEmpty()) without(KEYBOARD_LAYOUT_SET)
else with(KEYBOARD_LAYOUT_SET, map.toExtraValue())
}
fun isAdditionalSubtype(prefs: SharedPreferences) =
prefs.getString(Settings.PREF_ADDITIONAL_SUBTYPES, Defaults.PREF_ADDITIONAL_SUBTYPES)!!
.split(Separators.SETS).contains(toPref())
companion object {
fun String.toSettingsSubtype() =
SettingsSubtype(substringBefore(Separators.SET).constructLocale(), substringAfter(Separators.SET))
fun String.getExtraValueOf(extraValueKey: String) = split(",")
.firstOrNull { it.startsWith("$extraValueKey=") }?.substringAfter("$extraValueKey=")
fun String.hasExtraValueOf(extraValueKey: String) = split(",")
.any { it.startsWith("$extraValueKey=") || it == extraValueKey }
/** Creates a SettingsSubtype from the given InputMethodSubtype.
* Will strip some extra values that are set when creating the InputMethodSubtype from SettingsSubtype */
fun InputMethodSubtype.toSettingsSubtype(): SettingsSubtype {
if (DebugFlags.DEBUG_ENABLED && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && locale().toLanguageTag() == "und") {
@Suppress("deprecation") // it's debug logging, better get all information
(Log.e(
SettingsSubtype::class.simpleName,
"unknown language, should not happen ${locale}, $languageTag, $extraValue, ${hashCode()}, $nameResId"
))
}
val filteredExtraValue = extraValue.split(",").filterNot {
it.isBlank()
|| it == ExtraValue.ASCII_CAPABLE
|| it == ExtraValue.EMOJI_CAPABLE
|| it == ExtraValue.IS_ADDITIONAL_SUBTYPE
|| it.startsWith(ExtraValue.UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)
}.joinToString(",")
require(!filteredExtraValue.contains(Separators.SETS) && !filteredExtraValue.contains(Separators.SET))
{ "extra value contains not allowed characters $filteredExtraValue" }
return SettingsSubtype(locale(), filteredExtraValue)
}
}
}

View file

@ -17,8 +17,8 @@ import helium314.keyboard.latin.common.LocaleUtils
import helium314.keyboard.latin.define.DebugFlags
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.settings.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SettingsSubtype.Companion.toSettingsSubtype
import java.util.Locale
object SubtypeSettings {

View file

@ -11,12 +11,9 @@ import helium314.keyboard.latin.common.Constants.Subtype.ExtraValue
import helium314.keyboard.latin.common.Constants.Subtype.ExtraValue.KEYBOARD_LAYOUT_SET
import helium314.keyboard.latin.common.LocaleUtils.constructLocale
import helium314.keyboard.latin.common.LocaleUtils.localizedDisplayName
import helium314.keyboard.latin.define.DebugFlags
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.LayoutType.Companion.toExtraValue
import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SettingsSubtype.Companion.getExtraValueOf
import helium314.keyboard.latin.settings.SettingsSubtype.Companion.getExtraValueOf
import org.xmlpull.v1.XmlPullParser
import java.util.Locale
@ -99,95 +96,3 @@ fun getMoreKeys(subtype: InputMethodSubtype, prefs: SharedPreferences): String =
fun getSecondaryLocales(extraValues: String): List<Locale> =
extraValues.getExtraValueOf(ExtraValue.SECONDARY_LOCALES)
?.split(Separators.KV)?.map { it.constructLocale() }.orEmpty()
// some kind of intermediate between the string stored in preferences and an InputMethodSubtype
data class SettingsSubtype(val locale: Locale, val extraValues: String) {
fun toPref() = locale.toLanguageTag() + Separators.SET + extraValues
/** Creates an additional subtype from the SettingsSubtype.
* Resulting InputMethodSubtypes are equal if SettingsSubtypes are equal */
fun toAdditionalSubtype(): InputMethodSubtype? {
val asciiCapable = locale.script() == ScriptUtils.SCRIPT_LATIN
val subtype = SubtypeUtilsAdditional.createAdditionalSubtype(locale, extraValues, asciiCapable, true)
if (subtype.nameResId == SubtypeLocaleUtils.UNKNOWN_KEYBOARD_LAYOUT
&& mainLayoutName()?.endsWith("+") != true // "+" layouts and custom layouts are always "unknown"
&& !LayoutUtilsCustom.isCustomLayout(mainLayoutName() ?: SubtypeLocaleUtils.QWERTY)) {
// Skip unknown keyboard layout subtype. This may happen when predefined keyboard
// layout has been removed.
Log.w(SettingsSubtype::class.simpleName, "unknown additional subtype $this")
return null
}
return subtype
}
fun mainLayoutName() = LayoutType.getMainLayoutFromExtraValue(extraValues)
fun layoutName(type: LayoutType) = LayoutType.getLayoutMap(getExtraValueOf(KEYBOARD_LAYOUT_SET) ?: "")[type]
fun with(extraValueKey: String, extraValue: String? = null): SettingsSubtype {
val newList = extraValues.split(",")
.filterNot { it.isBlank() || it.startsWith("$extraValueKey=") || it == extraValueKey }
val newValue = if (extraValue == null) extraValueKey else "$extraValueKey=$extraValue"
val newValues = (newList + newValue).joinToString(",")
return copy(extraValues = newValues)
}
fun without(extraValueKey: String): SettingsSubtype {
val newValues = extraValues.split(",")
.filterNot { it.isBlank() || it.startsWith("$extraValueKey=") || it == extraValueKey }
.joinToString(",")
return copy(extraValues = newValues)
}
fun getExtraValueOf(extraValueKey: String): String? = extraValues.getExtraValueOf(extraValueKey)
fun hasExtraValueOf(extraValueKey: String): Boolean = extraValues.hasExtraValueOf(extraValueKey)
fun withLayout(type: LayoutType, name: String): SettingsSubtype {
val map = LayoutType.getLayoutMap(getExtraValueOf(KEYBOARD_LAYOUT_SET) ?: "")
map[type] = name
return with(KEYBOARD_LAYOUT_SET, map.toExtraValue())
}
fun withoutLayout(type: LayoutType): SettingsSubtype {
val map = LayoutType.getLayoutMap(getExtraValueOf(KEYBOARD_LAYOUT_SET) ?: "")
map.remove(type)
return if (map.isEmpty()) without(KEYBOARD_LAYOUT_SET)
else with(KEYBOARD_LAYOUT_SET, map.toExtraValue())
}
fun isAdditionalSubtype(prefs: SharedPreferences) =
prefs.getString(Settings.PREF_ADDITIONAL_SUBTYPES, Defaults.PREF_ADDITIONAL_SUBTYPES)!!
.split(Separators.SETS).contains(toPref())
companion object {
fun String.toSettingsSubtype() =
SettingsSubtype(substringBefore(Separators.SET).constructLocale(), substringAfter(Separators.SET))
fun String.getExtraValueOf(extraValueKey: String) = split(",")
.firstOrNull { it.startsWith("$extraValueKey=") }?.substringAfter("$extraValueKey=")
fun String.hasExtraValueOf(extraValueKey: String) = split(",")
.any { it.startsWith("$extraValueKey=") || it == extraValueKey }
/** Creates a SettingsSubtype from the given InputMethodSubtype.
* Will strip some extra values that are set when creating the InputMethodSubtype from SettingsSubtype */
fun InputMethodSubtype.toSettingsSubtype(): SettingsSubtype {
if (DebugFlags.DEBUG_ENABLED && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && locale().toLanguageTag() == "und") {
@Suppress("deprecation") // it's debug logging, better get all information
Log.e(SettingsSubtype::class.simpleName, "unknown language, should not happen ${locale}, $languageTag, $extraValue, ${hashCode()}, $nameResId")
}
val filteredExtraValue = extraValue.split(",").filterNot {
it.isBlank()
|| it == ExtraValue.ASCII_CAPABLE
|| it == ExtraValue.EMOJI_CAPABLE
|| it == ExtraValue.IS_ADDITIONAL_SUBTYPE
|| it.startsWith(ExtraValue.UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)
}.joinToString(",")
require(!filteredExtraValue.contains(Separators.SETS) && !filteredExtraValue.contains(Separators.SET))
{ "extra value contains not allowed characters $filteredExtraValue" }
return SettingsSubtype(locale(), filteredExtraValue)
}
}
}

View file

@ -11,7 +11,8 @@ import helium314.keyboard.latin.common.Constants.Separators
import helium314.keyboard.latin.common.Constants.Subtype.ExtraValue
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.settings.SettingsSubtype
import helium314.keyboard.latin.settings.SettingsSubtype.Companion.toSettingsSubtype
import java.util.Locale
object SubtypeUtilsAdditional {

View file

@ -39,7 +39,7 @@ import androidx.core.graphics.drawable.toBitmap
import androidx.core.util.TypedValueCompat
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.latin.R
import helium314.keyboard.latin.customIconNames
import helium314.keyboard.latin.settings.customIconNames
import helium314.keyboard.latin.utils.getStringResourceOrName
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.Theme

View file

@ -22,7 +22,6 @@ import helium314.keyboard.latin.R
import helium314.keyboard.latin.utils.LayoutType
import helium314.keyboard.latin.utils.LayoutUtilsCustom
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.SettingsSubtype
import helium314.keyboard.latin.utils.SubtypeSettings
import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.getStringResourceOrName

View file

@ -52,6 +52,8 @@ import helium314.keyboard.latin.common.LocaleUtils.constructLocale
import helium314.keyboard.latin.common.LocaleUtils.localizedDisplayName
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.settings.SettingsSubtype
import helium314.keyboard.latin.settings.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.utils.LayoutType
import helium314.keyboard.latin.utils.LayoutType.Companion.displayNameId
import helium314.keyboard.latin.utils.LayoutUtils
@ -59,8 +61,6 @@ import helium314.keyboard.latin.utils.LayoutUtilsCustom
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.ScriptUtils
import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SettingsSubtype
import helium314.keyboard.latin.utils.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.utils.SubtypeLocaleUtils
import helium314.keyboard.latin.utils.SubtypeSettings
import helium314.keyboard.latin.utils.SubtypeUtilsAdditional

View file

@ -33,11 +33,11 @@ import helium314.keyboard.latin.common.LocaleUtils.constructLocale
import helium314.keyboard.latin.common.LocaleUtils.localizedDisplayName
import helium314.keyboard.latin.common.splitOnWhitespace
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.settings.USER_DICTIONARY_SUFFIX
import helium314.keyboard.latin.utils.DictionaryInfoUtils
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.MissingDictionaryDialog
import helium314.keyboard.latin.utils.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.utils.SubtypeLocaleUtils
import helium314.keyboard.latin.utils.SubtypeSettings
import helium314.keyboard.latin.utils.SubtypeUtilsAdditional