Unverified Commit 13f73c2d authored by Emma Malysz's avatar Emma Malysz Committed by GitHub
Browse files

For issue 7192: move unsetOpenLinksInAPrivateTabIfNecessary off main thread. (#7246)

Move method to Settings. There are two instances when we want to call this method: either
processing an intent, or within DefaultBrowserPreference
parent cf143489
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -113,7 +113,7 @@ events:
      preference_key:
        description: "The preference key for the boolean (true/false) preference the user toggled. We currently track:
          show_search_suggestions, remote_debugging, telemetry, tracking_protection, search_bookmarks,
          search_browsing_history, show_clipboard_suggestions, show_search_shortcuts, open_links_in_a_private_tab,
          search_browsing_history, show_clipboard_suggestions, show_search_shortcuts, open_links_in_a_private_tab (bug in implementation https://github.com/mozilla-mobile/fenix/issues/7384),
          pref_key_sync_logins, pref_key_sync_bookmarks, pref_key_sync_history
          and pref_key_show_search_suggestions_in_private"
      enabled:
+0 −19
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.support.base.feature.UserInteractionHandler
import mozilla.components.support.ktx.kotlin.isUrl
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
import mozilla.components.support.utils.Browsers
import mozilla.components.support.utils.SafeIntent
import mozilla.components.support.utils.toSafeIntent
import org.mozilla.fenix.browser.UriOpenedObserver
@@ -61,7 +60,6 @@ import org.mozilla.fenix.settings.SettingsFragmentDirections
import org.mozilla.fenix.settings.TrackingProtectionFragmentDirections
import org.mozilla.fenix.theme.DefaultThemeManager
import org.mozilla.fenix.theme.ThemeManager
import java.lang.ref.WeakReference

@SuppressWarnings("TooManyFunctions", "LargeClass")
open class HomeActivity : AppCompatActivity() {
@@ -118,8 +116,6 @@ open class HomeActivity : AppCompatActivity() {
    override fun onResume() {
        super.onResume()

        unsetOpenLinksInAPrivateTabIfNecessary()

        lifecycleScope.launch {
            with(components.backgroundServices) {
                // Make sure accountManager is initialized.
@@ -143,21 +139,6 @@ open class HomeActivity : AppCompatActivity() {
        hotStartMonitor.onPostResumeFinalMethodCall()
    }

    private fun unsetOpenLinksInAPrivateTabIfNecessary() {
        // Toggle off the open_link_in_private_tab pref if we are no longer set as the default browser
        // We do this on a separate thread to alleviate performance issues
        val weakReferenceContext = WeakReference(this)
        lifecycleScope.launch {
            val context = weakReferenceContext.get() ?: return@launch
            if (!Browsers.all(context).isDefaultBrowser) {
                context.settings().preferences
                    .edit()
                    .putBoolean(context.getString(R.string.pref_key_open_links_in_a_private_tab), false)
                    .apply()
            }
        }
    }

    /**
     * Handles intents received when the activity is open.
     */
+3 −11
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@ import android.os.Bundle
import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import mozilla.components.support.utils.Browsers
import org.mozilla.fenix.components.getType
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
@@ -36,17 +35,10 @@ class IntentReceiverActivity : Activity() {
    }

    suspend fun processIntent(intent: Intent) {
        if (!Browsers.all(this).isDefaultBrowser) {
            /* If the user has unset us as the default browser, unset openLinksInAPrivateTab */
            this.settings().openLinksInAPrivateTab = false
            components.analytics.metrics.track(Event.PreferenceToggled(
                preferenceKey = getString(R.string.pref_key_open_links_in_a_private_tab),
                enabled = false,
                context = applicationContext
            ))
        }
        val settings = settings()
        settings.unsetOpenLinksInAPrivateTabIfNecessary()

        val modeDependentProcessors = if (settings().openLinksInAPrivateTab) {
        val modeDependentProcessors = if (settings.openLinksInAPrivateTab) {
            components.analytics.metrics.track(Event.OpenedLink(Event.OpenedLink.Mode.PRIVATE))
            listOf(
                components.intentProcessors.privateCustomTabIntentProcessor,
+4 −1
Original line number Diff line number Diff line
@@ -51,9 +51,12 @@ class DefaultBrowserSettingsFragment : PreferenceFragmentCompat() {
        findPreference<DefaultBrowserPreference>(getPreferenceKey(R.string.pref_key_make_default_browser))
            ?.updateSwitch()

        val settings = context!!.settings()
        settings.unsetOpenLinksInAPrivateTabIfNecessary()

        findPreference<CheckBoxPreference>(getPreferenceKey(R.string.pref_key_open_links_in_a_private_tab))?.apply {
            isEnabled = Browsers.all(requireContext()).isDefaultBrowser
            isChecked = context.settings().openLinksInAPrivateTab
            isChecked = settings.openLinksInAPrivateTab
            onPreferenceChangeListener = SharedPreferenceUpdater()
        }
    }
+19 −0
Original line number Diff line number Diff line
@@ -19,12 +19,16 @@ import mozilla.components.support.ktx.android.content.floatPreference
import mozilla.components.support.ktx.android.content.intPreference
import mozilla.components.support.ktx.android.content.longPreference
import mozilla.components.support.ktx.android.content.stringPreference
import mozilla.components.support.utils.Browsers
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.Config
import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MozillaProductDetector
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.settings.PhoneFeature
import org.mozilla.fenix.settings.deletebrowsingdata.DeleteBrowsingDataOnQuitType
import java.security.InvalidParameterException
@@ -370,6 +374,21 @@ class Settings private constructor(
        ).apply()
    }

    fun unsetOpenLinksInAPrivateTabIfNecessary() {
        if (Browsers.all(appContext).isDefaultBrowser) {
            return
        }

        appContext.settings().openLinksInAPrivateTab = false
        appContext.components.analytics.metrics.track(
            Event.PreferenceToggled(
                preferenceKey = appContext.getString(R.string.pref_key_open_links_in_a_private_tab),
                enabled = false,
                context = appContext
            )
        )
    }

    private var showedPrivateModeContextualFeatureRecommender by booleanPreference(
        appContext.getPreferenceKey(R.string.pref_key_showed_private_mode_cfr),
        default = false
Loading