Simplify country detection methods.

This commit is contained in:
Isira Seneviratne 2023-01-14 15:18:27 +05:30
parent e01a3d8465
commit ad7b8bd866

View File

@ -5,6 +5,8 @@ import android.content.res.Configuration
import android.content.res.Resources 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.*
@ -44,53 +46,24 @@ object LocaleHelper {
resources.updateConfiguration(configuration, resources.getDisplayMetrics()) resources.updateConfiguration(configuration, resources.getDisplayMetrics())
} }
fun getDetectedCountry(context: Context, defaultCountryIsoCode: String): String { private fun getDetectedCountry(context: Context): String {
detectSIMCountry(context)?.let { return detectSIMCountry(context).ifEmpty {
if (it != "") return it detectNetworkCountry(context).ifEmpty {
} detectLocaleCountry(context).ifEmpty { "UK" }
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
} }
} catch (e: Exception) {
e.printStackTrace()
} }
return null }
private fun detectSIMCountry(context: Context): String {
return context.getSystemService<TelephonyManager>()?.simCountryIso.orEmpty()
}
private fun detectNetworkCountry(context: Context): String {
return context.getSystemService<TelephonyManager>()?.networkCountryIso.orEmpty()
}
private fun detectLocaleCountry(context: Context): String {
return ConfigurationCompat.getLocales(context.resources.configuration)[0]!!.country
} }
fun getAvailableCountries(): List<Country> { fun getAvailableCountries(): List<Country> {
@ -132,8 +105,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
} }