2016-12-13 14:47:46 -05:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
import android.content.res.Configuration
|
|
|
|
import android.os.Build
|
2016-12-26 10:56:19 -05:00
|
|
|
import android.os.LocaleList
|
2016-12-13 14:47:46 -05:00
|
|
|
import android.view.ContextThemeWrapper
|
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
|
|
|
import uy.kohesive.injekt.injectLazy
|
2016-12-20 18:42:46 -05:00
|
|
|
import java.util.*
|
2016-12-13 14:47:46 -05:00
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* Utility class to change the application's language in runtime.
|
|
|
|
*/
|
|
|
|
@Suppress("DEPRECATION")
|
2016-12-13 14:47:46 -05:00
|
|
|
object LocaleHelper {
|
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* Preferences helper.
|
|
|
|
*/
|
2016-12-13 14:47:46 -05:00
|
|
|
private val preferences: PreferencesHelper by injectLazy()
|
2016-12-13 15:07:48 -05:00
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
2016-12-26 10:56:19 -05:00
|
|
|
* The system's locale.
|
2016-12-20 18:42:46 -05:00
|
|
|
*/
|
2016-12-26 10:56:19 -05:00
|
|
|
private var systemLocale: Locale? = null
|
2016-12-13 14:47:46 -05:00
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* The application's locale. When it's null, the system locale is used.
|
|
|
|
*/
|
2016-12-26 09:44:59 -05:00
|
|
|
private var appLocale = getLocaleFromString(preferences.lang())
|
2016-12-20 18:42:46 -05:00
|
|
|
|
2016-12-27 14:18:38 -05:00
|
|
|
/**
|
|
|
|
* The currently applied locale. Used to avoid losing the selected language after a non locale
|
|
|
|
* configuration change to the application.
|
|
|
|
*/
|
|
|
|
private var currentLocale: Locale? = null
|
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
2016-12-26 09:44:59 -05:00
|
|
|
* Returns the locale for the value stored in preferences, or null if it's system language.
|
2016-12-20 18:42:46 -05:00
|
|
|
*
|
2016-12-26 09:44:59 -05:00
|
|
|
* @param pref the string value stored in preferences.
|
2016-12-20 18:42:46 -05:00
|
|
|
*/
|
2016-12-26 09:44:59 -05:00
|
|
|
fun getLocaleFromString(pref: String): Locale? {
|
|
|
|
if (pref.isNullOrEmpty()) {
|
|
|
|
return null
|
2016-12-20 18:42:46 -05:00
|
|
|
}
|
2016-12-26 09:44:59 -05:00
|
|
|
val parts = pref.split("_", "-")
|
|
|
|
val lang = parts[0]
|
|
|
|
val country = parts.getOrNull(1) ?: ""
|
|
|
|
return Locale(lang, country)
|
2016-12-13 14:47:46 -05:00
|
|
|
}
|
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* Changes the application's locale with a new preference.
|
|
|
|
*
|
|
|
|
* @param pref the new value stored in preferences.
|
|
|
|
*/
|
2016-12-26 09:44:59 -05:00
|
|
|
fun changeLocale(pref: String) {
|
|
|
|
appLocale = getLocaleFromString(pref)
|
2016-12-20 18:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-26 10:56:19 -05:00
|
|
|
* Updates the app's language to an activity.
|
2016-12-20 18:42:46 -05:00
|
|
|
*/
|
2016-12-26 10:56:19 -05:00
|
|
|
fun updateConfiguration(wrapper: ContextThemeWrapper) {
|
2016-12-20 18:42:46 -05:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && appLocale != null) {
|
2016-12-18 16:31:20 -05:00
|
|
|
val config = Configuration(preferences.context.resources.configuration)
|
2016-12-20 18:42:46 -05:00
|
|
|
config.setLocale(appLocale)
|
2016-12-13 14:47:46 -05:00
|
|
|
wrapper.applyOverrideConfiguration(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
2016-12-26 10:56:19 -05:00
|
|
|
* Updates the app's language to the application.
|
2016-12-20 18:42:46 -05:00
|
|
|
*/
|
2016-12-26 10:56:19 -05:00
|
|
|
fun updateConfiguration(app: Application, config: Configuration, configChange: Boolean = false) {
|
|
|
|
if (systemLocale == null) {
|
|
|
|
systemLocale = getConfigLocale(config)
|
2016-12-13 14:47:46 -05:00
|
|
|
}
|
2017-01-01 14:54:41 -05:00
|
|
|
if (configChange) {
|
2016-12-27 14:18:38 -05:00
|
|
|
val configLocale = getConfigLocale(config)
|
|
|
|
if (currentLocale == configLocale) {
|
2016-12-26 10:56:19 -05:00
|
|
|
return
|
|
|
|
}
|
2016-12-27 14:18:38 -05:00
|
|
|
systemLocale = configLocale
|
2016-12-26 10:56:19 -05:00
|
|
|
}
|
2016-12-27 14:18:38 -05:00
|
|
|
currentLocale = appLocale ?: systemLocale ?: Locale.getDefault()
|
|
|
|
val newConfig = updateConfigLocale(config, currentLocale!!)
|
2016-12-26 10:56:19 -05:00
|
|
|
val resources = app.resources
|
|
|
|
resources.updateConfiguration(newConfig, resources.displayMetrics)
|
2016-12-13 14:47:46 -05:00
|
|
|
}
|
|
|
|
|
2016-12-26 11:21:17 -05:00
|
|
|
/**
|
|
|
|
* Returns the locale applied in the given configuration.
|
|
|
|
*/
|
2016-12-26 10:56:19 -05:00
|
|
|
private fun getConfigLocale(config: Configuration): Locale {
|
|
|
|
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
|
|
|
config.locale
|
|
|
|
} else {
|
|
|
|
config.locales[0]
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 14:47:46 -05:00
|
|
|
|
2016-12-26 11:21:17 -05:00
|
|
|
/**
|
|
|
|
* Returns a new configuration with the given locale applied.
|
|
|
|
*/
|
|
|
|
private fun updateConfigLocale(config: Configuration, locale: Locale): Configuration {
|
|
|
|
val newConfig = Configuration(config)
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
|
|
|
newConfig.locale = locale
|
|
|
|
} else {
|
|
|
|
newConfig.locales = LocaleList(locale)
|
|
|
|
}
|
|
|
|
return newConfig
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:47:46 -05:00
|
|
|
}
|