2022-06-07 13:05:49 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
|
|
|
import android.content.Context
|
2022-11-06 14:32:45 +05:30
|
|
|
import android.content.res.Configuration
|
|
|
|
import android.content.res.Resources
|
2022-06-07 13:05:49 +05:30
|
|
|
import android.os.Build
|
2022-07-08 02:00:47 +05:30
|
|
|
import android.telephony.TelephonyManager
|
2023-01-14 15:18:27 +05:30
|
|
|
import androidx.core.content.getSystemService
|
|
|
|
import androidx.core.os.ConfigurationCompat
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-09-24 16:42:50 +05:30
|
|
|
import com.github.libretube.obj.Country
|
2022-06-07 13:05:49 +05:30
|
|
|
import java.util.*
|
|
|
|
|
2022-06-26 15:56:19 +05:30
|
|
|
object LocaleHelper {
|
2022-06-07 13:05:49 +05:30
|
|
|
|
|
|
|
fun updateLanguage(context: Context) {
|
2022-07-17 21:48:39 +05:30
|
|
|
val languageName = PreferenceHelper.getString(PreferenceKeys.LANGUAGE, "sys")
|
2022-11-06 14:32:45 +05:30
|
|
|
val locale = when {
|
|
|
|
languageName == "sys" -> Locale.getDefault()
|
|
|
|
languageName.contains("-") == true -> {
|
|
|
|
val languageParts = languageName.split("-")
|
|
|
|
Locale(
|
|
|
|
languageParts[0],
|
|
|
|
languageParts[1].replace("r", "")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else -> Locale(languageName)
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
2022-11-06 14:32:45 +05:30
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) updateResources(context, locale)
|
|
|
|
updateResourcesLegacy(context, locale)
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
|
|
|
|
2022-11-06 14:32:45 +05:30
|
|
|
private fun updateResources(context: Context, locale: Locale) {
|
2022-06-07 13:05:49 +05:30
|
|
|
Locale.setDefault(locale)
|
2022-11-06 14:32:45 +05:30
|
|
|
val configuration: Configuration = context.resources.configuration
|
|
|
|
configuration.setLocale(locale)
|
|
|
|
context.createConfigurationContext(configuration)
|
|
|
|
}
|
2022-06-07 13:05:49 +05:30
|
|
|
|
2022-11-06 14:32:45 +05:30
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
private fun updateResourcesLegacy(context: Context, locale: Locale) {
|
|
|
|
Locale.setDefault(locale)
|
|
|
|
val resources: Resources = context.resources
|
|
|
|
val configuration: Configuration = resources.getConfiguration()
|
|
|
|
configuration.locale = locale
|
|
|
|
resources.updateConfiguration(configuration, resources.getDisplayMetrics())
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
2022-07-08 02:00:47 +05:30
|
|
|
|
2023-01-14 15:18:27 +05:30
|
|
|
private fun getDetectedCountry(context: Context): String {
|
|
|
|
return detectSIMCountry(context).ifEmpty {
|
|
|
|
detectNetworkCountry(context).ifEmpty {
|
|
|
|
detectLocaleCountry(context).ifEmpty { "UK" }
|
|
|
|
}
|
2022-07-08 02:00:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 15:18:27 +05:30
|
|
|
private fun detectSIMCountry(context: Context): String {
|
|
|
|
return context.getSystemService<TelephonyManager>()?.simCountryIso.orEmpty()
|
2022-07-08 02:00:47 +05:30
|
|
|
}
|
|
|
|
|
2023-01-14 15:18:27 +05:30
|
|
|
private fun detectNetworkCountry(context: Context): String {
|
|
|
|
return context.getSystemService<TelephonyManager>()?.networkCountryIso.orEmpty()
|
2022-07-08 02:00:47 +05:30
|
|
|
}
|
|
|
|
|
2023-01-14 15:18:27 +05:30
|
|
|
private fun detectLocaleCountry(context: Context): String {
|
|
|
|
return ConfigurationCompat.getLocales(context.resources.configuration)[0]!!.country
|
2022-07-08 02:00:47 +05:30
|
|
|
}
|
2022-09-24 16:42:50 +05:30
|
|
|
|
|
|
|
fun getAvailableCountries(): List<Country> {
|
|
|
|
val isoCountries = Locale.getISOCountries()
|
|
|
|
val countries = mutableListOf<Country>()
|
|
|
|
isoCountries.forEach { countryCode ->
|
|
|
|
val locale = Locale("", countryCode)
|
|
|
|
val countryName = locale.displayCountry
|
|
|
|
countries.add(
|
|
|
|
Country(
|
|
|
|
countryName,
|
|
|
|
countryCode
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
countries.sortBy { it.name }
|
|
|
|
return countries
|
|
|
|
}
|
2022-09-24 16:51:31 +05:30
|
|
|
|
|
|
|
fun getAvailableLocales(): List<Country> {
|
|
|
|
val availableLocales: Array<Locale> = Locale.getAvailableLocales()
|
|
|
|
val locales = mutableListOf<Country>()
|
|
|
|
|
|
|
|
availableLocales.forEach { locale ->
|
|
|
|
if (locales.filter { it.code == locale.language }.isEmpty()) {
|
|
|
|
locales.add(
|
|
|
|
Country(
|
|
|
|
locale.displayLanguage,
|
|
|
|
locale.language
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return locales
|
|
|
|
}
|
2022-11-17 22:46:12 +05:30
|
|
|
|
|
|
|
fun getTrendingRegion(context: Context): String {
|
|
|
|
val regionPref = PreferenceHelper.getString(PreferenceKeys.REGION, "sys")
|
|
|
|
|
|
|
|
// get the system default country if auto region selected
|
|
|
|
return if (regionPref == "sys") {
|
2023-01-14 15:18:27 +05:30
|
|
|
getDetectedCountry(context).uppercase()
|
2022-11-17 22:46:12 +05:30
|
|
|
} else {
|
|
|
|
regionPref
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|