Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FlutterNativeView.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.view;
6
7import android.app.Activity;
8import android.content.Context;
9import androidx.annotation.NonNull;
10import androidx.annotation.UiThread;
11import io.flutter.Log;
12import io.flutter.app.FlutterPluginRegistry;
13import io.flutter.embedding.engine.FlutterEngine.EngineLifecycleListener;
14import io.flutter.embedding.engine.FlutterJNI;
15import io.flutter.embedding.engine.dart.DartExecutor;
16import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
17import io.flutter.plugin.common.*;
18import java.nio.ByteBuffer;
19
20/**
21 * @deprecated {@link io.flutter.embedding.android.FlutterView} is the new API that now replaces
22 * this class. See https://flutter.dev/go/android-project-migration for more migration details.
23 */
24@Deprecated
25public class FlutterNativeView implements BinaryMessenger {
26 private static final String TAG = "FlutterNativeView";
27
28 private final FlutterPluginRegistry mPluginRegistry;
29 private final DartExecutor dartExecutor;
30 private FlutterView mFlutterView;
31 private final FlutterJNI mFlutterJNI;
32 private final Context mContext;
33 private boolean applicationIsRunning;
34
35 private final FlutterUiDisplayListener flutterUiDisplayListener =
36 new FlutterUiDisplayListener() {
37 @Override
38 public void onFlutterUiDisplayed() {
39 if (mFlutterView == null) {
40 return;
41 }
42 mFlutterView.onFirstFrame();
43 }
44
45 @Override
46 public void onFlutterUiNoLongerDisplayed() {
47 // no-op
48 }
49 };
50
51 public FlutterNativeView(@NonNull Context context) {
52 this(context, false);
53 }
54
55 public FlutterNativeView(@NonNull Context context, boolean isBackgroundView) {
56 if (isBackgroundView) {
57 Log.w(TAG, "'isBackgroundView' is no longer supported and will be ignored");
58 }
59 mContext = context;
60 mPluginRegistry = new FlutterPluginRegistry(this, context);
61 mFlutterJNI = new FlutterJNI();
62 mFlutterJNI.addIsDisplayingFlutterUiListener(flutterUiDisplayListener);
63 this.dartExecutor = new DartExecutor(mFlutterJNI, context.getAssets());
64 mFlutterJNI.addEngineLifecycleListener(new EngineLifecycleListenerImpl());
65 attach(this);
67 }
68
69 public void detachFromFlutterView() {
70 mPluginRegistry.detach();
71 mFlutterView = null;
72 }
73
74 public void destroy() {
75 mPluginRegistry.destroy();
76 dartExecutor.onDetachedFromJNI();
77 mFlutterView = null;
78 mFlutterJNI.removeIsDisplayingFlutterUiListener(flutterUiDisplayListener);
79 mFlutterJNI.detachFromNativeAndReleaseResources();
80 applicationIsRunning = false;
81 }
82
83 @NonNull
84 public DartExecutor getDartExecutor() {
85 return dartExecutor;
86 }
87
88 @NonNull
89 public FlutterPluginRegistry getPluginRegistry() {
90 return mPluginRegistry;
91 }
92
93 public void attachViewAndActivity(FlutterView flutterView, Activity activity) {
94 mFlutterView = flutterView;
95 mPluginRegistry.attach(flutterView, activity);
96 }
97
98 public boolean isAttached() {
99 return mFlutterJNI.isAttached();
100 }
101
102 public void assertAttached() {
103 if (!isAttached()) throw new AssertionError("Platform view is not attached");
104 }
105
107 if (args.entrypoint == null) {
108 throw new AssertionError("An entrypoint must be specified");
109 }
111 if (applicationIsRunning)
112 throw new AssertionError("This Flutter engine instance is already running an application");
113 mFlutterJNI.runBundleAndSnapshotFromLibrary(
114 args.bundlePath,
115 args.entrypoint,
116 args.libraryPath,
117 mContext.getResources().getAssets(),
118 null);
119
120 applicationIsRunning = true;
121 }
122
123 public boolean isApplicationRunning() {
124 return applicationIsRunning;
125 }
126
127 @Deprecated
128 public static String getObservatoryUri() {
129 return FlutterJNI.getVMServiceUri();
130 }
131
132 public static String getVMServiceUri() {
133 return FlutterJNI.getVMServiceUri();
134 }
135
136 @Override
137 @UiThread
138 public TaskQueue makeBackgroundTaskQueue(TaskQueueOptions options) {
139 return dartExecutor.getBinaryMessenger().makeBackgroundTaskQueue(options);
140 }
141
142 @Override
143 @UiThread
144 public void send(String channel, ByteBuffer message) {
145 dartExecutor.getBinaryMessenger().send(channel, message);
146 }
147
148 @Override
149 @UiThread
150 public void send(String channel, ByteBuffer message, BinaryReply callback) {
151 if (!isAttached()) {
152 Log.d(TAG, "FlutterView.send called on a detached view, channel=" + channel);
153 return;
154 }
155
156 dartExecutor.getBinaryMessenger().send(channel, message, callback);
157 }
158
159 @Override
160 @UiThread
161 public void setMessageHandler(String channel, BinaryMessageHandler handler) {
162 dartExecutor.getBinaryMessenger().setMessageHandler(channel, handler);
163 }
164
165 @Override
166 @UiThread
167 public void setMessageHandler(String channel, BinaryMessageHandler handler, TaskQueue taskQueue) {
168 dartExecutor.getBinaryMessenger().setMessageHandler(channel, handler, taskQueue);
169 }
170
171 @Override
173
174 @Override
176
177 /*package*/ FlutterJNI getFlutterJNI() {
178 return mFlutterJNI;
179 }
180
181 private void attach(FlutterNativeView view) {
182 mFlutterJNI.attachToNative();
183 dartExecutor.onAttachedToJNI();
184 }
185
186 private final class EngineLifecycleListenerImpl implements EngineLifecycleListener {
187 // Called by native to notify right before the engine is restarted (cold reload).
188 @SuppressWarnings("unused")
189 public void onPreEngineRestart() {
190 if (mFlutterView != null) {
191 mFlutterView.resetAccessibilityTree();
192 }
193 if (mPluginRegistry == null) {
194 return;
195 }
196 mPluginRegistry.onPreEngineRestart();
197 }
198
199 public void onEngineWillDestroy() {
200 // The old embedding doesn't actually have a FlutterEngine. It interacts with the JNI
201 // directly.
202 }
203 }
204}
const char * options
static void d(@NonNull String tag, @NonNull String message)
Definition Log.java:64
static void w(@NonNull String tag, @NonNull String message)
Definition Log.java:76
void setMessageHandler(String channel, BinaryMessageHandler handler)
FlutterNativeView(@NonNull Context context, boolean isBackgroundView)
void send(String channel, ByteBuffer message, BinaryReply callback)
void send(String channel, ByteBuffer message)
FlutterPluginRegistry getPluginRegistry()
void attachViewAndActivity(FlutterView flutterView, Activity activity)
FlutterNativeView(@NonNull Context context)
void setMessageHandler(String channel, BinaryMessageHandler handler, TaskQueue taskQueue)
TaskQueue makeBackgroundTaskQueue(TaskQueueOptions options)
void runFromBundle(FlutterRunArguments args)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
Win32Message message
#define TAG()