For #13229 - Cache deviceId for reuse
parent
1e806b25c1
commit
ef7cf4fdcf
|
@ -5,7 +5,9 @@
|
||||||
package org.mozilla.fenix.components.metrics
|
package org.mozilla.fenix.components.metrics
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.content.Context.MODE_PRIVATE
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.annotation.VisibleForTesting
|
||||||
import com.leanplum.Leanplum
|
import com.leanplum.Leanplum
|
||||||
import com.leanplum.LeanplumActivityHelper
|
import com.leanplum.LeanplumActivityHelper
|
||||||
import com.leanplum.annotations.Parser
|
import com.leanplum.annotations.Parser
|
||||||
|
@ -48,7 +50,10 @@ private val Event.name: String?
|
||||||
else -> null
|
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)
|
val scope = CoroutineScope(Dispatchers.IO)
|
||||||
var leanplumJob: Job? = null
|
var leanplumJob: Job? = null
|
||||||
|
|
||||||
|
@ -72,13 +77,27 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
|
||||||
override val type = MetricServiceType.Marketing
|
override val type = MetricServiceType.Marketing
|
||||||
private val token = Token(LeanplumId, LeanplumToken)
|
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() {
|
override fun start() {
|
||||||
|
|
||||||
if (!application.settings().isMarketingTelemetryEnabled) return
|
if (!application.settings().isMarketingTelemetryEnabled) return
|
||||||
|
|
||||||
Leanplum.setIsTestModeEnabled(false)
|
Leanplum.setIsTestModeEnabled(false)
|
||||||
Leanplum.setApplicationContext(application)
|
Leanplum.setApplicationContext(application)
|
||||||
Leanplum.setDeviceId(randomUUID().toString())
|
Leanplum.setDeviceId(deviceId)
|
||||||
Parser.parseVariables(application)
|
Parser.parseVariables(application)
|
||||||
|
|
||||||
leanplumJob = scope.launch {
|
leanplumJob = scope.launch {
|
||||||
|
@ -212,5 +231,8 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
|
||||||
"ara", // Arabic
|
"ara", // Arabic
|
||||||
"jpn" // Japanese
|
"jpn" // Japanese
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private val PREFERENCE_NAME = "LEANPLUM_PREFERENCES"
|
||||||
|
private 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…
Reference in New Issue