From 268da65b3f5cd3cf1387e227e82f0a919f5e8fa0 Mon Sep 17 00:00:00 2001 From: Grisha Kruglov Date: Tue, 13 Oct 2020 14:34:20 -0700 Subject: [PATCH] [fenix] Closes https://github.com/mozilla-mobile/fenix/issues/15816 - Attempt to handle 'allowInPrivate..' pref with detached context --- .../fenix/addons/AddonsManagementFragment.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/addons/AddonsManagementFragment.kt b/app/src/main/java/org/mozilla/fenix/addons/AddonsManagementFragment.kt index b6f3b60cad..6ed2544662 100644 --- a/app/src/main/java/org/mozilla/fenix/addons/AddonsManagementFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/addons/AddonsManagementFragment.kt @@ -35,6 +35,7 @@ import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.showToolbar import org.mozilla.fenix.theme.ThemeManager +import java.lang.ref.WeakReference import java.util.concurrent.CancellationException /** @@ -169,7 +170,15 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management) private fun showInstallationDialog(addon: Addon) { if (!isInstallationInProgress && !hasExistingAddonInstallationDialogFragment()) { requireComponents.analytics.metrics.track(Event.AddonInstalled(addon.id)) - val addonCollectionProvider = requireContext().components.addonCollectionProvider + val context = requireContext() + val addonCollectionProvider = context.components.addonCollectionProvider + + // Fragment may not be attached to the context anymore during onConfirmButtonClicked handling, + // but we still want to be able to process user selection of the 'allowInPrivateBrowsing' pref. + // This is a best-effort attempt to do so - retain a weak reference to the application context + // (to avoid a leak), which we attempt to use to access addonManager. + // See https://github.com/mozilla-mobile/fenix/issues/15816 + val weakApplicationContext: WeakReference = WeakReference(context) val dialog = AddonInstallationDialogFragment.newInstance( addon = addon, @@ -189,7 +198,7 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management) ), onConfirmButtonClicked = { _, allowInPrivateBrowsing -> if (allowInPrivateBrowsing) { - requireContext().components.addonManager.setAddonAllowedInPrivateBrowsing( + weakApplicationContext.get()?.components?.addonManager?.setAddonAllowedInPrivateBrowsing( addon, allowInPrivateBrowsing, onSuccess = {