Skip to content

Commit 864fbcb

Browse files
committed
Addressing Rayo's comments.
1 parent d17d570 commit 864fbcb

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public Task<String> getId() {
231231
}
232232

233233
Task<String> task = addGetIdListener();
234-
backgroundExecutor.execute(this::doGetId);
234+
backgroundExecutor.execute(() -> doRegistrationInternal(false));
235235
return task;
236236
}
237237

@@ -248,11 +248,7 @@ public Task<String> getId() {
248248
public Task<InstallationTokenResult> getToken(boolean forceRefresh) {
249249
preConditionChecks();
250250
Task<InstallationTokenResult> task = addGetAuthTokenListener();
251-
if (forceRefresh) {
252-
backgroundExecutor.execute(this::doGetAuthTokenForceRefresh);
253-
} else {
254-
backgroundExecutor.execute(this::doGetAuthTokenWithoutForceRefresh);
255-
}
251+
backgroundExecutor.execute(() -> doRegistrationInternal(forceRefresh));
256252
return task;
257253
}
258254

@@ -270,20 +266,22 @@ public Task<Void> delete() {
270266
private Task<String> addGetIdListener() {
271267
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
272268
StateListener l = new GetIdListener(taskCompletionSource);
273-
synchronized (lock) {
274-
listeners.add(l);
275-
}
269+
addStateListeners(l);
276270
return taskCompletionSource.getTask();
277271
}
278272

279273
private Task<InstallationTokenResult> addGetAuthTokenListener() {
280274
TaskCompletionSource<InstallationTokenResult> taskCompletionSource =
281275
new TaskCompletionSource<>();
282276
StateListener l = new GetAuthTokenListener(utils, taskCompletionSource);
277+
addStateListeners(l);
278+
return taskCompletionSource.getTask();
279+
}
280+
281+
private void addStateListeners(StateListener l) {
283282
synchronized (lock) {
284283
listeners.add(l);
285284
}
286-
return taskCompletionSource.getTask();
287285
}
288286

289287
private void triggerOnStateReached(PersistedInstallationEntry persistedInstallationEntry) {
@@ -320,18 +318,6 @@ private synchronized String getCacheFid() {
320318
return cachedFid;
321319
}
322320

323-
private final void doGetAuthTokenForceRefresh() {
324-
doRegistrationInternal(true);
325-
}
326-
327-
private final void doGetAuthTokenWithoutForceRefresh() {
328-
doRegistrationInternal(false);
329-
}
330-
331-
private final void doGetId() {
332-
doRegistrationInternal(false);
333-
}
334-
335321
/**
336322
* Logic for handling get id and the two forms of get auth token. This handles all the work,
337323
* including creating a new FID if one hasn't been generated yet and making the network calls to

firebase-installations/src/main/java/com/google/firebase/installations/GetIdListener.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import com.google.android.gms.tasks.TaskCompletionSource;
1818
import com.google.firebase.installations.local.PersistedInstallationEntry;
1919

20+
/**
21+
* This class manages {@link PersistedInstallationEntry} state transitions valid for getId() and
22+
* updates the caller once the id is generated.
23+
*/
2024
class GetIdListener implements StateListener {
2125
final TaskCompletionSource<String> taskCompletionSource;
2226

@@ -32,6 +36,8 @@ public boolean onStateReached(PersistedInstallationEntry persistedInstallationEn
3236
taskCompletionSource.trySetResult(persistedInstallationEntry.getFirebaseInstallationId());
3337
return true;
3438
}
39+
// Don't update the caller if the PersistedInstallationEntry registration status is
40+
// ATTEMPT_MIGRATION or NOT_GENERATED.
3541
return false;
3642
}
3743

0 commit comments

Comments
 (0)