Skip to content

Commit 80a6a3f

Browse files
committed
fallback mode
1 parent 9eb1210 commit 80a6a3f

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SharedSessionRepository.kt

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,34 @@ constructor(
4949
) : SharedSessionRepository {
5050
/** Local copy of the session data. Can get out of sync, must be double-checked in datastore. */
5151
private lateinit var localSessionData: SessionData
52+
private var shouldFallback: Boolean = false
5253

5354
init {
54-
CoroutineScope(backgroundDispatcher).launch {
55-
sessionDataStore.data.collect { sessionData ->
56-
localSessionData = sessionData
57-
val sessionId = sessionData.sessionDetails.sessionId
55+
if (!sessionsSettings.sessionsEnabled) {
56+
shouldFallback = true
57+
} else {
58+
CoroutineScope(backgroundDispatcher).launch {
59+
sessionDataStore.data.collect { sessionData ->
60+
localSessionData = sessionData
61+
val sessionId = sessionData.sessionDetails.sessionId
5862

59-
FirebaseSessionsDependencies.getRegisteredSubscribers().values.forEach { subscriber ->
60-
// Notify subscribers, regardless of sampling and data collection state
61-
subscriber.onSessionChanged(SessionSubscriber.SessionDetails(sessionId))
62-
Log.d(TAG, "Notified ${subscriber.sessionSubscriberName} of new session $sessionId")
63+
FirebaseSessionsDependencies.getRegisteredSubscribers().values.forEach { subscriber ->
64+
// Notify subscribers, regardless of sampling and data collection state
65+
subscriber.onSessionChanged(SessionSubscriber.SessionDetails(sessionId))
66+
Log.d(TAG, "Notified ${subscriber.sessionSubscriberName} of new session $sessionId")
67+
}
6368
}
6469
}
6570
}
6671
}
6772

6873
override fun appBackground() {
74+
// Fallback mode, not using datastore, pre multi-process support stage
75+
if (shouldFallback) {
76+
localSessionData.copy(backgroundTime = timeProvider.currentTime())
77+
return
78+
}
79+
6980
if (!::localSessionData.isInitialized) {
7081
Log.d(TAG, "App backgrounded, but local SessionData not initialized")
7182
return
@@ -82,6 +93,27 @@ constructor(
8293
}
8394

8495
override fun appForeground() {
96+
// Fallback mode, not using datastore, pre multi-process support stage
97+
if (shouldFallback) {
98+
if (shouldInitiateNewSession(localSessionData)) {
99+
val newSessionDetails = sessionGenerator.generateNewSession(localSessionData.sessionDetails)
100+
localSessionData.copy(sessionDetails = newSessionDetails)
101+
CoroutineScope(backgroundDispatcher).launch {
102+
// Only notify subscriber for session change, not send to event to firelog
103+
FirebaseSessionsDependencies.getRegisteredSubscribers().values.forEach { subscriber ->
104+
subscriber.onSessionChanged(
105+
SessionSubscriber.SessionDetails(newSessionDetails.sessionId)
106+
)
107+
Log.d(
108+
TAG,
109+
"Notified ${subscriber.sessionSubscriberName} of new session $newSessionDetails.sessionId with fallback mode"
110+
)
111+
}
112+
}
113+
}
114+
return
115+
}
116+
85117
if (!::localSessionData.isInitialized) {
86118
Log.d(TAG, "App foregrounded, but local SessionData not initialized")
87119
return

0 commit comments

Comments
 (0)