Skip to content

Commit cf038c8

Browse files
[firebase_performance] support v2 android embedding (#266)
1 parent eac7894 commit cf038c8

File tree

17 files changed

+162
-34
lines changed

17 files changed

+162
-34
lines changed

packages/firebase_performance/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.3.1
2+
3+
* Support v2 embedding. This will remain compatible with the original embedding and won't require
4+
app migration.
5+
16
## 0.3.0+5
27

38
* Update documentation to reflect new repository location.

packages/firebase_performance/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+
compileOnly "android.arch.lifecycle:runtime:$lifecycle_version"
72+
compileOnly "android.arch.lifecycle:common:$lifecycle_version"
73+
compileOnly "android.arch.lifecycle:common-java8:$lifecycle_version"
74+
}
75+
}
76+
}
77+
}
78+
5379
apply from: file("./user-agent.gradle")

packages/firebase_performance/android/gradle.properties

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

packages/firebase_performance/android/src/main/java/io/flutter/plugins/firebaseperformance/FirebasePerformancePlugin.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,55 @@
55
package io.flutter.plugins.firebaseperformance;
66

77
import android.util.SparseArray;
8+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
89
import io.flutter.plugin.common.MethodCall;
910
import io.flutter.plugin.common.MethodChannel;
1011
import io.flutter.plugin.common.PluginRegistry.Registrar;
1112

12-
/** FirebasePerformancePlugin */
13-
public class FirebasePerformancePlugin implements MethodChannel.MethodCallHandler {
13+
/**
14+
* Flutter plugin accessing Firebase Performance API.
15+
*
16+
* <p>Instantiate this in an add to app scenario to gracefully handle activity and context changes.
17+
*/
18+
public class FirebasePerformancePlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
1419
private static final String CHANNEL_NAME = "plugins.flutter.io/firebase_performance";
1520

16-
private static final SparseArray<MethodChannel.MethodCallHandler> handlers = new SparseArray<>();
21+
private final SparseArray<MethodChannel.MethodCallHandler> handlers = new SparseArray<>();
22+
private MethodChannel channel;
1723

24+
/**
25+
* Registers a plugin with the v1 embedding api {@code io.flutter.plugin.common}.
26+
*
27+
* <p>Calling this will register the plugin with the passed registrar. However, plugins
28+
* initialized this way won't react to changes in activity or context.
29+
*
30+
* @param registrar connects this plugin's {@link
31+
* io.flutter.plugin.common.MethodChannel.MethodCallHandler} to its {@link
32+
* io.flutter.plugin.common.BinaryMessenger}.
33+
*/
1834
public static void registerWith(Registrar registrar) {
1935
final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME);
2036
channel.setMethodCallHandler(new FirebasePerformancePlugin());
2137
}
2238

39+
@Override
40+
public void onAttachedToEngine(FlutterPluginBinding binding) {
41+
channel = new MethodChannel(binding.getFlutterEngine().getDartExecutor(), CHANNEL_NAME);
42+
channel.setMethodCallHandler(this);
43+
}
44+
45+
@Override
46+
public void onDetachedFromEngine(FlutterPluginBinding binding) {
47+
channel.setMethodCallHandler(null);
48+
}
49+
2350
@Override
2451
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
2552
if (call.method.equals("FirebasePerformance#instance")) {
2653
handlers.clear();
27-
FlutterFirebasePerformance.getInstance(call, result);
54+
final Integer handle = call.argument("handle");
55+
addHandler(handle, new FlutterFirebasePerformance(this));
56+
result.success(null);
2857
} else {
2958
final MethodChannel.MethodCallHandler handler = getHandler(call);
3059

@@ -36,7 +65,7 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
3665
}
3766
}
3867

