@@ -18,8 +18,11 @@ package com.google.firebase.sessions
18
18
19
19
import android.content.Context
20
20
import android.util.Log
21
+ import androidx.datastore.core.DataMigration
21
22
import androidx.datastore.core.DataStore
23
+ import androidx.datastore.core.DataStoreFactory
22
24
import androidx.datastore.core.MultiProcessDataStoreFactory
25
+ import androidx.datastore.core.Serializer
23
26
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
24
27
import androidx.datastore.dataStoreFile
25
28
import com.google.android.datatransport.TransportFactory
@@ -43,6 +46,7 @@ import dagger.BindsInstance
43
46
import dagger.Component
44
47
import dagger.Module
45
48
import dagger.Provides
49
+ import java.io.File
46
50
import javax.inject.Qualifier
47
51
import javax.inject.Singleton
48
52
import kotlin.coroutines.CoroutineContext
@@ -137,7 +141,7 @@ internal interface FirebaseSessionsComponent {
137
141
appContext : Context ,
138
142
@Blocking blockingDispatcher : CoroutineContext ,
139
143
): DataStore <SessionConfigs > =
140
- MultiProcessDataStoreFactory .create (
144
+ createDataStore (
141
145
serializer = SessionConfigsSerializer ,
142
146
corruptionHandler =
143
147
ReplaceFileCorruptionHandler { ex ->
@@ -154,7 +158,7 @@ internal interface FirebaseSessionsComponent {
154
158
appContext : Context ,
155
159
@Blocking blockingDispatcher : CoroutineContext ,
156
160
): DataStore <SessionData > =
157
- MultiProcessDataStoreFactory .create (
161
+ createDataStore (
158
162
serializer = SessionDataSerializer ,
159
163
corruptionHandler =
160
164
ReplaceFileCorruptionHandler { ex ->
@@ -164,6 +168,37 @@ internal interface FirebaseSessionsComponent {
164
168
scope = CoroutineScope (blockingDispatcher),
165
169
produceFile = { appContext.dataStoreFile(" aqs/sessionDataStore.data" ) },
166
170
)
171
+
172
+ private fun <T > createDataStore (
173
+ serializer : Serializer <T >,
174
+ corruptionHandler : ReplaceFileCorruptionHandler <T >,
175
+ migrations : List <DataMigration <T >> = listOf(),
176
+ scope : CoroutineScope ,
177
+ produceFile : () -> File ,
178
+ ): DataStore <T > =
179
+ if (loadDataStoreSharedCounter()) {
180
+ MultiProcessDataStoreFactory .create(
181
+ serializer,
182
+ corruptionHandler,
183
+ migrations,
184
+ scope,
185
+ produceFile,
186
+ )
187
+ } else {
188
+ DataStoreFactory .create(serializer, corruptionHandler, migrations, scope, produceFile)
189
+ }
190
+
191
+ /* * This native library in unavailable in some conditions, for example, Robolectric tests */
192
+ // TODO(mrober): Remove this when b/392626815 is resolved
193
+ private fun loadDataStoreSharedCounter (): Boolean =
194
+ try {
195
+ System .loadLibrary(" datastore_shared_counter" )
196
+ true
197
+ } catch (_: UnsatisfiedLinkError ) {
198
+ false
199
+ } catch (_: SecurityException ) {
200
+ false
201
+ }
167
202
}
168
203
}
169
204
}
0 commit comments