Simplify country detection further.

This commit is contained in:
Isira Seneviratne 2023-01-16 12:14:30 +05:30
parent e68677ab6d
commit 8ccc91024e

View File

@ -2,7 +2,6 @@ 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.content.getSystemService
@ -40,30 +39,30 @@ 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 = resources.configuration val configuration = resources.configuration
configuration.locale = locale configuration.locale = locale
resources.updateConfiguration(configuration, resources.displayMetrics) resources.updateConfiguration(configuration, resources.displayMetrics)
} }
private fun getDetectedCountry(context: Context): String { private fun getDetectedCountry(context: Context): String {
return detectSIMCountry(context).ifEmpty { return detectSIMCountry(context)
detectNetworkCountry(context).ifEmpty { ?: detectNetworkCountry(context)
detectLocaleCountry(context).ifEmpty { "UK" } ?: detectLocaleCountry(context)
} ?: "UK"
}
} }
private fun detectSIMCountry(context: Context): String { private fun detectSIMCountry(context: Context): String? {
return context.getSystemService<TelephonyManager>()?.simCountryIso.orEmpty() return context.getSystemService<TelephonyManager>()?.simCountryIso?.ifEmpty { null }
} }
private fun detectNetworkCountry(context: Context): String { private fun detectNetworkCountry(context: Context): String? {
return context.getSystemService<TelephonyManager>()?.networkCountryIso.orEmpty() return context.getSystemService<TelephonyManager>()?.networkCountryIso?.ifEmpty { null }
} }
private fun detectLocaleCountry(context: Context): String { private fun detectLocaleCountry(context: Context): String? {
return ConfigurationCompat.getLocales(context.resources.configuration)[0]!!.country return ConfigurationCompat.getLocales(context.resources.configuration)[0]!!.country
.ifEmpty { null }
} }
fun getAvailableCountries(): List<Country> { fun getAvailableCountries(): List<Country> {