diff --git a/vector/src/main/java/im/vector/riotx/core/utils/SystemUtils.kt b/vector/src/main/java/im/vector/riotx/core/utils/SystemUtils.kt index 23f3cbc875..9e5af038ef 100644 --- a/vector/src/main/java/im/vector/riotx/core/utils/SystemUtils.kt +++ b/vector/src/main/java/im/vector/riotx/core/utils/SystemUtils.kt @@ -33,9 +33,6 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import im.vector.riotx.R import im.vector.riotx.features.notifications.NotificationUtils -import im.vector.riotx.features.settings.VectorLocale -import timber.log.Timber -import java.util.Locale /** * Tells if the application ignores battery optimizations. @@ -94,24 +91,6 @@ fun copyToClipboard(context: Context, text: CharSequence, showToast: Boolean = t } } -/** - * Provides the device locale - * - * @return the device locale - */ -fun getDeviceLocale(context: Context): Locale { - return try { - val packageManager = context.packageManager - val resources = packageManager.getResourcesForApplication("android") - @Suppress("DEPRECATION") - resources.configuration.locale - } catch (e: Exception) { - Timber.e(e, "## getDeviceLocale() failed") - // Fallback to application locale - VectorLocale.applicationLocale - } -} - /** * Shows notification settings for the current app. * In android O will directly opens the notification settings, in lower version it will show the App settings diff --git a/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt b/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt index 7d58c4aacc..8515a5ac50 100755 --- a/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt +++ b/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt @@ -31,9 +31,9 @@ import im.vector.riotx.BuildConfig import im.vector.riotx.R import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.extensions.toOnOff -import im.vector.riotx.core.utils.getDeviceLocale import im.vector.riotx.features.settings.VectorLocale import im.vector.riotx.features.settings.VectorPreferences +import im.vector.riotx.features.settings.locale.SystemLocaleProvider import im.vector.riotx.features.themes.ThemeUtils import im.vector.riotx.features.version.VersionProvider import okhttp3.Call @@ -58,10 +58,13 @@ import javax.inject.Singleton * BugReporter creates and sends the bug reports. */ @Singleton -class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSessionHolder, - private val versionProvider: VersionProvider, - private val vectorPreferences: VectorPreferences, - private val vectorFileLogger: VectorFileLogger) { +class BugReporter @Inject constructor( + private val activeSessionHolder: ActiveSessionHolder, + private val versionProvider: VersionProvider, + private val vectorPreferences: VectorPreferences, + private val vectorFileLogger: VectorFileLogger, + private val systemLocaleProvider: SystemLocaleProvider +) { var inMultiWindowMode = false companion object { @@ -240,7 +243,7 @@ class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSes + Build.VERSION.INCREMENTAL + "-" + Build.VERSION.CODENAME) .addFormDataPart("locale", Locale.getDefault().toString()) .addFormDataPart("app_language", VectorLocale.applicationLocale.toString()) - .addFormDataPart("default_app_language", getDeviceLocale(context).toString()) + .addFormDataPart("default_app_language", systemLocaleProvider.getSystemLocale().toString()) .addFormDataPart("theme", ThemeUtils.getApplicationTheme(context)) val buildNumber = context.getString(R.string.build_number) diff --git a/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerController.kt b/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerController.kt index ecaeac31c1..3745ba513e 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerController.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerController.kt @@ -60,7 +60,7 @@ class LocalePickerController @Inject constructor( title(stringProvider.getString(R.string.choose_locale_other_locales_title)) } list - .filter { it != data.currentLocale } + .filter { it.toString() != data.currentLocale.toString() } .forEach { localeItem { id(it.toString()) diff --git a/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerViewState.kt b/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerViewState.kt index 6a0f39ab66..e32cdc632c 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerViewState.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerViewState.kt @@ -17,9 +17,10 @@ package im.vector.riotx.features.settings.locale import com.airbnb.mvrx.MvRxState +import im.vector.riotx.features.settings.VectorLocale import java.util.Locale data class LocalePickerViewState( - val currentLocale: Locale = Locale.getDefault(), + val currentLocale: Locale = VectorLocale.applicationLocale, val locales: List = emptyList() ) : MvRxState diff --git a/vector/src/main/java/im/vector/riotx/features/settings/locale/SystemLocaleProvider.kt b/vector/src/main/java/im/vector/riotx/features/settings/locale/SystemLocaleProvider.kt new file mode 100644 index 0000000000..d3265f3179 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/settings/locale/SystemLocaleProvider.kt @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotx.features.settings.locale + +import android.content.Context +import timber.log.Timber +import java.util.Locale +import javax.inject.Inject + +class SystemLocaleProvider @Inject constructor( + private val context: Context +) { + + /** + * Provides the device locale + * + * @return the device locale, or null in case of error + */ + fun getSystemLocale(): Locale? { + return try { + val packageManager = context.packageManager + val resources = packageManager.getResourcesForApplication("android") + @Suppress("DEPRECATION") + resources.configuration.locale + } catch (e: Exception) { + Timber.e(e, "## getDeviceLocale() failed") + null + } + } +}