Skip to content

Commit

Permalink
fix: return 'sessionId' property to Amplitude class (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy authored Feb 14, 2023
1 parent ae53c13 commit 7a938a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 10 additions & 4 deletions android/src/main/java/com/amplitude/android/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ 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<EventQueueMessage> = 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

internal fun start() {
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

Expand Down Expand Up @@ -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())
}

Expand Down
7 changes: 7 additions & 0 deletions android/src/test/java/com/amplitude/android/AmplitudeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ class AmplitudeTest {
Assertions.assertEquals(2200L, it.timestamp)
Assertions.assertEquals(2000L, it.sessionId)
}

Assertions.assertEquals(2000L, amplitude.sessionId)
}

@Test
Expand All @@ -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"
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 7a938a1

Please sign in to comment.