mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
commit
4378d33f5e
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user