Skip to content

Commit 0d05282

Browse files
authored
Add Firebase Segmentation SDK and some skeleton code in Firebase Android SDK (#514)
* Add Firebase Segmentation SDK and some skeleton code in Firebase Android SDK * Add Firebase Segmentation SDK and some skeleton code in Firebase Android SDK * Address comments #1 * Address comments #1 * Address comments #2
1 parent 9d66b1f commit 0d05282

File tree

10 files changed

+373
-0
lines changed

10 files changed

+373
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
plugins {
16+
id 'firebase-library'
17+
id 'com.google.protobuf'
18+
}
19+
20+
firebaseLibrary {
21+
testLab.enabled = true
22+
}
23+
24+
protobuf {
25+
// Configure the protoc executable
26+
protoc {
27+
// Download from repositories
28+
artifact = 'com.google.protobuf:protoc:3.4.0'
29+
}
30+
plugins {
31+
grpc {
32+
artifact = 'io.grpc:protoc-gen-grpc-java:1.12.0'
33+
}
34+
javalite {
35+
// The codegen for lite comes as a separate artifact
36+
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
37+
}
38+
}
39+
generateProtoTasks {
40+
all().each { task ->
41+
task.builtins {
42+
// In most cases you don't need the full Java output
43+
// if you use the lite output.
44+
remove java
45+
}
46+
task.plugins {
47+
grpc {
48+
option 'lite'
49+
}
50+
javalite {}
51+
}
52+
}
53+
}
54+
}
55+
56+
android {
57+
compileSdkVersion project.targetSdkVersion
58+
59+
defaultConfig {
60+
minSdkVersion project.minSdkVersion
61+
targetSdkVersion project.targetSdkVersion
62+
multiDexEnabled true
63+
versionName version
64+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
65+
}
66+
sourceSets {
67+
main {
68+
proto {
69+
srcDir 'src/main/proto'
70+
}
71+
}
72+
}
73+
compileOptions {
74+
sourceCompatibility JavaVersion.VERSION_1_8
75+
targetCompatibility JavaVersion.VERSION_1_8
76+
}
77+
testOptions {
78+
unitTests {
79+
includeAndroidResources = true
80+
}
81+
}
82+
}
83+
84+
dependencies {
85+
implementation project(':firebase-common')
86+
87+
implementation('com.google.firebase:firebase-iid:17.0.3') {
88+
exclude group: "com.google.firebase", module: "firebase-common"
89+
}
90+
implementation 'androidx.appcompat:appcompat:1.0.2'
91+
implementation 'androidx.multidex:multidex:2.0.0'
92+
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
93+
94+
testImplementation 'androidx.test:core:1.2.0'
95+
testImplementation 'junit:junit:4.12'
96+
testImplementation "org.robolectric:robolectric:$robolectricVersion"
97+
98+
androidTestImplementation 'androidx.annotation:annotation:1.1.0'
99+
androidTestImplementation 'androidx.test:runner:1.2.0'
100+
androidTestImplementation 'androidx.test:rules:1.2.0'
101+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
102+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version=17.1.1

firebase-segmentation/lint.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<lint>
3+
<!--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.-->
4+
<!--See : https://github.com/grpc/grpc-java/blob/b0f423295b4674cb5247a6143fd211b050ef0065/core/src/main/java/io/grpc/internal/JndiResourceResolverFactory.java#L73-->
5+
<issue id="InvalidPackage">
6+
<ignore path="*/io.grpc/grpc-core/*"/>
7+
</issue>
8+
9+
<!-- Disable the given check in this project -->
10+
<issue id="GradleCompatible" severity="ignore" />
11+
</lint>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright 2019 Google LLC -->
3+
<!-- -->
4+
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
5+
<!-- you may not use this file except in compliance with the License. -->
6+
<!-- You may obtain a copy of the License at -->
7+
<!-- -->
8+
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
9+
<!-- -->
10+
<!-- Unless required by applicable law or agreed to in writing, software -->
11+
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
12+
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
13+
<!-- See the License for the specific language governing permissions and -->
14+
<!-- limitations under the License. -->
15+
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17+
package="com.google.firebase.segmentation">
18+
19+
<application>
20+
<uses-library android:name="android.test.runner"/>
21+
</application>
22+
23+
<instrumentation
24+
android:name="androidx.test.runner.AndroidJUnitRunner"
25+
android:targetPackage="com.google.firebase.segmentation" />
26+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.segmentation;
16+
17+
import static org.junit.Assert.assertNull;
18+
19+
import androidx.test.InstrumentationRegistry;
20+
import androidx.test.ext.junit.runners.AndroidJUnit4;
21+
import com.google.firebase.FirebaseApp;
22+
import com.google.firebase.FirebaseOptions;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
/**
28+
* Instrumented test, which will execute on an Android device.
29+
*
30+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
31+
*/
32+
@RunWith(AndroidJUnit4.class)
33+
public class FirebaseSegmentationInstrumentedTest {
34+
35+
private FirebaseApp firebaseApp;
36+
37+
@Before
38+
public void setUp() {
39+
FirebaseApp.clearInstancesForTest();
40+
firebaseApp =
41+
FirebaseApp.initializeApp(
42+
InstrumentationRegistry.getContext(),
43+
new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
44+
}
45+
46+
@Test
47+
public void useAppContext() {
48+
assertNull(FirebaseSegmentation.getInstance().setCustomInstallationId("123123").getResult());
49+
}
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright 2019 Google LLC -->
3+
<!-- -->
4+
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
5+
<!-- you may not use this file except in compliance with the License. -->
6+
<!-- You may obtain a copy of the License at -->
7+
<!-- -->
8+
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
9+
<!-- -->
10+
<!-- Unless required by applicable law or agreed to in writing, software -->
11+
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
12+
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
13+
<!-- See the License for the specific language governing permissions and -->
14+
<!-- limitations under the License. -->
15+
16+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17+
package="com.google.firebase.segmentation">
18+
19+
<application>
20+
<service
21+
android:name="com.google.firebase.components.ComponentDiscoveryService"
22+
android:exported="false">
23+
<meta-data
24+
android:name="com.google.firebase.components:com.google.firebase.segmentation.FirebaseSegmentationRegistrar"
25+
android:value="com.google.firebase.components.ComponentRegistrar" />
26+
</service>
27+
</application>
28+
</manifest>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.segmentation;
16+
17+
import androidx.annotation.NonNull;
18+
import com.google.android.gms.common.internal.Preconditions;
19+
import com.google.android.gms.tasks.Task;
20+
import com.google.android.gms.tasks.Tasks;
21+
import com.google.firebase.FirebaseApp;
22+
import com.google.firebase.iid.FirebaseInstanceId;
23+
24+
/** Entry point of Firebase Segmentation SDK. */
25+
public class FirebaseSegmentation {
26+
27+
private final FirebaseApp firebaseApp;
28+
private final FirebaseInstanceId firebaseInstanceId;
29+
30+
FirebaseSegmentation(FirebaseApp firebaseApp) {
31+
this.firebaseApp = firebaseApp;
32+
this.firebaseInstanceId = FirebaseInstanceId.getInstance(firebaseApp);
33+
}
34+
35+
/**
36+
* Returns the {@link FirebaseSegmentation} initialized with the default {@link FirebaseApp}.
37+
*
38+
* @return a {@link FirebaseSegmentation} instance
39+
*/
40+
@NonNull
41+
public static FirebaseSegmentation getInstance() {
42+
FirebaseApp defaultFirebaseApp = FirebaseApp.getInstance();
43+
return getInstance(defaultFirebaseApp);
44+
}
45+
46+
/**
47+
* Returns the {@link FirebaseSegmentation} initialized with a custom {@link FirebaseApp}.
48+
*
49+
* @param app a custom {@link FirebaseApp}
50+
* @return a {@link FirebaseSegmentation} instance
51+
*/
52+
@NonNull
53+
public static FirebaseSegmentation getInstance(@NonNull FirebaseApp app) {
54+
Preconditions.checkArgument(app != null, "Null is not a valid value of FirebaseApp.");
55+
return app.get(FirebaseSegmentation.class);
56+
}
57+
58+
Task<Void> setCustomInstallationId(String customInstallationId) {
59+
return Tasks.forResult(null);
60+
}
61+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.segmentation;
16+
17+
import com.google.firebase.FirebaseApp;
18+
import com.google.firebase.components.Component;
19+
import com.google.firebase.components.ComponentRegistrar;
20+
import com.google.firebase.components.Dependency;
21+
import com.google.firebase.platforminfo.LibraryVersionComponent;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
public class FirebaseSegmentationRegistrar implements ComponentRegistrar {
26+
27+
@Override
28+
public List<Component<?>> getComponents() {
29+
return Arrays.asList(
30+
Component.builder(FirebaseSegmentation.class)
31+
.add(Dependency.required(FirebaseApp.class))
32+
.factory(c -> new FirebaseSegmentation(c.get(FirebaseApp.class)))
33+
.build(),
34+
LibraryVersionComponent.create("fire-segmentation", BuildConfig.VERSION_NAME));
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.segmentation;
16+
17+
import static org.junit.Assert.assertNotNull;
18+
import static org.junit.Assert.assertNull;
19+
20+
import androidx.test.core.app.ApplicationProvider;
21+
import com.google.firebase.FirebaseApp;
22+
import com.google.firebase.FirebaseOptions;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.robolectric.RobolectricTestRunner;
27+
28+
@RunWith(RobolectricTestRunner.class)
29+
public class FirebaseSegmentationRegistrarTest {
30+
31+
@Before
32+
public void setUp() {
33+
FirebaseApp.clearInstancesForTest();
34+
}
35+
36+
@Test
37+
public void getFirebaseInstallationsInstance() {
38+
FirebaseApp defaultApp =
39+
FirebaseApp.initializeApp(
40+
ApplicationProvider.getApplicationContext(),
41+
new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
42+
43+
FirebaseApp anotherApp =
44+
FirebaseApp.initializeApp(
45+
ApplicationProvider.getApplicationContext(),
46+
new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(),
47+
"firebase_app_1");
48+
49+
FirebaseSegmentation defaultSegmentation = FirebaseSegmentation.getInstance();
50+
assertNotNull(defaultSegmentation);
51+
assertNull(defaultSegmentation.setCustomInstallationId("12345").getResult());
52+
53+
FirebaseSegmentation anotherSegmentation = FirebaseSegmentation.getInstance(anotherApp);
54+
assertNotNull(anotherSegmentation);
55+
assertNull(anotherSegmentation.setCustomInstallationId("ghdjaas").getResult());
56+
}
57+
}

subprojects.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ firebase-datatransport
1111
fiamui-app
1212
firebase-storage
1313
firebase-storage:test-app
14+
firebase-segmentation
1415
protolite-well-known-types
1516

1617
transport

0 commit comments

Comments
 (0)