For #11664 — Fixup MissingResourceExceptions being thrown in exotic Locales (#13124) (#13138)

Our kotlin code is not catching the `MissingResourceException` in the `LeanplumMetricsService` which results in the app crashing when the locale isn't known by the device.

Catches the exception, and falls back to the ISO 639 language code. This isn't a great solution, because ISO 639 isn't especially stable.

In practice however this is almost certainly never going to be a problem because Leanplum isn't going to be supported in such exotic locales.

In this case, using the ISO 639 language code allows the error message to be more informative.

Co-authored-by: jhugman <jhugman@users.noreply.github.com>
releases/v79.0.0 v79.0.1
liuche 4 years ago committed by GitHub
parent 20016c88e1
commit 12694008ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,6 +21,7 @@ import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.components.metrics.MozillaProductDetector.MozillaProducts
import org.mozilla.fenix.ext.settings
import java.util.Locale
import java.util.MissingResourceException
import java.util.UUID.randomUUID
private val Event.name: String?
@ -79,12 +80,19 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
leanplumJob = scope.launch {
val applicationSetLocale = LocaleManager.getCurrentLocale(application)
val currentLocale = when (applicationSetLocale != null) {
true -> applicationSetLocale.isO3Language
false -> Locale.getDefault().isO3Language
}
if (!isLeanplumEnabled(currentLocale)) {
Log.i(LOGTAG, "Leanplum is not available for this locale: $currentLocale")
val currentLocale = applicationSetLocale ?: Locale.getDefault()
val languageCode =
currentLocale.iso3LanguageOrNull
?: currentLocale.language.let {
if (it.isNotBlank()) {
it
} else {
currentLocale.toString()
}
}
if (!isLeanplumEnabled(languageCode)) {
Log.i(LOGTAG, "Leanplum is not available for this locale: $languageCode")
return@launch
}
@ -155,6 +163,12 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
return LEANPLUM_ENABLED_LOCALES.contains(locale)
}
private val Locale.iso3LanguageOrNull: String?
get() =
try {
this.isO3Language
} catch (_: MissingResourceException) { null }
companion object {
private const val LOGTAG = "LeanplumMetricsService"
@ -166,7 +180,7 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
get() = BuildConfig.LEANPLUM_TOKEN.orEmpty()
// Leanplum needs to be enabled for the following locales.
// Irrespective of the actual device location.
private val LEANPLUM_ENABLED_LOCALES = listOf(
private val LEANPLUM_ENABLED_LOCALES = setOf(
"eng", // English
"zho", // Chinese
"deu", // German

Loading…
Cancel
Save