@@ -73,6 +73,10 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
73
73
74
74
/* used for thread-level synchronization of generating and persisting fids */
75
75
private static final Object lockGenerateFid = new Object ();
76
+
77
+ /* used for thread-level synchronization of getting a fid. */
78
+ private static final Object lockGetFid = new Object ();
79
+
76
80
/* file used for process-level synchronization of generating and persisting fids */
77
81
private static final String LOCKFILE_NAME_GENERATE_FID = "generatefid.lock" ;
78
82
private static final String CHIME_FIREBASE_APP_NAME = "CHIME_ANDROID_SDK" ;
@@ -213,9 +217,11 @@ String getName() {
213
217
@ Override
214
218
public Task <String > getId () {
215
219
preConditionChecks ();
216
- Task <String > task = addGetIdListener ();
217
- backgroundExecutor .execute (this ::doGetId );
218
- return task ;
220
+ synchronized (lockGetFid ) {
221
+ TaskCompletionSource <String > taskCompletionSource = new TaskCompletionSource <>();
222
+ taskCompletionSource .trySetResult (doGetId ());
223
+ return taskCompletionSource .getTask ();
224
+ }
219
225
}
220
226
221
227
/**
@@ -231,11 +237,7 @@ public Task<String> getId() {
231
237
public Task <InstallationTokenResult > getToken (boolean forceRefresh ) {
232
238
preConditionChecks ();
233
239
Task <InstallationTokenResult > task = addGetAuthTokenListener ();
234
- if (forceRefresh ) {
235
- backgroundExecutor .execute (this ::doGetAuthTokenForceRefresh );
236
- } else {
237
- backgroundExecutor .execute (this ::doGetAuthTokenWithoutForceRefresh );
238
- }
240
+ backgroundExecutor .execute (() -> doGetAuthToken (forceRefresh ));
239
241
return task ;
240
242
}
241
243
@@ -250,15 +252,6 @@ public Task<Void> delete() {
250
252
return Tasks .call (backgroundExecutor , this ::deleteFirebaseInstallationId );
251
253
}
252
254
253
- private Task <String > addGetIdListener () {
254
- TaskCompletionSource <String > taskCompletionSource = new TaskCompletionSource <>();
255
- StateListener l = new GetIdListener (taskCompletionSource );
256
- synchronized (lock ) {
257
- listeners .add (l );
258
- }
259
- return taskCompletionSource .getTask ();
260
- }
261
-
262
255
private Task <InstallationTokenResult > addGetAuthTokenListener () {
263
256
TaskCompletionSource <InstallationTokenResult > taskCompletionSource =
264
257
new TaskCompletionSource <>();
@@ -295,16 +288,12 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
295
288
}
296
289
}
297
290
298
- private final void doGetId () {
299
- doRegistrationInternal (false );
300
- }
301
-
302
- private final void doGetAuthTokenWithoutForceRefresh () {
303
- doRegistrationInternal (false );
304
- }
305
-
306
- private final void doGetAuthTokenForceRefresh () {
307
- doRegistrationInternal (true );
291
+ private String doGetId () {
292
+ PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe ();
293
+ // Execute network calls (CreateInstallations) to the FIS Servers on a separate executor
294
+ // i.e networkExecutor
295
+ networkExecutor .execute (() -> doNetworkCall (false ));
296
+ return prefs .getFirebaseInstallationId ();
308
297
}
309
298
310
299
/**
@@ -316,7 +305,7 @@ private final void doGetAuthTokenForceRefresh() {
316
305
* @param forceRefresh true if this is for a getAuthToken call and if the caller wants to fetch a
317
306
* new auth token from the server even if an unexpired auth token exists on the client.
318
307
*/
319
- private final void doRegistrationInternal (boolean forceRefresh ) {
308
+ private void doGetAuthToken (boolean forceRefresh ) {
320
309
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe ();
321
310
322
311
// Since the caller wants to force an authtoken refresh remove the authtoken from the
@@ -520,7 +509,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
520
509
* storage.
521
510
*/
522
511
private Void deleteFirebaseInstallationId () throws FirebaseInstallationsException , IOException {
523
- PersistedInstallationEntry entry = persistedInstallation . readPersistedInstallationEntryValue ();
512
+ PersistedInstallationEntry entry = getPrefsWithGeneratedIdMultiProcessSafe ();
524
513
if (entry .isRegistered ()) {
525
514
// Call the FIS servers to delete this Firebase Installation Id.
526
515
try {
0 commit comments