mirror of
https://gitlab.futo.org/keyboard/latinime.git
synced 2024-09-28 14:54:30 +01:00
Fix an issue where locale extensions are unexpectedly appearing
This commit is contained in:
parent
9f70a84dc8
commit
798a8f0b5e
@ -2,6 +2,8 @@ package org.futo.inputmethod.latin
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.os.Build
|
||||||
|
import android.util.Log
|
||||||
import android.view.inputmethod.InputMethodManager
|
import android.view.inputmethod.InputMethodManager
|
||||||
import android.view.inputmethod.InputMethodSubtype
|
import android.view.inputmethod.InputMethodSubtype
|
||||||
import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder
|
import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder
|
||||||
@ -45,6 +47,16 @@ import org.futo.inputmethod.latin.uix.theme.Typography
|
|||||||
import org.futo.inputmethod.latin.utils.SubtypeLocaleUtils
|
import org.futo.inputmethod.latin.utils.SubtypeLocaleUtils
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
|
fun Locale.stripExtensionsIfNeeded(): Locale {
|
||||||
|
val newLocale = if(Build.VERSION.SDK_INT >= 26) {
|
||||||
|
this.stripExtensions().stripExtensions() // TODO: Sometimes this requires two calls??
|
||||||
|
} else {
|
||||||
|
this
|
||||||
|
}
|
||||||
|
|
||||||
|
return newLocale
|
||||||
|
}
|
||||||
|
|
||||||
val SubtypesSetting = SettingsKey(
|
val SubtypesSetting = SettingsKey(
|
||||||
stringSetPreferencesKey("subtypes"),
|
stringSetPreferencesKey("subtypes"),
|
||||||
setOf()
|
setOf()
|
||||||
@ -56,16 +68,43 @@ val ActiveSubtype = SettingsKey(
|
|||||||
)
|
)
|
||||||
|
|
||||||
object Subtypes {
|
object Subtypes {
|
||||||
|
// Removes extensions from existing existing subtypes which are not meant to be there
|
||||||
|
private fun removeExtensionsIfNecessary(context: Context) {
|
||||||
|
val currentSubtypes = context.getSettingBlocking(SubtypesSetting)
|
||||||
|
if(currentSubtypes.isEmpty()) return
|
||||||
|
|
||||||
|
val newSubtypes = currentSubtypes.map {
|
||||||
|
val subtype = convertToSubtype(it)
|
||||||
|
if(subtype.locale.contains("_#u-")) {
|
||||||
|
subtypeToString(InputMethodSubtypeBuilder()
|
||||||
|
.setSubtypeLocale(subtype.locale.split("_#u-")[0])
|
||||||
|
.setSubtypeExtraValue(subtype.extraValue)
|
||||||
|
.setLanguageTag(subtype.languageTag)
|
||||||
|
.build())
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}.toSet()
|
||||||
|
|
||||||
|
if(newSubtypes != currentSubtypes) {
|
||||||
|
Log.w("Subtypes", "Removed extensions: $currentSubtypes - $newSubtypes")
|
||||||
|
context.setSettingBlocking(SubtypesSetting.key, newSubtypes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun addDefaultSubtypesIfNecessary(context: Context) {
|
fun addDefaultSubtypesIfNecessary(context: Context) {
|
||||||
val currentSubtypes = context.getSettingBlocking(SubtypesSetting)
|
val currentSubtypes = context.getSettingBlocking(SubtypesSetting)
|
||||||
if(currentSubtypes.isNotEmpty()) return
|
if(currentSubtypes.isNotEmpty()) {
|
||||||
|
removeExtensionsIfNecessary(context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
val locales = context.resources.configuration.locales
|
val locales = context.resources.configuration.locales
|
||||||
if(locales.size() == 0) return
|
if(locales.size() == 0) return
|
||||||
|
|
||||||
var numAdded = 0
|
var numAdded = 0
|
||||||
for(i in 0 until locales.size()) {
|
for(i in 0 until locales.size()) {
|
||||||
val locale = locales.get(i)
|
val locale = locales.get(i).stripExtensionsIfNeeded()
|
||||||
val layout = findClosestLocaleLayouts(locale).firstOrNull() ?: continue
|
val layout = findClosestLocaleLayouts(locale).firstOrNull() ?: continue
|
||||||
|
|
||||||
addLanguage(context, locale, layout)
|
addLanguage(context, locale, layout)
|
||||||
@ -139,7 +178,7 @@ object Subtypes {
|
|||||||
fun addLanguage(context: Context, language: Locale, layout: String) {
|
fun addLanguage(context: Context, language: Locale, layout: String) {
|
||||||
val value = subtypeToString(
|
val value = subtypeToString(
|
||||||
InputMethodSubtypeBuilder()
|
InputMethodSubtypeBuilder()
|
||||||
.setSubtypeLocale(language.toString())
|
.setSubtypeLocale(language.stripExtensionsIfNeeded().toString())
|
||||||
.setSubtypeExtraValue("KeyboardLayoutSet=$layout")
|
.setSubtypeExtraValue("KeyboardLayoutSet=$layout")
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
@ -157,7 +196,7 @@ object Subtypes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getLocale(locale: String): Locale {
|
fun getLocale(locale: String): Locale {
|
||||||
return Locale.forLanguageTag(locale.replace("_", "-"))
|
return Locale.forLanguageTag(locale.replace("_", "-")).stripExtensionsIfNeeded()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getLocale(inputMethodSubtype: InputMethodSubtype): Locale {
|
fun getLocale(inputMethodSubtype: InputMethodSubtype): Locale {
|
||||||
|
@ -16,12 +16,13 @@ import androidx.compose.ui.unit.dp
|
|||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import androidx.navigation.compose.rememberNavController
|
import androidx.navigation.compose.rememberNavController
|
||||||
import org.futo.inputmethod.latin.Subtypes
|
import org.futo.inputmethod.latin.Subtypes
|
||||||
|
import org.futo.inputmethod.latin.stripExtensionsIfNeeded
|
||||||
import org.futo.inputmethod.latin.uix.settings.DropDownPicker
|
import org.futo.inputmethod.latin.uix.settings.DropDownPicker
|
||||||
import org.futo.inputmethod.latin.uix.settings.ScreenTitle
|
import org.futo.inputmethod.latin.uix.settings.ScreenTitle
|
||||||
import org.futo.inputmethod.latin.uix.settings.ScrollableList
|
import org.futo.inputmethod.latin.uix.settings.ScrollableList
|
||||||
import org.futo.inputmethod.latin.uix.settings.SettingItem
|
import org.futo.inputmethod.latin.uix.settings.SettingItem
|
||||||
|
|
||||||
val QwertyVariants = listOf("qwerty", "qwertz", "dvorak", "azerty", "colemak", "bepo", "pcqwerty")
|
val QwertyVariants = listOf("qwerty", "qwertz", "dvorak", "azerty", "colemak", "bepo", "pcqwerty", "nordic")
|
||||||
|
|
||||||
fun makeQwertyWithPrimary(primary: String): List<String> {
|
fun makeQwertyWithPrimary(primary: String): List<String> {
|
||||||
return listOf(primary) + QwertyVariants.filter { it != primary }
|
return listOf(primary) + QwertyVariants.filter { it != primary }
|
||||||
@ -120,7 +121,7 @@ val LocaleLayoutMap = mapOf(
|
|||||||
fun AddLanguageScreen(navController: NavHostController = rememberNavController()) {
|
fun AddLanguageScreen(navController: NavHostController = rememberNavController()) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
val selectedLocale: MutableState<String> = remember { mutableStateOf(context.resources.configuration.locale.toString()) }
|
val selectedLocale: MutableState<String> = remember { mutableStateOf(context.resources.configuration.locale.stripExtensionsIfNeeded().toString()) }
|
||||||
val selectedLayout: MutableState<String> = remember { mutableStateOf("qwerty") }
|
val selectedLayout: MutableState<String> = remember { mutableStateOf("qwerty") }
|
||||||
|
|
||||||
val keys = remember { LocaleLayoutMap.keys.toList() }
|
val keys = remember { LocaleLayoutMap.keys.toList() }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user