@@ -21,6 +21,7 @@ import android.util.Log
21
21
import androidx.datastore.preferences.preferencesDataStore
22
22
import com.google.firebase.installations.FirebaseInstallationsApi
23
23
import com.google.firebase.sessions.ApplicationInfo
24
+ import java.util.concurrent.atomic.AtomicBoolean
24
25
import kotlin.time.Duration
25
26
import kotlin.time.Duration.Companion.seconds
26
27
import kotlinx.coroutines.Dispatchers
@@ -39,7 +40,7 @@ internal class RemoteSettings(
39
40
) : SettingsProvider {
40
41
private val Context .dataStore by preferencesDataStore(name = dataStoreName)
41
42
private val settingsCache = SettingsCache (context.dataStore)
42
- private var fetchInProgress = false
43
+ private var fetchInProgress = AtomicBoolean ( false )
43
44
44
45
override val sessionEnabled: Boolean?
45
46
get() {
@@ -74,7 +75,7 @@ internal class RemoteSettings(
74
75
75
76
suspend private fun fetchConfigs () {
76
77
// Check if a fetch is in progress. If yes, return
77
- if (fetchInProgress) {
78
+ if (fetchInProgress.get() ) {
78
79
return
79
80
}
80
81
@@ -83,74 +84,76 @@ internal class RemoteSettings(
83
84
return
84
85
}
85
86
86
- fetchInProgress = true
87
+ fetchInProgress.set( true )
87
88
88
89
// Get the installations ID before making a remote config fetch
89
90
var installationId = firebaseInstallationsApi.id.await()
90
91
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?
130
119
}
131
- }
132
120
133
- sessionsEnabled?.let { settingsCache.updateSettingsEnabled(sessionsEnabled) }
121
+ if (aqsSettings.has(" sampling_rate" )) {
122
+ sessionSamplingRate = aqsSettings.get(" sampling_rate" ) as Double?
123
+ }
134
124
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
+ }
138
128
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
+ }
140
136
141
- cacheDuration?.let { settingsCache.updateSessionCacheDuration(cacheDuration) }
142
- ? : let { settingsCache.updateSessionCacheDuration(86400 ) }
137
+ sessionsEnabled?.let { settingsCache.updateSettingsEnabled(sessionsEnabled) }
143
138
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)
151
141
}
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
+ )
154
157
}
155
158
156
159
companion object {
0 commit comments