Skip to content

Commit 55c64a9

Browse files
Point old plugin registry accessors to new embedding plugin accessors. (flutter#44225) (flutter#13739)
1 parent 20e3c5b commit 55c64a9

File tree

3 files changed

+124
-5
lines changed

3 files changed

+124
-5
lines changed

shell/platform/android/io/flutter/embedding/engine/plugins/FlutterPlugin.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@
5050
* be retrieved through a {@link Context}. Developers can access the application context via
5151
* {@link FlutterPluginBinding#getApplicationContext()}.
5252
* <p>
53-
* TODO(mattcarroll): explain ActivityAware when it's added to the new plugin API surface.
53+
* Some plugins may require access to the {@code Activity} that is displaying a Flutter experience,
54+
* or may need to react to {@code Activity} lifecycle events, e.g., {@code onCreate()},
55+
* {@code onStart()}, {@code onResume()}, {@code onPause()}, {@code onStop()}, {@code onDestroy()}.
56+
* Any such plugin should implement
57+
* {@link io.flutter.embedding.engine.plugins.activity.ActivityAware} in addition to implementing
58+
* {@code FlutterPlugin}. {@code ActivityAware} provides callback hooks that expose access to an
59+
* associated {@code Activity} and its {@code Lifecycle}. All plugins must respect the possibility
60+
* that a Flutter experience may never be associated with an {@code Activity}, e.g., when Flutter
61+
* is used for background behavior. Additionally, all plugins must respect that a {@code Activity}s
62+
* may come and go over time, thus requiring plugins to cleanup resources and recreate those
63+
* resources as the {@code Activity} comes and goes.
5464
*/
5565
public interface FlutterPlugin {
5666

shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityPluginBinding.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
/**
1515
* Binding that gives {@link ActivityAware} plugins access to an associated {@link Activity} and
1616
* the {@link Activity}'s lifecycle methods.
17+
* <p>
18+
* To obtain an instance of an {@code ActivityPluginBinding} in a Flutter plugin, implement the
19+
* {@link ActivityAware} interface. A binding is provided in {@link ActivityAware#onAttachedToActivity(ActivityPluginBinding)}
20+
* and {@link ActivityAware#onReattachedToActivityForConfigChanges(ActivityPluginBinding)}.
1721
*/
1822
public interface ActivityPluginBinding {
1923

shell/platform/android/io/flutter/plugin/common/PluginRegistry.java

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import android.app.Activity;
88
import android.content.Context;
99
import android.content.Intent;
10+
11+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
12+
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
13+
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
1014
import io.flutter.plugin.platform.PlatformViewRegistry;
1115
import io.flutter.view.FlutterNativeView;
1216
import io.flutter.view.FlutterView;
@@ -59,6 +63,9 @@ public interface PluginRegistry {
5963

6064
/**
6165
* Receiver of registrations from a single plugin.
66+
*
67+
* <p>This registrar is for Flutter's v1 embedding. For instructions on migrating a plugin from
68+
* Flutter's v1 Android embedding to v2, visit http://flutter.dev/go/android-plugin-migration
6269
*/
6370
interface Registrar {
6471
/**
@@ -73,43 +80,92 @@ interface Registrar {
7380
* <p>When there is no foreground activity in the application, this
7481
* will return null. If a {@link Context} is needed, use context() to
7582
* get the application's context.</p>
83+
*
84+
* <p>This registrar is for Flutter's v1 embedding. To access an {@code Activity} from a
85+
* plugin using the v2 embedding, see {@link ActivityPluginBinding#getActivity()}. To obtain
86+
* an instance of an {@link ActivityPluginBinding} in a Flutter plugin, implement the
87+
* {@link ActivityAware} interface. A binding is provided in {@link ActivityAware#onAttachedToActivity(ActivityPluginBinding)}
88+
* and {@link ActivityAware#onReattachedToActivityForConfigChanges(ActivityPluginBinding)}.
89+
*
90+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
91+
* visit http://flutter.dev/go/android-plugin-migration
7692
*/
7793
Activity activity();
7894

7995
/**
8096
* Returns the {@link android.app.Application}'s {@link Context}.
97+
*
98+
* <p>This registrar is for Flutter's v1 embedding. To access a {@code Context} from a
99+
* plugin using the v2 embedding, see {@link FlutterPlugin.FlutterPluginBinding#getApplicationContext()}
100+
*
101+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
102+
* visit http://flutter.dev/go/android-plugin-migration
81103
*/
82104
Context context();
83105

84106
/**
85-
* Returns the active {@link Context}.
86-
*
87-
* @return the current {@link #activity() Activity}, if not null, otherwise the {@link #context() Application}.
88-
*/
107+
* Returns the active {@link Context}.
108+
*
109+
* <p>This registrar is for Flutter's v1 embedding. In the v2 embedding, there is no
110+
* concept of an "active context". Either use the application {@code Context} or an attached
111+
* {@code Activity}. See {@link #context()} and {@link #activity()} for more details.
112+
*
113+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
114+
* visit http://flutter.dev/go/android-plugin-migration
115+
*
116+
* @return the current {@link #activity() Activity}, if not null, otherwise the {@link #context() Application}.
117+
*/
89118
Context activeContext();
90119

91120
/**
92121
* Returns a {@link BinaryMessenger} which the plugin can use for
93122
* creating channels for communicating with the Dart side.
123+
*
124+
* <p>This registrar is for Flutter's v1 embedding. To access a {@code BinaryMessenger} from
125+
* a plugin using the v2 embedding, see {@link FlutterPlugin.FlutterPluginBinding#getBinaryMessenger()}
126+
*
127+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
128+
* visit http://flutter.dev/go/android-plugin-migration
94129
*/
95130
BinaryMessenger messenger();
96131

97132
/**
98133
* Returns a {@link TextureRegistry} which the plugin can use for
99134
* managing backend textures.
135+
*
136+
* <p>This registrar is for Flutter's v1 embedding. To access a {@code TextureRegistry} from
137+
* a plugin using the v2 embedding, see {@link FlutterPlugin.FlutterPluginBinding#getTextureRegistry()}
138+
*
139+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
140+
* visit http://flutter.dev/go/android-plugin-migration
100141
*/
101142
TextureRegistry textures();
102143

103144
/**
104145
* Returns the application's {@link PlatformViewRegistry}.
105146
*
106147
* Plugins can use the platform registry to register their view factories.
148+
*
149+
* <p>This registrar is for Flutter's v1 embedding. To access a {@code PlatformViewRegistry}
150+
* from a plugin using the v2 embedding, see {@link FlutterPlugin.FlutterPluginBinding#getPlatformViewRegistry()}
151+
*
152+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
153+
* visit http://flutter.dev/go/android-plugin-migration
107154
*/
108155
PlatformViewRegistry platformViewRegistry();
109156

110157
/**
111158
* Returns the {@link FlutterView} that's instantiated by this plugin's
112159
* {@link #activity() activity}.
160+
*
161+
* <p>This registrar is for Flutter's v1 embedding. The {@link FlutterView} referenced by
162+
* this method does not exist in the v2 embedding. Additionally, no {@code View} is exposed
163+
* to any plugins in the v2 embedding. Platform views can access their containing
164+
* {@code View} using the platform views APIs. If you have a use-case that absolutely
165+
* requires a plugin to access an Android {@code View}, please file a ticket on GitHub.
166+
*
167+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
168+
* visit http://flutter.dev/go/android-plugin-migration
113169
*/
114170
FlutterView view();
115171

@@ -119,6 +175,8 @@ interface Registrar {
119175
* The returned file name can be used to access the asset in the APK
120176
* through the {@link android.content.res.AssetManager} API.
121177
*
178+
* TODO(mattcarroll): point this method towards new lookup method.
179+
*
122180
* @param asset the name of the asset. The name can be hierarchical
123181
* @return the filename to be used with {@link android.content.res.AssetManager}
124182
*/
@@ -129,6 +187,8 @@ interface Registrar {
129187
* specified packageName. The returned file name can be used to access
130188
* the asset in the APK through the {@link android.content.res.AssetManager} API.
131189
*
190+
* TODO(mattcarroll): point this method towards new lookup method.
191+
*
132192
* @param asset the name of the asset. The name can be hierarchical
133193
* @param packageName the name of the package from which the asset originates
134194
* @return the file name to be used with {@link android.content.res.AssetManager}
@@ -148,6 +208,12 @@ interface Registrar {
148208
*
149209
* <p>Overwrites any previously published value.</p>
150210
*
211+
* <p>This registrar is for Flutter's v1 embedding. The concept of publishing values from
212+
* plugins is not supported in the v2 embedding.
213+
*
214+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
215+
* visit http://flutter.dev/go/android-plugin-migration
216+
*
151217
* @param value the value, possibly null.
152218
* @return this {@link Registrar}.
153219
*/
@@ -158,6 +224,12 @@ interface Registrar {
158224
* calls to {@code Activity#onRequestPermissionsResult(int, String[], int[])}
159225
* or {@code android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[])}.
160226
*
227+
* <p>This registrar is for Flutter's v1 embedding. To listen for permission results in the
228+
* v2 embedding, use {@link ActivityPluginBinding#addRequestPermissionsResultListener(RequestPermissionsResultListener)}.
229+
*
230+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
231+
* visit http://flutter.dev/go/android-plugin-migration
232+
*
161233
* @param listener a {@link RequestPermissionsResultListener} callback.
162234
* @return this {@link Registrar}.
163235
*/
@@ -167,6 +239,12 @@ interface Registrar {
167239
* Adds a callback allowing the plugin to take part in handling incoming
168240
* calls to {@link Activity#onActivityResult(int, int, Intent)}.
169241
*
242+
* <p>This registrar is for Flutter's v1 embedding. To listen for {@code Activity} results
243+
* in the v2 embedding, use {@link ActivityPluginBinding#addActivityResultListener(ActivityResultListener)}.
244+
*
245+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
246+
* visit http://flutter.dev/go/android-plugin-migration
247+
*
170248
* @param listener an {@link ActivityResultListener} callback.
171249
* @return this {@link Registrar}.
172250
*/
@@ -176,6 +254,12 @@ interface Registrar {
176254
* Adds a callback allowing the plugin to take part in handling incoming
177255
* calls to {@link Activity#onNewIntent(Intent)}.
178256
*
257+
* <p>This registrar is for Flutter's v1 embedding. To listen for new {@code Intent}s in the
258+
* v2 embedding, use {@link ActivityPluginBinding#addOnNewIntentListener(NewIntentListener)}.
259+
*
260+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
261+
* visit http://flutter.dev/go/android-plugin-migration
262+
*
179263
* @param listener a {@link NewIntentListener} callback.
180264
* @return this {@link Registrar}.
181265
*/
@@ -185,6 +269,12 @@ interface Registrar {
185269
* Adds a callback allowing the plugin to take part in handling incoming
186270
* calls to {@link Activity#onUserLeaveHint()}.
187271
*
272+
* <p>This registrar is for Flutter's v1 embedding. To listen for leave hints in the
273+
* v2 embedding, use {@link ActivityPluginBinding#addOnUserLeaveHintListener(UserLeaveHintListener)}.
274+
*
275+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
276+
* visit http://flutter.dev/go/android-plugin-migration
277+
*
188278
* @param listener a {@link UserLeaveHintListener} callback.
189279
* @return this {@link Registrar}.
190280
*/
@@ -194,9 +284,24 @@ interface Registrar {
194284
* Adds a callback allowing the plugin to take part in handling incoming
195285
* calls to {@link Activity#onDestroy()}.
196286
*
287+
* <p>This registrar is for Flutter's v1 embedding. The concept of {@code View}
288+
* destruction does not exist in the v2 embedding. However, plugins in the v2 embedding
289+
* can respond to {@link ActivityAware#onDetachedFromActivityForConfigChanges()} and
290+
* {@link ActivityAware#onDetachedFromActivity()}, which indicate the loss of a visual
291+
* context for the running Flutter experience. Developers should implement
292+
* {@link ActivityAware} for their {@link FlutterPlugin} if such callbacks are needed. Also,
293+
* plugins can respond to
294+
* {@link FlutterPlugin#onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding)}, which
295+
* indicates that the given plugin has been completely disconnected from the associated
296+
* Flutter experience and should clean up any resources.
297+
*
298+
* <p>For instructions on migrating a plugin from Flutter's v1 Android embedding to v2,
299+
* visit http://flutter.dev/go/android-plugin-migration
300+
*
197301
* @param listener a {@link ViewDestroyListener} callback.
198302
* @return this {@link Registrar}.
199303
*/
304+
// TODO(amirh): Add a line in the javadoc above that points to a Platform Views website guide when one is available (but not a website API doc)
200305
Registrar addViewDestroyListener(ViewDestroyListener listener);
201306
}
202307

0 commit comments

Comments
 (0)