diff --git a/firebase-segmentation/firebase-segmentation.gradle b/firebase-segmentation/firebase-segmentation.gradle new file mode 100644 index 00000000000..88ea12a1ed0 --- /dev/null +++ b/firebase-segmentation/firebase-segmentation.gradle @@ -0,0 +1,102 @@ +// 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. + +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 + + defaultConfig { + minSdkVersion project.minSdkVersion + targetSdkVersion project.targetSdkVersion + multiDexEnabled true + versionName version + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + sourceSets { + main { + proto { + srcDir 'src/main/proto' + } + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + testOptions { + unitTests { + includeAndroidResources = true + } + } +} + +dependencies { + implementation project(':firebase-common') + + implementation('com.google.firebase:firebase-iid:17.0.3') { + exclude group: "com.google.firebase", module: "firebase-common" + } + 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' + + testImplementation 'androidx.test:core:1.2.0' + testImplementation 'junit:junit:4.12' + testImplementation "org.robolectric:robolectric:$robolectricVersion" + + androidTestImplementation 'androidx.annotation:annotation:1.1.0' + androidTestImplementation 'androidx.test:runner:1.2.0' + androidTestImplementation 'androidx.test:rules:1.2.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' +} diff --git a/firebase-segmentation/gradle.properties b/firebase-segmentation/gradle.properties new file mode 100644 index 00000000000..752913a3eb5 --- /dev/null +++ b/firebase-segmentation/gradle.properties @@ -0,0 +1 @@ +version=17.1.1 diff --git a/firebase-segmentation/lint.xml b/firebase-segmentation/lint.xml new file mode 100644 index 00000000000..9c521180b8f --- /dev/null +++ b/firebase-segmentation/lint.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/firebase-segmentation/src/androidTest/AndroidManifest.xml b/firebase-segmentation/src/androidTest/AndroidManifest.xml new file mode 100644 index 00000000000..f3ec53d62a2 --- /dev/null +++ b/firebase-segmentation/src/androidTest/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/firebase-segmentation/src/androidTest/java/com/google/firebase/segmentation/FirebaseSegmentationInstrumentedTest.java b/firebase-segmentation/src/androidTest/java/com/google/firebase/segmentation/FirebaseSegmentationInstrumentedTest.java new file mode 100644 index 00000000000..db8fcb9b873 --- /dev/null +++ b/firebase-segmentation/src/androidTest/java/com/google/firebase/segmentation/FirebaseSegmentationInstrumentedTest.java @@ -0,0 +1,50 @@ +// 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.segmentation; + +import static org.junit.Assert.assertNull; + +import androidx.test.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class FirebaseSegmentationInstrumentedTest { + + private FirebaseApp firebaseApp; + + @Before + public void setUp() { + FirebaseApp.clearInstancesForTest(); + firebaseApp = + FirebaseApp.initializeApp( + InstrumentationRegistry.getContext(), + new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build()); + } + + @Test + public void useAppContext() { + assertNull(FirebaseSegmentation.getInstance().setCustomInstallationId("123123").getResult()); + } +} diff --git a/firebase-segmentation/src/main/AndroidManifest.xml b/firebase-segmentation/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..1502b31485f --- /dev/null +++ b/firebase-segmentation/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/firebase-segmentation/src/main/java/com/google/firebase/segmentation/FirebaseSegmentation.java b/firebase-segmentation/src/main/java/com/google/firebase/segmentation/FirebaseSegmentation.java new file mode 100644 index 00000000000..eca517db1b3 --- /dev/null +++ b/firebase-segmentation/src/main/java/com/google/firebase/segmentation/FirebaseSegmentation.java @@ -0,0 +1,61 @@ +// 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.segmentation; + +import androidx.annotation.NonNull; +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.firebase.FirebaseApp; +import com.google.firebase.iid.FirebaseInstanceId; + +/** Entry point of Firebase Segmentation SDK. */ +public class FirebaseSegmentation { + + private final FirebaseApp firebaseApp; + private final FirebaseInstanceId firebaseInstanceId; + + FirebaseSegmentation(FirebaseApp firebaseApp) { + this.firebaseApp = firebaseApp; + this.firebaseInstanceId = FirebaseInstanceId.getInstance(firebaseApp); + } + + /** + * Returns the {@link FirebaseSegmentation} initialized with the default {@link FirebaseApp}. + * + * @return a {@link FirebaseSegmentation} instance + */ + @NonNull + public static FirebaseSegmentation getInstance() { + FirebaseApp defaultFirebaseApp = FirebaseApp.getInstance(); + return getInstance(defaultFirebaseApp); + } + + /** + * Returns the {@link FirebaseSegmentation} initialized with a custom {@link FirebaseApp}. + * + * @param app a custom {@link FirebaseApp} + * @return a {@link FirebaseSegmentation} instance + */ + @NonNull + public static FirebaseSegmentation getInstance(@NonNull FirebaseApp app) { + Preconditions.checkArgument(app != null, "Null is not a valid value of FirebaseApp."); + return app.get(FirebaseSegmentation.class); + } + + Task setCustomInstallationId(String customInstallationId) { + return Tasks.forResult(null); + } +} diff --git a/firebase-segmentation/src/main/java/com/google/firebase/segmentation/FirebaseSegmentationRegistrar.java b/firebase-segmentation/src/main/java/com/google/firebase/segmentation/FirebaseSegmentationRegistrar.java new file mode 100644 index 00000000000..7d1d5fcfaab --- /dev/null +++ b/firebase-segmentation/src/main/java/com/google/firebase/segmentation/FirebaseSegmentationRegistrar.java @@ -0,0 +1,36 @@ +// 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.segmentation; + +import com.google.firebase.FirebaseApp; +import com.google.firebase.components.Component; +import com.google.firebase.components.ComponentRegistrar; +import com.google.firebase.components.Dependency; +import com.google.firebase.platforminfo.LibraryVersionComponent; +import java.util.Arrays; +import java.util.List; + +public class FirebaseSegmentationRegistrar implements ComponentRegistrar { + + @Override + public List> getComponents() { + return Arrays.asList( + Component.builder(FirebaseSegmentation.class) + .add(Dependency.required(FirebaseApp.class)) + .factory(c -> new FirebaseSegmentation(c.get(FirebaseApp.class))) + .build(), + LibraryVersionComponent.create("fire-segmentation", BuildConfig.VERSION_NAME)); + } +} diff --git a/firebase-segmentation/src/test/java/com/google/firebase/segmentation/FirebaseSegmentationRegistrarTest.java b/firebase-segmentation/src/test/java/com/google/firebase/segmentation/FirebaseSegmentationRegistrarTest.java new file mode 100644 index 00000000000..1f3c441808f --- /dev/null +++ b/firebase-segmentation/src/test/java/com/google/firebase/segmentation/FirebaseSegmentationRegistrarTest.java @@ -0,0 +1,57 @@ +// 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.segmentation; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import androidx.test.core.app.ApplicationProvider; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public class FirebaseSegmentationRegistrarTest { + + @Before + public void setUp() { + FirebaseApp.clearInstancesForTest(); + } + + @Test + public void getFirebaseInstallationsInstance() { + FirebaseApp defaultApp = + FirebaseApp.initializeApp( + ApplicationProvider.getApplicationContext(), + new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build()); + + FirebaseApp anotherApp = + FirebaseApp.initializeApp( + ApplicationProvider.getApplicationContext(), + new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(), + "firebase_app_1"); + + FirebaseSegmentation defaultSegmentation = FirebaseSegmentation.getInstance(); + assertNotNull(defaultSegmentation); + assertNull(defaultSegmentation.setCustomInstallationId("12345").getResult()); + + FirebaseSegmentation anotherSegmentation = FirebaseSegmentation.getInstance(anotherApp); + assertNotNull(anotherSegmentation); + assertNull(anotherSegmentation.setCustomInstallationId("ghdjaas").getResult()); + } +} diff --git a/subprojects.cfg b/subprojects.cfg index 811bd1c0d99..f949f8c6b2a 100644 --- a/subprojects.cfg +++ b/subprojects.cfg @@ -11,6 +11,7 @@ firebase-datatransport fiamui-app firebase-storage firebase-storage:test-app +firebase-segmentation protolite-well-known-types transport