From cccbefeee8268a26da244b036efe3873d15b3ee5 Mon Sep 17 00:00:00 2001 From: liuche Date: Thu, 30 Jul 2020 19:22:01 -0700 Subject: [PATCH] =?UTF-8?q?For=20#11664=20=E2=80=94=20Fixup=20MissingResou?= =?UTF-8?q?rceExceptions=20being=20thrown=20in=20exotic=20L=E2=80=A6=20(#1?= =?UTF-8?q?3142)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * For #11664 — Fixup MissingResourceExceptions being thrown in exotic Locales (#13124) 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. * For #13117 - Hide save to collection button for private tabs in tab tray Co-authored-by: jhugman Co-authored-by: ekager --- .../metrics/LeanplumMetricsService.kt | 28 ++++++++++++++----- .../tabtray/SaveToCollectionsButtonAdapter.kt | 11 ++++++++ .../org/mozilla/fenix/tabtray/TabTrayView.kt | 12 ++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt index 99a0d4a50..aa8f712c9 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt @@ -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? @@ -83,12 +84,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 } @@ -170,6 +178,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" @@ -181,7 +195,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 diff --git a/app/src/main/java/org/mozilla/fenix/tabtray/SaveToCollectionsButtonAdapter.kt b/app/src/main/java/org/mozilla/fenix/tabtray/SaveToCollectionsButtonAdapter.kt index 779e8445a..896fdea7d 100644 --- a/app/src/main/java/org/mozilla/fenix/tabtray/SaveToCollectionsButtonAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/tabtray/SaveToCollectionsButtonAdapter.kt @@ -32,6 +32,17 @@ class SaveToCollectionsButtonAdapter( return ViewHolder(itemView, interactor) } + override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList) { + if (payloads.isNullOrEmpty()) { + onBindViewHolder(holder, position) + return + } + + (payloads[0] as TabTrayView.TabChange).let { + holder.itemView.isVisible = it == TabTrayView.TabChange.NORMAL + } + } + override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.itemView.isVisible = interactor.onModeRequested() is TabTrayDialogFragmentState.Mode.Normal diff --git a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt index 44b1f609d..f11930604 100644 --- a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt +++ b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt @@ -233,9 +233,21 @@ class TabTrayView( behavior.state = BottomSheetBehavior.STATE_EXPANDED } + enum class TabChange { + PRIVATE, NORMAL + } + + private fun toggleSaveToCollectionButton(isPrivate: Boolean) { + collectionsButtonAdapter.notifyItemChanged( + 0, + if (isPrivate) TabChange.PRIVATE else TabChange.NORMAL + ) + } + override fun onTabSelected(tab: TabLayout.Tab?) { toggleFabText(isPrivateModeSelected) filterTabs.invoke(isPrivateModeSelected) + toggleSaveToCollectionButton(isPrivateModeSelected) updateUINormalMode(view.context.components.core.store.state) scrollToTab(view.context.components.core.store.state.selectedTabId)