Android embedding refactor pr5 add flutterengine impl (flutter/engine#7878)

This commit is contained in:
Matt Carroll
2019-02-20 17:06:33 -08:00
committed by GitHub
parent 7187e271f2
commit e3ea15b38a
2 changed files with 140 additions and 4 deletions

View File

@@ -7,6 +7,8 @@ package io.flutter.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.platform.PlatformViewRegistry;
@@ -49,6 +51,12 @@ public class FlutterPluginRegistry
mPlatformViewsController = new PlatformViewsController();
}
public FlutterPluginRegistry(FlutterEngine engine, Context context) {
// TODO(mattcarroll): implement use of engine instead of nativeView.
mAppContext = context;
mPlatformViewsController = new PlatformViewsController();
}
@Override
public boolean hasPlugin(String key) {
return mPluginMap.containsKey(key);

View File

@@ -4,6 +4,14 @@
package io.flutter.embedding.engine;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import io.flutter.app.FlutterPluginRegistry;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
/**
* A single Flutter execution environment.
*
@@ -19,16 +27,136 @@ package io.flutter.embedding.engine;
* Android app.
*
* To start running Flutter within this {@code FlutterEngine}, get a reference to this engine's
* {@link DartExecutor} and then use {@link DartExecutor#runFromBundle(FlutterRunArguments)}.
* The {@link DartExecutor#runFromBundle(FlutterRunArguments)} method must not be invoked twice on the same
* {@code FlutterEngine}.
* {@link DartExecutor} and then use {@link DartExecutor#executeDartEntrypoint(DartExecutor.DartEntrypoint)}.
* The {@link DartExecutor#executeDartEntrypoint(DartExecutor.DartEntrypoint)} method must not be
* invoked twice on the same {@code FlutterEngine}.
*
* To start rendering Flutter content to the screen, use {@link #getRenderer()} to obtain a
* {@link FlutterRenderer} and then attach a {@link FlutterRenderer.RenderSurface}. Consider using
* a {@link io.flutter.embedding.android.FlutterView} as a {@link FlutterRenderer.RenderSurface}.
*/
public class FlutterEngine {
// TODO(mattcarroll): bring in FlutterEngine implementation in future PR
private static final String TAG = "FlutterEngine";
@NonNull
private final FlutterJNI flutterJNI;
@NonNull
private final FlutterRenderer renderer;
@NonNull
private final DartExecutor dartExecutor;
// TODO(mattcarroll): integrate system channels with FlutterEngine
@NonNull
private final FlutterPluginRegistry pluginRegistry;
private final EngineLifecycleListener engineLifecycleListener = new EngineLifecycleListener() {
@SuppressWarnings("unused")
public void onPreEngineRestart() {
pluginRegistry.onPreEngineRestart();
}
};
/**
* Constructs a new {@code FlutterEngine}.
*
* A new {@code FlutterEngine} does not execute any Dart code automatically. See
* {@link #getDartExecutor()} and {@link DartExecutor#executeDartEntrypoint(DartExecutor.DartEntrypoint)}
* to begin executing Dart code within this {@code FlutterEngine}.
*
* A new {@code FlutterEngine} will not display any UI until a
* {@link io.flutter.embedding.engine.renderer.FlutterRenderer.RenderSurface} is registered. See
* {@link #getRenderer()} and {@link FlutterRenderer#attachToRenderSurface(FlutterRenderer.RenderSurface)}.
*
* A new {@code FlutterEngine} does not come with any Flutter plugins attached. To attach plugins,
* see {@link #getPluginRegistry()}.
*
* A new {@code FlutterEngine} does come with all default system channels attached.
*/
public FlutterEngine(@NonNull Context context) {
this.flutterJNI = new FlutterJNI();
flutterJNI.addEngineLifecycleListener(engineLifecycleListener);
attachToJni();
this.dartExecutor = new DartExecutor(flutterJNI);
this.dartExecutor.onAttachedToJNI();
// TODO(mattcarroll): FlutterRenderer is temporally coupled to attach(). Remove that coupling if possible.
this.renderer = new FlutterRenderer(flutterJNI);
this.pluginRegistry = new FlutterPluginRegistry(this, context);
}
private void attachToJni() {
// TODO(mattcarroll): update native call to not take in "isBackgroundView"
flutterJNI.attachToNative(false);
if (!isAttachedToJni()) {
throw new RuntimeException("FlutterEngine failed to attach to its native Object reference.");
}
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean isAttachedToJni() {
return flutterJNI.isAttached();
}
/**
* Detaches this {@code FlutterEngine} from Flutter's native implementation, but allows
* reattachment later.
*
* // TODO(mattcarroll): document use-cases for this behavior.
*/
public void detachFromJni() {
pluginRegistry.detach();
dartExecutor.onDetachedFromJNI();
flutterJNI.removeEngineLifecycleListener(engineLifecycleListener);
// TODO(mattcarroll): investigate detach vs destroy. document user-cases. update code if needed.
flutterJNI.detachFromNativeButKeepNativeResources();
}
/**
* Cleans up all components within this {@code FlutterEngine} and then detaches from Flutter's
* native implementation.
*
* This {@code FlutterEngine} instance should be discarded after invoking this method.
*/
public void destroy() {
pluginRegistry.destroy();
dartExecutor.onDetachedFromJNI();
flutterJNI.removeEngineLifecycleListener(engineLifecycleListener);
flutterJNI.detachFromNativeAndReleaseResources();
}
/**
* The Dart execution context associated with this {@code FlutterEngine}.
*
* The {@link DartExecutor} can be used to start executing Dart code from a given entrypoint.
* See {@link DartExecutor#executeDartEntrypoint(DartExecutor.DartEntrypoint)}.
*
* Use the {@link DartExecutor} to connect any desired message channels and method channels
* to facilitate communication between Android and Dart/Flutter.
*/
@NonNull
public DartExecutor getDartExecutor() {
return dartExecutor;
}
/**
* The rendering system associated with this {@code FlutterEngine}.
*
* To render a Flutter UI that is produced by this {@code FlutterEngine}'s Dart code, attach
* a {@link io.flutter.embedding.engine.renderer.FlutterRenderer.RenderSurface} to this
* {@link FlutterRenderer}.
*/
@NonNull
public FlutterRenderer getRenderer() {
return renderer;
}
// TODO(mattcarroll): propose a robust story for plugin backward compability and future facing API.
@NonNull
public FlutterPluginRegistry getPluginRegistry() {
return pluginRegistry;
}
/**
* Lifecycle callbacks for Flutter engine lifecycle events.