Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
6518: For mozilla-mobile#6517 - Always try to set a locale in updateResources r=Amejia481 a=boek



Co-authored-by: Jeff Boek <[email protected]>
  • Loading branch information
MozLando and boek committed Apr 6, 2020
2 parents fa55f28 + f4cc229 commit df55e79
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ object LocaleManager {
* The latest stored locale saved by [setNewLocale].
*/
fun getCurrentLocale(context: Context): Locale? {
var currentLocale: Locale? = null

if (currentLocale == null) {
val locale = Storage.getLocale(context)
if (locale != null) {
currentLocale = locale.toLocale()
}
}
return currentLocale
return Storage.getLocale(context)?.toLocale()
}

/**
Expand All @@ -63,28 +55,24 @@ object LocaleManager {
fun resetToSystemDefault(context: Context) {
clear(context)
val locale = getSystemDefault()
locale?.let {
updateSystemLocale(locale)
updateConfiguration(context, locale)
}

updateSystemLocale(locale)
updateConfiguration(context, locale)
}

/**
* Returns the locale set by the system
*/
fun getSystemDefault(): Locale? {
fun getSystemDefault(): Locale {
val config = Resources.getSystem().configuration
return ConfigurationCompat.getLocales(config).get(0)
}

internal fun updateResources(baseContext: Context): Context {
val locale = getCurrentLocale(baseContext)
return if (locale != null) {
updateSystemLocale(locale)
updateConfiguration(baseContext, locale)
} else {
baseContext
}
val locale = getCurrentLocale(baseContext) ?: getSystemDefault()

updateSystemLocale(locale)
return updateConfiguration(baseContext, locale)
}

private fun updateConfiguration(context: Context, locale: Locale): Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ class LocaleManagerTest {

@Test
@Config(qualifiers = "en-rUS")
fun `when calling updateResources with none current language must not change the system locale neither change configurations`() {
fun `when calling updateResources without a stored language we must not change the system locale`() {
val previousSystemLocale = Locale.getDefault()
val context = LocaleManager.updateResources(testContext)
LocaleManager.updateResources(testContext)

assertEquals(testContext, context)
assertEquals(previousSystemLocale, Locale.getDefault())
}

Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Config.kt)

* **support-locale**
* Updates `updateResources` to always update the context configuration

* **feature-toolbar**
* Added `forceExpand` to [BrowserToolbarBottomBehavior] so consumers can expand the BrowserToolbar on demand.

Expand Down

0 comments on commit df55e79

Please sign in to comment.