39-
static void addHandler(final int handle, final MethodChannel.MethodCallHandler handler) {
68+
void addHandler(final int handle, final MethodChannel.MethodCallHandler handler) {
4069
if (handlers.get(handle) != null) {
4170
final String message = String.format("Object for handle already exists: %s", handle);
4271
throw new IllegalArgumentException(message);
@@ -45,11 +74,11 @@ static void addHandler(final int handle, final MethodChannel.MethodCallHandler h
4574
handlers.put(handle, handler);
4675
}
4776

48-
static void removeHandler(final int handle) {
77+
void removeHandler(final int handle) {
4978
handlers.remove(handle);
5079
}
5180

52-
private static MethodChannel.MethodCallHandler getHandler(final MethodCall call) {
81+
private MethodChannel.MethodCallHandler getHandler(final MethodCall call) {
5382
final Integer handle = call.argument("handle");
5483

5584
if (handle == null) return null;

packages/firebase_performance/android/src/main/java/io/flutter/plugins/firebaseperformance/FlutterFirebasePerformance.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import io.flutter.plugin.common.MethodCall;
1111
import io.flutter.plugin.common.MethodChannel;
1212

13-
public class FlutterFirebasePerformance implements MethodChannel.MethodCallHandler {
13+
class FlutterFirebasePerformance implements MethodChannel.MethodCallHandler {
1414
private static String parseHttpMethod(String httpMethod) {
1515
switch (httpMethod) {
1616
case "HttpMethod.Connect":
@@ -36,16 +36,11 @@ private static String parseHttpMethod(String httpMethod) {
3636
}
3737
}
3838

39+
private final FirebasePerformancePlugin plugin;
3940
private final FirebasePerformance performance;
4041

41-
@SuppressWarnings("ConstantConditions")
42-
static void getInstance(MethodCall call, MethodChannel.Result result) {
43-
final Integer handle = call.argument("handle");
44-
FirebasePerformancePlugin.addHandler(handle, new FlutterFirebasePerformance());
45-
result.success(null);
46-
}
47-
48-
private FlutterFirebasePerformance() {
42+
FlutterFirebasePerformance(FirebasePerformancePlugin plugin) {
43+
this.plugin = plugin;
4944
this.performance = FirebasePerformance.getInstance();
5045
}
5146

@@ -87,7 +82,7 @@ private void newTrace(MethodCall call, MethodChannel.Result result) {
8782
final Trace trace = performance.newTrace(name);
8883

8984
final Integer handle = call.argument("traceHandle");
90-
FirebasePerformancePlugin.addHandler(handle, new FlutterTrace(trace));
85+
plugin.addHandler(handle, new FlutterTrace(plugin, trace));
9186

9287
result.success(null);
9388
}
@@ -100,7 +95,7 @@ private void newHttpMetric(MethodCall call, MethodChannel.Result result) {
10095
final HttpMetric metric = performance.newHttpMetric(url, parseHttpMethod(httpMethod));
10196

10297
final Integer handle = call.argument("httpMetricHandle");
103-
FirebasePerformancePlugin.addHandler(handle, new FlutterHttpMetric(metric));
98+
plugin.addHandler(handle, new FlutterHttpMetric(plugin, metric));
10499

105100
result.success(null);
106101
}

packages/firebase_performance/android/src/main/java/io/flutter/plugins/firebaseperformance/FlutterHttpMetric.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import io.flutter.plugin.common.MethodCall;
99
import io.flutter.plugin.common.MethodChannel;
1010

11-
public class FlutterHttpMetric implements MethodChannel.MethodCallHandler {
11+
class FlutterHttpMetric implements MethodChannel.MethodCallHandler {
12+
private final FirebasePerformancePlugin plugin;
1213
private final HttpMetric httpMetric;
1314

14-
FlutterHttpMetric(final HttpMetric metric) {
15+
FlutterHttpMetric(FirebasePerformancePlugin plugin, final HttpMetric metric) {
16+
this.plugin = plugin;
1517
this.httpMetric = metric;
1618
}
1719

@@ -60,7 +62,7 @@ private void stop(MethodCall call, MethodChannel.Result result) {
6062
httpMetric.stop();
6163

6264
final Integer handle = call.argument("handle");
63-
FirebasePerformancePlugin.removeHandler(handle);
65+
plugin.removeHandler(handle);
6466

6567
result.success(null);
6668
}

packages/firebase_performance/android/src/main/java/io/flutter/plugins/firebaseperformance/FlutterTrace.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import io.flutter.plugin.common.MethodCall;
99
import io.flutter.plugin.common.MethodChannel;
1010

11-
public class FlutterTrace implements MethodChannel.MethodCallHandler {
11+
class FlutterTrace implements MethodChannel.MethodCallHandler {
12+
private final FirebasePerformancePlugin plugin;
1213
private final Trace trace;
1314

14-
FlutterTrace(final Trace trace) {
15+
FlutterTrace(FirebasePerformancePlugin plugin, final Trace trace) {
16+
this.plugin = plugin;
1517
this.trace = trace;
1618
}
1719

@@ -57,7 +59,7 @@ private void stop(MethodCall call, MethodChannel.Result result) {
5759
trace.stop();
5860

5961
final Integer handle = call.argument("handle");
60-
FirebasePerformancePlugin.removeHandler(handle);
62+
plugin.removeHandler(handle);
6163

6264
result.success(null);
6365
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ flutter {
5252
}
5353

5454
dependencies {
55+
androidTestImplementation 'androidx.test:runner:1.2.0'
56+
androidTestImplementation 'androidx.test:rules:1.2.0'
57+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
5558
}
5659

5760
apply plugin: 'com.google.gms.google-services'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.flutter.plugins.firebaseperformance;
2+
3+
import androidx.test.rule.ActivityTestRule;
4+
import dev.flutter.plugins.e2e.FlutterRunner;
5+
import io.flutter.plugins.firebaseperformanceexample.EmbeddingV1Activity;
6+
import org.junit.Rule;
7+
import org.junit.runner.RunWith;
8+
9+
@RunWith(FlutterRunner.class)
10+
public class EmbeddingV1ActivityTest {
11+
@Rule
12+
public ActivityTestRule<EmbeddingV1Activity> rule =
13+
new ActivityTestRule<>(EmbeddingV1Activity.class);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.flutter.plugins.firebaseperformance;
2+
3+
import androidx.test.rule.ActivityTestRule;
4+
import dev.flutter.plugins.e2e.FlutterRunner;
5+
import io.flutter.plugins.firebaseperformanceexample.MainActivity;
6+
import org.junit.Rule;
7+
import org.junit.runner.RunWith;
8+
9+
@RunWith(FlutterRunner.class)
10+
public class MainActivityTest {
11+
@Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
12+
}

packages/firebase_performance/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_performance_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|screenLayout|density"
@@ -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|screenLayout|density"
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.firebaseperformanceexample;
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.firebaseperformanceexample;
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.firebaseperformance.FirebasePerformancePlugin;
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 FirebasePerformancePlugin());
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_performance/example/pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ dependencies:
1111
firebase_core: ^0.4.0
1212

1313
dev_dependencies:
14+
e2e: ^0.2.1
1415
flutter_test:
1516
sdk: flutter
1617
flutter_driver:
1718
sdk: flutter
1819

1920
flutter:
2021
uses-material-design: true
22+
23+
environment:
24+
sdk: ">=2.0.0-dev.28.0 <3.0.0"

packages/firebase_performance/pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ description: Flutter plugin for Google Performance Monitoring for Firebase, an a
44
iOS.
55
author: Flutter Team <[email protected]>
66
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_performance
7-
version: 0.3.0+5
7+
version: 0.3.1
88

99
dependencies:
1010
flutter:
1111
sdk: flutter
1212

1313
dev_dependencies:
14+
e2e: ^0.2.1
1415
http: ^0.12.0
1516
flutter_test:
1617
sdk: flutter
@@ -27,4 +28,4 @@ flutter:
2728

2829
environment:
2930
sdk: ">=2.0.0-dev.28.0 <3.0.0"
30-
flutter: ">=0.2.4 <2.0.0"
31+
flutter: ">=1.9.1+hotfix.5 <2.0.0"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:firebase_performance/firebase_performance.dart';
3+
import 'package:e2e/e2e.dart';
4+
5+
void main() {
6+
E2EWidgetsFlutterBinding.ensureInitialized();
7+
8+
testWidgets('Enable performance collection', (WidgetTester tester) async {
9+
final FirebasePerformance performance = FirebasePerformance.instance;
10+
await performance.setPerformanceCollectionEnabled(true);
11+
expect(performance.isPerformanceCollectionEnabled(), completion(isTrue));
12+
});
13+
}

0 commit comments

Comments
 (0)