Skip to content

Commit 86542f6

Browse files
[firebase_core] v2 embedding API (#274)
1 parent d277542 commit 86542f6

File tree

15 files changed

+180
-27
lines changed

15 files changed

+180
-27
lines changed

packages/firebase_core/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.1
2+
3+
* Support the v2 Android embedding.
4+
15
## 0.4.0+9
26

37
* Update documentation to reflect new repository location.

packages/firebase_core/android/build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,30 @@ android {
5050
}
5151
}
5252

53+
// TODO(bparrishMines): Remove this hack once androidx.lifecycle is included on stable. https://github.com/flutter/flutter/issues/42348
54+
afterEvaluate {
55+
def containsEmbeddingDependencies = false
56+
for (def configuration : configurations.all) {
57+
for (def dependency : configuration.dependencies) {
58+
if (dependency.group == 'io.flutter' &&
59+
dependency.name.startsWith('flutter_embedding') &&
60+
dependency.isTransitive())
61+
{
62+
containsEmbeddingDependencies = true
63+
break
64+
}
65+
}
66+
}
67+
if (!containsEmbeddingDependencies) {
68+
android {
69+
dependencies {
70+
def lifecycle_version = "1.1.1"
71+
api "android.arch.lifecycle:runtime:$lifecycle_version"
72+
api "android.arch.lifecycle:common:$lifecycle_version"
73+
api "android.arch.lifecycle:common-java8:$lifecycle_version"
74+
}
75+
}
76+
}
77+
}
78+
5379
apply from: file("./user-agent.gradle")

packages/firebase_core/android/gradle.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/firebase_core/android/src/main/java/io/flutter/plugins/firebase/core/FirebaseCorePlugin.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,65 @@
11
// Copyright 2017 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
54
package io.flutter.plugins.firebase.core;
65

76
import android.content.Context;
87
import com.google.firebase.FirebaseApp;
98
import com.google.firebase.FirebaseOptions;
9+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
1010
import io.flutter.plugin.common.MethodCall;
1111
import io.flutter.plugin.common.MethodChannel;
12-
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
13-
import io.flutter.plugin.common.MethodChannel.Result;
1412
import io.flutter.plugin.common.PluginRegistry;
15-
import java.lang.String;
1613
import java.util.ArrayList;
1714
import java.util.HashMap;
1815
import java.util.List;
1916
import java.util.Map;
2017

21-
public class FirebaseCorePlugin implements MethodCallHandler {
18+
/**
19+
* Flutter plugin implementation controlling the entrypoint for the Firebase SDK.
20+
*
21+
* <p>Instantiate this in an add to app scenario to gracefully handle activity and context changes.
22+
*/
23+
public class FirebaseCorePlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
24+
private static final String CHANNEL_NAME = "plugins.flutter.io/firebase_core";
2225

23-
private final Context context;
26+
private MethodChannel channel;
27+
private Context applicationContext;
2428

29+
/**
30+
* Registers a plugin with the v1 embedding api {@code io.flutter.plugin.common}.
31+
*
32+
* <p>Calling this will register the plugin with the passed registrar. However plugins initialized
33+
* this way won't react to changes in activity or context, unlike {@link FirebaseCorePlugin}.
34+
*/
2535
public static void registerWith(PluginRegistry.Registrar registrar) {
26-
final MethodChannel channel =
27-
new MethodChannel(registrar.messenger(), "plugins.flutter.io/firebase_core");
36+
final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME);
2837
channel.setMethodCallHandler(new FirebaseCorePlugin(registrar.context()));
2938
}
3039

