From 01f4d322f52eefabfd146d90d5be21e1c3b54c67 Mon Sep 17 00:00:00 2001 From: Ankita Jhawar Date: Wed, 7 Aug 2019 10:17:16 -0700 Subject: [PATCH 1/4] Implementing cache for FIS SDK --- .../firebase-installations.gradle | 5 +- .../src/androidTest/AndroidManifest.xml | 26 ++++ .../FirebaseInstallationIdCacheTest.java | 112 +++++++++++++++++ .../installations/FirebaseInstallations.java | 2 +- .../local/FirebaseInstallationIdCache.java | 113 ++++++++++++++++++ ...FirebaseInstallationIdCacheEntryValue.java | 54 +++++++++ 6 files changed, 310 insertions(+), 2 deletions(-) create mode 100644 firebase-installations/src/androidTest/AndroidManifest.xml create mode 100644 firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java create mode 100644 firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java create mode 100644 firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java diff --git a/firebase-installations/firebase-installations.gradle b/firebase-installations/firebase-installations.gradle index ec0224515dd..c5fea1c252b 100644 --- a/firebase-installations/firebase-installations.gradle +++ b/firebase-installations/firebase-installations.gradle @@ -53,6 +53,9 @@ dependencies { testImplementation 'junit:junit:4.12' testImplementation "org.robolectric:robolectric:$robolectricVersion" + androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test:runner:1.2.0' - implementation 'com.google.guava:guava:16.0.+' + androidTestImplementation "com.google.truth:truth:$googleTruthVersion" + androidTestImplementation 'junit:junit:4.12' + androidTestImplementation "androidx.annotation:annotation:1.0.0" } diff --git a/firebase-installations/src/androidTest/AndroidManifest.xml b/firebase-installations/src/androidTest/AndroidManifest.xml new file mode 100644 index 00000000000..d4f525f3fff --- /dev/null +++ b/firebase-installations/src/androidTest/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java b/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java new file mode 100644 index 00000000000..e9d122e71a3 --- /dev/null +++ b/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java @@ -0,0 +1,112 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.firebase.installation.local; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.runner.AndroidJUnit4; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import com.google.firebase.installations.local.FirebaseInstallationIdCache; +import com.google.firebase.installations.local.FirebaseInstallationIdCacheEntryValue; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Instrumented tests for {@link FirebaseInstallationIdCache} */ +@RunWith(AndroidJUnit4.class) +public class FirebaseInstallationIdCacheTest { + + private FirebaseApp firebaseApp0; + private FirebaseApp firebaseApp1; + private FirebaseInstallationIdCache cache0; + private FirebaseInstallationIdCache cache1; + private final String AUTH_TOKEN = "auth_token"; + private final String REFRESH_TOKEN = "refresh_token"; + + private final long TIMESTAMP_IN_SECONDS = 100L; + + @Before + public void setUp() { + FirebaseApp.clearInstancesForTest(); + firebaseApp0 = + FirebaseApp.initializeApp( + ApplicationProvider.getApplicationContext(), + new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build()); + firebaseApp1 = + FirebaseApp.initializeApp( + ApplicationProvider.getApplicationContext(), + new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(), + "firebase_app_1"); + cache0 = new FirebaseInstallationIdCache(firebaseApp0); + cache1 = new FirebaseInstallationIdCache(firebaseApp1); + } + + @After + public void cleanUp() throws Exception { + cache0.clear(); + cache1.clear(); + } + + @Test + public void testReadCacheEntry_Null() { + assertNull(cache0.readCacheEntryValue()); + assertNull(cache1.readCacheEntryValue()); + } + + @Test + public void testUpdateAndReadCacheEntry() throws Exception { + assertTrue( + cache0.insertOrUpdateCacheEntry( + FirebaseInstallationIdCacheEntryValue.create( + "123456", + FirebaseInstallationIdCache.CacheStatus.UNREGISTERED, + AUTH_TOKEN, + REFRESH_TOKEN, + TIMESTAMP_IN_SECONDS, + TIMESTAMP_IN_SECONDS))); + FirebaseInstallationIdCacheEntryValue entryValue = cache0.readCacheEntryValue(); + assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456"); + assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN); + assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN); + assertThat(entryValue.getCacheStatus()) + .isEqualTo(FirebaseInstallationIdCache.CacheStatus.UNREGISTERED); + assertThat(entryValue.getExpiresIn()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertThat(entryValue.getTokenCreationTime()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertNull(cache1.readCacheEntryValue()); + + assertTrue( + cache0.insertOrUpdateCacheEntry( + FirebaseInstallationIdCacheEntryValue.create( + "123456", + FirebaseInstallationIdCache.CacheStatus.REGISTERED, + AUTH_TOKEN, + REFRESH_TOKEN, + 200L, + TIMESTAMP_IN_SECONDS))); + entryValue = cache0.readCacheEntryValue(); + assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456"); + assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN); + assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN); + assertThat(entryValue.getCacheStatus()) + .isEqualTo(FirebaseInstallationIdCache.CacheStatus.REGISTERED); + assertThat(entryValue.getExpiresIn()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertThat(entryValue.getTokenCreationTime()).isEqualTo(200L); + } +} diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java index e33664b6e32..75affe7e15f 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java @@ -15,10 +15,10 @@ package com.google.firebase.installations; import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; import com.google.android.gms.common.internal.Preconditions; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; -import com.google.common.annotations.VisibleForTesting; import com.google.firebase.FirebaseApp; /** diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java new file mode 100644 index 00000000000..c398db49702 --- /dev/null +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java @@ -0,0 +1,113 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.firebase.installations.local; + +import android.content.Context; +import android.content.SharedPreferences; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.google.firebase.FirebaseApp; + +/** + * A layer that locally caches a few Firebase Installation attributes on top the Firebase + * Installation backend API. + */ +public class FirebaseInstallationIdCache { + // Status of each cache entry + // NOTE: never change the ordinal of the enum values because the enum values are stored in cache + // as their ordinal numbers. + public enum CacheStatus { + // Cache entry is synced to Firebase backend + REGISTERED, + // Cache entry is waiting for Firebase backend response or internal network retry + UNREGISTERED, + // Cache entry is in error state when syncing with Firebase backend + REGISTER_ERROR, + // Cache entry is in delete state before syncing with Firebase backend + DELETED + } + + private static final String SHARED_PREFS_NAME = "FirebaseInstallationIdCache"; + + private static final String FIREBASE_INSTALLATION_ID_KEY = "Fid"; + private static final String AUTH_TOKEN_KEY = "AuthToken"; + private static final String REFRESH_TOKEN_KEY = "RefreshToken"; + private static final String TOKEN_CREATION_TIME_IN_SECONDS = "TokenCreationTime"; + private static final String EXPIRES_IN_SECONDS = "ExpiresIn"; + private static final String CACHE_STATUS_KEY = "Status"; + + private final SharedPreferences prefs; + private final String persistenceKey; + + public FirebaseInstallationIdCache(@NonNull FirebaseApp firebaseApp) { + // Different FirebaseApp in the same Android application should have the same application + // context and same dir path + prefs = + firebaseApp + .getApplicationContext() + .getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE); + persistenceKey = firebaseApp.getPersistenceKey(); + } + + @Nullable + public synchronized FirebaseInstallationIdCacheEntryValue readCacheEntryValue() { + String iid = prefs.getString(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), null); + int status = prefs.getInt(getSharedPreferencesKey(CACHE_STATUS_KEY), -1); + String authToken = prefs.getString(getSharedPreferencesKey(AUTH_TOKEN_KEY), null); + String refreshToken = prefs.getString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), null); + long tokenCreationTime = + prefs.getLong(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS), 0); + long expiresIn = prefs.getLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS), 0); + + if (iid == null || status == -1) { + return null; + } + + return FirebaseInstallationIdCacheEntryValue.create( + iid, CacheStatus.values()[status], authToken, refreshToken, tokenCreationTime, expiresIn); + } + + @NonNull + public synchronized boolean insertOrUpdateCacheEntry( + @NonNull FirebaseInstallationIdCacheEntryValue entryValue) { + SharedPreferences.Editor editor = prefs.edit(); + editor.putString( + getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), + entryValue.getFirebaseInstallationId()); + editor.putInt(getSharedPreferencesKey(CACHE_STATUS_KEY), entryValue.getCacheStatus().ordinal()); + editor.putString(getSharedPreferencesKey(AUTH_TOKEN_KEY), entryValue.getAuthToken()); + editor.putString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), entryValue.getRefreshToken()); + editor.putLong( + getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS), entryValue.getTokenCreationTime()); + editor.putLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS), entryValue.getExpiresIn()); + return editor.commit(); + } + + @NonNull + public synchronized boolean clear() { + SharedPreferences.Editor editor = prefs.edit(); + editor.remove(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY)); + editor.remove(getSharedPreferencesKey(CACHE_STATUS_KEY)); + editor.remove(getSharedPreferencesKey(AUTH_TOKEN_KEY)); + editor.remove(getSharedPreferencesKey(REFRESH_TOKEN_KEY)); + editor.remove(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS)); + editor.remove(getSharedPreferencesKey(EXPIRES_IN_SECONDS)); + return editor.commit(); + } + + private String getSharedPreferencesKey(String key) { + return String.format("%s|%s", persistenceKey, key); + } +} diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java new file mode 100644 index 00000000000..e610b184b65 --- /dev/null +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java @@ -0,0 +1,54 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.firebase.installations.local; + +import androidx.annotation.NonNull; +import com.google.auto.value.AutoValue; + +/** + * This class represents a cache entry value in {@link FirebaseInstallationIdCache}, which contains + * a Firebase instance id and the cache status of this entry. + */ +@AutoValue +public abstract class FirebaseInstallationIdCacheEntryValue { + + @NonNull + public abstract String getFirebaseInstallationId(); + + @NonNull + public abstract FirebaseInstallationIdCache.CacheStatus getCacheStatus(); + + @NonNull + public abstract String getAuthToken(); + + @NonNull + public abstract String getRefreshToken(); + + public abstract long getExpiresIn(); + + public abstract long getTokenCreationTime(); + + @NonNull + public static FirebaseInstallationIdCacheEntryValue create( + @NonNull String firebaseInstallationId, + @NonNull FirebaseInstallationIdCache.CacheStatus cacheStatus, + @NonNull String authToken, + @NonNull String refreshToken, + long tokenCreationTime, + long expiresIn) { + return new AutoValue_FirebaseInstallationIdCacheEntryValue( + firebaseInstallationId, cacheStatus, authToken, refreshToken, expiresIn, tokenCreationTime); + } +} From c3c3b39e45a88ecab5705021896b5ea2544c43bb Mon Sep 17 00:00:00 2001 From: Ankita Jhawar Date: Wed, 7 Aug 2019 10:17:16 -0700 Subject: [PATCH 2/4] Implementing cache for FIS SDK --- .../firebase-installations.gradle | 5 +- .../src/androidTest/AndroidManifest.xml | 26 ++++ .../FirebaseInstallationIdCacheTest.java | 112 +++++++++++++++++ .../installations/FirebaseInstallations.java | 2 +- .../local/FirebaseInstallationIdCache.java | 113 ++++++++++++++++++ ...FirebaseInstallationIdCacheEntryValue.java | 54 +++++++++ 6 files changed, 310 insertions(+), 2 deletions(-) create mode 100644 firebase-installations/src/androidTest/AndroidManifest.xml create mode 100644 firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java create mode 100644 firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java create mode 100644 firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java diff --git a/firebase-installations/firebase-installations.gradle b/firebase-installations/firebase-installations.gradle index ec0224515dd..c5fea1c252b 100644 --- a/firebase-installations/firebase-installations.gradle +++ b/firebase-installations/firebase-installations.gradle @@ -53,6 +53,9 @@ dependencies { testImplementation 'junit:junit:4.12' testImplementation "org.robolectric:robolectric:$robolectricVersion" + androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test:runner:1.2.0' - implementation 'com.google.guava:guava:16.0.+' + androidTestImplementation "com.google.truth:truth:$googleTruthVersion" + androidTestImplementation 'junit:junit:4.12' + androidTestImplementation "androidx.annotation:annotation:1.0.0" } diff --git a/firebase-installations/src/androidTest/AndroidManifest.xml b/firebase-installations/src/androidTest/AndroidManifest.xml new file mode 100644 index 00000000000..43c54f991fd --- /dev/null +++ b/firebase-installations/src/androidTest/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java b/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java new file mode 100644 index 00000000000..e9d122e71a3 --- /dev/null +++ b/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java @@ -0,0 +1,112 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.firebase.installation.local; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.runner.AndroidJUnit4; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import com.google.firebase.installations.local.FirebaseInstallationIdCache; +import com.google.firebase.installations.local.FirebaseInstallationIdCacheEntryValue; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Instrumented tests for {@link FirebaseInstallationIdCache} */ +@RunWith(AndroidJUnit4.class) +public class FirebaseInstallationIdCacheTest { + + private FirebaseApp firebaseApp0; + private FirebaseApp firebaseApp1; + private FirebaseInstallationIdCache cache0; + private FirebaseInstallationIdCache cache1; + private final String AUTH_TOKEN = "auth_token"; + private final String REFRESH_TOKEN = "refresh_token"; + + private final long TIMESTAMP_IN_SECONDS = 100L; + + @Before + public void setUp() { + FirebaseApp.clearInstancesForTest(); + firebaseApp0 = + FirebaseApp.initializeApp( + ApplicationProvider.getApplicationContext(), + new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build()); + firebaseApp1 = + FirebaseApp.initializeApp( + ApplicationProvider.getApplicationContext(), + new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(), + "firebase_app_1"); + cache0 = new FirebaseInstallationIdCache(firebaseApp0); + cache1 = new FirebaseInstallationIdCache(firebaseApp1); + } + + @After + public void cleanUp() throws Exception { + cache0.clear(); + cache1.clear(); + } + + @Test + public void testReadCacheEntry_Null() { + assertNull(cache0.readCacheEntryValue()); + assertNull(cache1.readCacheEntryValue()); + } + + @Test + public void testUpdateAndReadCacheEntry() throws Exception { + assertTrue( + cache0.insertOrUpdateCacheEntry( + FirebaseInstallationIdCacheEntryValue.create( + "123456", + FirebaseInstallationIdCache.CacheStatus.UNREGISTERED, + AUTH_TOKEN, + REFRESH_TOKEN, + TIMESTAMP_IN_SECONDS, + TIMESTAMP_IN_SECONDS))); + FirebaseInstallationIdCacheEntryValue entryValue = cache0.readCacheEntryValue(); + assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456"); + assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN); + assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN); + assertThat(entryValue.getCacheStatus()) + .isEqualTo(FirebaseInstallationIdCache.CacheStatus.UNREGISTERED); + assertThat(entryValue.getExpiresIn()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertThat(entryValue.getTokenCreationTime()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertNull(cache1.readCacheEntryValue()); + + assertTrue( + cache0.insertOrUpdateCacheEntry( + FirebaseInstallationIdCacheEntryValue.create( + "123456", + FirebaseInstallationIdCache.CacheStatus.REGISTERED, + AUTH_TOKEN, + REFRESH_TOKEN, + 200L, + TIMESTAMP_IN_SECONDS))); + entryValue = cache0.readCacheEntryValue(); + assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456"); + assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN); + assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN); + assertThat(entryValue.getCacheStatus()) + .isEqualTo(FirebaseInstallationIdCache.CacheStatus.REGISTERED); + assertThat(entryValue.getExpiresIn()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertThat(entryValue.getTokenCreationTime()).isEqualTo(200L); + } +} diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java index e33664b6e32..75affe7e15f 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java @@ -15,10 +15,10 @@ package com.google.firebase.installations; import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; import com.google.android.gms.common.internal.Preconditions; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; -import com.google.common.annotations.VisibleForTesting; import com.google.firebase.FirebaseApp; /** diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java new file mode 100644 index 00000000000..c398db49702 --- /dev/null +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java @@ -0,0 +1,113 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.firebase.installations.local; + +import android.content.Context; +import android.content.SharedPreferences; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.google.firebase.FirebaseApp; + +/** + * A layer that locally caches a few Firebase Installation attributes on top the Firebase + * Installation backend API. + */ +public class FirebaseInstallationIdCache { + // Status of each cache entry + // NOTE: never change the ordinal of the enum values because the enum values are stored in cache + // as their ordinal numbers. + public enum CacheStatus { + // Cache entry is synced to Firebase backend + REGISTERED, + // Cache entry is waiting for Firebase backend response or internal network retry + UNREGISTERED, + // Cache entry is in error state when syncing with Firebase backend + REGISTER_ERROR, + // Cache entry is in delete state before syncing with Firebase backend + DELETED + } + + private static final String SHARED_PREFS_NAME = "FirebaseInstallationIdCache"; + + private static final String FIREBASE_INSTALLATION_ID_KEY = "Fid"; + private static final String AUTH_TOKEN_KEY = "AuthToken"; + private static final String REFRESH_TOKEN_KEY = "RefreshToken"; + private static final String TOKEN_CREATION_TIME_IN_SECONDS = "TokenCreationTime"; + private static final String EXPIRES_IN_SECONDS = "ExpiresIn"; + private static final String CACHE_STATUS_KEY = "Status"; + + private final SharedPreferences prefs; + private final String persistenceKey; + + public FirebaseInstallationIdCache(@NonNull FirebaseApp firebaseApp) { + // Different FirebaseApp in the same Android application should have the same application + // context and same dir path + prefs = + firebaseApp + .getApplicationContext() + .getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE); + persistenceKey = firebaseApp.getPersistenceKey(); + } + + @Nullable + public synchronized FirebaseInstallationIdCacheEntryValue readCacheEntryValue() { + String iid = prefs.getString(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), null); + int status = prefs.getInt(getSharedPreferencesKey(CACHE_STATUS_KEY), -1); + String authToken = prefs.getString(getSharedPreferencesKey(AUTH_TOKEN_KEY), null); + String refreshToken = prefs.getString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), null); + long tokenCreationTime = + prefs.getLong(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS), 0); + long expiresIn = prefs.getLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS), 0); + + if (iid == null || status == -1) { + return null; + } + + return FirebaseInstallationIdCacheEntryValue.create( + iid, CacheStatus.values()[status], authToken, refreshToken, tokenCreationTime, expiresIn); + } + + @NonNull + public synchronized boolean insertOrUpdateCacheEntry( + @NonNull FirebaseInstallationIdCacheEntryValue entryValue) { + SharedPreferences.Editor editor = prefs.edit(); + editor.putString( + getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), + entryValue.getFirebaseInstallationId()); + editor.putInt(getSharedPreferencesKey(CACHE_STATUS_KEY), entryValue.getCacheStatus().ordinal()); + editor.putString(getSharedPreferencesKey(AUTH_TOKEN_KEY), entryValue.getAuthToken()); + editor.putString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), entryValue.getRefreshToken()); + editor.putLong( + getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS), entryValue.getTokenCreationTime()); + editor.putLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS), entryValue.getExpiresIn()); + return editor.commit(); + } + + @NonNull + public synchronized boolean clear() { + SharedPreferences.Editor editor = prefs.edit(); + editor.remove(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY)); + editor.remove(getSharedPreferencesKey(CACHE_STATUS_KEY)); + editor.remove(getSharedPreferencesKey(AUTH_TOKEN_KEY)); + editor.remove(getSharedPreferencesKey(REFRESH_TOKEN_KEY)); + editor.remove(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS)); + editor.remove(getSharedPreferencesKey(EXPIRES_IN_SECONDS)); + return editor.commit(); + } + + private String getSharedPreferencesKey(String key) { + return String.format("%s|%s", persistenceKey, key); + } +} diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java new file mode 100644 index 00000000000..e610b184b65 --- /dev/null +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java @@ -0,0 +1,54 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.firebase.installations.local; + +import androidx.annotation.NonNull; +import com.google.auto.value.AutoValue; + +/** + * This class represents a cache entry value in {@link FirebaseInstallationIdCache}, which contains + * a Firebase instance id and the cache status of this entry. + */ +@AutoValue +public abstract class FirebaseInstallationIdCacheEntryValue { + + @NonNull + public abstract String getFirebaseInstallationId(); + + @NonNull + public abstract FirebaseInstallationIdCache.CacheStatus getCacheStatus(); + + @NonNull + public abstract String getAuthToken(); + + @NonNull + public abstract String getRefreshToken(); + + public abstract long getExpiresIn(); + + public abstract long getTokenCreationTime(); + + @NonNull + public static FirebaseInstallationIdCacheEntryValue create( + @NonNull String firebaseInstallationId, + @NonNull FirebaseInstallationIdCache.CacheStatus cacheStatus, + @NonNull String authToken, + @NonNull String refreshToken, + long tokenCreationTime, + long expiresIn) { + return new AutoValue_FirebaseInstallationIdCacheEntryValue( + firebaseInstallationId, cacheStatus, authToken, refreshToken, expiresIn, tokenCreationTime); + } +} From 381551c95e150f3ee0c0d85332afeb84e7b1fa12 Mon Sep 17 00:00:00 2001 From: Ankita Jhawar Date: Wed, 7 Aug 2019 13:20:33 -0700 Subject: [PATCH 3/4] Addressing Di's comments. --- ...ionIdCacheTest.java => FiidCacheTest.java} | 32 +++++++++---------- ...nstallationIdCache.java => FiidCache.java} | 30 ++++++++--------- ...tryValue.java => FiidCacheEntryValue.java} | 29 ++++++++++------- 3 files changed, 47 insertions(+), 44 deletions(-) rename firebase-installations/src/androidTest/java/com/google/firebase/installation/local/{FirebaseInstallationIdCacheTest.java => FiidCacheTest.java} (76%) rename firebase-installations/src/main/java/com/google/firebase/installations/local/{FirebaseInstallationIdCache.java => FiidCache.java} (81%) rename firebase-installations/src/main/java/com/google/firebase/installations/local/{FirebaseInstallationIdCacheEntryValue.java => FiidCacheEntryValue.java} (58%) diff --git a/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java b/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FiidCacheTest.java similarity index 76% rename from firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java rename to firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FiidCacheTest.java index e9d122e71a3..1fec6f0ad97 100644 --- a/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java +++ b/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FiidCacheTest.java @@ -22,21 +22,21 @@ import androidx.test.runner.AndroidJUnit4; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; -import com.google.firebase.installations.local.FirebaseInstallationIdCache; -import com.google.firebase.installations.local.FirebaseInstallationIdCacheEntryValue; +import com.google.firebase.installations.local.FiidCache; +import com.google.firebase.installations.local.FiidCacheEntryValue; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -/** Instrumented tests for {@link FirebaseInstallationIdCache} */ +/** Instrumented tests for {@link FiidCache} */ @RunWith(AndroidJUnit4.class) -public class FirebaseInstallationIdCacheTest { +public class FiidCacheTest { private FirebaseApp firebaseApp0; private FirebaseApp firebaseApp1; - private FirebaseInstallationIdCache cache0; - private FirebaseInstallationIdCache cache1; + private FiidCache cache0; + private FiidCache cache1; private final String AUTH_TOKEN = "auth_token"; private final String REFRESH_TOKEN = "refresh_token"; @@ -54,8 +54,8 @@ public void setUp() { ApplicationProvider.getApplicationContext(), new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(), "firebase_app_1"); - cache0 = new FirebaseInstallationIdCache(firebaseApp0); - cache1 = new FirebaseInstallationIdCache(firebaseApp1); + cache0 = new FiidCache(firebaseApp0); + cache1 = new FiidCache(firebaseApp1); } @After @@ -74,28 +74,27 @@ public void testReadCacheEntry_Null() { public void testUpdateAndReadCacheEntry() throws Exception { assertTrue( cache0.insertOrUpdateCacheEntry( - FirebaseInstallationIdCacheEntryValue.create( + FiidCacheEntryValue.create( "123456", - FirebaseInstallationIdCache.CacheStatus.UNREGISTERED, + FiidCache.CacheStatus.UNREGISTERED, AUTH_TOKEN, REFRESH_TOKEN, TIMESTAMP_IN_SECONDS, TIMESTAMP_IN_SECONDS))); - FirebaseInstallationIdCacheEntryValue entryValue = cache0.readCacheEntryValue(); + FiidCacheEntryValue entryValue = cache0.readCacheEntryValue(); assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456"); assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN); assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN); - assertThat(entryValue.getCacheStatus()) - .isEqualTo(FirebaseInstallationIdCache.CacheStatus.UNREGISTERED); + assertThat(entryValue.getCacheStatus()).isEqualTo(FiidCache.CacheStatus.UNREGISTERED); assertThat(entryValue.getExpiresIn()).isEqualTo(TIMESTAMP_IN_SECONDS); assertThat(entryValue.getTokenCreationTime()).isEqualTo(TIMESTAMP_IN_SECONDS); assertNull(cache1.readCacheEntryValue()); assertTrue( cache0.insertOrUpdateCacheEntry( - FirebaseInstallationIdCacheEntryValue.create( + FiidCacheEntryValue.create( "123456", - FirebaseInstallationIdCache.CacheStatus.REGISTERED, + FiidCache.CacheStatus.REGISTERED, AUTH_TOKEN, REFRESH_TOKEN, 200L, @@ -104,8 +103,7 @@ public void testUpdateAndReadCacheEntry() throws Exception { assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456"); assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN); assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN); - assertThat(entryValue.getCacheStatus()) - .isEqualTo(FirebaseInstallationIdCache.CacheStatus.REGISTERED); + assertThat(entryValue.getCacheStatus()).isEqualTo(FiidCache.CacheStatus.REGISTERED); assertThat(entryValue.getExpiresIn()).isEqualTo(TIMESTAMP_IN_SECONDS); assertThat(entryValue.getTokenCreationTime()).isEqualTo(200L); } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCache.java similarity index 81% rename from firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java rename to firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCache.java index c398db49702..589c9fb9266 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCache.java @@ -24,7 +24,7 @@ * A layer that locally caches a few Firebase Installation attributes on top the Firebase * Installation backend API. */ -public class FirebaseInstallationIdCache { +public class FiidCache { // Status of each cache entry // NOTE: never change the ordinal of the enum values because the enum values are stored in cache // as their ordinal numbers. @@ -39,19 +39,19 @@ public enum CacheStatus { DELETED } - private static final String SHARED_PREFS_NAME = "FirebaseInstallationIdCache"; + private static final String SHARED_PREFS_NAME = "FiidCache"; private static final String FIREBASE_INSTALLATION_ID_KEY = "Fid"; private static final String AUTH_TOKEN_KEY = "AuthToken"; private static final String REFRESH_TOKEN_KEY = "RefreshToken"; - private static final String TOKEN_CREATION_TIME_IN_SECONDS = "TokenCreationTime"; - private static final String EXPIRES_IN_SECONDS = "ExpiresIn"; + private static final String TOKEN_CREATION_TIME_IN_SECONDS_KEY = "TokenCreationEpochInSecs"; + private static final String EXPIRES_IN_SECONDS_KEY = "ExpiresInSecs"; private static final String CACHE_STATUS_KEY = "Status"; private final SharedPreferences prefs; private final String persistenceKey; - public FirebaseInstallationIdCache(@NonNull FirebaseApp firebaseApp) { + public FiidCache(@NonNull FirebaseApp firebaseApp) { // Different FirebaseApp in the same Android application should have the same application // context and same dir path prefs = @@ -62,26 +62,25 @@ public FirebaseInstallationIdCache(@NonNull FirebaseApp firebaseApp) { } @Nullable - public synchronized FirebaseInstallationIdCacheEntryValue readCacheEntryValue() { + public synchronized FiidCacheEntryValue readCacheEntryValue() { String iid = prefs.getString(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), null); int status = prefs.getInt(getSharedPreferencesKey(CACHE_STATUS_KEY), -1); String authToken = prefs.getString(getSharedPreferencesKey(AUTH_TOKEN_KEY), null); String refreshToken = prefs.getString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), null); long tokenCreationTime = - prefs.getLong(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS), 0); - long expiresIn = prefs.getLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS), 0); + prefs.getLong(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY), 0); + long expiresIn = prefs.getLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY), 0); if (iid == null || status == -1) { return null; } - return FirebaseInstallationIdCacheEntryValue.create( + return FiidCacheEntryValue.create( iid, CacheStatus.values()[status], authToken, refreshToken, tokenCreationTime, expiresIn); } @NonNull - public synchronized boolean insertOrUpdateCacheEntry( - @NonNull FirebaseInstallationIdCacheEntryValue entryValue) { + public synchronized boolean insertOrUpdateCacheEntry(@NonNull FiidCacheEntryValue entryValue) { SharedPreferences.Editor editor = prefs.edit(); editor.putString( getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), @@ -90,8 +89,9 @@ public synchronized boolean insertOrUpdateCacheEntry( editor.putString(getSharedPreferencesKey(AUTH_TOKEN_KEY), entryValue.getAuthToken()); editor.putString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), entryValue.getRefreshToken()); editor.putLong( - getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS), entryValue.getTokenCreationTime()); - editor.putLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS), entryValue.getExpiresIn()); + getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY), + entryValue.getTokenCreationEpochInSecs()); + editor.putLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY), entryValue.getExpiresInSecs()); return editor.commit(); } @@ -102,8 +102,8 @@ public synchronized boolean clear() { editor.remove(getSharedPreferencesKey(CACHE_STATUS_KEY)); editor.remove(getSharedPreferencesKey(AUTH_TOKEN_KEY)); editor.remove(getSharedPreferencesKey(REFRESH_TOKEN_KEY)); - editor.remove(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS)); - editor.remove(getSharedPreferencesKey(EXPIRES_IN_SECONDS)); + editor.remove(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY)); + editor.remove(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY)); return editor.commit(); } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCacheEntryValue.java similarity index 58% rename from firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java rename to firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCacheEntryValue.java index e610b184b65..3d42ca15bbf 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCacheEntryValue.java @@ -18,17 +18,17 @@ import com.google.auto.value.AutoValue; /** - * This class represents a cache entry value in {@link FirebaseInstallationIdCache}, which contains - * a Firebase instance id and the cache status of this entry. + * This class represents a cache entry value in {@link FiidCache}, which contains a Firebase + * instance id and the cache status of this entry. */ @AutoValue -public abstract class FirebaseInstallationIdCacheEntryValue { +public abstract class FiidCacheEntryValue { @NonNull public abstract String getFirebaseInstallationId(); @NonNull - public abstract FirebaseInstallationIdCache.CacheStatus getCacheStatus(); + public abstract FiidCache.CacheStatus getCacheStatus(); @NonNull public abstract String getAuthToken(); @@ -36,19 +36,24 @@ public abstract class FirebaseInstallationIdCacheEntryValue { @NonNull public abstract String getRefreshToken(); - public abstract long getExpiresIn(); + public abstract long getExpiresInSecs(); - public abstract long getTokenCreationTime(); + public abstract long getTokenCreationEpochInSecs(); @NonNull - public static FirebaseInstallationIdCacheEntryValue create( + public static FiidCacheEntryValue create( @NonNull String firebaseInstallationId, - @NonNull FirebaseInstallationIdCache.CacheStatus cacheStatus, + @NonNull FiidCache.CacheStatus cacheStatus, @NonNull String authToken, @NonNull String refreshToken, - long tokenCreationTime, - long expiresIn) { - return new AutoValue_FirebaseInstallationIdCacheEntryValue( - firebaseInstallationId, cacheStatus, authToken, refreshToken, expiresIn, tokenCreationTime); + long tokenCreationEpochInSecs, + long expiresInSecs) { + return new AutoValue_FiidCacheEntryValue( + firebaseInstallationId, + cacheStatus, + authToken, + refreshToken, + expiresInSecs, + tokenCreationEpochInSecs); } } From 0f939a2983a724dbf21d09335aab29f9e8ca6f85 Mon Sep 17 00:00:00 2001 From: Ankita Jhawar Date: Wed, 7 Aug 2019 13:20:33 -0700 Subject: [PATCH 4/4] Addressing Di's comments. --- ...ionIdCacheTest.java => FiidCacheTest.java} | 40 +++++++++---------- ...nstallationIdCache.java => FiidCache.java} | 30 +++++++------- ...tryValue.java => FiidCacheEntryValue.java} | 29 ++++++++------ 3 files changed, 51 insertions(+), 48 deletions(-) rename firebase-installations/src/androidTest/java/com/google/firebase/installation/local/{FirebaseInstallationIdCacheTest.java => FiidCacheTest.java} (69%) rename firebase-installations/src/main/java/com/google/firebase/installations/local/{FirebaseInstallationIdCache.java => FiidCache.java} (81%) rename firebase-installations/src/main/java/com/google/firebase/installations/local/{FirebaseInstallationIdCacheEntryValue.java => FiidCacheEntryValue.java} (58%) diff --git a/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java b/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FiidCacheTest.java similarity index 69% rename from firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java rename to firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FiidCacheTest.java index e9d122e71a3..c43a6729610 100644 --- a/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FirebaseInstallationIdCacheTest.java +++ b/firebase-installations/src/androidTest/java/com/google/firebase/installation/local/FiidCacheTest.java @@ -22,21 +22,21 @@ import androidx.test.runner.AndroidJUnit4; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; -import com.google.firebase.installations.local.FirebaseInstallationIdCache; -import com.google.firebase.installations.local.FirebaseInstallationIdCacheEntryValue; +import com.google.firebase.installations.local.FiidCache; +import com.google.firebase.installations.local.FiidCacheEntryValue; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -/** Instrumented tests for {@link FirebaseInstallationIdCache} */ +/** Instrumented tests for {@link FiidCache} */ @RunWith(AndroidJUnit4.class) -public class FirebaseInstallationIdCacheTest { +public class FiidCacheTest { private FirebaseApp firebaseApp0; private FirebaseApp firebaseApp1; - private FirebaseInstallationIdCache cache0; - private FirebaseInstallationIdCache cache1; + private FiidCache cache0; + private FiidCache cache1; private final String AUTH_TOKEN = "auth_token"; private final String REFRESH_TOKEN = "refresh_token"; @@ -54,8 +54,8 @@ public void setUp() { ApplicationProvider.getApplicationContext(), new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(), "firebase_app_1"); - cache0 = new FirebaseInstallationIdCache(firebaseApp0); - cache1 = new FirebaseInstallationIdCache(firebaseApp1); + cache0 = new FiidCache(firebaseApp0); + cache1 = new FiidCache(firebaseApp1); } @After @@ -74,28 +74,27 @@ public void testReadCacheEntry_Null() { public void testUpdateAndReadCacheEntry() throws Exception { assertTrue( cache0.insertOrUpdateCacheEntry( - FirebaseInstallationIdCacheEntryValue.create( + FiidCacheEntryValue.create( "123456", - FirebaseInstallationIdCache.CacheStatus.UNREGISTERED, + FiidCache.CacheStatus.UNREGISTERED, AUTH_TOKEN, REFRESH_TOKEN, TIMESTAMP_IN_SECONDS, TIMESTAMP_IN_SECONDS))); - FirebaseInstallationIdCacheEntryValue entryValue = cache0.readCacheEntryValue(); + FiidCacheEntryValue entryValue = cache0.readCacheEntryValue(); assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456"); assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN); assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN); - assertThat(entryValue.getCacheStatus()) - .isEqualTo(FirebaseInstallationIdCache.CacheStatus.UNREGISTERED); - assertThat(entryValue.getExpiresIn()).isEqualTo(TIMESTAMP_IN_SECONDS); - assertThat(entryValue.getTokenCreationTime()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertThat(entryValue.getCacheStatus()).isEqualTo(FiidCache.CacheStatus.UNREGISTERED); + assertThat(entryValue.getExpiresInSecs()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertThat(entryValue.getTokenCreationEpochInSecs()).isEqualTo(TIMESTAMP_IN_SECONDS); assertNull(cache1.readCacheEntryValue()); assertTrue( cache0.insertOrUpdateCacheEntry( - FirebaseInstallationIdCacheEntryValue.create( + FiidCacheEntryValue.create( "123456", - FirebaseInstallationIdCache.CacheStatus.REGISTERED, + FiidCache.CacheStatus.REGISTERED, AUTH_TOKEN, REFRESH_TOKEN, 200L, @@ -104,9 +103,8 @@ public void testUpdateAndReadCacheEntry() throws Exception { assertThat(entryValue.getFirebaseInstallationId()).isEqualTo("123456"); assertThat(entryValue.getAuthToken()).isEqualTo(AUTH_TOKEN); assertThat(entryValue.getRefreshToken()).isEqualTo(REFRESH_TOKEN); - assertThat(entryValue.getCacheStatus()) - .isEqualTo(FirebaseInstallationIdCache.CacheStatus.REGISTERED); - assertThat(entryValue.getExpiresIn()).isEqualTo(TIMESTAMP_IN_SECONDS); - assertThat(entryValue.getTokenCreationTime()).isEqualTo(200L); + assertThat(entryValue.getCacheStatus()).isEqualTo(FiidCache.CacheStatus.REGISTERED); + assertThat(entryValue.getExpiresInSecs()).isEqualTo(TIMESTAMP_IN_SECONDS); + assertThat(entryValue.getTokenCreationEpochInSecs()).isEqualTo(200L); } } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCache.java similarity index 81% rename from firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java rename to firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCache.java index c398db49702..589c9fb9266 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCache.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCache.java @@ -24,7 +24,7 @@ * A layer that locally caches a few Firebase Installation attributes on top the Firebase * Installation backend API. */ -public class FirebaseInstallationIdCache { +public class FiidCache { // Status of each cache entry // NOTE: never change the ordinal of the enum values because the enum values are stored in cache // as their ordinal numbers. @@ -39,19 +39,19 @@ public enum CacheStatus { DELETED } - private static final String SHARED_PREFS_NAME = "FirebaseInstallationIdCache"; + private static final String SHARED_PREFS_NAME = "FiidCache"; private static final String FIREBASE_INSTALLATION_ID_KEY = "Fid"; private static final String AUTH_TOKEN_KEY = "AuthToken"; private static final String REFRESH_TOKEN_KEY = "RefreshToken"; - private static final String TOKEN_CREATION_TIME_IN_SECONDS = "TokenCreationTime"; - private static final String EXPIRES_IN_SECONDS = "ExpiresIn"; + private static final String TOKEN_CREATION_TIME_IN_SECONDS_KEY = "TokenCreationEpochInSecs"; + private static final String EXPIRES_IN_SECONDS_KEY = "ExpiresInSecs"; private static final String CACHE_STATUS_KEY = "Status"; private final SharedPreferences prefs; private final String persistenceKey; - public FirebaseInstallationIdCache(@NonNull FirebaseApp firebaseApp) { + public FiidCache(@NonNull FirebaseApp firebaseApp) { // Different FirebaseApp in the same Android application should have the same application // context and same dir path prefs = @@ -62,26 +62,25 @@ public FirebaseInstallationIdCache(@NonNull FirebaseApp firebaseApp) { } @Nullable - public synchronized FirebaseInstallationIdCacheEntryValue readCacheEntryValue() { + public synchronized FiidCacheEntryValue readCacheEntryValue() { String iid = prefs.getString(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), null); int status = prefs.getInt(getSharedPreferencesKey(CACHE_STATUS_KEY), -1); String authToken = prefs.getString(getSharedPreferencesKey(AUTH_TOKEN_KEY), null); String refreshToken = prefs.getString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), null); long tokenCreationTime = - prefs.getLong(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS), 0); - long expiresIn = prefs.getLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS), 0); + prefs.getLong(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY), 0); + long expiresIn = prefs.getLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY), 0); if (iid == null || status == -1) { return null; } - return FirebaseInstallationIdCacheEntryValue.create( + return FiidCacheEntryValue.create( iid, CacheStatus.values()[status], authToken, refreshToken, tokenCreationTime, expiresIn); } @NonNull - public synchronized boolean insertOrUpdateCacheEntry( - @NonNull FirebaseInstallationIdCacheEntryValue entryValue) { + public synchronized boolean insertOrUpdateCacheEntry(@NonNull FiidCacheEntryValue entryValue) { SharedPreferences.Editor editor = prefs.edit(); editor.putString( getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), @@ -90,8 +89,9 @@ public synchronized boolean insertOrUpdateCacheEntry( editor.putString(getSharedPreferencesKey(AUTH_TOKEN_KEY), entryValue.getAuthToken()); editor.putString(getSharedPreferencesKey(REFRESH_TOKEN_KEY), entryValue.getRefreshToken()); editor.putLong( - getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS), entryValue.getTokenCreationTime()); - editor.putLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS), entryValue.getExpiresIn()); + getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY), + entryValue.getTokenCreationEpochInSecs()); + editor.putLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY), entryValue.getExpiresInSecs()); return editor.commit(); } @@ -102,8 +102,8 @@ public synchronized boolean clear() { editor.remove(getSharedPreferencesKey(CACHE_STATUS_KEY)); editor.remove(getSharedPreferencesKey(AUTH_TOKEN_KEY)); editor.remove(getSharedPreferencesKey(REFRESH_TOKEN_KEY)); - editor.remove(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS)); - editor.remove(getSharedPreferencesKey(EXPIRES_IN_SECONDS)); + editor.remove(getSharedPreferencesKey(TOKEN_CREATION_TIME_IN_SECONDS_KEY)); + editor.remove(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY)); return editor.commit(); } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCacheEntryValue.java similarity index 58% rename from firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java rename to firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCacheEntryValue.java index e610b184b65..3d42ca15bbf 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/local/FirebaseInstallationIdCacheEntryValue.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/FiidCacheEntryValue.java @@ -18,17 +18,17 @@ import com.google.auto.value.AutoValue; /** - * This class represents a cache entry value in {@link FirebaseInstallationIdCache}, which contains - * a Firebase instance id and the cache status of this entry. + * This class represents a cache entry value in {@link FiidCache}, which contains a Firebase + * instance id and the cache status of this entry. */ @AutoValue -public abstract class FirebaseInstallationIdCacheEntryValue { +public abstract class FiidCacheEntryValue { @NonNull public abstract String getFirebaseInstallationId(); @NonNull - public abstract FirebaseInstallationIdCache.CacheStatus getCacheStatus(); + public abstract FiidCache.CacheStatus getCacheStatus(); @NonNull public abstract String getAuthToken(); @@ -36,19 +36,24 @@ public abstract class FirebaseInstallationIdCacheEntryValue { @NonNull public abstract String getRefreshToken(); - public abstract long getExpiresIn(); + public abstract long getExpiresInSecs(); - public abstract long getTokenCreationTime(); + public abstract long getTokenCreationEpochInSecs(); @NonNull - public static FirebaseInstallationIdCacheEntryValue create( + public static FiidCacheEntryValue create( @NonNull String firebaseInstallationId, - @NonNull FirebaseInstallationIdCache.CacheStatus cacheStatus, + @NonNull FiidCache.CacheStatus cacheStatus, @NonNull String authToken, @NonNull String refreshToken, - long tokenCreationTime, - long expiresIn) { - return new AutoValue_FirebaseInstallationIdCacheEntryValue( - firebaseInstallationId, cacheStatus, authToken, refreshToken, expiresIn, tokenCreationTime); + long tokenCreationEpochInSecs, + long expiresInSecs) { + return new AutoValue_FiidCacheEntryValue( + firebaseInstallationId, + cacheStatus, + authToken, + refreshToken, + expiresInSecs, + tokenCreationEpochInSecs); } }