remove essentially duplicate functions

This commit is contained in:
Helium314 2025-05-24 09:14:35 +02:00
parent 82e6d8a5cb
commit 9dbce40fd7
8 changed files with 18 additions and 31 deletions

View file

@ -34,6 +34,7 @@ import helium314.keyboard.latin.utils.defaultPinnedToolbarPref
import helium314.keyboard.latin.utils.getResourceSubtypes import helium314.keyboard.latin.utils.getResourceSubtypes
import helium314.keyboard.latin.utils.locale import helium314.keyboard.latin.utils.locale
import helium314.keyboard.latin.utils.mainLayoutName import helium314.keyboard.latin.utils.mainLayoutName
import helium314.keyboard.latin.utils.mainLayoutNameOrQwerty
import helium314.keyboard.latin.utils.prefs import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.latin.utils.upgradeToolbarPrefs import helium314.keyboard.latin.utils.upgradeToolbarPrefs
import helium314.keyboard.latin.utils.writeCustomKeyCodes import helium314.keyboard.latin.utils.writeCustomKeyCodes
@ -445,19 +446,19 @@ fun checkVersionUpgrade(context: Context) {
val mainLayoutName = oldSplit[1] val mainLayoutName = oldSplit[1]
// we now need more information than just locale and main layout name, get it from existing subtypes // we now need more information than just locale and main layout name, get it from existing subtypes
val filtered = additionalSubtypes.filter { val filtered = additionalSubtypes.filter {
it.locale().toLanguageTag() == languageTag && (it.mainLayoutName() ?: "qwerty") == mainLayoutName it.locale().toLanguageTag() == languageTag && (it.mainLayoutNameOrQwerty()) == mainLayoutName
} }
if (filtered.isNotEmpty()) if (filtered.isNotEmpty())
return@joinToString filtered.first().toSettingsSubtype().toPref() return@joinToString filtered.first().toSettingsSubtype().toPref()
// find best matching resource subtype // find best matching resource subtype
val goodMatch = resourceSubtypes.filter { val goodMatch = resourceSubtypes.filter {
it.locale().toLanguageTag() == languageTag && (it.mainLayoutName() ?: "qwerty") == mainLayoutName it.locale().toLanguageTag() == languageTag && (it.mainLayoutNameOrQwerty()) == mainLayoutName
} }
if (goodMatch.isNotEmpty()) if (goodMatch.isNotEmpty())
return@joinToString goodMatch.first().toSettingsSubtype().toPref() return@joinToString goodMatch.first().toSettingsSubtype().toPref()
// not sure how we can get here, but better deal with it // not sure how we can get here, but better deal with it
val okMatch = resourceSubtypes.filter { val okMatch = resourceSubtypes.filter {
it.locale().language == languageTag.constructLocale().language && (it.mainLayoutName() ?: "qwerty") == mainLayoutName it.locale().language == languageTag.constructLocale().language && (it.mainLayoutNameOrQwerty()) == mainLayoutName
} }
if (okMatch.isNotEmpty()) if (okMatch.isNotEmpty())
okMatch.first().toSettingsSubtype().toPref() okMatch.first().toSettingsSubtype().toPref()

View file

@ -305,9 +305,8 @@ public class RichInputMethodManager {
final int count = myImi.getSubtypeCount(); final int count = myImi.getSubtypeCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
final InputMethodSubtype subtype = myImi.getSubtypeAt(i); final InputMethodSubtype subtype = myImi.getSubtypeAt(i);
final String layoutName = SubtypeLocaleUtils.getMainLayoutName(subtype); final String layoutName = SubtypeUtilsKt.mainLayoutNameOrQwerty(subtype);
if (locale.equals(SubtypeUtilsKt.locale(subtype)) if (locale.equals(SubtypeUtilsKt.locale(subtype)) && keyboardLayoutSetName.equals(layoutName)) {
&& keyboardLayoutSetName.equals(layoutName)) {
return subtype; return subtype;
} }
} }

View file

