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,27 @@ 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 =
91
+ new File (
92
+ firebaseApp .getApplicationContext ().getFilesDir (),
93
+ SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp .getPersistenceKey () + ".json" );
94
+ }
95
+ }
96
+ }
97
+
98
+ return dataFile ;
99
+ }
100
+
89
101
@ NonNull
90
102
public PersistedInstallationEntry readPersistedInstallationEntryValue () {
91
103
JSONObject json = readJSONFromFile ();
@@ -114,7 +126,7 @@ public PersistedInstallationEntry readPersistedInstallationEntryValue() {
114
126
private JSONObject readJSONFromFile () {
115
127
final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
116
128
final byte [] tmpBuf = new byte [16 * 1024 ];
117
- try (FileInputStream fis = new FileInputStream (dataFile )) {
129
+ try (FileInputStream fis = new FileInputStream (getDataFile () )) {
118
130
while (true ) {
119
131
int numRead = fis .read (tmpBuf , 0 , tmpBuf .length );
120
132
if (numRead < 0 ) {
@@ -156,7 +168,7 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
156
168
fos .close ();
157
169
158
170
// Snapshot the temp file to the actual file
159
- if (!tmpFile .renameTo (dataFile )) {
171
+ if (!tmpFile .renameTo (getDataFile () )) {
160
172
throw new IOException ("unable to rename the tmpfile to " + SETTINGS_FILE_NAME_PREFIX );
161
173
}
162
174
} catch (JSONException | IOException e ) {
@@ -173,6 +185,6 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
173
185
174
186
/** Sets the state to ATTEMPT_MIGRATION. */
175
187
public void clearForTesting () {
176
- dataFile .delete ();
188
+ getDataFile () .delete ();
177
189
}
178
190
}
0 commit comments