Skip to content

Commit 83d90ba

Browse files
committed
Add Firebase Segmentation SDK and some skeleton code in Firebase Android SDK
1 parent c49e106 commit 83d90ba

File tree

10 files changed

+366
-0
lines changed

10 files changed

+366
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
protobuf {
21+
// Configure the protoc executable
22+
protoc {
23+
// Download from repositories
24+
artifact = 'com.google.protobuf:protoc:3.4.0'
25+
}
26+
plugins {
27+
grpc {
28+
artifact = 'io.grpc:protoc-gen-grpc-java:1.12.0'
29+
}
30+
javalite {
31+
// The codegen for lite comes as a separate artifact
32+
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
33+
}
34+
}
35+
generateProtoTasks {
36+
all().each { task ->
37+
task.builtins {
38+
// In most cases you don't need the full Java output
39+
// if you use the lite output.
40+
remove java
41+
}
42+
task.plugins {
43+
grpc {
44+
option 'lite'
45+
}
46+
javalite {}
47+
}
48+
}
49+
}
50+
}
51+
52+
android {
53+
compileSdkVersion project.targetSdkVersion
54+
55+
defaultConfig {
56+
minSdkVersion project.minSdkVersion
57+
targetSdkVersion project.targetSdkVersion
58+
multiDexEnabled true
59+
versionName version
60+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
61+
}
62+
sourceSets {
63+
main {
64+
proto {
65+
srcDir 'src/main/proto'
66+
}
67+
}
68+
}
69+
compileOptions {
70+
sourceCompatibility JavaVersion.VERSION_1_8
71+
targetCompatibility JavaVersion.VERSION_1_8
72+
}
73+
testOptions {
74+
unitTests {
75+
includeAndroidResources = true
76+
}
77+
}
78+
}
79+
80+
dependencies {
81+
implementation project(':firebase-common')
82+
83+
implementation ('com.google.firebase:firebase-iid:17.0.3') {
84+
exclude group: "com.google.firebase", module: "firebase-common"
85+
}
86+
implementation 'com.android.support:appcompat-v7:28.0.0'
87+
implementation 'com.android.support:multidex:1.0.3'
88+
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
89+
90+
testImplementation 'androidx.test:core:1.0.0'
91+
testImplementation 'junit:junit:4.12'
92+
testImplementation "org.robolectric:robolectric:$robolectricVersion"
93+
94+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
95+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version=17.1.1

firebase-segmentation/lint.xml

+11
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>
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="android.support.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.assertNotNull;
18+
19+
import androidx.test.InstrumentationRegistry;
20+
import androidx.test.runner.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+
assertNotNull(FirebaseSegmentation.getInstance().setCustomInstallationId("123123").getResult());
49+
}
50+
}
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>
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+
}
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

+1
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)