@ -17,6 +17,7 @@ import helium314.keyboard.latin.utils.ScriptUtils
import helium314.keyboard.latin.utils.ScriptUtils.script import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SubtypeLocaleUtils import helium314.keyboard.latin.utils.SubtypeLocaleUtils
import helium314.keyboard.latin.utils.locale import helium314.keyboard.latin.utils.locale
import helium314.keyboard.latin.utils.mainLayoutNameOrQwerty
import java.util.Locale import java.util.Locale
/** /**
@ -43,7 +44,7 @@ class RichInputMethodSubtype private constructor(val rawSubtype: InputMethodSubt
val fullDisplayName: String get() { val fullDisplayName: String get() {
if (isNoLanguage) { if (isNoLanguage) {
return SubtypeLocaleUtils.getMainLayoutDisplayName(rawSubtype)!! return SubtypeLocaleUtils.getMainLayoutDisplayName(rawSubtype.mainLayoutNameOrQwerty())!!
} }
return SubtypeLocaleUtils.getSubtypeLocaleDisplayName(locale) return SubtypeLocaleUtils.getSubtypeLocaleDisplayName(locale)
} }
@ -52,7 +53,7 @@ class RichInputMethodSubtype private constructor(val rawSubtype: InputMethodSubt
// Get the RichInputMethodSubtype's middle display name in its locale. // Get the RichInputMethodSubtype's middle display name in its locale.
get() { get() {
if (isNoLanguage) { if (isNoLanguage) {
return SubtypeLocaleUtils.getMainLayoutDisplayName(rawSubtype)!! return SubtypeLocaleUtils.getMainLayoutDisplayName(rawSubtype.mainLayoutNameOrQwerty())!!
} }
return SubtypeLocaleUtils.getSubtypeLanguageDisplayName(locale) return SubtypeLocaleUtils.getSubtypeLanguageDisplayName(locale)
} }

View file

@ -43,16 +43,12 @@ public final class LanguageOnSpacebarUtils {
return FORMAT_TYPE_NONE; return FORMAT_TYPE_NONE;
} }
final Locale locale = subtype.getLocale(); final Locale locale = subtype.getLocale();
if (locale == null) {
return FORMAT_TYPE_NONE;
}
final String keyboardLanguage = locale.getLanguage(); final String keyboardLanguage = locale.getLanguage();
final String keyboardLayout = subtype.getMainLayoutName(); final String keyboardLayout = subtype.getMainLayoutName();
int sameLanguageAndLayoutCount = 0; int sameLanguageAndLayoutCount = 0;
for (final InputMethodSubtype ims : sEnabledSubtypes) { for (final InputMethodSubtype ims : sEnabledSubtypes) {
final String language = SubtypeUtilsKt.locale(ims).getLanguage(); final String language = SubtypeUtilsKt.locale(ims).getLanguage();
if (keyboardLanguage.equals(language) && keyboardLayout.equals( if (keyboardLanguage.equals(language) && keyboardLayout.equals(SubtypeUtilsKt.mainLayoutNameOrQwerty(ims))) {
SubtypeLocaleUtils.getMainLayoutName(ims))) {
sameLanguageAndLayoutCount++; sameLanguageAndLayoutCount++;
} }
} }

View file

@ -17,7 +17,7 @@ object LayoutUtils {
return context.assets.list(layoutType.folder)?.map { it.substringBefore(".") }.orEmpty() return context.assets.list(layoutType.folder)?.map { it.substringBefore(".") }.orEmpty()
if (locale == null) if (locale == null)
return SubtypeSettings.getAllAvailableSubtypes() return SubtypeSettings.getAllAvailableSubtypes()
.mapTo(HashSet()) { it.mainLayoutName()?.substringBefore("+") ?: "qwerty" } .mapTo(HashSet()) { it.mainLayoutNameOrQwerty().substringBefore("+") }
.apply { addAll(context.resources.getStringArray(R.array.predefined_layouts)) } .apply { addAll(context.resources.getStringArray(R.array.predefined_layouts)) }
val layouts = SubtypeSettings.getResourceSubtypesForLocale(locale).mapNotNullTo(mutableSetOf()) { it.mainLayoutName() } val layouts = SubtypeSettings.getResourceSubtypesForLocale(locale).mapNotNullTo(mutableSetOf()) { it.mainLayoutName() }
if (locale.script() == ScriptUtils.SCRIPT_LATIN) if (locale.script() == ScriptUtils.SCRIPT_LATIN)

View file

@ -17,7 +17,6 @@ import helium314.keyboard.latin.common.StringUtils
import helium314.keyboard.latin.utils.LayoutType.Companion.getMainLayoutFromExtraValue import helium314.keyboard.latin.utils.LayoutType.Companion.getMainLayoutFromExtraValue
import helium314.keyboard.latin.utils.LayoutUtilsCustom.getDisplayName import helium314.keyboard.latin.utils.LayoutUtilsCustom.getDisplayName
import helium314.keyboard.latin.utils.LayoutUtilsCustom.isCustomLayout import helium314.keyboard.latin.utils.LayoutUtilsCustom.isCustomLayout
import helium314.keyboard.latin.utils.SubtypeSettings.getResourceSubtypesForLocale
import java.util.Locale import java.util.Locale
import kotlin.concurrent.Volatile import kotlin.concurrent.Volatile
@ -189,7 +188,7 @@ object SubtypeLocaleUtils {
getMainLayoutDisplayName(mainLayoutName)?.let { return it } // works for custom and latin layouts getMainLayoutDisplayName(mainLayoutName)?.let { return it } // works for custom and latin layouts
// we have some locale-specific layout // we have some locale-specific layout
for (subtype in getResourceSubtypesForLocale(locale)) { for (subtype in SubtypeSettings.getResourceSubtypesForLocale(locale)) {
if (mainLayoutName == getMainLayoutFromExtraValue(subtype.extraValue)) if (mainLayoutName == getMainLayoutFromExtraValue(subtype.extraValue))
return getSubtypeDisplayNameInSystemLocale(subtype) return getSubtypeDisplayNameInSystemLocale(subtype)
} }
@ -216,7 +215,7 @@ object SubtypeLocaleUtils {
if (subtype == null) { if (subtype == null) {
return "<null subtype>" return "<null subtype>"
} }
return subtype.locale().toString() + "/" + getMainLayoutName(subtype) return subtype.locale().toString() + "/" + subtype.mainLayoutNameOrQwerty()
} }
private fun getSubtypeDisplayNameInternal(subtype: InputMethodSubtype, displayLocale: Locale): String { private fun getSubtypeDisplayNameInternal(subtype: InputMethodSubtype, displayLocale: Locale): String {
@ -231,23 +230,12 @@ object SubtypeLocaleUtils {
} }
} }
fun getMainLayoutDisplayName(subtype: InputMethodSubtype): String? =
getMainLayoutDisplayName(getMainLayoutName(subtype))
fun getMainLayoutDisplayName(layoutName: String): String? = fun getMainLayoutDisplayName(layoutName: String): String? =
if (isCustomLayout(layoutName)) getDisplayName(layoutName) if (isCustomLayout(layoutName)) getDisplayName(layoutName)
else keyboardLayoutToDisplayName[layoutName] else keyboardLayoutToDisplayName[layoutName]
@JvmStatic @JvmStatic
fun getMainLayoutName(subtype: InputMethodSubtype): String { fun getCombiningRulesExtraValue(subtype: InputMethodSubtype): String? = subtype.getExtraValueOf(ExtraValue.COMBINING_RULES)
subtype.mainLayoutName()?.let { return it }
if (!subtype.isAsciiCapable)
Log.w(TAG, "KeyboardLayoutSet not found, use QWERTY: locale=${subtype.locale()} extraValue=${subtype.extraValue}")
return QWERTY
}
@JvmStatic
fun getCombiningRulesExtraValue(subtype: InputMethodSubtype): String = subtype.getExtraValueOf(ExtraValue.COMBINING_RULES)
// Special language code to represent "no language". // Special language code to represent "no language".
const val NO_LANGUAGE = "zz" const val NO_LANGUAGE = "zz"

View file

@ -241,7 +241,7 @@ object SubtypeSettings {
continue continue
} }
val subtype = subtypesForLocale.firstOrNull { SubtypeLocaleUtils.getMainLayoutName(it) == (settingsSubtype.mainLayoutName() ?: SubtypeLocaleUtils.QWERTY) } val subtype = subtypesForLocale.firstOrNull { it.mainLayoutNameOrQwerty() == (settingsSubtype.mainLayoutName() ?: SubtypeLocaleUtils.QWERTY) }
if (subtype == null) { if (subtype == null) {
val message = "subtype $settingsSubtype could not be loaded" val message = "subtype $settingsSubtype could not be loaded"
Log.w(TAG, message) Log.w(TAG, message)

View file

@ -30,6 +30,8 @@ fun InputMethodSubtype.mainLayoutName(): String? {
return map[LayoutType.MAIN] return map[LayoutType.MAIN]
} }
fun InputMethodSubtype.mainLayoutNameOrQwerty(): String = mainLayoutName() ?: SubtypeLocaleUtils.QWERTY
fun getResourceSubtypes(resources: Resources): List<InputMethodSubtype> { fun getResourceSubtypes(resources: Resources): List<InputMethodSubtype> {
val subtypes = mutableListOf<InputMethodSubtype>() val subtypes = mutableListOf<InputMethodSubtype>()
val xml = resources.getXml(R.xml.method) val xml = resources.getXml(R.xml.method)
@ -67,7 +69,7 @@ fun getResourceSubtypes(resources: Resources): List<InputMethodSubtype> {
/** Workaround for SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale ignoring custom layout names */ /** Workaround for SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale ignoring custom layout names */
// todo (later): this should be done properly and in SubtypeLocaleUtils // todo (later): this should be done properly and in SubtypeLocaleUtils
fun InputMethodSubtype.displayName(context: Context): String { fun InputMethodSubtype.displayName(context: Context): String {
val layoutName = SubtypeLocaleUtils.getMainLayoutName(this) val layoutName = mainLayoutNameOrQwerty()
if (LayoutUtilsCustom.isCustomLayout(layoutName)) if (LayoutUtilsCustom.isCustomLayout(layoutName))
return "${locale().localizedDisplayName(context)} (${LayoutUtilsCustom.getDisplayName(layoutName)})" return "${locale().localizedDisplayName(context)} (${LayoutUtilsCustom.getDisplayName(layoutName)})"
return SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(this) return SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(this)