Merge pull request #717 from Bnyro/master

attempt to fix auto regions
This commit is contained in:
Bnyro 2022-07-07 22:31:50 +02:00 committed by GitHub
commit 4378d33f5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import com.github.libretube.R
import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.databinding.FragmentHomeBinding
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.LocaleHelper
import com.github.libretube.util.RetrofitInstance
import retrofit2.HttpException
import java.io.IOException
@ -49,7 +50,11 @@ class HomeFragment : Fragment() {
val regionPref = PreferenceHelper.getString(requireContext(), "region", "sys")!!
// get the system default country if auto region selected
region = if (regionPref == "sys") Locale.getDefault().country else regionPref
region = if (regionPref == "sys") {
LocaleHelper
.getDetectedCountry(requireContext(), "UK")
.uppercase()
} else regionPref
binding.recview.layoutManager = GridLayoutManager(view.context, grid.toInt())
fetchJson()

View File

@ -2,6 +2,7 @@ package com.github.libretube.util
import android.content.Context
import android.os.Build
import android.telephony.TelephonyManager
import com.github.libretube.preferences.PreferenceHelper
import java.util.*
@ -37,4 +38,50 @@ object LocaleHelper {
}
res.updateConfiguration(conf, dm)
}
fun getDetectedCountry(context: Context, defaultCountryIsoCode: String): String {
detectSIMCountry(context)?.let {
return it
}
detectNetworkCountry(context)?.let {
return it
}
detectLocaleCountry(context)?.let {
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 {
return context.resources.configuration.locales[0].country
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
}