From 13f73c2d2c55537b85af30d645b440020955a107 Mon Sep 17 00:00:00 2001 From: Emma Malysz Date: Mon, 30 Dec 2019 14:42:13 -0800 Subject: [PATCH] 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 --- app/metrics.yaml | 2 +- .../java/org/mozilla/fenix/HomeActivity.kt | 19 ------------------- .../mozilla/fenix/IntentReceiverActivity.kt | 14 +++----------- .../DefaultBrowserSettingsFragment.kt | 5 ++++- .../java/org/mozilla/fenix/utils/Settings.kt | 19 +++++++++++++++++++ docs/metrics.md | 2 +- 6 files changed, 28 insertions(+), 33 deletions(-) diff --git a/app/metrics.yaml b/app/metrics.yaml index f42d4d1c6..6625a2393 100644 --- a/app/metrics.yaml +++ b/app/metrics.yaml @@ -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: diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index 941fae102..b611010ed 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -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. */ diff --git a/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt b/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt index 71f7c8a67..ab9f0608c 100644 --- a/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt @@ -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, diff --git a/app/src/main/java/org/mozilla/fenix/settings/DefaultBrowserSettingsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/DefaultBrowserSettingsFragment.kt index 993f535f1..43282efd1 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/DefaultBrowserSettingsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/DefaultBrowserSettingsFragment.kt @@ -51,9 +51,12 @@ class DefaultBrowserSettingsFragment : PreferenceFragmentCompat() { findPreference(getPreferenceKey(R.string.pref_key_make_default_browser)) ?.updateSwitch() + val settings = context!!.settings() + settings.unsetOpenLinksInAPrivateTabIfNecessary() + findPreference(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() } } diff --git a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt index 573af72c0..c44b6d9dd 100644 --- a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt +++ b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt @@ -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 diff --git a/docs/metrics.md b/docs/metrics.md index 2c8f4043e..6038ed88d 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -84,7 +84,7 @@ The following metrics are added to the ping: | events.entered_url |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user entered a url |[1](https://github.com/mozilla-mobile/fenix/pull/1067#issuecomment-474598673)|
  • autocomplete: A boolean that tells us whether the URL was autofilled by an Autocomplete suggestion
|2020-03-01 | | events.opened_link |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user opened a link with Fenix |[1](https://github.com/mozilla-mobile/fenix/pull/5975)|
  • mode: The mode the link was opened in. Either 'PRIVATE' or 'NORMAL'
|2020-03-01 | | events.performed_search |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user performed a search |[1](https://github.com/mozilla-mobile/fenix/pull/1067#issuecomment-474598673), [2](https://github.com/mozilla-mobile/fenix/pull/1677)|
  • source: A string that tells us how the user performed the search. Possible values are: * default.action * default.suggestion * shortcut.action * shortcut.suggestion
|2020-03-01 | -| events.preference_toggled |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user toggled a boolean preference in settings |[1](https://github.com/mozilla-mobile/fenix/pull/1896), [2](https://github.com/mozilla-mobile/fenix/pull/5704), [3](https://github.com/mozilla-mobile/fenix/pull/5886), [4](https://github.com/mozilla-mobile/fenix/pull/5975), [5](https://github.com/mozilla-mobile/fenix/pull/6352), [6](https://github.com/mozilla-mobile/fenix/pull/6601), [7](https://github.com/mozilla-mobile/fenix/pull/6746)|
  • enabled: Whether or not the preference is *now* enabled
  • preference_key: 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, pref_key_sync_logins, pref_key_sync_bookmarks, pref_key_sync_history and pref_key_show_search_suggestions_in_private
|2020-03-01 | +| events.preference_toggled |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user toggled a boolean preference in settings |[1](https://github.com/mozilla-mobile/fenix/pull/1896), [2](https://github.com/mozilla-mobile/fenix/pull/5704), [3](https://github.com/mozilla-mobile/fenix/pull/5886), [4](https://github.com/mozilla-mobile/fenix/pull/5975), [5](https://github.com/mozilla-mobile/fenix/pull/6352), [6](https://github.com/mozilla-mobile/fenix/pull/6601), [7](https://github.com/mozilla-mobile/fenix/pull/6746)|
  • enabled: Whether or not the preference is *now* enabled
  • preference_key: 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 (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
|2020-03-01 | | events.search_bar_tapped |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user tapped the search bar |[1](https://github.com/mozilla-mobile/fenix/pull/1067#issuecomment-474598673)|
  • source: The view the user was on when they initiated the search (For example: `Home` or `Browser`)
|2020-03-01 | | events.whats_new_tapped |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user opened the "what's new" page button |[1](https://github.com/mozilla-mobile/fenix/pull/5090)|
  • source: The location from which the user selected the what's new button. Either 'about' or 'home'
|2020-03-01 | | find_in_page.closed |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user closed the find in page UI |[1](https://github.com/mozilla-mobile/fenix/pull/1344#issuecomment-479285010)||2020-03-01 |