Fix switch language issue

This commit is contained in:
Benoit Marty 2020-08-24 18:05:11 +02:00 committed by Benoit Marty
parent 116e6fb3c0
commit 52cf4d24d3
7 changed files with 52 additions and 29 deletions

View File

@ -9,6 +9,7 @@ Improvements 🙌:
Bugfix 🐛:
- Display name not shown under Settings/General (#1926)
- Fix changing language issue
Translations 🗣:
-

View File

@ -32,10 +32,6 @@ import com.airbnb.epoxy.EpoxyAsyncUtil
import com.airbnb.epoxy.EpoxyController
import com.facebook.stetho.Stetho
import com.gabrielittner.threetenbp.LazyThreeTen
import org.matrix.android.sdk.api.Matrix
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.legacy.LegacySessionImporter
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.DaggerVectorComponent
import im.vector.app.core.di.HasVectorInjector
@ -50,16 +46,21 @@ import im.vector.app.features.notifications.NotificationUtils
import im.vector.app.features.pin.PinLocker
import im.vector.app.features.popup.PopupAlertManager
import im.vector.app.features.rageshake.VectorUncaughtExceptionHandler
import im.vector.app.features.settings.VectorLocale
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.themes.ThemeUtils
import im.vector.app.features.version.VersionProvider
import im.vector.app.push.fcm.FcmHelper
import org.matrix.android.sdk.api.Matrix
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.legacy.LegacySessionImporter
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.Executors
import javax.inject.Inject
import androidx.work.Configuration as WorkConfiguration
class VectorApplication :
@ -119,7 +120,9 @@ class VectorApplication :
R.array.com_google_android_gms_fonts_certs
)
FontsContractCompat.requestFont(this, fontRequest, emojiCompatFontProvider, getFontThreadHandler())
vectorConfiguration.initConfiguration()
VectorLocale.init(this)
ThemeUtils.init(this)
vectorConfiguration.applyToApplicationContext()
emojiCompatWrapper.init(fontRequest)

View File

@ -17,8 +17,10 @@
package im.vector.app.core.services
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import im.vector.app.core.extensions.vectorComponent
import timber.log.Timber
/**
@ -31,6 +33,10 @@ abstract class VectorService : Service() {
*/
private var mIsSelfDestroyed = false
override fun attachBaseContext(base: Context) {
super.attachBaseContext(vectorComponent().vectorConfiguration().getLocalisedContext(base))
}
override fun onCreate() {
super.onCreate()

View File

@ -16,10 +16,11 @@
package im.vector.app.features.configuration
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import androidx.annotation.RequiresApi
import im.vector.app.features.settings.FontScale
import im.vector.app.features.settings.VectorLocale
import im.vector.app.features.themes.ThemeUtils
@ -40,14 +41,9 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
}
}
/**
* Init the configuration from the saved one
*/
fun initConfiguration() {
VectorLocale.init(context)
fun applyToApplicationContext() {
val locale = VectorLocale.applicationLocale
val fontScale = FontScale.getFontScaleValue(context)
val theme = ThemeUtils.getApplicationTheme(context)
Locale.setDefault(locale)
val config = Configuration(context.resources.configuration)
@ -56,9 +52,6 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
config.fontScale = fontScale.scale
@Suppress("DEPRECATION")
context.resources.updateConfiguration(config, context.resources.displayMetrics)
// init the theme
ThemeUtils.setApplicationTheme(context, theme)
}
/**
@ -67,26 +60,22 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
* @param context the context
* @return the localised context
*/
@SuppressLint("NewApi")
fun getLocalisedContext(context: Context): Context {
try {
val resources = context.resources
val locale = VectorLocale.applicationLocale
val configuration = resources.configuration
// create new configuration passing old configuration from original Context
val configuration = Configuration(context.resources.configuration)
configuration.fontScale = FontScale.getFontScaleValue(context).scale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setLocaleForApi24(configuration, locale)
} else {
configuration.setLocale(locale)
}
configuration.setLayoutDirection(locale)
return context.createConfigurationContext(configuration)
} else {
@Suppress("DEPRECATION")
configuration.locale = locale
configuration.setLayoutDirection(locale)
@Suppress("DEPRECATION")
resources.updateConfiguration(configuration, resources.displayMetrics)
return context
}
} catch (e: Exception) {
Timber.e(e, "## getLocalisedContext() failed")
}
@ -94,6 +83,20 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
return context
}
@RequiresApi(Build.VERSION_CODES.N)
private fun setLocaleForApi24(config: Configuration, locale: Locale) {
val set: MutableSet<Locale> = LinkedHashSet()
// bring the user locale to the front of the list
set.add(locale)
val all = LocaleList.getDefault()
for (i in 0 until all.size()) {
// append other locales supported by the user
set.add(all[i])
}
val locales = set.toTypedArray()
config.setLocales(LocaleList(*locales))
}
/**
* Compute the locale status value
* @return the local status value

View File

@ -168,6 +168,7 @@ class VectorSettingsPreferencesFragment @Inject constructor(
v.setOnClickListener {
dialog.dismiss()
FontScale.updateFontScale(activity, i)
vectorConfiguration.applyToApplicationContext()
activity.restart()
}
}

View File

@ -26,11 +26,13 @@ import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.configuration.VectorConfiguration
import im.vector.app.features.settings.VectorLocale
import kotlinx.coroutines.launch
class LocalePickerViewModel @AssistedInject constructor(
@Assisted initialState: LocalePickerViewState
@Assisted initialState: LocalePickerViewState,
private val vectorConfiguration: VectorConfiguration
) : VectorViewModel<LocalePickerViewState, LocalePickerAction, LocalePickerViewEvents>(initialState) {
@AssistedInject.Factory
@ -70,6 +72,7 @@ class LocalePickerViewModel @AssistedInject constructor(
private fun handleSelectLocale(action: LocalePickerAction.SelectLocale) {
VectorLocale.saveApplicationLocale(action.locale)
vectorConfiguration.applyToApplicationContext()
_viewEvents.post(LocalePickerViewEvents.RestartActivity)
}
}

View File

@ -44,6 +44,12 @@ object ThemeUtils {
private val mColorByAttr = HashMap<Int, Int>()
// init the theme
fun init(context: Context) {
val theme = getApplicationTheme(context)
setApplicationTheme(context, theme)
}
/**
* @return true if current theme is Light or Status
*/