diff --git a/app/src/main/java/com/amaze/filemanager/utils/ContextLocaleExt.kt b/app/src/main/java/com/amaze/filemanager/utils/ContextLocaleExt.kt index fa15ba6903..7c3041899d 100644 --- a/app/src/main/java/com/amaze/filemanager/utils/ContextLocaleExt.kt +++ b/app/src/main/java/com/amaze/filemanager/utils/ContextLocaleExt.kt @@ -58,7 +58,26 @@ fun Context.getLocaleListFromXml(): LocaleListCompat { tagsList.remove("he") } - return LocaleListCompat.forLanguageTags(tagsList.joinToString(",")) + return try { + val seenLocales = LinkedHashSet() + val dedupedTags = mutableListOf() + + for (tag in tagsList) { + val resolvedLocale = Locale.forLanguageTag(tag.toString()) + // add() returns false if an equal Locale was already seen - + // this mirrors exactly what LocaleList's own dedupe check does, + // so anything that would crash it gets filtered out here first. + if (seenLocales.add(resolvedLocale)) { + dedupedTags.add(tag.toString()) + } + } + + LocaleListCompat.forLanguageTags(dedupedTags.joinToString(",")) + } catch (e: IllegalArgumentException) { + // Last-resort safety net for any OEM ICU quirk we didn't anticipate above. + e.printStackTrace() + LocaleListCompat.getEmptyLocaleList() + } } /** @@ -91,9 +110,9 @@ fun Context.getLangPreferenceDropdownEntries(): Map { xmlLocale.getDisplayName(Locale.getDefault()) } else { val nameInCurrentLocale = - currentLocaleList.first { locale -> + currentLocaleList.firstOrNull { locale -> xmlLocale.getDisplayName(locale).isNotEmpty() - } + } ?: Locale.getDefault() xmlLocale.getDisplayName(nameInCurrentLocale) } @@ -102,4 +121,4 @@ fun Context.getLangPreferenceDropdownEntries(): Map { } return map -} +} \ No newline at end of file