Skip to content

Commit

Permalink
Make FlutterEngineGroup support more params (flutter#107394) (flutter…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nayuta403 authored Aug 4, 2022
1 parent 86d862d commit 7b6911a
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,10 @@ private boolean isAttachedToJni() {
@NonNull Context context,
@NonNull DartEntrypoint dartEntrypoint,
@Nullable String initialRoute,
@Nullable List<String> dartEntrypointArgs) {
@Nullable List<String> dartEntrypointArgs,
@Nullable PlatformViewsController platformViewsController,
boolean automaticallyRegisterPlugins,
boolean waitForRestorationData) {
if (!isAttachedToJni()) {
throw new IllegalStateException(
"Spawn can only be called on a fully constructed FlutterEngine");
Expand All @@ -409,7 +412,11 @@ private boolean isAttachedToJni() {
context, // Context.
null, // FlutterLoader. A null value passed here causes the constructor to get it from the
// FlutterInjector.
newFlutterJNI); // FlutterJNI.
newFlutterJNI, // FlutterJNI.
platformViewsController, // PlatformViewsController.
null, // String[]. The Dart VM has already started, this arguments will have no effect.
automaticallyRegisterPlugins, // boolean.
waitForRestorationData); // boolean
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint;
import io.flutter.embedding.engine.loader.FlutterLoader;
import io.flutter.plugin.platform.PlatformViewsController;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -142,20 +143,39 @@ public FlutterEngine createAndRunEngine(@NonNull Options options) {
DartEntrypoint dartEntrypoint = options.getDartEntrypoint();
String initialRoute = options.getInitialRoute();
List<String> dartEntrypointArgs = options.getDartEntrypointArgs();
PlatformViewsController platformViewsController = options.getPlatformViewsController();
platformViewsController =
platformViewsController != null ? platformViewsController : new PlatformViewsController();
boolean automaticallyRegisterPlugins = options.getAutomaticallyRegisterPlugins();
boolean waitForRestorationData = options.getWaitForRestorationData();

if (dartEntrypoint == null) {
dartEntrypoint = DartEntrypoint.createDefault();
}

if (activeEngines.size() == 0) {
engine = createEngine(context);
engine =
createEngine(
context,
platformViewsController,
automaticallyRegisterPlugins,
waitForRestorationData);
if (initialRoute != null) {
engine.getNavigationChannel().setInitialRoute(initialRoute);
}
engine.getDartExecutor().executeDartEntrypoint(dartEntrypoint, dartEntrypointArgs);
} else {
engine =
activeEngines.get(0).spawn(context, dartEntrypoint, initialRoute, dartEntrypointArgs);
activeEngines
.get(0)
.spawn(
context,
dartEntrypoint,
initialRoute,
dartEntrypointArgs,
platformViewsController,
automaticallyRegisterPlugins,
waitForRestorationData);
}

activeEngines.add(engine);
Expand All @@ -178,8 +198,19 @@ public void onEngineWillDestroy() {
}

@VisibleForTesting
/* package */ FlutterEngine createEngine(Context context) {
return new FlutterEngine(context);
/* package */ FlutterEngine createEngine(
Context context,
@NonNull PlatformViewsController platformViewsController,
boolean automaticallyRegisterPlugins,
boolean waitForRestorationData) {
return new FlutterEngine(
context, // Context.
null, // FlutterLoader.
null, // FlutterJNI.
platformViewsController, // PlatformViewsController.
null, // String[]. The Dart VM has already started, this arguments will have no effect.
automaticallyRegisterPlugins, // boolean.
waitForRestorationData); // boolean.
}

/** Options that control how a FlutterEngine should be created. */
Expand All @@ -188,6 +219,9 @@ public static class Options {
@Nullable private DartEntrypoint dartEntrypoint;
@Nullable private String initialRoute;
@Nullable private List<String> dartEntrypointArgs;
@NonNull private PlatformViewsController platformViewsController;
private boolean automaticallyRegisterPlugins = true;
private boolean waitForRestorationData = false;

public Options(@NonNull Context context) {
this.context = context;
Expand Down Expand Up @@ -219,6 +253,28 @@ public List<String> getDartEntrypointArgs() {
return dartEntrypointArgs;
}

/** Manages platform views. */
public PlatformViewsController getPlatformViewsController() {
return platformViewsController;
}

/**
* If plugins are automatically registered, then they are registered during the {@link
* io.flutter.embedding.engine.FlutterEngine}'s constructor.
*/
public boolean getAutomaticallyRegisterPlugins() {
return automaticallyRegisterPlugins;
}

/**
* The waitForRestorationData flag controls whether the engine delays responding to requests
* from the framework for restoration data until that data has been provided to the engine via
* {@code RestorationChannel.setRestorationData(byte[] data)}.
*/
public boolean getWaitForRestorationData() {
return waitForRestorationData;
}

/**
* Setter for `dartEntrypoint` property.
*
Expand Down Expand Up @@ -251,5 +307,41 @@ public Options setDartEntrypointArgs(List<String> dartEntrypointArgs) {
this.dartEntrypointArgs = dartEntrypointArgs;
return this;
}

/**
* Setter for `platformViewsController` property.
*
* @param platformViewsController Manages platform views.
*/
public Options setPlatformViewsController(
@NonNull PlatformViewsController platformViewsController) {
this.platformViewsController = platformViewsController;
return this;
}

/**
* Setter for `automaticallyRegisterPlugins` property.
*
* @param automaticallyRegisterPlugins If plugins are automatically registered, then they are
* registered during the execution of {@link io.flutter.embedding.engine.FlutterEngine}'s
* constructor.
*/
public Options setAutomaticallyRegisterPlugins(boolean automaticallyRegisterPlugins) {
this.automaticallyRegisterPlugins = automaticallyRegisterPlugins;
return this;
}

/**
* Setter for `waitForRestorationData` property.
*
* @param waitForRestorationData The waitForRestorationData flag controls whether the engine
* delays responding to requests from the framework for restoration data until that data has
* been provided to the engine via {@code RestorationChannel.setRestorationData(byte[]
* data)}.
*/
public Options setWaitForRestorationData(boolean waitForRestorationData) {
this.waitForRestorationData = waitForRestorationData;
return this;
}
}
}
Loading

0 comments on commit 7b6911a

Please sign in to comment.