5package io.flutter.embedding.engine;
8import android.content.pm.PackageManager.NameNotFoundException;
9import android.content.res.AssetManager;
10import androidx.annotation.NonNull;
11import androidx.annotation.Nullable;
12import androidx.annotation.VisibleForTesting;
13import io.flutter.FlutterInjector;
15import io.flutter.embedding.engine.dart.DartExecutor;
16import io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint;
17import io.flutter.embedding.engine.deferredcomponents.DeferredComponentManager;
18import io.flutter.embedding.engine.loader.FlutterLoader;
19import io.flutter.embedding.engine.plugins.PluginRegistry;
20import io.flutter.embedding.engine.plugins.activity.ActivityControlSurface;
21import io.flutter.embedding.engine.plugins.broadcastreceiver.BroadcastReceiverControlSurface;
22import io.flutter.embedding.engine.plugins.contentprovider.ContentProviderControlSurface;
23import io.flutter.embedding.engine.plugins.service.ServiceControlSurface;
24import io.flutter.embedding.engine.plugins.util.GeneratedPluginRegister;
25import io.flutter.embedding.engine.renderer.FlutterRenderer;
26import io.flutter.embedding.engine.renderer.RenderSurface;
27import io.flutter.embedding.engine.systemchannels.AccessibilityChannel;
28import io.flutter.embedding.engine.systemchannels.BackGestureChannel;
29import io.flutter.embedding.engine.systemchannels.DeferredComponentChannel;
30import io.flutter.embedding.engine.systemchannels.LifecycleChannel;
31import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
32import io.flutter.embedding.engine.systemchannels.MouseCursorChannel;
33import io.flutter.embedding.engine.systemchannels.NavigationChannel;
34import io.flutter.embedding.engine.systemchannels.PlatformChannel;
35import io.flutter.embedding.engine.systemchannels.ProcessTextChannel;
36import io.flutter.embedding.engine.systemchannels.RestorationChannel;
37import io.flutter.embedding.engine.systemchannels.SettingsChannel;
38import io.flutter.embedding.engine.systemchannels.SpellCheckChannel;
39import io.flutter.embedding.engine.systemchannels.SystemChannel;
40import io.flutter.embedding.engine.systemchannels.TextInputChannel;
41import io.flutter.plugin.localization.LocalizationPlugin;
42import io.flutter.plugin.platform.PlatformViewsController;
43import io.flutter.plugin.text.ProcessTextPlugin;
44import io.flutter.util.ViewUtils;
45import java.util.HashSet;
84 private static final String TAG =
"FlutterEngine";
112 @NonNull
private final Set<EngineLifecycleListener> engineLifecycleListeners =
new HashSet<>();
117 @SuppressWarnings(
"unused")
118 public void onPreEngineRestart() {
119 Log.
v(
TAG,
"onPreEngineRestart()");
121 lifecycleListener.onPreEngineRestart();
124 platformViewsController.onPreEngineRestart();
125 restorationChannel.clearData();
129 public void onEngineWillDestroy() {
169 public FlutterEngine(@NonNull Context context, @Nullable String[] dartVmArgs) {
170 this(context,
null,
null, dartVmArgs,
true);
180 @NonNull Context context,
181 @Nullable String[] dartVmArgs,
182 boolean automaticallyRegisterPlugins) {
188 automaticallyRegisterPlugins);
210 @NonNull Context context,
211 @Nullable String[] dartVmArgs,
212 boolean automaticallyRegisterPlugins,
213 boolean waitForRestorationData) {
220 automaticallyRegisterPlugins,
221 waitForRestorationData);
232 @NonNull Context context,
235 this(context, flutterLoader, flutterJNI,
null,
true);
245 @NonNull Context context,
248 @Nullable String[] dartVmArgs,
249 boolean automaticallyRegisterPlugins) {
256 automaticallyRegisterPlugins);
264 @NonNull Context context,
268 @Nullable String[] dartVmArgs,
269 boolean automaticallyRegisterPlugins) {
274 platformViewsController,
276 automaticallyRegisterPlugins,
282 @NonNull Context context,
286 @Nullable String[] dartVmArgs,
287 boolean automaticallyRegisterPlugins,
288 boolean waitForRestorationData) {
293 platformViewsController,
295 automaticallyRegisterPlugins,
296 waitForRestorationData,
300 @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
302 @NonNull Context context,
306 @Nullable String[] dartVmArgs,
307 boolean automaticallyRegisterPlugins,
308 boolean waitForRestorationData,
310 AssetManager assetManager;
312 assetManager = context.createPackageContext(context.getPackageName(), 0).getAssets();
313 }
catch (NameNotFoundException
e) {
314 assetManager = context.getAssets();
319 if (flutterJNI ==
null) {
322 this.flutterJNI = flutterJNI;
324 this.dartExecutor =
new DartExecutor(flutterJNI, assetManager);
325 this.dartExecutor.onAttachedToJNI();
338 processTextChannel =
new ProcessTextChannel(dartExecutor, context.getPackageManager());
345 if (deferredComponentManager !=
null) {
351 if (flutterLoader ==
null) {
352 flutterLoader = injector.flutterLoader();
355 if (!flutterJNI.isAttached()) {
357 flutterLoader.ensureInitializationComplete(context, dartVmArgs);
360 flutterJNI.addEngineLifecycleListener(engineLifecycleListener);
361 flutterJNI.setPlatformViewsController(platformViewsController);
362 flutterJNI.setLocalizationPlugin(localizationPlugin);
363 flutterJNI.setDeferredComponentManager(injector.deferredComponentManager());
368 if (!flutterJNI.isAttached()) {
376 this.platformViewsController = platformViewsController;
377 this.platformViewsController.onAttachedToJNI();
379 this.pluginRegistry =
381 context.getApplicationContext(),
this, flutterLoader,
group);
383 localizationPlugin.sendLocalesToFlutter(context.getResources().getConfiguration());
387 if (automaticallyRegisterPlugins && flutterLoader.automaticallyRegisterPlugins()) {
394 this.pluginRegistry.add(processTextPlugin);
397 private void attachToJni() {
398 Log.
v(TAG,
"Attaching to JNI.");
399 flutterJNI.attachToNative();
401 if (!isAttachedToJni()) {
402 throw new RuntimeException(
"FlutterEngine failed to attach to its native Object reference.");
406 @SuppressWarnings(
"BooleanMethodIsAlwaysInverted")
407 private
boolean isAttachedToJni() {
408 return flutterJNI.isAttached();
429 @NonNull Context context,
431 @Nullable String initialRoute,
434 boolean automaticallyRegisterPlugins,
435 boolean waitForRestorationData) {
436 if (!isAttachedToJni()) {
437 throw new IllegalStateException(
438 "Spawn can only be called on a fully constructed FlutterEngine");
443 dartEntrypoint.dartEntrypointFunctionName,
444 dartEntrypoint.dartEntrypointLibrary,
452 platformViewsController,
454 automaticallyRegisterPlugins,
455 waitForRestorationData);
467 listener.onEngineWillDestroy();
470 pluginRegistry.destroy();
471 platformViewsController.onDetachedFromJNI();
472 dartExecutor.onDetachedFromJNI();
473 flutterJNI.removeEngineLifecycleListener(engineLifecycleListener);
474 flutterJNI.setDeferredComponentManager(
null);
475 flutterJNI.detachFromNativeAndReleaseResources();
478 deferredComponentChannel.setDeferredComponentManager(
null);
487 engineLifecycleListeners.add(listener);
495 engineLifecycleListeners.remove(listener);
526 return accessibilityChannel;
532 return lifecycleChannel;
538 return localizationChannel;
544 return navigationChannel;
550 return backGestureChannel;
559 return platformChannel;
565 return processTextChannel;
577 return restorationChannel;
586 return settingsChannel;
592 return deferredComponentChannel;
598 return systemChannel;
604 return mouseCursorChannel;
610 return textInputChannel;
616 return spellCheckChannel;
624 return pluginRegistry;
630 return localizationPlugin;
639 return platformViewsController;
644 return pluginRegistry;
649 return pluginRegistry;
654 return pluginRegistry;
659 return pluginRegistry;
676 flutterJNI.updateDisplayMetrics(0 ,
width,
height, density);
FlutterJNI.Factory getFlutterJNIFactory()
static void v(@NonNull String tag, @NonNull String message)
PlatformViewsController getPlatformViewsController()
ActivityControlSurface getActivityControlSurface()
BackGestureChannel getBackGestureChannel()
FlutterEngine( @NonNull Context context, @Nullable FlutterLoader flutterLoader, @NonNull FlutterJNI flutterJNI)
FlutterEngine(@NonNull Context context, @Nullable String[] dartVmArgs)
RestorationChannel getRestorationChannel()
MouseCursorChannel getMouseCursorChannel()
PlatformChannel getPlatformChannel()
BroadcastReceiverControlSurface getBroadcastReceiverControlSurface()
FlutterRenderer getRenderer()
NavigationChannel getNavigationChannel()
AccessibilityChannel getAccessibilityChannel()
SettingsChannel getSettingsChannel()
FlutterEngine(@NonNull Context context)
ContentProviderControlSurface getContentProviderControlSurface()
void updateDisplayMetrics(float width, float height, float density)
FlutterEngine( @NonNull Context context, @Nullable FlutterLoader flutterLoader, @NonNull FlutterJNI flutterJNI, @NonNull PlatformViewsController platformViewsController, @Nullable String[] dartVmArgs, boolean automaticallyRegisterPlugins, boolean waitForRestorationData)
FlutterEngine spawn( @NonNull Context context, @NonNull DartEntrypoint dartEntrypoint, @Nullable String initialRoute, @Nullable List< String > dartEntrypointArgs, @Nullable PlatformViewsController platformViewsController, boolean automaticallyRegisterPlugins, boolean waitForRestorationData)
LocalizationChannel getLocalizationChannel()
LocalizationPlugin getLocalizationPlugin()
DartExecutor getDartExecutor()
ProcessTextChannel getProcessTextChannel()
LifecycleChannel getLifecycleChannel()
ServiceControlSurface getServiceControlSurface()
PluginRegistry getPlugins()
SystemChannel getSystemChannel()
TextInputChannel getTextInputChannel()
void addEngineLifecycleListener(@NonNull EngineLifecycleListener listener)
FlutterEngine( @NonNull Context context, @Nullable FlutterLoader flutterLoader, @NonNull FlutterJNI flutterJNI, @Nullable String[] dartVmArgs, boolean automaticallyRegisterPlugins)
SpellCheckChannel getSpellCheckChannel()
FlutterEngine( @NonNull Context context, @Nullable String[] dartVmArgs, boolean automaticallyRegisterPlugins, boolean waitForRestorationData)
void removeEngineLifecycleListener(@NonNull EngineLifecycleListener listener)
DeferredComponentChannel getDeferredComponentChannel()
FlutterEngine( @NonNull Context context, @Nullable String[] dartVmArgs, boolean automaticallyRegisterPlugins)
FlutterEngine( @NonNull Context context, @Nullable FlutterLoader flutterLoader, @NonNull FlutterJNI flutterJNI, @NonNull PlatformViewsController platformViewsController, @Nullable String[] dartVmArgs, boolean automaticallyRegisterPlugins)
FlutterJNI spawn( @Nullable String entrypointFunctionName, @Nullable String pathToEntrypointFunction, @Nullable String initialRoute, @Nullable List< String > entrypointArgs)
void startInitialization(@NonNull Context applicationContext)
static void registerGeneratedPlugins(@NonNull FlutterEngine flutterEngine)
static void calculateMaximumDisplayMetrics( @Nullable Context context, @NonNull DisplayUpdater updater)
void onPreEngineRestart()
void onEngineWillDestroy()
abstract void setDeferredComponentChannel(DeferredComponentChannel channel)