Skip to content

Commit

Permalink
Migrate cloud_firestore to android v2 embedding. (firebase#2269)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidongze91 authored Apr 14, 2020
1 parent 3158071 commit 9c6fb46
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 50 deletions.
4 changes: 4 additions & 0 deletions packages/cloud_firestore/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.5

* Migrate cloud_firestore to android v2 embedding.

## 0.13.4+2

* Fix for missing UserAgent.h compilation failures.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
import com.google.firebase.firestore.Source;
import com.google.firebase.firestore.Transaction;
import com.google.firebase.firestore.WriteBatch;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
Expand All @@ -55,11 +59,11 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class CloudFirestorePlugin implements MethodCallHandler {
public class CloudFirestorePlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {

private static final String TAG = "CloudFirestorePlugin";
private final MethodChannel channel;
private final Activity activity;
private MethodChannel channel;
private Activity activity;

// Handles are ints used as indexes into the sparse array of active observers
private int nextListenerHandle = 0;
Expand All @@ -72,17 +76,18 @@ public class CloudFirestorePlugin implements MethodCallHandler {
private final SparseArray<TaskCompletionSource> completionTasks = new SparseArray<>();

public static void registerWith(PluginRegistry.Registrar registrar) {
final MethodChannel channel =
CloudFirestorePlugin instance = new CloudFirestorePlugin();
instance.activity = registrar.activity();
instance.initInstance(registrar.messenger());
}

private void initInstance(BinaryMessenger messenger) {
channel =
new MethodChannel(
registrar.messenger(),
messenger,
"plugins.flutter.io/cloud_firestore",
new StandardMethodCodec(FirestoreMessageCodec.INSTANCE));
channel.setMethodCallHandler(new CloudFirestorePlugin(channel, registrar.activity()));
}

private CloudFirestorePlugin(MethodChannel channel, Activity activity) {
this.channel = channel;
this.activity = activity;
channel.setMethodCallHandler(this);
}

private FirebaseFirestore getFirestore(Map<String, Object> arguments) {
Expand Down Expand Up @@ -464,6 +469,45 @@ public void onFailure(@NonNull Exception e) {
});
}

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
initInstance(binding.getBinaryMessenger());
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
channel = null;
}

private void attachToActivity(ActivityPluginBinding activityPluginBinding) {
this.activity = activityPluginBinding.getActivity();
}

private void detachToActivity() {
this.activity = null;
}

@Override
public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
attachToActivity(activityPluginBinding);
}

@Override
public void onDetachedFromActivityForConfigChanges() {
detachToActivity();
}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding activityPluginBinding) {
attachToActivity(activityPluginBinding);
}

@Override
public void onDetachedFromActivity() {
detachToActivity();
}

@Override
public void onMethodCall(MethodCall call, final Result result) {
switch (call.method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ flutter {

dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

apply plugin: 'com.google.gms.google-services'
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.firebase.firestoreexample;

import androidx.test.rule.ActivityTestRule;
import dev.flutter.plugins.e2e.FlutterRunner;
import io.flutter.embedding.android.FlutterActivity;
import org.junit.Rule;
import org.junit.runner.RunWith;

@RunWith(FlutterRunner.class)
public class FlutterActivityTest {
@Rule
public ActivityTestRule<FlutterActivity> rule = new ActivityTestRule<>(FlutterActivity.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

<uses-permission android:name="android.permission.INTERNET"/>

<application android:name="io.flutter.app.FlutterApplication" android:label="firestore_example" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity"
<application android:label="firestore_example" android:icon="@mipmap/ic_launcher">
<activity android:name="io.flutter.embedding.android.FlutterActivity"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
Expand All @@ -15,5 +15,13 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".EmbeddingV1Activity"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
</activity>
<meta-data android:name="flutterEmbedding" android:value="2"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.firebase.firestoreexample;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin;
import io.flutter.view.FlutterMain;

public class EmbeddingV1Activity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
FlutterMain.startInitialization(this);
super.onCreate(savedInstanceState);
CloudFirestorePlugin.registerWith(registrarFor("io.flutter.plugins.firebase.cloudfirestore"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.firebase.firebasedatabaseexample;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin;
import io.flutter.view.FlutterMain;

public class EmbeddingV1Activity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
FlutterMain.startInitialization(this);
super.onCreate(savedInstanceState);
CloudFirestorePlugin.registerWith(registrarFor("io.flutter.plugins.firebase.cloudfirestore"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dev_dependencies:
flutter_driver:
sdk: flutter
test: any
e2e: ^0.2.1

flutter:
uses-material-design: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:e2e/e2e.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() {
E2EWidgetsFlutterBinding.ensureInitialized();

testWidgets('Can initialize the plugin', (WidgetTester tester) async {
Firestore firestore = Firestore();
expect(firestore, isNotNull);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';
import 'package:flutter_driver/flutter_driver.dart';

Future<void> main() async {
final FlutterDriver driver = await FlutterDriver.connect();
final String result =
await driver.requestData(null, timeout: const Duration(minutes: 1));
await driver.close();
exit(result == 'pass' ? 0 : 1);
}
3 changes: 2 additions & 1 deletion packages/cloud_firestore/cloud_firestore/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description:
Flutter plugin for Cloud Firestore, a cloud-hosted, noSQL database with
live synchronization and offline support on Android and iOS.
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/cloud_firestore/cloud_firestore
version: 0.13.4+2
version: 0.13.5

flutter:
plugin:
Expand Down Expand Up @@ -38,6 +38,7 @@ dev_dependencies:
flutter_driver:
sdk: flutter
test: any
e2e: ^0.2.1

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
Expand Down

0 comments on commit 9c6fb46

Please sign in to comment.