Skip to content

Http client in Firebase Segmentation SDK to call backend service. #573

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 45 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
45 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
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
47 changes: 7 additions & 40 deletions firebase-segmentation/firebase-segmentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,12 @@

plugins {
id 'firebase-library'
id 'com.google.protobuf'
}

firebaseLibrary {
testLab.enabled = true
}

protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.4.0'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.12.0'
}
javalite {
// The codegen for lite comes as a separate artifact
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
// In most cases you don't need the full Java output
// if you use the lite output.
remove java
}
task.plugins {
grpc {
option 'lite'
}
javalite {}
}
}
}
}

android {
compileSdkVersion project.targetSdkVersion

Expand All @@ -65,8 +32,11 @@ android {
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
java {
}
}
test {
java {
}
}
}
Expand All @@ -83,14 +53,11 @@ android {

dependencies {
implementation project(':firebase-common')
implementation project(':protolite-well-known-types')

implementation('com.google.firebase:firebase-iid:17.0.3') {
exclude group: "com.google.firebase", module: "firebase-common"
}
implementation 'io.grpc:grpc-stub:1.21.0'
implementation 'io.grpc:grpc-protobuf-lite:1.21.0'
implementation 'io.grpc:grpc-okhttp:1.21.0'

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.multidex:multidex:2.0.0'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
Expand All @@ -103,7 +70,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:$robolectricVersion"

androidTestImplementation "androidx.annotation:annotation:1.1.0"
androidTestImplementation "androidx.annotation:annotation:1.0.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 @@ -69,19 +69,24 @@ public void setUp() {
firebaseApp =
FirebaseApp.initializeApp(
ApplicationProvider.getApplicationContext(),
new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
new FirebaseOptions.Builder()
.setApplicationId("1:123456789:android:abcdef")
.setApiKey("api_key")
.build());
actualCache = new CustomInstallationIdCache(firebaseApp);

when(backendClientReturnsOk.updateCustomInstallationId(
anyLong(), anyString(), anyString(), anyString()))
anyLong(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.OK));
when(backendClientReturnsOk.clearCustomInstallationId(anyLong(), anyString(), anyString()))
when(backendClientReturnsOk.clearCustomInstallationId(
anyLong(), anyString(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.OK));
when(backendClientReturnsError.updateCustomInstallationId(
anyLong(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_ERROR));
when(backendClientReturnsError.clearCustomInstallationId(
anyLong(), anyString(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_INTERNAL_ERROR));
when(backendClientReturnsError.clearCustomInstallationId(anyLong(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_INTERNAL_ERROR));
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_ERROR));
when(firebaseInstanceId.getInstanceId())
.thenReturn(
Tasks.forResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private Task<Void> updateCustomInstallationId(String customInstallationId) {
String iidToken = instanceIdResultTask.getResult().getToken();
return backendServiceClient.updateCustomInstallationId(
Utils.getProjectNumberFromAppId(firebaseApp.getOptions().getApplicationId()),
firebaseApp.getOptions().getApiKey(),
customInstallationId,
iid,
iidToken);
Expand All @@ -154,7 +155,9 @@ private Task<Void> updateCustomInstallationId(String customInstallationId) {
customInstallationId,
instanceIdResultTask.getResult().getId(),
CustomInstallationIdCache.CacheStatus.SYNCED));
case ALREADY_EXISTS:
case HTTP_CLIENT_ERROR:
throw new SetCustomInstallationIdException(Status.CLIENT_ERROR);
case CONFLICT:
throw new SetCustomInstallationIdException(
Status.DUPLICATED_CUSTOM_INSTALLATION_ID);
default:
Expand Down Expand Up @@ -209,6 +212,7 @@ private Task<Void> clearCustomInstallationId() {
String iidToken = instanceIdResultTask.getResult().getToken();
return backendServiceClient.clearCustomInstallationId(
Utils.getProjectNumberFromAppId(firebaseApp.getOptions().getApplicationId()),
firebaseApp.getOptions().getApiKey(),
iid,
iidToken);
} else {
Expand All @@ -220,10 +224,13 @@ private Task<Void> clearCustomInstallationId() {
Task<Boolean> finalUpdateCacheResultTask =
backendRequestResultTask.onSuccessTask(
backendRequestResult -> {
if (backendRequestResult == Code.OK) {
return localCache.clear();
} else {
throw new SetCustomInstallationIdException(Status.BACKEND_ERROR);
switch (backendRequestResult) {
case OK:
return localCache.clear();
case HTTP_CLIENT_ERROR:
throw new SetCustomInstallationIdException(Status.CLIENT_ERROR);
default:
throw new SetCustomInstallationIdException(Status.BACKEND_ERROR);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.firebase.segmentation;

import androidx.annotation.NonNull;
import com.google.firebase.FirebaseApp;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentRegistrar;
Expand All @@ -25,6 +26,7 @@
public class FirebaseSegmentationRegistrar implements ComponentRegistrar {

@Override
@NonNull
public List<Component<?>> getComponents() {
return Arrays.asList(
Component.builder(FirebaseSegmentation.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
Expand Down Expand Up @@ -53,7 +54,7 @@ public enum CacheStatus {
private final SharedPreferences prefs;
private final String persistenceKey;

public CustomInstallationIdCache(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 =
Expand All @@ -77,8 +78,9 @@ public synchronized CustomInstallationIdCacheEntryValue readCacheEntryValue() {
return CustomInstallationIdCacheEntryValue.create(cid, iid, CacheStatus.values()[status]);
}

@NonNull
public synchronized Task<Boolean> insertOrUpdateCacheEntry(
CustomInstallationIdCacheEntryValue entryValue) {
@NonNull CustomInstallationIdCacheEntryValue entryValue) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(
getSharedPreferencesKey(CUSTOM_INSTALLATION_ID_KEY), entryValue.getCustomInstallationId());
Expand All @@ -87,6 +89,7 @@ public synchronized Task<Boolean> insertOrUpdateCacheEntry(
return commitSharedPreferencesEditAsync(editor);
}

@NonNull
public synchronized Task<Boolean> clear() {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(getSharedPreferencesKey(CUSTOM_INSTALLATION_ID_KEY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.firebase.segmentation.local;

import androidx.annotation.NonNull;
import com.google.auto.value.AutoValue;
import com.google.firebase.segmentation.local.CustomInstallationIdCache.CacheStatus;

Expand All @@ -23,14 +24,20 @@
*/
@AutoValue
public abstract class CustomInstallationIdCacheEntryValue {
@NonNull
public abstract String getCustomInstallationId();

@NonNull
public abstract String getFirebaseInstanceId();

@NonNull
public abstract CacheStatus getCacheStatus();

@NonNull
public static CustomInstallationIdCacheEntryValue create(
String customInstallationId, String firebaseInstanceId, CacheStatus cacheStatus) {
@NonNull String customInstallationId,
@NonNull String firebaseInstanceId,
@NonNull CacheStatus cacheStatus) {
return new AutoValue_CustomInstallationIdCacheEntryValue(
customInstallationId, firebaseInstanceId, cacheStatus);
}
Expand Down
Loading