From 8e7d6adfe5a3058c3e3c668c352940e4671bfe69 Mon Sep 17 00:00:00 2001 From: Justin Fiedler Date: Fri, 1 Mar 2024 10:37:30 -0800 Subject: [PATCH] feat: allow setting sessionId in initial configuration --- .../main/java/com/amplitude/android/Amplitude.kt | 2 +- .../java/com/amplitude/android/Configuration.kt | 2 ++ .../main/java/com/amplitude/android/Timeline.kt | 15 +++++++++++---- .../java/com/amplitude/android/AmplitudeTest.kt | 14 ++++++++++++++ .../main/java/com/amplitude/core/Configuration.kt | 1 + 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/amplitude/android/Amplitude.kt b/android/src/main/java/com/amplitude/android/Amplitude.kt index 6e07e626..f3df2cee 100644 --- a/android/src/main/java/com/amplitude/android/Amplitude.kt +++ b/android/src/main/java/com/amplitude/android/Amplitude.kt @@ -33,7 +33,7 @@ open class Amplitude( } override fun createTimeline(): Timeline { - return Timeline().also { it.amplitude = this } + return Timeline(configuration.sessionId).also { it.amplitude = this } } override fun createIdentityConfiguration(): IdentityConfiguration { diff --git a/android/src/main/java/com/amplitude/android/Configuration.kt b/android/src/main/java/com/amplitude/android/Configuration.kt index 81efdbb4..dccdf6fe 100644 --- a/android/src/main/java/com/amplitude/android/Configuration.kt +++ b/android/src/main/java/com/amplitude/android/Configuration.kt @@ -48,6 +48,7 @@ open class Configuration @JvmOverloads constructor( var migrateLegacyData: Boolean = true, override var offline: Boolean? = false, override var deviceId: String? = null, + override var sessionId: Long? = null, ) : Configuration( apiKey, flushQueueSize, @@ -70,6 +71,7 @@ open class Configuration @JvmOverloads constructor( identityStorageProvider, offline, deviceId, + sessionId, ) { companion object { const val MIN_TIME_BETWEEN_SESSIONS_MILLIS: Long = 300000 diff --git a/android/src/main/java/com/amplitude/android/Timeline.kt b/android/src/main/java/com/amplitude/android/Timeline.kt index 6da6cc5f..900220d8 100644 --- a/android/src/main/java/com/amplitude/android/Timeline.kt +++ b/android/src/main/java/com/amplitude/android/Timeline.kt @@ -7,24 +7,31 @@ import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.launch import java.util.concurrent.atomic.AtomicLong -class Timeline : Timeline() { +class Timeline( + private val initialSessionId: Long? = null, +) : Timeline() { private val eventMessageChannel: Channel = Channel(Channel.UNLIMITED) - private val _sessionId = AtomicLong(-1) + private val _sessionId = AtomicLong(initialSessionId ?: -1L) val sessionId: Long get() { return _sessionId.get() } internal var lastEventId: Long = 0 - var lastEventTime: Long = -1 + var lastEventTime: Long = -1L internal fun start() { amplitude.amplitudeScope.launch(amplitude.storageIODispatcher) { // Wait until build (including possible legacy data migration) is finished. amplitude.isBuilt.await() - _sessionId.set(amplitude.storage.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLongOrNull() ?: -1) + if (initialSessionId == null) { + _sessionId.set( + amplitude.storage.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLongOrNull() + ?: -1 + ) + } lastEventId = amplitude.storage.read(Storage.Constants.LAST_EVENT_ID)?.toLongOrNull() ?: 0 lastEventTime = amplitude.storage.read(Storage.Constants.LAST_EVENT_TIME)?.toLongOrNull() ?: -1 diff --git a/android/src/test/java/com/amplitude/android/AmplitudeTest.kt b/android/src/test/java/com/amplitude/android/AmplitudeTest.kt index d757561c..bb4de5ab 100644 --- a/android/src/test/java/com/amplitude/android/AmplitudeTest.kt +++ b/android/src/test/java/com/amplitude/android/AmplitudeTest.kt @@ -78,6 +78,7 @@ class AmplitudeTest { minTimeBetweenSessionsMillis: Long? = null, storageProvider: StorageProvider = InMemoryStorageProvider(), deviceId: String? = null, + sessionId: Long? = null, ): Configuration { val configuration = Configuration( apiKey = "api-key", @@ -88,6 +89,7 @@ class AmplitudeTest { loggerProvider = ConsoleLoggerProvider(), identifyInterceptStorageProvider = InMemoryStorageProvider(), identityStorageProvider = IMIdentityStorageProvider(), + sessionId = sessionId, ) if (deviceId != null) { @@ -217,6 +219,18 @@ class AmplitudeTest { } } + @Test + fun amplitude_should_set_sessionId_from_configuration() = runTest { + val testSessionId = 1337L + // set device Id in the config + amplitude = Amplitude(createConfiguration(sessionId = testSessionId)) + setDispatcher(testScheduler) + + if (amplitude?.isBuilt!!.await()) { + Assertions.assertEquals(testSessionId, amplitude?.sessionId) + } + } + companion object { const val instanceName = "testInstance" } diff --git a/core/src/main/java/com/amplitude/core/Configuration.kt b/core/src/main/java/com/amplitude/core/Configuration.kt index b0ba2ddf..54f24f6b 100644 --- a/core/src/main/java/com/amplitude/core/Configuration.kt +++ b/core/src/main/java/com/amplitude/core/Configuration.kt @@ -32,6 +32,7 @@ open class Configuration @JvmOverloads constructor( open var identityStorageProvider: IdentityStorageProvider = IMIdentityStorageProvider(), open var offline: Boolean? = false, open var deviceId: String? = null, + open var sessionId: Long? = null, ) { companion object {