17
17
package com.google.firebase.sessions
18
18
19
19
import android.content.Context
20
+ import android.os.Build
20
21
import android.util.Log
21
22
import androidx.datastore.core.DataMigration
22
23
import androidx.datastore.core.DataStore
@@ -48,6 +49,8 @@ import dagger.Component
48
49
import dagger.Module
49
50
import dagger.Provides
50
51
import java.io.File
52
+ import java.io.IOException
53
+ import java.nio.file.Files
51
54
import javax.inject.Qualifier
52
55
import javax.inject.Singleton
53
56
import kotlin.coroutines.CoroutineContext
@@ -146,7 +149,11 @@ internal interface FirebaseSessionsComponent {
146
149
SessionConfigsSerializer .defaultValue
147
150
},
148
151
scope = CoroutineScope (blockingDispatcher),
149
- produceFile = { appContext.dataStoreFile(" aqs/sessionConfigsDataStore.data" ) },
152
+ produceFile = {
153
+ prepDataStoreFile(
154
+ appContext.dataStoreFile(" firebaseSessions/sessionConfigsDataStore.data" )
155
+ )
156
+ },
150
157
)
151
158
152
159
@Provides
@@ -164,7 +171,9 @@ internal interface FirebaseSessionsComponent {
164
171
sessionDataSerializer.defaultValue
165
172
},
166
173
scope = CoroutineScope (blockingDispatcher),
167
- produceFile = { appContext.dataStoreFile(" aqs/sessionDataStore.data" ) },
174
+ produceFile = {
175
+ prepDataStoreFile(appContext.dataStoreFile(" firebaseSessions/sessionDataStore.data" ))
176
+ },
168
177
)
169
178
170
179
private fun <T > createDataStore (
@@ -197,6 +206,45 @@ internal interface FirebaseSessionsComponent {
197
206
} catch (_: SecurityException ) {
198
207
false
199
208
}
209
+
210
+ /* *
211
+ * Prepares the DataStore file by ensuring its parent directory exists. Throws [IOException]
212
+ * if the directory could not be created, or if a conflicting file could not be removed.
213
+ */
214
+ private fun prepDataStoreFile (dataStoreFile : File ): File {
215
+ val parentDir = dataStoreFile.parentFile ? : return dataStoreFile
216
+
217
+ // Check if something exists at the path, but isn't a directory
218
+ if (parentDir.exists() && ! parentDir.isDirectory) {
219
+ // Only delete it if it's the specific file we know we can safely remove
220
+ if (parentDir.name == " firebaseSessions" ) {
221
+ if (! parentDir.delete()) {
222
+ throw IOException (" Failed to delete conflicting file: $parentDir " )
223
+ }
224
+ }
225
+ }
226
+
227
+ if (parentDir.isDirectory) {
228
+ return dataStoreFile
229
+ }
230
+
231
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
232
+ try {
233
+ Files .createDirectories(parentDir.toPath())
234
+ } catch (ex: Exception ) {
235
+ throw IOException (" Failed to create directory: $parentDir " , ex)
236
+ }
237
+ } else {
238
+ if (! parentDir.mkdirs()) {
239
+ // It's possible another thread created it in the meantime, so we double-check
240
+ if (! parentDir.isDirectory) {
241
+ throw IOException (" Failed to create directory: $parentDir " )
242
+ }
243
+ }
244
+ }
245
+
246
+ return dataStoreFile
247
+ }
200
248
}
201
249
}
202
250
}
0 commit comments