For #13229 - Cache deviceId for reuse

releases/v79.0.0
Jeff Boek 4 years ago committed by Sebastian Kaspari
parent ceae079a50
commit f10bfb7e16

@ -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"
}
}

@ -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", ""))
}
}
Loading…
Cancel
Save