Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
8881325
Implement Firebase segmentation SDK device local cache
diwu-arete Jun 13, 2019
864748f
[Firebase Segmentation] Add custom installation id cache layer and te…
diwu-arete Jun 14, 2019
0a3ebf6
Add test for updating cache
diwu-arete Jun 14, 2019
2d158ed
Switch to use SQLiteOpenHelper
diwu-arete Jun 15, 2019
f118d39
Switch to use SharedPreferences from SQLite.
diwu-arete Jun 17, 2019
4da5d31
Change the cache class to be singleton
diwu-arete Jun 18, 2019
d1ff0ec
Wrap shared pref commit in a async task.
diwu-arete Jun 18, 2019
2c5102c
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 18, 2019
41fbfee
Address comments
diwu-arete Jun 18, 2019
5fd2fa0
Google format fix
diwu-arete Jun 18, 2019
e950003
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 18, 2019
dba0c0e
Replace some deprecated code.
diwu-arete Jun 18, 2019
dc37bf8
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 18, 2019
ebdd626
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 18, 2019
f26741e
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 18, 2019
38c403f
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 18, 2019
a9a43a4
Package refactor
diwu-arete Jun 18, 2019
ca6dacf
nit
diwu-arete Jun 18, 2019
e7fff81
nit
diwu-arete Jun 18, 2019
bb8bf45
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 18, 2019
b381889
Add the state machine of updating custom installation id in the local
diwu-arete Jun 19, 2019
a72cf60
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 19, 2019
1adcfbd
minor format fix
diwu-arete Jun 20, 2019
6091f82
Address comments #1
diwu-arete Jun 20, 2019
ada5577
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 20, 2019
af5bcd1
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 20, 2019
de6fb6d
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 24, 2019
75e523b
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 24, 2019
134f06e
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 24, 2019
6a0f502
Http client in Firebase Segmentation SDK to call backend service.
diwu-arete Jun 26, 2019
4ac7db5
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 26, 2019
7f40978
Revert unintentional change
diwu-arete Jun 26, 2019
b2fc302
Fix connected device test
diwu-arete Jun 26, 2019
1f2ab34
Fix connected device test
diwu-arete Jun 26, 2019
402d8b6
Merge branch 'arete-floc' of github.com:firebase/firebase-android-sdk…
diwu-arete Jun 26, 2019
ce698cf
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jul 8, 2019
0e0278e
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jul 8, 2019
9880624
1. Add a few annotations to make java code Kotlin friendly
diwu-arete Jul 8, 2019
8ffb5bb
Fix java format
diwu-arete Jul 8, 2019
daf4698
Fix API version
diwu-arete Jul 8, 2019
7985e14
Change the segmentation API implementation to synchronous and put the
diwu-arete Jul 9, 2019
9f36d35
Fix a async getResult race issue.
diwu-arete Jul 9, 2019
047c0af
OkHttpClient -> HttpsUrlConnection
diwu-arete Jul 10, 2019
8b39c31
Use gzip for compressing content and fix ourput stream memory leak risk.
diwu-arete Jul 18, 2019
dc1a63c
Addressed a few comments
diwu-arete Jul 22, 2019
b401d31
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jul 22, 2019
8ba1c9b
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jul 22, 2019
143ed74
FirebaseSegmentation SDK
diwu-arete Jul 30, 2019
e5f0cd3
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jul 30, 2019
a5250c1
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jul 30, 2019
6daae2a
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Aug 5, 2019
4d5b2de
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Aug 6, 2019
78ce7cd
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Aug 6, 2019
d467379
Restrict Firebase API key to Android app package name.
diwu-arete Aug 6, 2019
818e471
Explicitly add internet permission
diwu-arete Aug 6, 2019
e2fbc88
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Aug 6, 2019
a7ef2db
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Aug 6, 2019
f866315
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Dec 10, 2019
a4fcd34
Disable registrar test for FirebaseSegmentation.
diwu-arete Dec 10, 2019
557ea7a
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Dec 10, 2019
0149308
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Dec 10, 2019
f757b26
Disable test lab
diwu-arete Dec 10, 2019
34caf2c
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Dec 10, 2019
461f654
Add api info for segmentation API
diwu-arete Dec 12, 2019
2a0a7a4
[FLoC] Replace the custom installation id cache SharedPref
diwu-arete Dec 13, 2019
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
72 changes: 72 additions & 0 deletions firebase-segmentation/api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Signature format: 2.0
package com.google.firebase.segmentation {

public class FirebaseSegmentation {
method @NonNull public static com.google.firebase.segmentation.FirebaseSegmentation getInstance();
method @NonNull public static com.google.firebase.segmentation.FirebaseSegmentation getInstance(@NonNull com.google.firebase.FirebaseApp);
method @NonNull public com.google.android.gms.tasks.Task<java.lang.Void> setCustomInstallationId(@Nullable String);
field public static final String TAG = "FirebaseSegmentation";
}

public class FirebaseSegmentationRegistrar implements com.google.firebase.components.ComponentRegistrar {
ctor public FirebaseSegmentationRegistrar();
method @NonNull public java.util.List<com.google.firebase.components.Component<?>> getComponents();
}

public class SetCustomInstallationIdException extends com.google.firebase.FirebaseException {
method @NonNull public com.google.firebase.segmentation.SetCustomInstallationIdException.Status getStatus();
}

public enum SetCustomInstallationIdException.Status {
enum_constant public static final com.google.firebase.segmentation.SetCustomInstallationIdException.Status BACKEND_ERROR;
enum_constant public static final com.google.firebase.segmentation.SetCustomInstallationIdException.Status CLIENT_ERROR;
enum_constant public static final com.google.firebase.segmentation.SetCustomInstallationIdException.Status DUPLICATED_CUSTOM_INSTALLATION_ID;
enum_constant public static final com.google.firebase.segmentation.SetCustomInstallationIdException.Status UNKOWN;
}

}

