diff --git a/app/src/main/java/org/mozilla/fenix/components/Analytics.kt b/app/src/main/java/org/mozilla/fenix/components/Analytics.kt index 9f9bd201b..120a27ac1 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Analytics.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Analytics.kt @@ -18,7 +18,6 @@ import org.mozilla.fenix.Config import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R import org.mozilla.fenix.ReleaseChannel -import org.mozilla.fenix.StrictModeManager import org.mozilla.fenix.components.metrics.AdjustMetricsService import org.mozilla.fenix.components.metrics.GleanMetricsService import org.mozilla.fenix.components.metrics.LeanplumMetricsService @@ -35,8 +34,7 @@ import org.mozilla.geckoview.BuildConfig.MOZ_UPDATE_CHANNEL */ @Mockable class Analytics( - private val context: Context, - strictMode: StrictModeManager + private val context: Context ) { val crashReporter: CrashReporter by lazy { val services = mutableListOf() @@ -86,10 +84,7 @@ class Analytics( ) } - val leanplumMetricsService by lazy { LeanplumMetricsService( - context as Application, - strictMode - ) } + val leanplumMetricsService by lazy { LeanplumMetricsService(context as Application) } val metrics: MetricController by lazy { MetricController.create( diff --git a/app/src/main/java/org/mozilla/fenix/components/Components.kt b/app/src/main/java/org/mozilla/fenix/components/Components.kt index 92b9d97d0..cbd96e20e 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Components.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Components.kt @@ -125,7 +125,7 @@ class Components(private val context: Context) { AddonManager(core.store, core.engine, addonCollectionProvider, addonUpdater) } - val analytics by lazy { Analytics(context, strictMode) } + val analytics by lazy { Analytics(context) } val publicSuffixList by lazy { PublicSuffixList(context) } val clipboardHandler by lazy { ClipboardHandler(context) } val migrationStore by lazy { MigrationStore() } 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 fab3b956c..79526cd92 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 @@ -5,11 +5,8 @@ package org.mozilla.fenix.components.metrics import android.app.Application -import android.content.Context.MODE_PRIVATE import android.net.Uri -import android.os.StrictMode import android.util.Log -import androidx.annotation.VisibleForTesting import com.leanplum.Leanplum import com.leanplum.LeanplumActivityHelper import com.leanplum.annotations.Parser @@ -22,7 +19,6 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import mozilla.components.support.locale.LocaleManager import org.mozilla.fenix.BuildConfig -import org.mozilla.fenix.StrictModeManager import org.mozilla.fenix.components.metrics.MozillaProductDetector.MozillaProducts import org.mozilla.fenix.ext.settings import org.mozilla.fenix.home.intent.DeepLinkIntentProcessor @@ -57,9 +53,7 @@ private val Event.name: String? } class LeanplumMetricsService( - private val application: Application, - strictMode: StrictModeManager, - private val deviceIdGenerator: () -> String = { randomUUID().toString() } + private val application: Application ) : MetricsService, DeepLinkIntentProcessor.DeepLinkVerifier { val scope = CoroutineScope(Dispatchers.IO) var leanplumJob: Job? = null @@ -84,32 +78,14 @@ class LeanplumMetricsService( override val type = MetricServiceType.Marketing private val token = Token(LeanplumId, LeanplumToken) - private val preferences = strictMode.resetAfter(StrictMode.allowThreadDiskReads()) { - application.getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE) - } - - @VisibleForTesting - internal val deviceId by lazy { - var deviceId = preferences.getString(DEVICE_ID_KEY, null) - - if (deviceId == null) { - deviceId = deviceIdGenerator.invoke() - preferences.edit().putString(DEVICE_ID_KEY, deviceId).apply() - } - - deviceId - } - @Suppress("ComplexMethod") override fun start() { if (!application.settings().isMarketingTelemetryEnabled) return - Log.i(LOGTAG, "Starting Leanplum with device id: $deviceId") - Leanplum.setIsTestModeEnabled(false) Leanplum.setApplicationContext(application) - Leanplum.setDeviceId(deviceId) + Leanplum.setDeviceId(randomUUID().toString()) Parser.parseVariables(application) leanplumJob = scope.launch { @@ -189,10 +165,7 @@ class LeanplumMetricsService( // We compare the local Leanplum device ID against the "uid" query parameter and only // accept deep links where both values match. val uid = deepLink.getQueryParameter("uid") - if (uid != deviceId) { - Log.i(LOGTAG, "Rejecting Leanplum deep link because uid $uid does not match $deviceId") - } - return uid == deviceId + return uid == Leanplum.getDeviceId() } override fun stop() { diff --git a/app/src/test/java/org/mozilla/fenix/components/TestComponents.kt b/app/src/test/java/org/mozilla/fenix/components/TestComponents.kt index a2d1f92c5..1d31eaf56 100644 --- a/app/src/test/java/org/mozilla/fenix/components/TestComponents.kt +++ b/app/src/test/java/org/mozilla/fenix/components/TestComponents.kt @@ -28,7 +28,7 @@ class TestComponents(private val context: Context) : Components(context) { ) } override val intentProcessors by lazy { mockk(relaxed = true) } - override val analytics by lazy { Analytics(context, strictMode) } + override val analytics by lazy { Analytics(context) } override val clipboardHandler by lazy { ClipboardHandler(context) } diff --git a/app/src/test/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceTest.kt b/app/src/test/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceTest.kt deleted file mode 100644 index ea0e90302..000000000 --- a/app/src/test/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceTest.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.components.metrics - -import android.content.Context.MODE_PRIVATE -import io.mockk.mockk -import mozilla.components.support.test.robolectric.testContext -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNull -import org.junit.Test -import org.junit.runner.RunWith -import org.mozilla.fenix.ext.application -import org.mozilla.fenix.helpers.FenixRobolectricTestRunner - -@RunWith(FenixRobolectricTestRunner::class) -class LeanplumMetricsServiceTest { - @Test - fun `deviceId is only generated on first run`() { - var callCount = 0 - val idGenerator = { - callCount++ - "TEST_DEVICE_ID" - } - - val sharedPreferences = testContext.application.getSharedPreferences( - "LEANPLUM_PREFERENCES", - MODE_PRIVATE - ) - - assertNull(sharedPreferences.getString("LP_DEVICE_ID", null)) - - val leanplumMetricService = LeanplumMetricsService( - testContext.application, - mockk(relaxed = true), - idGenerator - ) - assertEquals("TEST_DEVICE_ID", leanplumMetricService.deviceId) - - val leanplumMetricService2 = LeanplumMetricsService( - testContext.application, - mockk(relaxed = true), - idGenerator - ) - assertEquals("TEST_DEVICE_ID", leanplumMetricService2.deviceId) - assertEquals(1, callCount) - - assertEquals("TEST_DEVICE_ID", sharedPreferences.getString("LP_DEVICE_ID", "")) - } -}