From f10bfb7e16a76987bfb632170b3f66599678889d Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Mon, 3 Aug 2020 10:13:10 -0700 Subject: [PATCH] For #13229 - Cache deviceId for reuse --- .../metrics/LeanplumMetricsService.kt | 26 +++++++++++- .../metrics/LeanplumMetricsServiceTest.kt | 42 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceTest.kt 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 41cddf7b4..db7d78b24 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,7 +5,9 @@ package org.mozilla.fenix.components.metrics import android.app.Application +import android.content.Context.MODE_PRIVATE import android.util.Log +import androidx.annotation.VisibleForTesting import com.leanplum.Leanplum import com.leanplum.LeanplumActivityHelper import com.leanplum.annotations.Parser @@ -44,7 +46,10 @@ private val Event.name: String? else -> "" } -class LeanplumMetricsService(private val application: Application) : MetricsService { +class LeanplumMetricsService( + private val application: Application, + private val deviceIdGenerator: () -> String = { randomUUID().toString() } +) : MetricsService { val scope = CoroutineScope(Dispatchers.IO) var leanplumJob: Job? = null @@ -68,13 +73,27 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ override val type = MetricServiceType.Marketing private val token = Token(LeanplumId, LeanplumToken) + private val preferences = 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 + } + override fun start() { if (!application.settings().isMarketingTelemetryEnabled) return Leanplum.setIsTestModeEnabled(false) Leanplum.setApplicationContext(application) - Leanplum.setDeviceId(randomUUID().toString()) + Leanplum.setDeviceId(deviceId) Parser.parseVariables(application) leanplumJob = scope.launch { @@ -197,5 +216,8 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ "ara", // Arabic "jpn" // Japanese ) + + private const val PREFERENCE_NAME = "LEANPLUM_PREFERENCES" + private const val DEVICE_ID_KEY = "LP_DEVICE_ID" } } 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 new file mode 100644 index 000000000..6027a100a --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceTest.kt @@ -0,0 +1,42 @@ +/* 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 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, idGenerator) + assertEquals("TEST_DEVICE_ID", leanplumMetricService.deviceId) + + val leanplumMetricService2 = LeanplumMetricsService(testContext.application, idGenerator) + assertEquals("TEST_DEVICE_ID", leanplumMetricService2.deviceId) + assertEquals(1, callCount) + + assertEquals("TEST_DEVICE_ID", sharedPreferences.getString("LP_DEVICE_ID", "")) + } +}