package com.google.firebase.segmentation.local {

public class CustomInstallationIdCache {
ctor public CustomInstallationIdCache(@NonNull com.google.firebase.FirebaseApp);
method @NonNull public boolean clear();
method @NonNull public boolean insertOrUpdateCacheEntry(@NonNull com.google.firebase.segmentation.local.CustomInstallationIdCacheEntryValue);
method @Nullable public com.google.firebase.segmentation.local.CustomInstallationIdCacheEntryValue readCacheEntryValue();
}

public enum CustomInstallationIdCache.CacheStatus {
enum_constant public static final com.google.firebase.segmentation.local.CustomInstallationIdCache.CacheStatus PENDING_CLEAR;
enum_constant public static final com.google.firebase.segmentation.local.CustomInstallationIdCache.CacheStatus PENDING_UPDATE;
enum_constant public static final com.google.firebase.segmentation.local.CustomInstallationIdCache.CacheStatus SYNCED;
}

public abstract class CustomInstallationIdCacheEntryValue {
ctor public CustomInstallationIdCacheEntryValue();
method @NonNull public static com.google.firebase.segmentation.local.CustomInstallationIdCacheEntryValue create(@NonNull String, @NonNull String, @NonNull com.google.firebase.segmentation.local.CustomInstallationIdCache.CacheStatus);
method @NonNull public abstract com.google.firebase.segmentation.local.CustomInstallationIdCache.CacheStatus getCacheStatus();
method @NonNull public abstract String getCustomInstallationId();
method @NonNull public abstract String getFirebaseInstanceId();
}

}

package com.google.firebase.segmentation.remote {

public class SegmentationServiceClient {
ctor public SegmentationServiceClient(@NonNull android.content.Context);
method @NonNull public com.google.firebase.segmentation.remote.SegmentationServiceClient.Code clearCustomInstallationId(long, @NonNull String, @NonNull String, @NonNull String);
method @NonNull public com.google.firebase.segmentation.remote.SegmentationServiceClient.Code updateCustomInstallationId(long, @NonNull String, @NonNull String, @NonNull String, @NonNull String);
}

public enum SegmentationServiceClient.Code {
enum_constant public static final com.google.firebase.segmentation.remote.SegmentationServiceClient.Code CONFLICT;
enum_constant public static final com.google.firebase.segmentation.remote.SegmentationServiceClient.Code HTTP_CLIENT_ERROR;
enum_constant public static final com.google.firebase.segmentation.remote.SegmentationServiceClient.Code NETWORK_ERROR;
enum_constant public static final com.google.firebase.segmentation.remote.SegmentationServiceClient.Code OK;
enum_constant public static final com.google.firebase.segmentation.remote.SegmentationServiceClient.Code SERVER_ERROR;
enum_constant public static final com.google.firebase.segmentation.remote.SegmentationServiceClient.Code UNAUTHORIZED;
}

}

8 changes: 2 additions & 6 deletions firebase-segmentation/firebase-segmentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ plugins {
id 'firebase-library'
}

firebaseLibrary {
testLab.enabled = true
}

