31
31
* @hide
32
32
*/
33
33
public class PersistedInstallation {
34
- private final File dataFile ;
34
+ private File dataFile ;
35
35
@ NonNull private final FirebaseApp firebaseApp ;
36
36
37
37
// Registration Status of each persisted fid entry
@@ -77,15 +77,26 @@ public enum RegistrationStatus {
77
77
private static final String FIS_ERROR_KEY = "FisError" ;
78
78
79
79
public PersistedInstallation (@ NonNull FirebaseApp firebaseApp ) {
80
- // Different FirebaseApp in the same Android application should have the same application
81
- // context and same dir path
82
- dataFile =
83
- new File (
84
- firebaseApp .getApplicationContext ().getFilesDir (),
85
- SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp .getPersistenceKey () + ".json" );
86
80
this .firebaseApp = firebaseApp ;
87
81
}
88
82
83
+ private File getDataFile () {
84
+
85
+ if (dataFile == null ) {
86
+ synchronized (this ) {
87
+ if (dataFile == null ) {
88
+ // Different FirebaseApp in the same Android application should have the same application
89
+ // context and same dir path
90
+ dataFile = new File (
91
+ firebaseApp .getApplicationContext ().getFilesDir (),
92
+ SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp .getPersistenceKey () + ".json" );
93
+ }
94
+ }
95
+ }
96
+
97
+ return dataFile ;
98
+ }
99
+
89
100
@ NonNull
90
101
public PersistedInstallationEntry readPersistedInstallationEntryValue () {
91
102
JSONObject json = readJSONFromFile ();
@@ -114,7 +125,7 @@ public PersistedInstallationEntry readPersistedInstallationEntryValue() {
114
125
private JSONObject readJSONFromFile () {
115
126
final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
116
127
final byte [] tmpBuf = new byte [16 * 1024 ];
117
- try (FileInputStream fis = new FileInputStream (dataFile )) {
128
+ try (FileInputStream fis = new FileInputStream (getDataFile () )) {
118
129
while (true ) {
119
130
int numRead = fis .read (tmpBuf , 0 , tmpBuf .length );
120
131
if (numRead < 0 ) {
@@ -156,7 +167,7 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
156
167
fos .close ();
157
168
158
169
// Snapshot the temp file to the actual file
159
- if (!tmpFile .renameTo (dataFile )) {
170
+ if (!tmpFile .renameTo (getDataFile () )) {
160
171
throw new IOException ("unable to rename the tmpfile to " + SETTINGS_FILE_NAME_PREFIX );
161
172
}
162
173
} catch (JSONException | IOException e ) {
@@ -173,6 +184,6 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
173
184
174
185
/** Sets the state to ATTEMPT_MIGRATION. */
175
186
public void clearForTesting () {
176
- dataFile .delete ();
187
+ getDataFile () .delete ();
177
188
}
178
189
}
0 commit comments