From c5205b62367aecb024215a6854f3b49092c30adb Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Wed, 3 Apr 2019 11:59:08 -0700 Subject: [PATCH] For #945 - Disable leanplum when we stop telemetry --- .../java/org/mozilla/fenix/FenixApplication.kt | 4 +++- .../components/metrics/AdjustMetricsService.kt | 6 +++++- .../components/metrics/GleanMetricsService.kt | 4 ++++ .../components/metrics/LeanplumMetricsService.kt | 16 ++++++++++++++++ .../mozilla/fenix/components/metrics/Metrics.kt | 8 ++++++++ .../fenix/settings/DataChoicesFragment.kt | 13 +++++++++++++ 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index afc5fe689..b5e68f589 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -48,7 +48,9 @@ open class FenixApplication : Application() { setupLeakCanary() loadExperiments() - components.analytics.metrics.start() + if (Settings.getInstance(this).isTelemetryEnabled) { + components.analytics.metrics.start() + } } protected open fun setupLeakCanary() { diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/AdjustMetricsService.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/AdjustMetricsService.kt index 7c46d5f71..413a21086 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/AdjustMetricsService.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/AdjustMetricsService.kt @@ -35,10 +35,14 @@ class AdjustMetricsService(private val application: Application) : MetricsServic config.setLogLevel(LogLevel.SUPRESS) Adjust.onCreate(config) - + Adjust.setEnabled(true) application.registerActivityLifecycleCallbacks(AdjustLifecycleCallbacks()) } + override fun stop() { + Adjust.setEnabled(false) + } + // We're not currently sending events directly to Adjust override fun track(event: Event) { } override fun shouldTrack(event: Event): Boolean = false diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt index dce3f29da..aaf1a9d33 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt @@ -86,6 +86,10 @@ class GleanMetricsService(private val context: Context) : MetricsService { } } + override fun stop() { + Glean.setUploadEnabled(false) + } + override fun track(event: Event) { event.wrapper?.track(event) } 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 33267aeca..f989c96d5 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 @@ -8,6 +8,7 @@ import android.util.Log import com.leanplum.Leanplum import com.leanplum.LeanplumActivityHelper import com.leanplum.annotations.Parser +import com.leanplum.internal.LeanplumInternal import org.mozilla.fenix.BuildConfig import org.mozilla.fenix.utils.Settings @@ -79,6 +80,7 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ } } + Leanplum.setIsTestModeEnabled(false) Leanplum.setApplicationContext(application) Parser.parseVariables(application) @@ -86,6 +88,20 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ Leanplum.start(application) } + override fun stop() { + // As written in LeanPlum SDK documentation, "This prevents Leanplum from communicating with the server." + // as this "isTestMode" flag is checked before LeanPlum SDK does anything. + // Also has the benefit effect of blocking the display of already downloaded messages. + // The reverse of this - setIsTestModeEnabled(false) must be called before trying to init + // LP in the same session. + Leanplum.setIsTestModeEnabled(true) + + // This is just to allow restarting LP and it's functionality in the same app session + // as LP stores it's state internally and check against it + LeanplumInternal.setCalledStart(false) + LeanplumInternal.setHasStarted(false) + } + override fun track(event: Event) { event.name?.also { Leanplum.track(it, event.extras) diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt index db439e608..f0d78a796 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt @@ -117,6 +117,7 @@ private fun Fact.toEvent(): Event? = when (Pair(component, item)) { interface MetricsService { fun start() + fun stop() fun track(event: Event) fun shouldTrack(event: Event): Boolean } @@ -141,6 +142,13 @@ class Metrics(private val services: List, private val isTelemetr initialized = true } + fun stop() { + if (!initialized) { return } + + services.forEach { it.stop() } + initialized = false + } + fun track(event: Event) { if (BuildConfig.TELEMETRY && !isTelemetryEnabled.invoke() && !initialized) { return } diff --git a/app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt index da71d8a18..e66f77614 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt @@ -9,6 +9,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference import org.mozilla.fenix.R +import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.utils.Settings class DataChoicesFragment : PreferenceFragmentCompat() { @@ -17,6 +18,18 @@ class DataChoicesFragment : PreferenceFragmentCompat() { super.onCreate(savedInstanceState) (activity as AppCompatActivity).title = getString(R.string.preferences_data_choices) (activity as AppCompatActivity).supportActionBar?.show() + + preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener { sharedPreferences, key -> + when (key) { + getString(R.string.pref_key_telemetry) -> { + if (sharedPreferences.getBoolean(key, Settings.getInstance(requireContext()).isTelemetryEnabled)) { + requireComponents.analytics.metrics.start() + } else { + requireComponents.analytics.metrics.stop() + } + } + } + } } override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {