Merge pull request #2703 from Isira-Seneviratne/Simplify_LocaleHelper

Simplify LocaleHelper methods.
This commit is contained in:
Bnyro 2023-01-16 11:34:28 +01:00 committed by GitHub
commit b4b5c1a65d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 74 deletions

View File

@ -73,7 +73,7 @@ class PlayerSettings : BasePreferenceFragment() {
} }
private fun setupSubtitlePref(preference: ListPreference) { private fun setupSubtitlePref(preference: ListPreference) {
val locales = LocaleHelper.getAvailableLocales().sortedBy { it.name } val locales = LocaleHelper.getAvailableLocales()
val localeNames = locales.map { it.name } val localeNames = locales.map { it.name }
.toMutableList() .toMutableList()
localeNames.add(0, requireContext().getString(R.string.none)) localeNames.add(0, requireContext().getString(R.string.none))

View File

@ -2,9 +2,10 @@ package com.github.libretube.util
import android.content.Context import android.content.Context
import android.content.res.Configuration import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build import android.os.Build
import android.telephony.TelephonyManager import android.telephony.TelephonyManager
import androidx.core.content.getSystemService
import androidx.core.os.ConfigurationCompat
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.obj.Country import com.github.libretube.obj.Country
import java.util.* import java.util.*
@ -15,7 +16,7 @@ object LocaleHelper {
val languageName = PreferenceHelper.getString(PreferenceKeys.LANGUAGE, "sys") val languageName = PreferenceHelper.getString(PreferenceKeys.LANGUAGE, "sys")
val locale = when { val locale = when {
languageName == "sys" -> Locale.getDefault() languageName == "sys" -> Locale.getDefault()
languageName.contains("-") == true -> { languageName.contains("-") -> {
val languageParts = languageName.split("-") val languageParts = languageName.split("-")
Locale( Locale(
languageParts[0], languageParts[0],
@ -38,93 +39,43 @@ object LocaleHelper {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
private fun updateResourcesLegacy(context: Context, locale: Locale) { private fun updateResourcesLegacy(context: Context, locale: Locale) {
Locale.setDefault(locale) Locale.setDefault(locale)
val resources: Resources = context.resources val resources = context.resources
val configuration: Configuration = resources.getConfiguration() val configuration = resources.configuration
configuration.locale = locale configuration.locale = locale
resources.updateConfiguration(configuration, resources.getDisplayMetrics()) resources.updateConfiguration(configuration, resources.displayMetrics)
} }
fun getDetectedCountry(context: Context, defaultCountryIsoCode: String): String { private fun getDetectedCountry(context: Context): String {
detectSIMCountry(context)?.let { return detectSIMCountry(context)
if (it != "") return it ?: detectNetworkCountry(context)
} ?: detectLocaleCountry(context)
?: "UK"
detectNetworkCountry(context)?.let {
if (it != "") return it
}
detectLocaleCountry(context)?.let {
if (it != "") return it
}
return defaultCountryIsoCode
} }
private fun detectSIMCountry(context: Context): String? { private fun detectSIMCountry(context: Context): String? {
try { return context.getSystemService<TelephonyManager>()?.simCountryIso?.ifEmpty { null }
val telephonyManager =
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
return telephonyManager.simCountryIso
} catch (e: Exception) {
e.printStackTrace()
}
return null
} }
private fun detectNetworkCountry(context: Context): String? { private fun detectNetworkCountry(context: Context): String? {
try { return context.getSystemService<TelephonyManager>()?.networkCountryIso?.ifEmpty { null }
val telephonyManager =
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
return telephonyManager.networkCountryIso
} catch (e: Exception) {
e.printStackTrace()
}
return null
} }
private fun detectLocaleCountry(context: Context): String? { private fun detectLocaleCountry(context: Context): String? {
try { return ConfigurationCompat.getLocales(context.resources.configuration)[0]!!.country
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { .ifEmpty { null }
return context.resources.configuration.locales[0].country
}
} catch (e: Exception) {
e.printStackTrace()
}
return null
} }
fun getAvailableCountries(): List<Country> { fun getAvailableCountries(): List<Country> {
val isoCountries = Locale.getISOCountries() return Locale.getISOCountries()
val countries = mutableListOf<Country>() .map { Country(Locale("", it).displayCountry, it) }
isoCountries.forEach { countryCode -> .sortedBy { it.name }
val locale = Locale("", countryCode)
val countryName = locale.displayCountry
countries.add(
Country(
countryName,
countryCode
)
)
}
countries.sortBy { it.name }
return countries
} }
fun getAvailableLocales(): List<Country> { fun getAvailableLocales(): List<Country> {
val availableLocales: Array<Locale> = Locale.getAvailableLocales() return Locale.getAvailableLocales()
val locales = mutableListOf<Country>() .distinctBy { it.language }
.map { Country(it.displayLanguage, it.language) }
availableLocales.forEach { locale -> .sortedBy { it.name }
if (locales.filter { it.code == locale.language }.isEmpty()) {
locales.add(
Country(
locale.displayLanguage,
locale.language
)
)
}
}
return locales
} }
fun getTrendingRegion(context: Context): String { fun getTrendingRegion(context: Context): String {
@ -132,8 +83,7 @@ object LocaleHelper {
// get the system default country if auto region selected // get the system default country if auto region selected
return if (regionPref == "sys") { return if (regionPref == "sys") {
getDetectedCountry(context, "UK") getDetectedCountry(context).uppercase()
.uppercase()
} else { } else {
regionPref regionPref
} }