diff --git a/android/src/main/java/com/amplitude/android/Amplitude.kt b/android/src/main/java/com/amplitude/android/Amplitude.kt index 9a1dd920..75a54d8f 100644 --- a/android/src/main/java/com/amplitude/android/Amplitude.kt +++ b/android/src/main/java/com/amplitude/android/Amplitude.kt @@ -27,6 +27,11 @@ open class Amplitude( internal var inForeground = false private lateinit var androidContextPlugin: AndroidContextPlugin + val sessionId: Long + get() { + return (timeline as Timeline).sessionId + } + init { (timeline as Timeline).start() registerShutdownHook() diff --git a/android/src/main/java/com/amplitude/android/Timeline.kt b/android/src/main/java/com/amplitude/android/Timeline.kt index 4c44d142..a7b071d2 100644 --- a/android/src/main/java/com/amplitude/android/Timeline.kt +++ b/android/src/main/java/com/amplitude/android/Timeline.kt @@ -5,11 +5,17 @@ import com.amplitude.core.events.BaseEvent import com.amplitude.core.platform.Timeline import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.launch +import java.util.concurrent.atomic.AtomicLong class Timeline : Timeline() { private val eventMessageChannel: Channel = Channel(Channel.UNLIMITED) - var sessionId: Long = -1 - private set + + private val _sessionId = AtomicLong(-1) + val sessionId: Long + get() { + return _sessionId.get() + } + internal var lastEventId: Long = 0 var lastEventTime: Long = -1 @@ -17,7 +23,7 @@ class Timeline : Timeline() { amplitude.amplitudeScope.launch(amplitude.storageIODispatcher) { amplitude.isBuilt.await() - sessionId = amplitude.storage.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLong() ?: -1 + _sessionId.set(amplitude.storage.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLong() ?: -1) lastEventId = amplitude.storage.read(Storage.Constants.LAST_EVENT_ID)?.toLong() ?: 0 lastEventTime = amplitude.storage.read(Storage.Constants.LAST_EVENT_TIME)?.toLong() ?: -1 @@ -111,7 +117,7 @@ class Timeline : Timeline() { } private suspend fun setSessionId(timestamp: Long) { - sessionId = timestamp + _sessionId.set(timestamp) amplitude.storage.write(Storage.Constants.PREVIOUS_SESSION_ID, sessionId.toString()) } diff --git a/android/src/test/java/com/amplitude/android/AmplitudeTest.kt b/android/src/test/java/com/amplitude/android/AmplitudeTest.kt index a23ae4de..c7931a4c 100644 --- a/android/src/test/java/com/amplitude/android/AmplitudeTest.kt +++ b/android/src/test/java/com/amplitude/android/AmplitudeTest.kt @@ -313,6 +313,8 @@ class AmplitudeTest { Assertions.assertEquals(2200L, it.timestamp) Assertions.assertEquals(2000L, it.sessionId) } + + Assertions.assertEquals(2000L, amplitude.sessionId) } @Test @@ -332,6 +334,7 @@ class AmplitudeTest { Assertions.assertEquals(1000L, timeline1.sessionId) Assertions.assertEquals(1L, timeline1.lastEventId) Assertions.assertEquals(1000L, timeline1.lastEventTime) + Assertions.assertEquals(1000L, amplitude1.sessionId) val event1 = BaseEvent() event1.eventType = "test event 1" @@ -343,6 +346,7 @@ class AmplitudeTest { Assertions.assertEquals(1000L, timeline1.sessionId) Assertions.assertEquals(2L, timeline1.lastEventId) Assertions.assertEquals(1200L, timeline1.lastEventTime) + Assertions.assertEquals(1000L, amplitude1.sessionId) val amplitude2 = Amplitude(createConfiguration(100, InstanceStorageProvider(storage))) amplitude2.isBuilt.await() @@ -353,6 +357,7 @@ class AmplitudeTest { Assertions.assertEquals(1000L, timeline2.sessionId) Assertions.assertEquals(2L, timeline2.lastEventId) Assertions.assertEquals(1200L, timeline2.lastEventTime) + Assertions.assertEquals(1000L, amplitude2.sessionId) val amplitude3 = Amplitude(createConfiguration(100, InstanceStorageProvider(storage))) amplitude3.isBuilt.await() @@ -363,6 +368,7 @@ class AmplitudeTest { Assertions.assertEquals(1000L, timeline3.sessionId) Assertions.assertEquals(2L, timeline3.lastEventId) Assertions.assertEquals(1200L, timeline3.lastEventTime) + Assertions.assertEquals(1000L, amplitude3.sessionId) amplitude3.onEnterForeground(1400) advanceUntilIdle() @@ -371,6 +377,7 @@ class AmplitudeTest { Assertions.assertEquals(1400L, timeline3.sessionId) Assertions.assertEquals(4L, timeline3.lastEventId) Assertions.assertEquals(1400L, timeline3.lastEventTime) + Assertions.assertEquals(1400L, amplitude3.sessionId) } @Test