android {
compileSdkVersion project.targetSdkVersion

Expand Down Expand Up @@ -48,7 +44,7 @@ dependencies {
exclude group: "com.google.firebase", module: "firebase-common"
}

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.google.android.gms:play-services-tasks:17.0.0'

Expand All @@ -59,7 +55,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:$robolectricVersion"

androidTestImplementation "androidx.annotation:annotation:1.0.0"
androidTestImplementation "androidx.annotation:annotation:1.1.0"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

package com.google.firebase.segmentation.local;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.FirebaseApp;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;

/**
* A layer that locally caches a few Firebase Segmentation attributes on top the Segmentation
Expand All @@ -40,59 +45,94 @@ public enum CacheStatus {
PENDING_CLEAR
}

private static final String SHARED_PREFS_NAME = "CustomInstallationIdCache";
private static final String DATA_FILE_NAME_PREFIX = "PersistedCustomInstallationId";

private static final String CUSTOM_INSTALLATION_ID_KEY = "Cid";
private static final String INSTANCE_ID_KEY = "Iid";
private static final String CACHE_STATUS_KEY = "Status";

private final SharedPreferences prefs;
private final String persistenceKey;
private final File dataFile;
private final FirebaseApp firebaseApp;

public CustomInstallationIdCache(@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();
this.firebaseApp = firebaseApp;
// Store custom installation id in different file for different FirebaseApp.
dataFile =
new File(
firebaseApp.getApplicationContext().getFilesDir(),
String.format("%s.%s.json", DATA_FILE_NAME_PREFIX, firebaseApp.getPersistenceKey()));
}

@Nullable
public synchronized CustomInstallationIdCacheEntryValue readCacheEntryValue() {
String cid = prefs.getString(getSharedPreferencesKey(CUSTOM_INSTALLATION_ID_KEY), null);
String iid = prefs.getString(getSharedPreferencesKey(INSTANCE_ID_KEY), null);
int status = prefs.getInt(getSharedPreferencesKey(CACHE_STATUS_KEY), -1);
JSONObject cidInfo = readCidInfoFromFile();
String cid = cidInfo.optString(CUSTOM_INSTALLATION_ID_KEY, null);
String iid = cidInfo.optString(INSTANCE_ID_KEY, null);
int status = cidInfo.optInt(CACHE_STATUS_KEY, -1);

if (cid == null || iid == null || status == -1) {
return null;
}

return CustomInstallationIdCacheEntryValue.create(cid, iid, CacheStatus.values()[status]);
}

private JSONObject readCidInfoFromFile() {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final byte[] tmpBuf = new byte[16 * 1024];
try (FileInputStream fis = new FileInputStream(dataFile)) {
while (true) {
int numRead = fis.read(tmpBuf, 0, tmpBuf.length);
if (numRead < 0) {
break;
}
baos.write(tmpBuf, 0, numRead);
}
return new JSONObject(baos.toString());
} catch (IOException | JSONException e) {
return new JSONObject();
}
}

/**
* Write the prefs to a JSON object, serialize them into a JSON string and write the bytes to a
* temp file. After writing and closing the temp file, rename it over to the actual
* DATA_FILE_NAME.
*/
@NonNull
public synchronized boolean insertOrUpdateCacheEntry(
@NonNull CustomInstallationIdCacheEntryValue entryValue) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(
getSharedPreferencesKey(CUSTOM_INSTALLATION_ID_KEY), entryValue.getCustomInstallationId());
editor.putString(getSharedPreferencesKey(INSTANCE_ID_KEY), entryValue.getFirebaseInstanceId());
editor.putInt(getSharedPreferencesKey(CACHE_STATUS_KEY), entryValue.getCacheStatus().ordinal());
return editor.commit();
try {
// Write the prefs into a JSON object
JSONObject json = new JSONObject();
json.put(CUSTOM_INSTALLATION_ID_KEY, entryValue.getCustomInstallationId());
json.put(INSTANCE_ID_KEY, entryValue.getFirebaseInstanceId());
json.put(CACHE_STATUS_KEY, entryValue.getCacheStatus().ordinal());
File tmpFile =
File.createTempFile(
String.format("%s.%s", DATA_FILE_NAME_PREFIX, firebaseApp.getPersistenceKey()),
"tmp",
firebaseApp.getApplicationContext().getFilesDir());

// Serialize the JSON object into a string and write the bytes to a temp file
FileOutputStream fos = new FileOutputStream(tmpFile);
fos.write(json.toString().getBytes("UTF-8"));
fos.close();

// Snapshot the temp file to the actual file
if (!tmpFile.renameTo(dataFile)) {
throw new IOException("unable to rename the tmpfile to " + dataFile.getPath());
}
} catch (JSONException | IOException e) {
return false;
}
return true;
}

@NonNull
public synchronized boolean clear() {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(getSharedPreferencesKey(CUSTOM_INSTALLATION_ID_KEY));
editor.remove(getSharedPreferencesKey(INSTANCE_ID_KEY));
editor.remove(getSharedPreferencesKey(CACHE_STATUS_KEY));
return editor.commit();
}

private String getSharedPreferencesKey(String key) {
return String.format("%s|%s", persistenceKey, key);
if (!dataFile.exists()) {
return true;
}
return dataFile.delete();
}
}