Setting to always follow system language

Change-Id: Id441ca08b89d5bf3a9b23b2c4639ecf71a22e334
This commit is contained in:
SpiritCroc 2022-06-21 20:14:36 +02:00
parent e47183de8b
commit 59db6af93e
5 changed files with 67 additions and 0 deletions

View file

@ -18,6 +18,8 @@ package im.vector.app.features.settings
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import androidx.core.content.edit
import im.vector.app.BuildConfig
import im.vector.app.R
@ -51,6 +53,18 @@ object VectorLocale {
*/
var applicationLocale = defaultLocale
private set
get() {
return if (followSystemLocale) {
Locale.getDefault()
} else {
field
}
}
/**
* Whether to always follow the system locale
*/
var followSystemLocale: Boolean = false
private lateinit var context: Context
@ -61,6 +75,27 @@ object VectorLocale {
this.context = context
val preferences = DefaultSharedPreferences.getInstance(context)
followSystemLocale = preferences.getBoolean(VectorPreferences.SETTINGS_FOLLOW_SYSTEM_LOCALE, false)
reloadLocale()
}
fun reloadLocale() {
val preferences = DefaultSharedPreferences.getInstance(context)
if (followSystemLocale) {
// Locale.getDefault() may have been changed by us, so we need to restore it from the system configuration explicitly
val systemLocale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Resources.getSystem().configuration.locales[0]
} else {
@Suppress("DEPRECATION") // Deprecated in API level 24, for which we use above ^
Resources.getSystem().configuration.locale
}
if (systemLocale != Locale.getDefault()) {
Locale.setDefault(systemLocale)
}
return
}
if (preferences.contains(APPLICATION_LOCALE_LANGUAGE_KEY)) {
applicationLocale = Locale(
preferences.getString(APPLICATION_LOCALE_LANGUAGE_KEY, "")!!,
@ -85,6 +120,7 @@ object VectorLocale {
*/
fun saveApplicationLocale(locale: Locale) {
applicationLocale = locale
followSystemLocale = false
DefaultSharedPreferences.getInstance(context).edit {
val language = locale.language

View file

@ -226,6 +226,7 @@ class VectorPreferences @Inject constructor(
private const val SETTINGS_SHOW_OPEN_ANONYMOUS = "SETTINGS_SHOW_OPEN_ANONYMOUS"
private const val SETTINGS_FLOATING_DATE = "SETTINGS_FLOATING_DATE"
private const val SETTINGS_SPACE_BACK_NAVIGATION = "SETTINGS_SPACE_BACK_NAVIGATION"
const val SETTINGS_FOLLOW_SYSTEM_LOCALE = "SETTINGS_FOLLOW_SYSTEM_LOCALE"
private const val DID_ASK_TO_ENABLE_SESSION_PUSH = "DID_ASK_TO_ENABLE_SESSION_PUSH"
@ -1199,6 +1200,7 @@ class VectorPreferences @Inject constructor(
.putBoolean(SETTINGS_READ_RECEIPT_FOLLOWS_READ_MARKER, true)
.putBoolean(SETTINGS_SHOW_OPEN_ANONYMOUS, true)
.putBoolean(SETTINGS_FLOATING_DATE, true)
.putBoolean(SETTINGS_FOLLOW_SYSTEM_LOCALE, true)
.apply()
}

View file

@ -18,6 +18,8 @@ package im.vector.app.features.settings
import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import android.widget.CheckedTextView
import androidx.core.view.children
@ -40,6 +42,7 @@ import im.vector.app.features.themes.BubbleThemeUtils
import im.vector.app.features.themes.ThemeUtils
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.presence.model.PresenceEnum
import java.util.Locale
import javax.inject.Inject
class VectorSettingsPreferencesFragment @Inject constructor(
@ -78,6 +81,21 @@ class VectorSettingsPreferencesFragment @Inject constructor(
// user interface preferences
setUserInterfacePreferences()
// Language: follow system
findPreference<VectorSwitchPreference>(VectorPreferences.SETTINGS_FOLLOW_SYSTEM_LOCALE)?.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue is Boolean) {
VectorLocale.followSystemLocale = newValue
VectorLocale.reloadLocale()
vectorConfiguration.applyToApplicationContext()
// Restart the Activity
activity?.restart()
true
} else {
false
}
}
// Themes
val lightThemePref = findPreference<VectorListPreference>(ThemeUtils.APPLICATION_THEME_KEY)!!
val darkThemePref = findPreference<VectorListPreference>(ThemeUtils.APPLICATION_DARK_THEME_KEY)!!

View file

@ -211,4 +211,7 @@
<string name="settings_space_back_navigation">Spaces back stack</string>
<string name="settings_space_back_navigation_summary">Press back to show the previously selected space</string>
<string name="settings_follow_system_locale">Follow system language</string>
<string name="settings_follow_system_locale_summary">When changing the system language, also change the app\'s language</string>
</resources>

View file

@ -11,8 +11,16 @@
android:key="SETTINGS_INTERFACE_LANGUAGE_PREFERENCE_KEY"
android:persistent="false"
android:title="@string/settings_interface_language"
android:dependency="SETTINGS_FOLLOW_SYSTEM_LOCALE"
app:fragment="im.vector.app.features.settings.locale.LocalePickerFragment" />
<im.vector.app.core.preference.VectorSwitchPreference
android:key="SETTINGS_FOLLOW_SYSTEM_LOCALE"
android:defaultValue="false"
android:title="@string/settings_follow_system_locale"
android:summary="@string/settings_follow_system_locale_summary"
android:disableDependentsState="true" />
<im.vector.app.core.preference.VectorSwitchPreference
android:defaultValue="false"
android:key="SETTINGS_SIMPLIFIED_MODE"