Skip to content

Commit a65867f

Browse files
committed
Merge branch 'iid-migration' of github.com:firebase/firebase-android-sdk into fix_error
2 parents d90998d + ee514e8 commit a65867f

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

firebase-installations/api.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ package com.google.firebase.installations {
2626
package com.google.firebase.installations.local {
2727

2828
public class IidStore {
29-
ctor public IidStore(@NonNull FirebaseApp);
29+
ctor public IidStore();
3030
method @Nullable public String readIid();
3131
}
3232

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/FirebaseInstallations.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
7474
new FirebaseInstallationServiceClient(firebaseApp.getApplicationContext()),
7575
new PersistedInstallation(firebaseApp),
7676
new Utils(DefaultClock.getInstance()),
77-
new IidStore(firebaseApp));
77+
new IidStore());
7878
}
7979

8080
FirebaseInstallations(
@@ -226,11 +226,9 @@ private final void doRegistration() {
226226
// New FID needs to be created
227227
if (persistedInstallationEntry.isNotGenerated()) {
228228

229-
// Read the existing iid for this firebase installation
230-
String fid = iidStore.readIid();
231-
if (fid == null) {
232-
fid = utils.createRandomFid();
233-
}
229+
// For a default firebase installation read the existing iid. For other custom firebase
230+
// installations create a new fid
231+
String fid = readExistingIidOrCreateFid();
234232
persistFid(fid);
235233
persistedInstallationEntry = persistedInstallation.readPersistedInstallationEntryValue();
236234
}
@@ -287,6 +285,19 @@ private final void doRegistration() {
287285
}
288286
}
289287

288+
private String readExistingIidOrCreateFid() {
289+
// Check if this firebase app is the default (first initialized) instance
290+
if (!firebaseApp.equals(FirebaseApp.getInstance())) {
291+
return utils.createRandomFid();
292+
}
293+
// For a default firebase installation, read the existing iid from shared prefs
294+
String fid = iidStore.readIid();
295+
if (fid == null) {
296+
fid = utils.createRandomFid();
297+
}
298+
return fid;
299+
}
300+
290301
private void persistFid(String fid) throws FirebaseInstallationsException {
291302
boolean firstUpdateCacheResult =
292303
persistedInstallation.insertOrUpdatePersistedInstallationEntry(

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

Lines changed: 28 additions & 9 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|";
@@ -39,11 +42,11 @@ public class IidStore {
3942
@GuardedBy("iidPrefs")
4043
private final SharedPreferences iidPrefs;
4144

42-
public IidStore(@NonNull FirebaseApp firebaseApp) {
45+
public IidStore() {
4346
// Different FirebaseApp in the same Android application should have the same application
4447
// context and same dir path. We only read existing Iids for the default firebase application.
4548
iidPrefs =
46-
firebaseApp
49+
FirebaseApp.getInstance()
4750
.getApplicationContext()
4851
.getSharedPreferences(IID_SHARED_PREFS_NAME, Context.MODE_PRIVATE);
4952
}
@@ -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)