Skip to content
Merged
102 changes: 102 additions & 0 deletions firebase-segmentation/firebase-segmentation.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
1 change: 1 addition & 0 deletions firebase-segmentation/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=17.1.1
11 changes: 11 additions & 0 deletions firebase-segmentation/lint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!--gRPC's DNS name resolver checks for javax.naming at runtime to determine if it can be used, but fails gracefullt without it. This lint error is safe to ignore.-->
<!--See : https://github.com/grpc/grpc-java/blob/b0f423295b4674cb5247a6143fd211b050ef0065/core/src/main/java/io/grpc/internal/JndiResourceResolverFactory.java#L73-->
<issue id="InvalidPackage">
<ignore path="*/io.grpc/grpc-core/*"/>
</issue>

<!-- Disable the given check in this project -->
<issue id="GradleCompatible" severity="ignore" />
</lint>
26 changes: 26 additions & 0 deletions firebase-segmentation/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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. -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.segmentation">

<application>
<uses-library android:name="android.test.runner"/>
</application>

<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.google.firebase.segmentation" />
</manifest>
Original file line number Diff line number Diff line change
@@ -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 <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@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());
}
}
28 changes: 28 additions & 0 deletions firebase-segmentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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. -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.segmentation">

<application>
<service
android:name="com.google.firebase.components.ComponentDiscoveryService"
android:exported="false">
<meta-data
android:name="com.google.firebase.components:com.google.firebase.segmentation.FirebaseSegmentationRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -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<Void> setCustomInstallationId(String customInstallationId) {
return Tasks.forResult(null);
}
}
Original file line number Diff line number Diff line change
@@ -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<Component<?>> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
1 change: 1 addition & 0 deletions subprojects.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ firebase-datatransport
fiamui-app
firebase-storage
firebase-storage:test-app
firebase-segmentation
protolite-well-known-types

transport
Expand Down