2018-11-07 12:24:35 -08:00
|
|
|
// Copyright 2013 The Flutter Authors. All rights reserved.
|
2017-11-09 12:10:00 -08:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package io.flutter.view;
|
|
|
|
|
|
2017-11-30 11:05:55 -08:00
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.content.Context;
|
2019-02-20 17:05:31 -08:00
|
|
|
import android.support.annotation.NonNull;
|
2017-11-09 12:10:00 -08:00
|
|
|
import android.util.Log;
|
2017-11-30 11:05:55 -08:00
|
|
|
import io.flutter.app.FlutterPluginRegistry;
|
2018-12-21 01:07:04 -05:00
|
|
|
import io.flutter.embedding.engine.FlutterJNI;
|
|
|
|
|
import io.flutter.embedding.engine.FlutterEngine.EngineLifecycleListener;
|
2019-02-20 17:05:31 -08:00
|
|
|
import io.flutter.embedding.engine.dart.DartExecutor;
|
2019-02-26 01:54:17 -08:00
|
|
|
import io.flutter.embedding.engine.renderer.FlutterRenderer;
|
2018-12-21 01:07:04 -05:00
|
|
|
import io.flutter.embedding.engine.renderer.FlutterRenderer.RenderSurface;
|
2017-11-09 12:10:00 -08:00
|
|
|
import io.flutter.plugin.common.*;
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
2018-12-21 01:07:04 -05:00
|
|
|
|
|
|
|
|
import io.flutter.embedding.engine.dart.PlatformMessageHandler;
|
2017-11-09 12:10:00 -08:00
|
|
|
|
2017-11-13 13:56:48 -08:00
|
|
|
public class FlutterNativeView implements BinaryMessenger {
|
2017-11-09 12:10:00 -08:00
|
|
|
private static final String TAG = "FlutterNativeView";
|
|
|
|
|
|
2017-11-30 11:05:55 -08:00
|
|
|
private final FlutterPluginRegistry mPluginRegistry;
|
2019-02-20 17:05:31 -08:00
|
|
|
private final DartExecutor dartExecutor;
|
2017-11-09 12:10:00 -08:00
|
|
|
private FlutterView mFlutterView;
|
2019-02-20 17:05:31 -08:00
|
|
|
private final FlutterJNI mFlutterJNI;
|
2018-03-05 14:09:45 +01:00
|
|
|
private final Context mContext;
|
2018-05-15 12:05:54 -07:00
|
|
|
private boolean applicationIsRunning;
|
2017-11-09 12:10:00 -08:00
|
|
|
|
2019-02-20 17:05:31 -08:00
|
|
|
public FlutterNativeView(@NonNull Context context) {
|
2018-08-07 12:42:22 -07:00
|
|
|
this(context, false);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 17:05:31 -08:00
|
|
|
public FlutterNativeView(@NonNull Context context, boolean isBackgroundView) {
|
2018-03-05 14:09:45 +01:00
|
|
|
mContext = context;
|
2017-11-30 11:05:55 -08:00
|
|
|
mPluginRegistry = new FlutterPluginRegistry(this, context);
|
2018-12-21 01:07:04 -05:00
|
|
|
mFlutterJNI = new FlutterJNI();
|
|
|
|
|
mFlutterJNI.setRenderSurface(new RenderSurfaceImpl());
|
2019-02-20 17:05:31 -08:00
|
|
|
this.dartExecutor = new DartExecutor(mFlutterJNI);
|
2018-12-21 01:07:04 -05:00
|
|
|
mFlutterJNI.addEngineLifecycleListener(new EngineLifecycleListenerImpl());
|
2018-08-07 12:42:22 -07:00
|
|
|
attach(this, isBackgroundView);
|
2017-11-09 12:10:00 -08:00
|
|
|
assertAttached();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 18:08:58 -07:00
|
|
|
public void detachFromFlutterView() {
|
2017-11-30 11:05:55 -08:00
|
|
|
mPluginRegistry.detach();
|
2017-11-13 13:56:48 -08:00
|
|
|
mFlutterView = null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 12:10:00 -08:00
|
|
|
public void destroy() {
|
2018-07-11 14:11:15 -07:00
|
|
|
mPluginRegistry.destroy();
|
2019-02-20 17:05:31 -08:00
|
|
|
dartExecutor.onDetachedFromJNI();
|
2017-11-09 12:10:00 -08:00
|
|
|
mFlutterView = null;
|
2018-12-21 01:07:04 -05:00
|
|
|
mFlutterJNI.detachFromNativeAndReleaseResources();
|
2018-05-15 12:05:54 -07:00
|
|
|
applicationIsRunning = false;
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
2019-02-20 17:05:31 -08:00
|
|
|
@NonNull
|
|
|
|
|
public DartExecutor getDartExecutor() {
|
|
|
|
|
return dartExecutor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@NonNull
|
2017-11-30 11:05:55 -08:00
|
|
|
public FlutterPluginRegistry getPluginRegistry() {
|
|
|
|
|
return mPluginRegistry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void attachViewAndActivity(FlutterView flutterView, Activity activity) {
|
2017-11-13 13:56:48 -08:00
|
|
|
mFlutterView = flutterView;
|
2017-11-30 11:05:55 -08:00
|
|
|
mPluginRegistry.attach(flutterView, activity);
|
2017-11-13 13:56:48 -08:00
|
|
|
}
|
|
|
|
|
|
2017-11-09 12:10:00 -08:00
|
|
|
public boolean isAttached() {
|
2018-12-21 01:07:04 -05:00
|
|
|
return mFlutterJNI.isAttached();
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void assertAttached() {
|
2018-08-07 12:42:22 -07:00
|
|
|
if (!isAttached()) throw new AssertionError("Platform view is not attached");
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
2018-08-07 12:42:22 -07:00
|
|
|
public void runFromBundle(FlutterRunArguments args) {
|
2018-12-10 14:38:44 -08:00
|
|
|
boolean hasBundlePaths = args.bundlePaths != null && args.bundlePaths.length != 0;
|
|
|
|
|
if (args.bundlePath == null && !hasBundlePaths) {
|
2018-12-10 12:19:22 -08:00
|
|
|
throw new AssertionError("Either bundlePath or bundlePaths must be specified");
|
|
|
|
|
} else if ((args.bundlePath != null || args.defaultPath != null) &&
|
2018-12-10 14:38:44 -08:00
|
|
|
hasBundlePaths) {
|
2018-12-10 12:19:22 -08:00
|
|
|
throw new AssertionError("Can't specify both bundlePath and bundlePaths");
|
2018-08-07 12:42:22 -07:00
|
|
|
} else if (args.entrypoint == null) {
|
2018-12-10 12:19:22 -08:00
|
|
|
throw new AssertionError("An entrypoint must be specified");
|
|
|
|
|
}
|
2018-12-10 14:38:44 -08:00
|
|
|
if (hasBundlePaths) {
|
2018-12-10 12:19:22 -08:00
|
|
|
runFromBundleInternal(args.bundlePaths, args.entrypoint, args.libraryPath);
|
|
|
|
|
} else {
|
|
|
|
|
runFromBundleInternal(new String[] {args.bundlePath, args.defaultPath},
|
|
|
|
|
args.entrypoint, args.libraryPath);
|
2018-08-07 12:42:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @deprecated
|
2018-08-10 13:23:51 -07:00
|
|
|
* Please use runFromBundle with `FlutterRunArguments`.
|
|
|
|
|
* Parameter `reuseRuntimeController` has no effect.
|
2018-08-07 12:42:22 -07:00
|
|
|
*/
|
2018-08-08 13:02:41 -07:00
|
|
|
@Deprecated
|
2018-08-10 13:23:51 -07:00
|
|
|
public void runFromBundle(String bundlePath, String defaultPath, String entrypoint,
|
2018-08-07 12:42:22 -07:00
|
|
|
boolean reuseRuntimeController) {
|
2018-12-10 12:19:22 -08:00
|
|
|
runFromBundleInternal(new String[] {bundlePath, defaultPath}, entrypoint, null);
|
2018-08-07 12:42:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-10 12:19:22 -08:00
|
|
|
private void runFromBundleInternal(String[] bundlePaths, String entrypoint,
|
|
|
|
|
String libraryPath) {
|
2017-11-09 12:10:00 -08:00
|
|
|
assertAttached();
|
2018-05-15 12:05:54 -07:00
|
|
|
if (applicationIsRunning)
|
2018-08-07 12:42:22 -07:00
|
|
|
throw new AssertionError(
|
|
|
|
|
"This Flutter engine instance is already running an application");
|
2018-12-21 01:07:04 -05:00
|
|
|
mFlutterJNI.runBundleAndSnapshotFromLibrary(
|
|
|
|
|
bundlePaths,
|
|
|
|
|
entrypoint,
|
|
|
|
|
libraryPath,
|
|
|
|
|
mContext.getResources().getAssets()
|
|
|
|
|
);
|
2018-05-15 12:05:54 -07:00
|
|
|
|
|
|
|
|
applicationIsRunning = true;
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-15 12:05:54 -07:00
|
|
|
public boolean isApplicationRunning() {
|
2018-08-07 12:42:22 -07:00
|
|
|
return applicationIsRunning;
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getObservatoryUri() {
|
2018-12-21 01:07:04 -05:00
|
|
|
return FlutterJNI.nativeGetObservatoryUri();
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void send(String channel, ByteBuffer message) {
|
2019-02-20 17:05:31 -08:00
|
|
|
dartExecutor.send(channel, message);
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void send(String channel, ByteBuffer message, BinaryReply callback) {
|
|
|
|
|
if (!isAttached()) {
|
|
|
|
|
Log.d(TAG, "FlutterView.send called on a detached view, channel=" + channel);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 17:05:31 -08:00
|
|
|
dartExecutor.send(channel, message, callback);
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setMessageHandler(String channel, BinaryMessageHandler handler) {
|
2019-02-20 17:05:31 -08:00
|
|
|
dartExecutor.setMessageHandler(channel, handler);
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
2018-12-21 01:07:04 -05:00
|
|
|
/*package*/ FlutterJNI getFlutterJNI() {
|
|
|
|
|
return mFlutterJNI;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-07 12:42:22 -07:00
|
|
|
private void attach(FlutterNativeView view, boolean isBackgroundView) {
|
2018-12-21 01:07:04 -05:00
|
|
|
mFlutterJNI.attachToNative(isBackgroundView);
|
2019-02-20 17:05:31 -08:00
|
|
|
dartExecutor.onAttachedToJNI();
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
2018-12-21 01:07:04 -05:00
|
|
|
private final class RenderSurfaceImpl implements RenderSurface {
|
2019-02-26 01:54:17 -08:00
|
|
|
@Override
|
|
|
|
|
public void attachToRenderer(@NonNull FlutterRenderer renderer) {
|
|
|
|
|
// Not relevant for v1 embedding.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void detachFromRenderer() {
|
|
|
|
|
// Not relevant for v1 embedding.
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 01:07:04 -05:00
|
|
|
// Called by native to update the semantics/accessibility tree.
|
|
|
|
|
public void updateSemantics(ByteBuffer buffer, String[] strings) {
|
|
|
|
|
if (mFlutterView == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mFlutterView.updateSemantics(buffer, strings);
|
|
|
|
|
}
|
2017-11-09 12:10:00 -08:00
|
|
|
|
2018-12-21 01:07:04 -05:00
|
|
|
// Called by native to update the custom accessibility actions.
|
|
|
|
|
public void updateCustomAccessibilityActions(ByteBuffer buffer, String[] strings) {
|
|
|
|
|
if (mFlutterView == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mFlutterView.updateCustomAccessibilityActions(buffer, strings);
|
|
|
|
|
}
|
2018-07-11 10:27:50 -07:00
|
|
|
|
2018-12-21 01:07:04 -05:00
|
|
|
// Called by native to notify first Flutter frame rendered.
|
|
|
|
|
public void onFirstFrameRendered() {
|
|
|
|
|
if (mFlutterView == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mFlutterView.onFirstFrame();
|
2018-11-14 11:13:39 -08:00
|
|
|
}
|
2018-08-03 08:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-21 01:07:04 -05:00
|
|
|
private final class EngineLifecycleListenerImpl implements EngineLifecycleListener {
|
|
|
|
|
// Called by native to notify when the engine is restarted (cold reload).
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
public void onPreEngineRestart() {
|
|
|
|
|
if (mFlutterView != null) {
|
|
|
|
|
mFlutterView.resetAccessibilityTree();
|
|
|
|
|
}
|
|
|
|
|
if (mPluginRegistry == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mPluginRegistry.onPreEngineRestart();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-09 12:10:00 -08:00
|
|
|
}
|