From b9e116ed13f101500ca5221601e7d23f670ea23e Mon Sep 17 00:00:00 2001 From: Helium314 Date: Fri, 12 Jul 2024 22:25:46 +0200 Subject: [PATCH] adjust how priority language popups work (follow-up to #659) --- .../main/assets/locale_key_texts/bn-BD.txt | 20 ++-- .../main/assets/locale_key_texts/bn-IN.txt | 8 +- app/src/main/assets/locale_key_texts/el.txt | 8 +- app/src/main/assets/locale_key_texts/hi.txt | 34 +++--- app/src/main/assets/locale_key_texts/kn.txt | 20 ++-- app/src/main/assets/locale_key_texts/ml.txt | 20 ++-- app/src/main/assets/locale_key_texts/mr.txt | 18 ++-- app/src/main/assets/locale_key_texts/ta.txt | 10 +- app/src/main/assets/locale_key_texts/te.txt | 20 ++-- .../keyboard_parser/LocaleKeyboardInfos.kt | 100 +++++++++--------- 10 files changed, 131 insertions(+), 127 deletions(-) diff --git a/app/src/main/assets/locale_key_texts/bn-BD.txt b/app/src/main/assets/locale_key_texts/bn-BD.txt index 99a6e968d..6f602cf66 100644 --- a/app/src/main/assets/locale_key_texts/bn-BD.txt +++ b/app/src/main/assets/locale_key_texts/bn-BD.txt @@ -1,14 +1,14 @@ [popup_keys] -ঙ ং % -য য় % -ড ঢ % -প ফ % -ট ঠ % -চ ছ % -জ ঝ % -হ ঞ % -গ ঘ % -ড় ঢ় % +ঙ ং +য য় +ড ঢ +প ফ +ট ঠ +চ ছ +জ ঝ +হ ঞ +গ ঘ +ড় ঢ় ৃ ঋ ু উ ূ ঊ diff --git a/app/src/main/assets/locale_key_texts/bn-IN.txt b/app/src/main/assets/locale_key_texts/bn-IN.txt index 77fdac212..36387a9e7 100644 --- a/app/src/main/assets/locale_key_texts/bn-IN.txt +++ b/app/src/main/assets/locale_key_texts/bn-IN.txt @@ -1,8 +1,8 @@ [popup_keys] -ব ভ % -গ ঘ % -দ ধ % -জ ঝ জ্ঞ % +ব ভ +গ ঘ +দ ধ +জ ঝ জ্ঞ ড ড় ও ো এ ে diff --git a/app/src/main/assets/locale_key_texts/el.txt b/app/src/main/assets/locale_key_texts/el.txt index 4db3f77dd..6372cd786 100644 --- a/app/src/main/assets/locale_key_texts/el.txt +++ b/app/src/main/assets/locale_key_texts/el.txt @@ -1,8 +1,8 @@ [popup_keys] -ε έ % -υ ύ % ϋ ΰ -ι ί % ϊ ΐ -ο ό % +ε έ +υ ύ ϋ ΰ +ι ί ϊ ΐ +ο ό α ά η ή ω ώ diff --git a/app/src/main/assets/locale_key_texts/hi.txt b/app/src/main/assets/locale_key_texts/hi.txt index 4514c00a5..900946954 100644 --- a/app/src/main/assets/locale_key_texts/hi.txt +++ b/app/src/main/assets/locale_key_texts/hi.txt @@ -5,14 +5,14 @@ ई ईं ऊ ऊं ऊँ ध क्ष श्र -ौ ौं % -ै ैं % -ा ां ाँ % -ी ीं % -ू ूं ूँ % -ब ब॒ % -ग ज्ञ ग़ ग॒ % -ज ज॒ ज्ञ ज़ % +ौ ौं +ै ैं +ा ां ाँ +ी ीं +ू ूं ूँ +ब ब॒ +ग ज्ञ ग़ ग॒ +ज ज॒ ज्ञ ज़ ड ड॒ ड़ ओ ओं ऑ ऒ ए एं एँ ऍ ऎ @@ -38,15 +38,15 @@ य य़ ़ ॽ ॰ ऽ punctuation !autoColumnOrder!9 \, . ? ! # ) ( / ; ' @ : - " + \% & -औ ौ % -ऐ ै % -आ ा % -ई ी % -ऊ ू % -ब भ % -ग घ % -द ध % -ज झ ज्ञ % +औ ौ +ऐ ै +आ ा +ई ी +ऊ ू +ब भ +ग घ +द ध +ज झ ज्ञ ड ढ ओ ो ए े diff --git a/app/src/main/assets/locale_key_texts/kn.txt b/app/src/main/assets/locale_key_texts/kn.txt index f899054ac..237a2c4ad 100644 --- a/app/src/main/assets/locale_key_texts/kn.txt +++ b/app/src/main/assets/locale_key_texts/kn.txt @@ -1,14 +1,14 @@ [popup_keys] -ಅ % -ಆ ಾ % -ಇ ಿ % -ಈ ೀ % -ಉ ು % -ಊ ೂ % -ಋ ೄ ೃ ೠ % -ಎ ೆ % -ಏ ೇ % -ಐ ೖ ೈ % +ಅ +ಆ ಾ +ಇ ಿ +ಈ ೀ +ಉ ು +ಊ ೂ +ಋ ೄ ೃ ೠ +ಎ ೆ +ಏ ೇ +ಐ ೖ ೈ ಒ ೊ ಓ ೋ ಔ ೌ diff --git a/app/src/main/assets/locale_key_texts/ml.txt b/app/src/main/assets/locale_key_texts/ml.txt index 7782adc75..a227c63a4 100644 --- a/app/src/main/assets/locale_key_texts/ml.txt +++ b/app/src/main/assets/locale_key_texts/ml.txt @@ -1,14 +1,14 @@ [popup_keys] -് അ % -ാ ആ % -ി ഇ % -ീ ഈ % -ു ഉ % -ൂ ഊ % -ൃ ഋ % -െ എ ഐ ൈ % -േ ഏ % -ൊ ഒ % +് അ +ാ ആ +ി ഇ +ീ ഈ +ു ഉ +ൂ ഊ +ൃ ഋ +െ എ ഐ ൈ +േ ഏ +ൊ ഒ ോ ഓ ഔ ൗ ക ഖ ഗ ഘ diff --git a/app/src/main/assets/locale_key_texts/mr.txt b/app/src/main/assets/locale_key_texts/mr.txt index b8455c566..db6c8b72a 100644 --- a/app/src/main/assets/locale_key_texts/mr.txt +++ b/app/src/main/assets/locale_key_texts/mr.txt @@ -1,13 +1,13 @@ [popup_keys] -ौ औ % -ै ऐ % -ा आ % -ी ई % -ू ऊ % -ब भ % -ग घ % -द ध % -ज झ ज्ञ % +ौ औ +ै ऐ +ा आ +ी ई +ू ऊ +ब भ +ग घ +द ध +ज झ ज्ञ ड ढ ो ओ े ए diff --git a/app/src/main/assets/locale_key_texts/ta.txt b/app/src/main/assets/locale_key_texts/ta.txt index 4d692380a..5c2d9ecb2 100644 --- a/app/src/main/assets/locale_key_texts/ta.txt +++ b/app/src/main/assets/locale_key_texts/ta.txt @@ -1,9 +1,9 @@ [popup_keys] -ஔ ௌ % -ஐ ை % -ஆ ா % -ஈ ீ % -ஊ ூ % +ஔ ௌ +ஐ ை +ஆ ா +ஈ ீ +ஊ ூ ஓ ோ ௐ ஏ ே அ ஃ diff --git a/app/src/main/assets/locale_key_texts/te.txt b/app/src/main/assets/locale_key_texts/te.txt index c86b937c7..baf480a39 100644 --- a/app/src/main/assets/locale_key_texts/te.txt +++ b/app/src/main/assets/locale_key_texts/te.txt @@ -1,14 +1,14 @@ [popup_keys] -ౌ ఔ % -ై ఐ % -ా ఆ % -ీ ఈ % -ూ ఊ % -బ భ % -హ ః % -గ ఘ % -ద ధ % -జ ఝ % +ౌ ఔ +ై ఐ +ా ఆ +ీ ఈ +ూ ఊ +బ భ +హ ః +గ ఘ +ద ధ +జ ఝ డ ఢ ో ఓ ే ఏ diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LocaleKeyboardInfos.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LocaleKeyboardInfos.kt index ebf5ac382..722038061 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LocaleKeyboardInfos.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LocaleKeyboardInfos.kt @@ -16,8 +16,8 @@ import java.util.Locale import kotlin.math.round class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { - private val popupKeys = hashMapOf>() - private val priorityPopupKeys = hashMapOf>() + private val popupKeys = hashMapOf>() + private val priorityPopupKeys = hashMapOf>() private val extraKeys = Array?>(5) { null } var labelSymbol = "\\?123" private set @@ -57,22 +57,23 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { } init { - readStream(dataStream, false) + readStream(dataStream, false, true) // set default quote popupKeys if necessary // should this also be done with punctuation popupKeys? + // todo: those defaults should not be in here if ("\'" !in popupKeys) - popupKeys["\'"] = listOf("!fixedColumnOrder!5", "‚", "‘", "’", "‹", "›") + popupKeys["\'"] = mutableListOf("!fixedColumnOrder!5", "‚", "‘", "’", "‹", "›") if ("\"" !in popupKeys) - popupKeys["\""] = listOf("!fixedColumnOrder!5", "„", "“", "”", "«", "»") + popupKeys["\""] = mutableListOf("!fixedColumnOrder!5", "„", "“", "”", "«", "»") if ("!" !in popupKeys) - popupKeys["!"] = listOf("¡") + popupKeys["!"] = mutableListOf("¡") if (labelQuestion !in popupKeys) - popupKeys[labelQuestion] = if (labelQuestion == "?") listOf("¿") else listOf("?", "¿") + popupKeys[labelQuestion] = if (labelQuestion == "?") mutableListOf("¿") else mutableListOf("?", "¿") if ("punctuation" !in popupKeys) - popupKeys["punctuation"] = listOf("${Key.POPUP_KEYS_AUTO_COLUMN_ORDER}8", "\\,", "?", "!", "#", ")", "(", "/", ";", "'", "@", ":", "-", "\"", "+", "\\%", "&") + popupKeys["punctuation"] = mutableListOf("${Key.POPUP_KEYS_AUTO_COLUMN_ORDER}8", "\\,", "?", "!", "#", ")", "(", "/", ";", "'", "@", ":", "-", "\"", "+", "\\%", "&") } - private fun readStream(stream: InputStream?, onlyPopupKeys: Boolean) { + private fun readStream(stream: InputStream?, onlyPopupKeys: Boolean, priority: Boolean) { if (stream == null) return stream.reader().use { reader -> var mode = READER_MODE_NONE @@ -87,7 +88,7 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { "[number_row]" -> { mode = READER_MODE_NUMBER_ROW; return@forEachLine } } when (mode) { - READER_MODE_POPUP_KEYS -> addPopupKeys(line) + READER_MODE_POPUP_KEYS -> addPopupKeys(line, priority) READER_MODE_EXTRA_KEYS -> if (!onlyPopupKeys) addExtraKey(line.split(colonSpaceRegex, 2)) READER_MODE_LABELS -> if (!onlyPopupKeys) addLabel(line.split(colonSpaceRegex, 2)) READER_MODE_NUMBER_ROW -> setNumberRow(line.splitOnWhitespace(), onlyPopupKeys) @@ -109,19 +110,19 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { fun getShiftSymbolLabel(isTablet: Boolean) = if (isTablet) labelShiftSymbolTablet else labelShiftSymbol - fun getPopupKeys(label: String): List? = popupKeys[label] - fun getPriorityPopupKeys(label: String): List? = priorityPopupKeys[label] + fun getPopupKeys(label: String): Collection? = popupKeys[label] + fun getPriorityPopupKeys(label: String): Collection? = priorityPopupKeys[label] // used by simple parser only, but could be possible for json as well (if necessary) fun getExtraKeys(row: Int): List? = if (row > extraKeys.size) null else extraKeys[row] - fun addFile(dataStream: InputStream?) { - readStream(dataStream, true) + fun addFile(dataStream: InputStream?, priority: Boolean) { + readStream(dataStream, true, priority) } - private fun addPopupKeys(line: String) { + private fun addPopupKeys(line: String, priority: Boolean) { val split = if (line.contains("|")) // if a popup key contains label/code separately, there are cases where space can be in there too // normally this should work for all popup keys, but if we split them on whitespace there is less chance for unnecessary issues @@ -129,26 +130,14 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { else line.splitOnWhitespace() if (split.size == 1) return val key = split.first() - val priorityMarkerIndex = split.indexOf("%") - if (priorityMarkerIndex > 0) { - val existingPriorityPopupKeys = priorityPopupKeys[key] - val newPriorityPopupKeys = split.subList(1, priorityMarkerIndex) - priorityPopupKeys[key] = if (existingPriorityPopupKeys == null) newPriorityPopupKeys - else existingPriorityPopupKeys + newPriorityPopupKeys - val existingPopupKeys = popupKeys[key] - val newPopupKeys = split.subList(priorityMarkerIndex + 1, split.size) - popupKeys[key] = if (existingPopupKeys == null) newPopupKeys - else existingPopupKeys + newPopupKeys - } else { - // a but more special treatment, this should not occur together with priority marker (but technically could) - val existingPopupKeys = popupKeys[key] - val newPopupKeys = if (existingPopupKeys == null) - split.drop(1) - else mergePopupKeys(existingPopupKeys, split.drop(1)) - popupKeys[key] = when (key) { - "'", "\"", "«", "»" -> addFixedColumnOrder(newPopupKeys) - else -> newPopupKeys - } + val popupsMap = if (priority) priorityPopupKeys else popupKeys + if (popupsMap[key] is MutableList) + popupsMap[key] = popupsMap[key]!!.toMutableSet().also { it.addAll(split.drop(1)) } + else if (popupsMap.containsKey(key)) popupsMap[key]!!.addAll(split.drop(1)) + else popupsMap[key] = split.drop(1).toMutableList() // first use a list because usually it's enough + adjustAutoColumnOrder(popupsMap[key]!!) + when (key) { + "'", "\"", "«", "»" -> addFixedColumnOrder(popupsMap[key]!!) } } @@ -219,15 +208,31 @@ private fun mergePopupKeys(original: List, added: List): List): List { - val newPopupKeys = popupKeys.filterNot { it.startsWith(Key.POPUP_KEYS_FIXED_COLUMN_ORDER) } - return listOf("${Key.POPUP_KEYS_FIXED_COLUMN_ORDER}${newPopupKeys.size}") + newPopupKeys +private fun addFixedColumnOrder(popupKeys: MutableCollection) { + // use intermediate list, because we can't add first in a LinkedHashSet (i.e. MutableSet) + popupKeys.removeAll { it.startsWith(Key.POPUP_KEYS_FIXED_COLUMN_ORDER) } + val temp = popupKeys.toList() + popupKeys.clear() + popupKeys.add("${Key.POPUP_KEYS_FIXED_COLUMN_ORDER}${temp.size}") + popupKeys.addAll(temp) } +private fun adjustAutoColumnOrder(popupKeys: MutableCollection) { + // same style as above + // currently, POPUP_KEYS_AUTO_COLUMN_ORDER is only used for 2 lines of punctuation popups, so assume 2 lines + if (!popupKeys.removeAll { it.startsWith(Key.POPUP_KEYS_AUTO_COLUMN_ORDER) }) + return + val temp = popupKeys.toList() + popupKeys.clear() + popupKeys.add("${Key.POPUP_KEYS_AUTO_COLUMN_ORDER}${((temp.size + 1) / 2).coerceAtMost(10)}") + popupKeys.addAll(temp) +} + +// no caching because this might get called first, and thus can mess with the cache +// those 2 ways of creating could be unified, but whatever... fun getOrCreate(context: Context, locale: Locale): LocaleKeyboardInfos = - localeKeyboardInfosCache.getOrPut(locale.toString()) { - LocaleKeyboardInfos(getStreamForLocale(locale, context), locale) - } + localeKeyboardInfosCache[locale.toString()] + ?: LocaleKeyboardInfos(getStreamForLocale(locale, context), locale) fun addLocaleKeyTextsToParams(context: Context, params: KeyboardParams, popupKeysSetting: Int) { val locales = params.mSecondaryLocales + params.mId.locale @@ -238,15 +243,14 @@ fun addLocaleKeyTextsToParams(context: Context, params: KeyboardParams, popupKey private fun createLocaleKeyTexts(context: Context, params: KeyboardParams, popupKeysSetting: Int): LocaleKeyboardInfos { val lkt = LocaleKeyboardInfos(getStreamForLocale(params.mId.locale, context), params.mId.locale) - if (popupKeysSetting == POPUP_KEYS_MORE) - lkt.addFile(context.assets.open("$LOCALE_TEXTS_FOLDER/more_popups_more.txt")) - else if (popupKeysSetting == POPUP_KEYS_ALL) - lkt.addFile(context.assets.open("$LOCALE_TEXTS_FOLDER/more_popups_all.txt")) - else if (popupKeysSetting == POPUP_KEYS_MAIN) - lkt.addFile(context.assets.open("$LOCALE_TEXTS_FOLDER/more_popups_main.txt")) params.mSecondaryLocales.forEach { locale -> if (locale == params.mId.locale) return@forEach - lkt.addFile(getStreamForLocale(locale, context)) + lkt.addFile(getStreamForLocale(locale, context), true) + } + when (popupKeysSetting) { + POPUP_KEYS_MAIN -> lkt.addFile(context.assets.open("$LOCALE_TEXTS_FOLDER/more_popups_main.txt"), false) + POPUP_KEYS_MORE -> lkt.addFile(context.assets.open("$LOCALE_TEXTS_FOLDER/more_popups_more.txt"), false) + POPUP_KEYS_ALL -> lkt.addFile(context.assets.open("$LOCALE_TEXTS_FOLDER/more_popups_all.txt"), false) } return lkt }