GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Commit 8827739e authored by Jeff Boek's avatar Jeff Boek Committed by Sebastian Kaspari

For #13229 - Cache deviceId for reuse

parent ae031b7b
......@@ -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
......@@ -50,7 +52,10 @@ private val Event.name: String?
else -> null
}
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
......@@ -74,13 +79,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 {
......@@ -220,5 +239,8 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
"ara", // Arabic
"jpn" // Japanese
)
private val PREFERENCE_NAME = "LEANPLUM_PREFERENCES"
private val DEVICE_ID_KEY = "LP_DEVICE_ID"
}
}
/* 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", ""))
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment