@@ -56,7 +56,6 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
56
56
private final ExecutorService executor ;
57
57
private final Utils utils ;
58
58
private final Object lock = new Object ();
59
- private final Runnable runnable ;
60
59
61
60
@ GuardedBy ("lock" )
62
61
private boolean shouldRefreshAuthToken ;
@@ -85,7 +84,6 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
85
84
this .executor = executor ;
86
85
this .persistedFid = persistedFid ;
87
86
this .utils = utils ;
88
- this .runnable = doRegistration ();
89
87
}
90
88
91
89
/**
@@ -131,7 +129,7 @@ String getName() {
131
129
@ Override
132
130
public Task <String > getId () {
133
131
Task <String > task = addGetIdListener ();
134
- executor .execute (runnable );
132
+ executor .execute (this :: doRegistration );
135
133
return task ;
136
134
}
137
135
@@ -148,7 +146,7 @@ public Task<String> getId() {
148
146
@ Override
149
147
public Task <InstallationTokenResult > getAuthToken (@ AuthTokenOption int authTokenOption ) {
150
148
Task <InstallationTokenResult > task = addGetAuthTokenListener (authTokenOption );
151
- executor .execute (runnable );
149
+ executor .execute (this :: doRegistration );
152
150
return task ;
153
151
}
154
152
@@ -199,63 +197,61 @@ private void triggerOnStateReached(PersistedFidEntry persistedFidEntry) {
199
197
}
200
198
}
201
199
202
- private final Runnable doRegistration () {
203
- return () -> {
204
- try {
205
- PersistedFidEntry persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
200
+ private final void doRegistration () {
201
+ try {
202
+ PersistedFidEntry persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
206
203
207
- // New FID needs to be created
208
- if (persistedFidEntry .isErrored () || persistedFidEntry .isNotGenerated ()) {
209
- String fid = utils .createRandomFid ();
210
- persistFid (fid );
211
- persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
212
- }
204
+ // New FID needs to be created
205
+ if (persistedFidEntry .isErrored () || persistedFidEntry .isNotGenerated ()) {
206
+ String fid = utils .createRandomFid ();
207
+ persistFid (fid );
208
+ persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
209
+ }
213
210
214
- // Notify the listeners only after force refreshing auth token if shouldRefreshAuthToken is
215
- // true
216
- synchronized (lock ) {
217
- if (!shouldRefreshAuthToken ) {
218
- triggerOnStateReached (persistedFidEntry );
219
- }
211
+ // Always notify the GetIdListeners. For GetAuthTokenListeners, only notify if force
212
+ // refreshing auth token is not required.
213
+ synchronized (lock ) {
214
+ if (!shouldRefreshAuthToken ) {
215
+ triggerOnStateReached (persistedFidEntry );
220
216
}
217
+ }
221
218
222
- // FID needs to be registered
223
- if (persistedFidEntry .isUnregistered ()) {
224
- registerAndSaveFid (persistedFidEntry );
225
- persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
226
- }
219
+ // FID needs to be registered
220
+ if (persistedFidEntry .isUnregistered ()) {
221
+ registerAndSaveFid (persistedFidEntry );
222
+ persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
223
+ }
227
224
228
- // Don't notify the listeners at this point; we might as well make ure the auth token is up
229
- // to date before letting them know.
225
+ // Don't notify the listeners at this point; we might as well make ure the auth token is up
226
+ // to date before letting them know.
230
227
231
- boolean needRefresh = utils .isAuthTokenExpired (persistedFidEntry );
232
- if (!needRefresh ) {
233
- synchronized (lock ) {
234
- needRefresh = shouldRefreshAuthToken ;
235
- }
228
+ boolean needRefresh = utils .isAuthTokenExpired (persistedFidEntry );
229
+ if (!needRefresh ) {
230
+ synchronized (lock ) {
231
+ needRefresh = shouldRefreshAuthToken ;
236
232
}
233
+ }
237
234
238
- // Refresh Auth token if needed
239
- if (needRefresh ) {
240
- synchronized (lock ) {
241
- shouldRefreshAuthToken = false ;
242
- }
243
- fetchAuthTokenFromServer (persistedFidEntry );
244
- persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
235
+ // Refresh Auth token if needed
236
+ if (needRefresh ) {
237
+ fetchAuthTokenFromServer (persistedFidEntry );
238
+ synchronized (lock ) {
239
+ shouldRefreshAuthToken = false ;
245
240
}
246
-
247
- triggerOnStateReached (persistedFidEntry );
248
- } catch (Exception e ) {
249
- PersistedFidEntry persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
250
- PersistedFidEntry errorFidEntry =
251
- persistedFidEntry
252
- .toBuilder ()
253
- .setRegistrationStatus (RegistrationStatus .REGISTER_ERROR )
254
- .build ();
255
- persistedFid .insertOrUpdatePersistedFidEntry (errorFidEntry );
256
- triggerOnStateReached (errorFidEntry );
241
+ persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
257
242
}
258
- };
243
+
244
+ triggerOnStateReached (persistedFidEntry );
245
+ } catch (Exception e ) {
246
+ PersistedFidEntry persistedFidEntry = persistedFid .readPersistedFidEntryValue ();
247
+ PersistedFidEntry errorFidEntry =
248
+ persistedFidEntry
249
+ .toBuilder ()
250
+ .setRegistrationStatus (RegistrationStatus .REGISTER_ERROR )
251
+ .build ();
252
+ persistedFid .insertOrUpdatePersistedFidEntry (errorFidEntry );
253
+ triggerOnStateReached (errorFidEntry );
254
+ }
259
255
}
260
256
261
257
private void persistFid (String fid ) throws FirebaseInstallationsException {
0 commit comments