Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FlutterPlugin.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.embedding.engine.plugins;
6
7import android.content.Context;
8import androidx.annotation.NonNull;
9import androidx.annotation.Nullable;
10import androidx.lifecycle.Lifecycle;
11import io.flutter.embedding.engine.FlutterEngine;
12import io.flutter.embedding.engine.FlutterEngineGroup;
13import io.flutter.plugin.common.BinaryMessenger;
14import io.flutter.plugin.platform.PlatformViewRegistry;
15import io.flutter.view.TextureRegistry;
16
17/**
18 * Interface to be implemented by all Flutter plugins.
19 *
20 * <p>A Flutter plugin allows Flutter developers to interact with a host platform, e.g., Android and
21 * iOS, via Dart code. It includes platform code, as well as Dart code. A plugin author is
22 * responsible for setting up an appropriate {@link io.flutter.plugin.common.MethodChannel} to
23 * communicate between platform code and Dart code.
24 *
25 * <p>A Flutter plugin has a lifecycle. First, a developer must add a {@code FlutterPlugin} to an
26 * instance of {@link io.flutter.embedding.engine.FlutterEngine}. To do this, obtain a {@link
27 * PluginRegistry} with {@link FlutterEngine#getPlugins()}, then call {@link
28 * PluginRegistry#add(FlutterPlugin)}, passing the instance of the Flutter plugin. During the call
29 * to {@link PluginRegistry#add(FlutterPlugin)}, the {@link
30 * io.flutter.embedding.engine.FlutterEngine} will invoke {@link
31 * #onAttachedToEngine(FlutterPluginBinding)} on the given {@code FlutterPlugin}. If the {@code
32 * FlutterPlugin} is removed from the {@link io.flutter.embedding.engine.FlutterEngine} via {@link
33 * PluginRegistry#remove(Class)}, or if the {@link io.flutter.embedding.engine.FlutterEngine} is
34 * destroyed, the {@link FlutterEngine} will invoke {@link
35 * FlutterPlugin#onDetachedFromEngine(FlutterPluginBinding)} on the given {@code FlutterPlugin}.
36 *
37 * <p>Once a {@code FlutterPlugin} is attached to a {@link
38 * io.flutter.embedding.engine.FlutterEngine}, the plugin's code is permitted to access and invoke
39 * methods on resources within the {@link FlutterPluginBinding} that the {@link
40 * io.flutter.embedding.engine.FlutterEngine} gave to the {@code FlutterPlugin} in {@link
41 * #onAttachedToEngine(FlutterPluginBinding)}. This includes, for example, the application {@link
42 * Context} for the running app.
43 *
44 * <p>The {@link FlutterPluginBinding} provided in {@link #onAttachedToEngine(FlutterPluginBinding)}
45 * is no longer valid after the execution of {@link #onDetachedFromEngine(FlutterPluginBinding)}. Do
46 * not access any properties of the {@link FlutterPluginBinding} after the completion of {@link
47 * #onDetachedFromEngine(FlutterPluginBinding)}.
48 *
49 * <p>To register a {@link io.flutter.plugin.common.MethodChannel}, obtain a {@link BinaryMessenger}
50 * via the {@link FlutterPluginBinding}.
51 *
52 * <p>An Android Flutter plugin may require access to app resources or other artifacts that can only
53 * be retrieved through a {@link Context}. Developers can access the application context via {@link
54 * FlutterPluginBinding#getApplicationContext()}.
55 *
56 * <p>Some plugins may require access to the {@code Activity} that is displaying a Flutter
57 * experience, or may need to react to {@code Activity} lifecycle events, e.g., {@code onCreate()},
58 * {@code onStart()}, {@code onResume()}, {@code onPause()}, {@code onStop()}, {@code onDestroy()}.
59 * Any such plugin should implement {@link
60 * io.flutter.embedding.engine.plugins.activity.ActivityAware} in addition to implementing {@code
61 * FlutterPlugin}. {@code ActivityAware} provides callback hooks that expose access to an associated
62 * {@code Activity} and its {@code Lifecycle}. All plugins must respect the possibility that a
63 * Flutter experience may never be associated with an {@code Activity}, e.g., when Flutter is used
64 * for background behavior. Additionally, all plugins must respect that a {@code Activity}s may come
65 * and go over time, thus requiring plugins to cleanup resources and recreate those resources as the
66 * {@code Activity} comes and goes.
67 */
68public interface FlutterPlugin {
69
70 /**
71 * This {@code FlutterPlugin} has been associated with a {@link
72 * io.flutter.embedding.engine.FlutterEngine} instance.
73 *
74 * <p>Relevant resources that this {@code FlutterPlugin} may need are provided via the {@code
75 * binding}. The {@code binding} may be cached and referenced until {@link
76 * #onDetachedFromEngine(FlutterPluginBinding)} is invoked and returns.
77 */
79
80 /**
81 * This {@code FlutterPlugin} has been removed from a {@link
82 * io.flutter.embedding.engine.FlutterEngine} instance.
83 *
84 * <p>The {@code binding} passed to this method is the same instance that was passed in {@link
85 * #onAttachedToEngine(FlutterPluginBinding)}. It is provided again in this method as a
86 * convenience. The {@code binding} may be referenced during the execution of this method, but it
87 * must not be cached or referenced after this method returns.
88 *
89 * <p>{@code FlutterPlugin}s should release all resources in this method.
90 */
92
93 /**
94 * Resources made available to all plugins registered with a given {@link
95 * io.flutter.embedding.engine.FlutterEngine}.
96 *
97 * <p>The provided {@link BinaryMessenger} can be used to communicate with Dart code running in
98 * the Flutter context associated with this plugin binding.
99 *
100 * <p>Plugins that need to respond to {@code Lifecycle} events should implement the additional
101 * {@link io.flutter.embedding.engine.plugins.activity.ActivityAware} and/or {@link
102 * io.flutter.embedding.engine.plugins.service.ServiceAware} interfaces, where a {@link Lifecycle}
103 * reference can be obtained.
104 */
106 private final Context applicationContext;
107 private final FlutterEngine flutterEngine;
108 private final BinaryMessenger binaryMessenger;
109 private final TextureRegistry textureRegistry;
110 private final PlatformViewRegistry platformViewRegistry;
111 private final FlutterAssets flutterAssets;
112 private final FlutterEngineGroup group;
113
115 @NonNull Context applicationContext,
116 @NonNull FlutterEngine flutterEngine,
117 @NonNull BinaryMessenger binaryMessenger,
118 @NonNull TextureRegistry textureRegistry,
119 @NonNull PlatformViewRegistry platformViewRegistry,
120 @NonNull FlutterAssets flutterAssets,
121 @Nullable FlutterEngineGroup group) {
122 this.applicationContext = applicationContext;
123 this.flutterEngine = flutterEngine;
124 this.binaryMessenger = binaryMessenger;
125 this.textureRegistry = textureRegistry;
126 this.platformViewRegistry = platformViewRegistry;
127 this.flutterAssets = flutterAssets;
128 this.group = group;
129 }
130
131 @NonNull
133 return applicationContext;
134 }
135
136 /**
137 * @deprecated Use {@code getBinaryMessenger()}, {@code getTextureRegistry()}, or {@code
138 * getPlatformViewRegistry()} instead.
139 */
140 @Deprecated
141 @NonNull
143 return flutterEngine;
144 }
145
146 @NonNull
147 public BinaryMessenger getBinaryMessenger() {
148 return binaryMessenger;
149 }
150
151 @NonNull
152 public TextureRegistry getTextureRegistry() {
153 return textureRegistry;
154 }
155
156 @NonNull
157 public PlatformViewRegistry getPlatformViewRegistry() {
158 return platformViewRegistry;
159 }
160
161 @NonNull
163 return flutterAssets;
164 }
165
166 /**
167 * Accessor for the {@link FlutterEngineGroup} used to create the {@link FlutterEngine} for the
168 * app.
169 *
170 * <p>This is useful in the rare case that a plugin has to spawn its own engine (for example,
171 * running an engine the background). The result is nullable since old versions of Flutter and
172 * custom setups may not have used a {@link FlutterEngineGroup}. Failing to use this when it is
173 * available will result in suboptimal performance and odd behaviors related to Dart isolate
174 * groups.
175 */
176 @Nullable
178 return group;
179 }
180 }
181
182 /** Provides Flutter plugins with access to Flutter asset information. */
183 interface FlutterAssets {
184 /**
185 * Returns the relative file path to the Flutter asset with the given name, including the file's
186 * extension, e.g., {@code "myImage.jpg"}.
187 *
188 * <p>The returned file path is relative to the Android app's standard assets directory.
189 * Therefore, the returned path is appropriate to pass to Android's {@code AssetManager}, but
190 * the path is not appropriate to load as an absolute path.
191 */
192 String getAssetFilePathByName(@NonNull String assetFileName);
193
194 /**
195 * Same as {@link #getAssetFilePathByName(String)} but with added support for an explicit
196 * Android {@code packageName}.
197 */
198 String getAssetFilePathByName(@NonNull String assetFileName, @NonNull String packageName);
199
200 /**
201 * Returns the relative file path to the Flutter asset with the given subpath, including the
202 * file's extension, e.g., {@code "/dir1/dir2/myImage.jpg"}.
203 *
204 * <p>The returned file path is relative to the Android app's standard assets directory.
205 * Therefore, the returned path is appropriate to pass to Android's {@code AssetManager}, but
206 * the path is not appropriate to load as an absolute path.
207 */
208 String getAssetFilePathBySubpath(@NonNull String assetSubpath);
209
210 /**
211 * Same as {@link #getAssetFilePathBySubpath(String)} but with added support for an explicit
212 * Android {@code packageName}.
213 */
214 String getAssetFilePathBySubpath(@NonNull String assetSubpath, @NonNull String packageName);
215 }
216}
FlutterPluginBinding( @NonNull Context applicationContext, @NonNull FlutterEngine flutterEngine, @NonNull BinaryMessenger binaryMessenger, @NonNull TextureRegistry textureRegistry, @NonNull PlatformViewRegistry platformViewRegistry, @NonNull FlutterAssets flutterAssets, @Nullable FlutterEngineGroup group)
String getAssetFilePathBySubpath(@NonNull String assetSubpath)
String getAssetFilePathBySubpath(@NonNull String assetSubpath, @NonNull String packageName)
String getAssetFilePathByName(@NonNull String assetFileName)
String getAssetFilePathByName(@NonNull String assetFileName, @NonNull String packageName)
void onAttachedToEngine(@NonNull FlutterPluginBinding binding)
void onDetachedFromEngine(@NonNull FlutterPluginBinding binding)