Skip to content

Commit 8c12472

Browse files
committed
Address comments.
1 parent c87f38b commit 8c12472

File tree

1 file changed

+62
-59
lines changed
  • firebase-sessions/src/main/kotlin/com/google/firebase/sessions/settings

1 file changed

+62
-59
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/settings/RemoteSettings.kt

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import android.util.Log
2121
import androidx.datastore.preferences.preferencesDataStore
2222
import com.google.firebase.installations.FirebaseInstallationsApi
2323
import com.google.firebase.sessions.ApplicationInfo
24+
import java.util.concurrent.atomic.AtomicBoolean
2425
import kotlin.time.Duration
2526
import kotlin.time.Duration.Companion.seconds
2627
import kotlinx.coroutines.Dispatchers
@@ -39,7 +40,7 @@ internal class RemoteSettings(
3940
) : SettingsProvider {
4041
private val Context.dataStore by preferencesDataStore(name = dataStoreName)
4142
private val settingsCache = SettingsCache(context.dataStore)
42-
private var fetchInProgress = false
43+
private var fetchInProgress = AtomicBoolean(false)
4344

4445
override val sessionEnabled: Boolean?
4546
get() {
@@ -74,7 +75,7 @@ internal class RemoteSettings(
7475

7576
suspend private fun fetchConfigs() {
7677
// Check if a fetch is in progress. If yes, return
77-
if (fetchInProgress) {
78+
if (fetchInProgress.get()) {
7879
return
7980
}
8081

@@ -83,74 +84,76 @@ internal class RemoteSettings(
8384
return
8485
}
8586

86-
fetchInProgress = true
87+
fetchInProgress.set(true)
8788

8889
// Get the installations ID before making a remote config fetch
8990
var installationId = firebaseInstallationsApi.id.await()
9091
if (installationId == null) {
91-
fetchInProgress = false
92-
} else {
93-
val options =
94-
mapOf(
95-
"X-Crashlytics-Installation-ID" to installationId as String,
96-
"X-Crashlytics-Device-Model" to appInfo.deviceModel,
97-
// TODO(visum) Add OS version parameters
98-
// "X-Crashlytics-OS-Build-Version" to "",
99-
// "X-Crashlytics-OS-Display-Version" to "",
100-
"X-Crashlytics-API-Client-Version" to appInfo.sessionSdkVersion
101-
)
102-
103-
configsFetcher.doConfigFetch(
104-
headerOptions = options,
105-
onSuccess = {
106-
var sessionsEnabled: Boolean? = null
107-
var sessionSamplingRate: Double? = null
108-
var sessionTimeoutSeconds: Int? = null
109-
var cacheDuration: Int? = null
110-
if (it.has("app_quality")) {
111-
val aqsSettings = it.get("app_quality") as JSONObject
112-
try {
113-
if (aqsSettings.has("sessions_enabled")) {
114-
sessionsEnabled = aqsSettings.get("sessions_enabled") as Boolean?
115-
}
116-
117-
if (aqsSettings.has("sampling_rate")) {
118-
sessionSamplingRate = aqsSettings.get("sampling_rate") as Double?
119-
}
120-
121-
if (aqsSettings.has("session_timeout_seconds")) {
122-
sessionTimeoutSeconds = aqsSettings.get("session_timeout_seconds") as Int?
123-
}
124-
125-
if (aqsSettings.has("cache_duration")) {
126-
cacheDuration = aqsSettings.get("cache_duration") as Int?
127-
}
128-
} catch (exception: JSONException) {
129-
Log.e(TAG, "Error parsing the configs remotely fetched: ", exception)
92+
fetchInProgress.set(false)
93+
return
94+
}
95+
96+
// All the required fields are available, start making a network request.
97+
val options =
98+
mapOf(
99+
"X-Crashlytics-Installation-ID" to installationId as String,
100+
"X-Crashlytics-Device-Model" to appInfo.deviceModel,
101+
// TODO(visum) Add OS version parameters
102+
// "X-Crashlytics-OS-Build-Version" to "",
103+
// "X-Crashlytics-OS-Display-Version" to "",
104+
"X-Crashlytics-API-Client-Version" to appInfo.sessionSdkVersion
105+
)
106+
107+
configsFetcher.doConfigFetch(
108+
headerOptions = options,
109+
onSuccess = {
110+
var sessionsEnabled: Boolean? = null
111+
var sessionSamplingRate: Double? = null
112+
var sessionTimeoutSeconds: Int? = null
113+
var cacheDuration: Int? = null
114+
if (it.has("app_quality")) {
115+
val aqsSettings = it.get("app_quality") as JSONObject
116+
try {
117+
if (aqsSettings.has("sessions_enabled")) {
118+
sessionsEnabled = aqsSettings.get("sessions_enabled") as Boolean?
130119
}
131-
}
132120

133-
sessionsEnabled?.let { settingsCache.updateSettingsEnabled(sessionsEnabled) }
121+
if (aqsSettings.has("sampling_rate")) {
122+
sessionSamplingRate = aqsSettings.get("sampling_rate") as Double?
123+
}
134124

135-
sessionTimeoutSeconds?.let {
136-
settingsCache.updateSessionRestartTimeout(sessionTimeoutSeconds)
137-
}
125+
if (aqsSettings.has("session_timeout_seconds")) {
126+
sessionTimeoutSeconds = aqsSettings.get("session_timeout_seconds") as Int?
127+
}
138128

139-
sessionSamplingRate?.let { settingsCache.updateSamplingRate(sessionSamplingRate) }
129+
if (aqsSettings.has("cache_duration")) {
130+
cacheDuration = aqsSettings.get("cache_duration") as Int?
131+
}
132+
} catch (exception: JSONException) {
133+
Log.e(TAG, "Error parsing the configs remotely fetched: ", exception)
134+
}
135+
}
140136

141-
cacheDuration?.let { settingsCache.updateSessionCacheDuration(cacheDuration) }
142-
?: let { settingsCache.updateSessionCacheDuration(86400) }
137+
sessionsEnabled?.let { settingsCache.updateSettingsEnabled(sessionsEnabled) }
143138

144-
settingsCache.updateSessionCacheUpdatedTime(System.currentTimeMillis())
145-
fetchInProgress = false
146-
},
147-
onFailure = {
148-
// Network request failed here.
149-
Log.e(TAG, "Error failing to fetch the remote configs")
150-
fetchInProgress = false
139+
sessionTimeoutSeconds?.let {
140+
settingsCache.updateSessionRestartTimeout(sessionTimeoutSeconds)
151141
}
152-
)
153-
}
142+
143+
sessionSamplingRate?.let { settingsCache.updateSamplingRate(sessionSamplingRate) }
144+
145+
cacheDuration?.let { settingsCache.updateSessionCacheDuration(cacheDuration) }
146+
?: let { settingsCache.updateSessionCacheDuration(86400) }
147+
148+
settingsCache.updateSessionCacheUpdatedTime(System.currentTimeMillis())
149+
fetchInProgress.set(false)
150+
},
151+
onFailure = {
152+
// Network request failed here.
153+
Log.e(TAG, "Error failing to fetch the remote configs")
154+
fetchInProgress.set(false)
155+
}
156+
)
154157
}
155158

156159
companion object {

0 commit comments

Comments
 (0)