Skip to content

Commit ee514e8

Browse files
committed
Addressed Rayo's comments.
1 parent a5ffdd3 commit ee514e8

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

firebase-installations/src/androidTest/java/com/google/firebase/installations/FirebaseInstallationsInstrumentedTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ public void testGetId_PersistedInstallationOk_BackendOk() throws Exception {
226226
persistedInstallation.readPersistedInstallationEntryValue();
227227
assertThat(entryValue).hasFid(TEST_FID_1);
228228

229-
// Waiting for Task that registers FID on the FIS Servers
229+
// getId() returns fid immediately but registers fid asynchronously. Waiting for half a second
230+
// while we mock fid registration. We dont send an actual request to FIS in tests.
230231
executor.awaitTermination(500, TimeUnit.MILLISECONDS);
231232

232233
PersistedInstallationEntry updatedInstallationEntry =

firebase-installations/src/main/java/com/google/firebase/installations/local/IidStore.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import java.security.spec.InvalidKeySpecException;
3232
import java.security.spec.X509EncodedKeySpec;
3333

34+
/**
35+
* Read existing iid only for default (first initialized) instance of this firebase application.*
36+
*/
3437
public class IidStore {
3538
private static final String IID_SHARED_PREFS_NAME = "com.google.android.gms.appid";
3639
private static final String STORE_KEY_PUB = "|S||P|";
@@ -57,23 +60,39 @@ public String readIid() {
5760

5861
// If such a version was used by this App-Instance, we can directly read the existing
5962
// Instance-ID from storage and return it
60-
String id = iidPrefs.getString(STORE_KEY_ID, /* defaultValue= */ null);
63+
String id = readInstanceIdFromLocalStorage();
6164

6265
if (id != null) {
6366
return id;
6467
}
6568

6669
// If this App-Instance did not store the Instance-ID in local storage, we may be able to find
6770
// its Public-Key in order to calculate the App-Instance's Instance-ID.
71+
return readPublicKeyFromLocalStorageAndCalculateInstanceId();
72+
}
73+
}
74+
75+
@Nullable
76+
private String readInstanceIdFromLocalStorage() {
77+
synchronized (iidPrefs) {
78+
return iidPrefs.getString(STORE_KEY_ID, /* defaultValue= */ null);
79+
}
80+
}
81+
82+
@Nullable
83+
private String readPublicKeyFromLocalStorageAndCalculateInstanceId() {
84+
synchronized (iidPrefs) {
6885
String base64PublicKey = iidPrefs.getString(STORE_KEY_PUB, /* defaultValue= */ null);
69-
if (base64PublicKey != null) {
70-
PublicKey publicKey = parseKey(base64PublicKey);
71-
if (publicKey != null) {
72-
return getIdFromPublicKey(publicKey);
73-
}
86+
if (base64PublicKey == null) {
87+
return null;
88+
}
89+
90+
PublicKey publicKey = parseKey(base64PublicKey);
91+
if (publicKey == null) {
92+
return null;
7493
}
7594

76-
return null;
95+
return getIdFromPublicKey(publicKey);
7796
}
7897
}
7998

0 commit comments

Comments
 (0)