31-
private FirebaseCorePlugin(Context context) {
32-
this.context = context;
40+
/**
41+
* Default Constructor.
42+
*
43+
* <p>Use this constructor in an add to app scenario to gracefully handle activity and context
44+
* changes.
45+
*/
46+
public FirebaseCorePlugin() {}
47+
48+
private FirebaseCorePlugin(Context applicationContext) {
49+
this.applicationContext = applicationContext;
50+
}
51+
52+
@Override
53+
public void onAttachedToEngine(FlutterPluginBinding binding) {
54+
applicationContext = binding.getApplicationContext();
55+
channel = new MethodChannel(binding.getFlutterEngine().getDartExecutor(), CHANNEL_NAME);
56+
channel.setMethodCallHandler(this);
57+
}
58+
59+
@Override
60+
public void onDetachedFromEngine(FlutterPluginBinding binding) {
61+
channel.setMethodCallHandler(null);
62+
applicationContext = null;
3363
}
3464

3565
private Map<String, Object> asMap(FirebaseApp app) {
@@ -48,7 +78,7 @@ private Map<String, Object> asMap(FirebaseApp app) {
4878
}
4979

5080
@Override
51-
public void onMethodCall(MethodCall call, final Result result) {
81+
public void onMethodCall(MethodCall call, final MethodChannel.Result result) {
5282
switch (call.method) {
5383
case "FirebaseApp#configure":
5484
{
@@ -65,14 +95,14 @@ public void onMethodCall(MethodCall call, final Result result) {
6595
.setProjectId(optionsMap.get("projectID"))
6696
.setStorageBucket(optionsMap.get("storageBucket"))
6797
.build();
68-
FirebaseApp.initializeApp(context, options, name);
98+
FirebaseApp.initializeApp(applicationContext, options, name);
6999
result.success(null);
70100
break;
71101
}
72102
case "FirebaseApp#allApps":
73103
{
74104
List<Map<String, Object>> apps = new ArrayList<>();
75-
for (FirebaseApp app : FirebaseApp.getApps(context)) {
105+
for (FirebaseApp app : FirebaseApp.getApps(applicationContext)) {
76106
apps.add(asMap(app));
77107
}
78108
result.success(apps);

packages/firebase_core/example/android/app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ flutter {
5555

5656
dependencies {
5757
testImplementation 'junit:junit:4.12'
58-
androidTestImplementation 'androidx.test:runner:1.1.1'
59-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
58+
androidTestImplementation 'androidx.test:runner:1.2.0'
59+
androidTestImplementation 'androidx.test:rules:1.2.0'
60+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
6061
}

packages/firebase_core/example/android/app/gradle.properties

Lines changed: 0 additions & 2 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.firebase.core;
6+
7+
import androidx.test.rule.ActivityTestRule;
8+
import dev.flutter.plugins.e2e.FlutterRunner;
9+
import io.flutter.plugins.firebasecoreexample.EmbeddingV1Activity;
10+
import org.junit.Rule;
11+
import org.junit.runner.RunWith;
12+
13+
@RunWith(FlutterRunner.class)
14+
public class EmbeddingV1ActivityTest {
15+
@Rule
16+
public ActivityTestRule<EmbeddingV1Activity> rule =
17+
new ActivityTestRule<>(EmbeddingV1Activity.class);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.firebase.core;
6+
7+
import androidx.test.rule.ActivityTestRule;
8+
import dev.flutter.plugins.e2e.FlutterRunner;
9+
import io.flutter.plugins.firebasecoreexample.MainActivity;
10+
import org.junit.Rule;
11+
import org.junit.runner.RunWith;
12+
13+
@RunWith(FlutterRunner.class)
14+
public class MainActivityTest {
15+
@Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
16+
}

packages/firebase_core/example/android/app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
android:label="firebase_core_example"
99
android:icon="@mipmap/ic_launcher">
1010
<activity
11-
android:name=".MainActivity"
11+
android:name=".EmbeddingV1Activity"
1212
android:launchMode="singleTop"
1313
android:theme="@style/LaunchTheme"
1414
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
@@ -17,6 +17,13 @@
1717
<meta-data
1818
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
1919
android:value="true" />
20+
</activity>
21+
<activity
22+
android:name=".MainActivity"
23+
android:theme="@style/LaunchTheme"
24+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
25+
android:hardwareAccelerated="true"
26+
android:windowSoftInputMode="adjustResize">
2027
<intent-filter>
2128
<action android:name="android.intent.action.MAIN"/>
2229
<category android:name="android.intent.category.LAUNCHER"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.flutter.plugins.firebasecoreexample;
2+
3+
import android.os.Bundle;
4+
import io.flutter.app.FlutterActivity;
5+
import io.flutter.plugins.GeneratedPluginRegistrant;
6+
7+
public class EmbeddingV1Activity extends FlutterActivity {
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
GeneratedPluginRegistrant.registerWith(this);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package io.flutter.plugins.firebasecoreexample;
22

3-
import android.os.Bundle;
4-
import io.flutter.app.FlutterActivity;
5-
import io.flutter.plugins.GeneratedPluginRegistrant;
3+
import io.flutter.embedding.android.FlutterActivity;
4+
import io.flutter.embedding.engine.FlutterEngine;
5+
import io.flutter.plugins.firebase.core.FirebaseCorePlugin;
66

77
public class MainActivity extends FlutterActivity {
8+
// TODO(bparrishMines): Remove this once v2 of GeneratedPluginRegistrant rolls to stable. https://github.com/flutter/flutter/issues/42694
89
@Override
9-
protected void onCreate(Bundle savedInstanceState) {
10-
super.onCreate(savedInstanceState);
11-
GeneratedPluginRegistrant.registerWith(this);
10+
public void configureFlutterEngine(FlutterEngine flutterEngine) {
11+
flutterEngine.getPlugins().add(new FirebaseCorePlugin());
1212
}
1313
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
org.gradle.jvmargs=-Xmx1536M
2+
android.enableR8=true
3+
android.useAndroidX=true
4+
android.enableJetifier=true

packages/firebase_core/example/pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies:
1010
cupertino_icons: ^0.1.0
1111

1212
dev_dependencies:
13+
e2e: ^0.2.1
1314
flutter_test:
1415
sdk: flutter
1516

@@ -22,3 +23,6 @@ dev_dependencies:
2223
# The following section is specific to Flutter.
2324
flutter:
2425
uses-material-design: true
26+
27+
environment:
28+
sdk: ">=2.0.0-dev.28.0 <3.0.0"

packages/firebase_core/pubspec.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Firebase Core, enabling connecting to multiple
33
Firebase apps.
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_core
6-
version: 0.4.0+9
6+
version: 0.4.1
77

88
flutter:
99
plugin:
@@ -17,9 +17,12 @@ dependencies:
1717
meta: "^1.0.5"
1818

1919
dev_dependencies:
20+
e2e: ^0.2.1
21+
flutter_driver:
22+
sdk: flutter
2023
flutter_test:
2124
sdk: flutter
2225

2326
environment:
2427
sdk: ">=2.0.0-dev.28.0 <3.0.0"
25-
flutter: ">=1.5.0 <2.0.0"
28+
flutter: ">=1.9.1+hotfix.5 <2.0.0"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:firebase_core/firebase_core.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:e2e/e2e.dart';
8+
9+
void main() {
10+
E2EWidgetsFlutterBinding.ensureInitialized();
11+
12+
testWidgets('configure', (WidgetTester tester) async {
13+
await FirebaseApp.configure(
14+
name: 'foo',
15+
options: const FirebaseOptions(
16+
googleAppID: '1:297855924061:ios:c6de2b69b03a5be8',
17+
gcmSenderID: '297855924061',
18+
apiKey: 'AIzaSyBq6mcufFXfyqr79uELCiqM_O_1-G72PVU',
19+
),
20+
);
21+
22+
final List<FirebaseApp> apps = await FirebaseApp.allApps();
23+
expect(apps, hasLength(1));
24+
25+
final FirebaseOptions options = await apps[0].options;
26+
27+
expect(options.apiKey, 'AIzaSyBq6mcufFXfyqr79uELCiqM_O_1-G72PVU');
28+
expect(options.gcmSenderID, '297855924061');
29+
expect(options.googleAppID, '1:297855924061:ios:c6de2b69b03a5be8');
30+
});
31+
}

0 commit comments

Comments
 (0)