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,24 @@ 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
+ if (dataFile == null ) {
85
+ synchronized (this ) {
86
+ if (dataFile == null ) {
87
+ // Different FirebaseApp in the same Android application should have the same application
88
+ // context and same dir path
89
+ dataFile = new File (
90
+ firebaseApp .getApplicationContext ().getFilesDir (),
91
+ SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp .getPersistenceKey () + ".json" );
92
+ }
93
+ }
94
+ return dataFile
95
+ }
96
+ }
97
+
89
98
@ NonNull
90
99
public PersistedInstallationEntry readPersistedInstallationEntryValue () {
91
100
JSONObject json = readJSONFromFile ();
@@ -114,7 +123,7 @@ public PersistedInstallationEntry readPersistedInstallationEntryValue() {
114
123
private JSONObject readJSONFromFile () {
115
124
final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
116
125
final byte [] tmpBuf = new byte [16 * 1024 ];
117
- try (FileInputStream fis = new FileInputStream (dataFile )) {
126
+ try (FileInputStream fis = new FileInputStream (getDataFile () )) {
118
127
while (true ) {
119
128
int numRead = fis .read (tmpBuf , 0 , tmpBuf .length );
120
129
if (numRead < 0 ) {
@@ -156,7 +165,7 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
156
165
fos .close ();
157
166
158
167
// Snapshot the temp file to the actual file
159
- if (!tmpFile .renameTo (dataFile )) {
168
+ if (!tmpFile .renameTo (getDataFile () )) {
160
169
throw new IOException ("unable to rename the tmpfile to " + SETTINGS_FILE_NAME_PREFIX );
161
170
}
162
171
} catch (JSONException | IOException e ) {
@@ -173,6 +182,6 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
173
182
174
183
/** Sets the state to ATTEMPT_MIGRATION. */
175
184
public void clearForTesting () {
176
- dataFile .delete ();
185
+ getDataFile () .delete ();
177
186
}
178
187
}
0 commit comments