From ad7b8bd866d2a3aee533765422bb678801929a87 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 14 Jan 2023 15:18:27 +0530 Subject: [PATCH] Simplify country detection methods. --- .../com/github/libretube/util/LocaleHelper.kt | 66 ++++++------------- 1 file changed, 19 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/github/libretube/util/LocaleHelper.kt b/app/src/main/java/com/github/libretube/util/LocaleHelper.kt index 017a96022..9c1f5c769 100644 --- a/app/src/main/java/com/github/libretube/util/LocaleHelper.kt +++ b/app/src/main/java/com/github/libretube/util/LocaleHelper.kt @@ -5,6 +5,8 @@ import android.content.res.Configuration import android.content.res.Resources import android.os.Build import android.telephony.TelephonyManager +import androidx.core.content.getSystemService +import androidx.core.os.ConfigurationCompat import com.github.libretube.constants.PreferenceKeys import com.github.libretube.obj.Country import java.util.* @@ -44,53 +46,24 @@ object LocaleHelper { resources.updateConfiguration(configuration, resources.getDisplayMetrics()) } - fun getDetectedCountry(context: Context, defaultCountryIsoCode: String): String { - detectSIMCountry(context)?.let { - if (it != "") return it - } - - detectNetworkCountry(context)?.let { - if (it != "") return it - } - - detectLocaleCountry(context)?.let { - if (it != "") return it - } - - return defaultCountryIsoCode - } - - private fun detectSIMCountry(context: Context): String? { - try { - 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? { - try { - 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? { - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - return context.resources.configuration.locales[0].country + private fun getDetectedCountry(context: Context): String { + return detectSIMCountry(context).ifEmpty { + detectNetworkCountry(context).ifEmpty { + detectLocaleCountry(context).ifEmpty { "UK" } } - } catch (e: Exception) { - e.printStackTrace() } - return null + } + + private fun detectSIMCountry(context: Context): String { + return context.getSystemService()?.simCountryIso.orEmpty() + } + + private fun detectNetworkCountry(context: Context): String { + return context.getSystemService()?.networkCountryIso.orEmpty() + } + + private fun detectLocaleCountry(context: Context): String { + return ConfigurationCompat.getLocales(context.resources.configuration)[0]!!.country } fun getAvailableCountries(): List { @@ -132,8 +105,7 @@ object LocaleHelper { // get the system default country if auto region selected return if (regionPref == "sys") { - getDetectedCountry(context, "UK") - .uppercase() + getDetectedCountry(context).uppercase() } else { regionPref }