Skip to content

Adding a FidListener that propagates fid changes to the clients. #2195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.google.android.gms.tasks.Task;
import com.google.firebase.annotations.DeferredApi;
import com.google.firebase.installations.internal.FidListener;
import com.google.firebase.installations.internal.HandleFidListener;
import com.google.firebase.installations.internal.FidListenerHandle;

/**
* This is an interface of {@code FirebaseInstallations} that is only exposed to 2p via component
Expand Down Expand Up @@ -62,5 +62,5 @@ public interface FirebaseInstallationsApi {
* @hide
*/
@DeferredApi
HandleFidListener registerFidListener(@NonNull FidListener listener);
FidListenerHandle registerFidListener(@NonNull FidListener listener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
package com.google.firebase.installations.internal;

/**
* Interface for un-registering a previously registered listener.
* Interface for un-registering a previously registered listener {@link FidListener}.
*
* @hide
*/
public interface HandleFidListener {
public interface FidListenerHandle {

/**
* Unregisters a previously registered {@link FidListener}.
* Unregisters a previously registered {@link FidListener} being tracked by this {@code
* FidListenerHandle}. After the initial call, subsequent calls have no effect.
*
* @hide
*/
void unregister(FidListener fidListener);
void unregister();
}
1 change: 1 addition & 0 deletions firebase-installations/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package com.google.firebase.installations {
method @NonNull public static com.google.firebase.installations.FirebaseInstallations getInstance();
method @NonNull public static com.google.firebase.installations.FirebaseInstallations getInstance(@NonNull com.google.firebase.FirebaseApp);
method @NonNull public com.google.android.gms.tasks.Task<com.google.firebase.installations.InstallationTokenResult> getToken(boolean);
method @NonNull public com.google.firebase.installations.internal.FidListenerHandle registerFidListener(@NonNull com.google.firebase.installations.internal.FidListener);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsException.Status;
import com.google.firebase.installations.internal.FidListener;
import com.google.firebase.installations.internal.HandleFidListener;
import com.google.firebase.installations.internal.FidListenerHandle;
import com.google.firebase.installations.local.IidStore;
import com.google.firebase.installations.local.PersistedInstallation;
import com.google.firebase.installations.local.PersistedInstallationEntry;
Expand Down Expand Up @@ -76,7 +76,7 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
@GuardedBy("this")
private String cachedFid;

@GuardedBy("this")
@GuardedBy("FirebaseInstallations.this")
private Set<FidListener> fidListeners = new HashSet<>();

@GuardedBy("lock")
Expand Down Expand Up @@ -278,16 +278,22 @@ public Task<Void> delete() {
return Tasks.call(backgroundExecutor, this::deleteFirebaseInstallationId);
}

/** Register a callback {@link FidListener} to receive fid changes. */
/**
* Register a callback {@link FidListener} to receive fid changes.
*
* @hide
*/
@NonNull
@Override
public synchronized HandleFidListener registerFidListener(@NonNull FidListener listener) {
this.fidListeners.add(listener);
return new HandleFidListener() {
public synchronized FidListenerHandle registerFidListener(@NonNull FidListener listener) {
fidListeners.add(listener);
return new FidListenerHandle() {
@Override
public void unregister(FidListener fidListener) {
if (fidListeners.contains(fidListener)) {
fidListeners.remove(fidListener);
public void unregister() {
synchronized (FirebaseInstallations.this) {
if (fidListeners.contains(listener)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider removing the contains check as remove handles it internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Done

fidListeners.remove(listener);
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.installations.FirebaseInstallationsException.Status;
import com.google.firebase.installations.internal.FidListenerHandle;
import com.google.firebase.installations.local.IidStore;
import com.google.firebase.installations.local.PersistedInstallation;
import com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus;
Expand Down Expand Up @@ -468,13 +469,20 @@ public void testFidListener_fidChanged_successful() throws Exception {
.build());

FakeFidListener fidListener = new FakeFidListener();
// Register the FidListener
FakeFidListener fidListener2 = new FakeFidListener();

// Register the FidListeners
firebaseInstallations.registerFidListener(fidListener);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider adding a test for removing a registration as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this test to check the removing registration code as well. PTAL. Thanks

FidListenerHandle listenerHandle = firebaseInstallations.registerFidListener(fidListener2);

// Do the actual getId() call under test.
// Confirm both that it returns the expected ID, as does reading the prefs from storage.
TestOnCompleteListener<String> onCompleteListener = new TestOnCompleteListener<>();
Task<String> task = firebaseInstallations.getId();

// Unregister FidListener2
listenerHandle.unregister();

task.addOnCompleteListener(executor, onCompleteListener);
String fid = onCompleteListener.await();
assertWithMessage("getId Task failed.").that(fid).isEqualTo(TEST_FID_1);
Expand All @@ -486,6 +494,7 @@ public void testFidListener_fidChanged_successful() throws Exception {

// Verify FidListener receives fid changes.
assertThat(fidListener.getLatestFid(), equalTo(TEST_FID_2));
assertNull(fidListener2.getLatestFid());
}

@Test
Expand Down