From 8abee316da7ea99dc8319582f467a7b187f7577d Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Thu, 23 Nov 2023 14:28:10 -0500 Subject: [PATCH] Bug 1866293 - Add support for installing an add-on a from local file. --- .../fenix/settings/SettingsFragment.kt | 26 +++++++++++++++++ app/src/main/res/values/preference_keys.xml | 1 + app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/preferences.xml | 6 ++++ .../fenix/settings/SettingsFragmentTest.kt | 29 +++++++++++++++++++ 5 files changed, 64 insertions(+) diff --git a/app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt index 4f2461d4a1..695affc924 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt @@ -36,6 +36,7 @@ import mozilla.components.concept.sync.AccountObserver import mozilla.components.concept.sync.AuthType import mozilla.components.concept.sync.OAuthAccount import mozilla.components.concept.sync.Profile +import mozilla.components.feature.addons.ui.AddonFilePicker import mozilla.components.service.glean.private.NoExtras import mozilla.components.support.ktx.android.view.showKeyboard import mozilla.components.ui.widgets.withCenterAlignedButtons @@ -70,6 +71,7 @@ class SettingsFragment : PreferenceFragmentCompat() { private val args by navArgs() private lateinit var accountUiView: AccountUiView + private lateinit var addonFilePicker: AddonFilePicker private val profilerViewModel: ProfilerViewModel by activityViewModels() @VisibleForTesting @@ -107,6 +109,18 @@ class SettingsFragment : PreferenceFragmentCompat() { updateFxAAllowDomesticChinaServerMenu = ::updateFxAAllowDomesticChinaServerMenu, ) + addonFilePicker = AddonFilePicker( + requireContext(), + requireComponents.addonManager, + ) { + Toast.makeText( + context, + getString(R.string.mozac_feature_addons_failed_to_install_generic), + Toast.LENGTH_LONG, + ).show() + } + addonFilePicker.registerForResults(this) + // It's important to update the account UI state in onCreate since that ensures we'll never // display an incorrect state in the UI. We take care to not also call it as part of onResume // if it was just called here (via the 'creatingFragment' flag). @@ -386,6 +400,10 @@ class SettingsFragment : PreferenceFragmentCompat() { resources.getString(R.string.pref_key_nimbus_experiments) -> { SettingsFragmentDirections.actionSettingsFragmentToNimbusExperimentsFragment() } + resources.getString(R.string.pref_key_install_local_addon) -> { + addonFilePicker.launch() + null + } resources.getString(R.string.pref_key_override_amo_collection) -> { val context = requireContext() val dialogView = LayoutInflater.from(context).inflate(R.layout.amo_collection_override_dialog, null) @@ -488,6 +506,7 @@ class SettingsFragment : PreferenceFragmentCompat() { (requireContext().components.core.engine.profiler?.isProfilerActive() != null) } setupCookieBannerPreference() + setupInstallAddonFromFilePreference(requireContext().settings()) setupAmoCollectionOverridePreference(requireContext().settings()) setupGeckoLogsPreference(requireContext().settings()) setupAllowDomesticChinaFxaServerPreference() @@ -698,6 +717,13 @@ class SettingsFragment : PreferenceFragmentCompat() { } } + @VisibleForTesting + internal fun setupInstallAddonFromFilePreference(settings: Settings) { + with(requirePreference(R.string.pref_key_install_local_addon)) { + isVisible = settings.showSecretDebugMenuThisSession + } + } + @VisibleForTesting internal fun setupHttpsOnlyPreferences() { val httpsOnlyPreference = diff --git a/app/src/main/res/values/preference_keys.xml b/app/src/main/res/values/preference_keys.xml index 04d17339fc..c3dc8a9941 100644 --- a/app/src/main/res/values/preference_keys.xml +++ b/app/src/main/res/values/preference_keys.xml @@ -38,6 +38,7 @@ pref_key_delete_browsing_data_on_quit_categories pref_key_last_known_mode_private pref_key_addons + pref_key_install_local_addon pref_key_override_amo_user pref_key_override_amo_collection pref_key_enable_gecko_logs diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2425bbe02f..f72f41ef37 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -610,6 +610,8 @@ Add-ons + + Install add-on from file Notifications diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 8639c95004..3d0ef1699a 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -149,6 +149,12 @@ app:iconSpaceReserved="false" android:title="@string/preferences_addons" /> + + ( + settingsFragment.getPreferenceKey(R.string.pref_key_install_local_addon), + ) + + settingsFragment.setupInstallAddonFromFilePreference(mockk(relaxed = true)) + assertNotNull(preference) + assertFalse(preference!!.isVisible) + + val settings: Settings = mockk(relaxed = true) + + every { settings.showSecretDebugMenuThisSession } returns true + settingsFragment.setupInstallAddonFromFilePreference(settings) + assertTrue(preference.isVisible) + unmockkObject(Config) + } + @Test fun `Add-on collection override pref is visible if already configured and feature is enabled`() = runTestOnMain { val settingsFragment = SettingsFragment()