Flutter Engine
The Flutter Engine
ShimRegistrar.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.shim;
6
7import android.app.Activity;
8import android.content.Context;
9import androidx.annotation.NonNull;
10import io.flutter.FlutterInjector;
11import io.flutter.Log;
12import io.flutter.embedding.engine.plugins.FlutterPlugin;
13import io.flutter.embedding.engine.plugins.activity.ActivityAware;
14import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
15import io.flutter.plugin.common.BinaryMessenger;
16import io.flutter.plugin.common.PluginRegistry;
17import io.flutter.plugin.platform.PlatformViewRegistry;
18import io.flutter.view.FlutterView;
19import io.flutter.view.TextureRegistry;
20import java.util.HashSet;
21import java.util.Map;
22import java.util.Set;
23
24/**
25 * A {@link PluginRegistry.Registrar} that is shimmed let old plugins use the new Android embedding
26 * and plugin API behind the scenes.
27 *
28 * <p>Instances of {@code ShimRegistrar}s are vended internally by a {@link ShimPluginRegistry}.
29 */
31 private static final String TAG = "ShimRegistrar";
32
33 private final Map<String, Object> globalRegistrarMap;
34 private final String pluginId;
35 private final Set<PluginRegistry.ViewDestroyListener> viewDestroyListeners = new HashSet<>();
36 private final Set<PluginRegistry.RequestPermissionsResultListener>
37 requestPermissionsResultListeners = new HashSet<>();
38 private final Set<PluginRegistry.ActivityResultListener> activityResultListeners =
39 new HashSet<>();
40 private final Set<PluginRegistry.NewIntentListener> newIntentListeners = new HashSet<>();
41 private final Set<PluginRegistry.UserLeaveHintListener> userLeaveHintListeners = new HashSet<>();
42 private final Set<PluginRegistry.WindowFocusChangedListener> WindowFocusChangedListeners =
43 new HashSet<>();
44 private FlutterPlugin.FlutterPluginBinding pluginBinding;
45 private ActivityPluginBinding activityPluginBinding;
46
47 public ShimRegistrar(@NonNull String pluginId, @NonNull Map<String, Object> globalRegistrarMap) {
48 this.pluginId = pluginId;
49 this.globalRegistrarMap = globalRegistrarMap;
50 }
51
52 @Override
53 public Activity activity() {
54 return activityPluginBinding != null ? activityPluginBinding.getActivity() : null;
55 }
56
57 @Override
58 public Context context() {
59 return pluginBinding != null ? pluginBinding.getApplicationContext() : null;
60 }
61
62 @Override
63 public Context activeContext() {
64 return activityPluginBinding == null ? context() : activity();
65 }
66
67 @Override
69 return pluginBinding != null ? pluginBinding.getBinaryMessenger() : null;
70 }
71
72 @Override
74 return pluginBinding != null ? pluginBinding.getTextureRegistry() : null;
75 }
76
77 @Override
79 return pluginBinding != null ? pluginBinding.getPlatformViewRegistry() : null;
80 }
81
82 @Override
83 public FlutterView view() {
84 throw new UnsupportedOperationException(
85 "The new embedding does not support the old FlutterView.");
86 }
87
88 @Override
89 public String lookupKeyForAsset(String asset) {
90 return FlutterInjector.instance().flutterLoader().getLookupKeyForAsset(asset);
91 }
92
93 @Override
94 public String lookupKeyForAsset(String asset, String packageName) {
95 return FlutterInjector.instance().flutterLoader().getLookupKeyForAsset(asset, packageName);
96 }
97
98 @Override
99 public PluginRegistry.Registrar publish(Object value) {
100 globalRegistrarMap.put(pluginId, value);
101 return this;
102 }
103
104 @Override
105 public PluginRegistry.Registrar addRequestPermissionsResultListener(
106 PluginRegistry.RequestPermissionsResultListener listener) {
107 requestPermissionsResultListeners.add(listener);
108
109 if (activityPluginBinding != null) {
110 activityPluginBinding.addRequestPermissionsResultListener(listener);
111 }
112
113 return this;
114 }
115
116 @Override
117 public PluginRegistry.Registrar addActivityResultListener(
118 PluginRegistry.ActivityResultListener listener) {
119 activityResultListeners.add(listener);
120
121 if (activityPluginBinding != null) {
122 activityPluginBinding.addActivityResultListener(listener);
123 }
124
125 return this;
126 }
127
128 @Override
129 public PluginRegistry.Registrar addNewIntentListener(PluginRegistry.NewIntentListener listener) {
130 newIntentListeners.add(listener);
131
132 if (activityPluginBinding != null) {
133 activityPluginBinding.addOnNewIntentListener(listener);
134 }
135
136 return this;
137 }
138
139 @Override
140 public PluginRegistry.Registrar addUserLeaveHintListener(
141 PluginRegistry.UserLeaveHintListener listener) {
142 userLeaveHintListeners.add(listener);
143
144 if (activityPluginBinding != null) {
145 activityPluginBinding.addOnUserLeaveHintListener(listener);
146 }
147
148 return this;
149 }
150
151 @Override
152 public PluginRegistry.Registrar addWindowFocusChangedListener(
153 PluginRegistry.WindowFocusChangedListener listener) {
154 WindowFocusChangedListeners.add(listener);
155
156 if (activityPluginBinding != null) {
157 activityPluginBinding.addOnWindowFocusChangedListener(listener);
158 }
159
160 return this;
161 }
162
163 @Override
164 @NonNull
165 public PluginRegistry.Registrar addViewDestroyListener(
166 @NonNull PluginRegistry.ViewDestroyListener listener) {
167 viewDestroyListeners.add(listener);
168 return this;
169 }
170
171 @Override
172 public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
173 Log.v(TAG, "Attached to FlutterEngine.");
174 pluginBinding = binding;
175 }
176
177 @Override
178 public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
179 Log.v(TAG, "Detached from FlutterEngine.");
180 for (PluginRegistry.ViewDestroyListener listener : viewDestroyListeners) {
181 // The following invocation might produce unexpected behavior in old plugins because
182 // we have no FlutterNativeView to pass to onViewDestroy(). This is a limitation of this shim.
183 listener.onViewDestroy(null);
184 }
185
186 pluginBinding = null;
187 activityPluginBinding = null;
188 }
189
190 @Override
191 public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
192 Log.v(TAG, "Attached to an Activity.");
193 activityPluginBinding = binding;
194 addExistingListenersToActivityPluginBinding();
195 }
196
197 @Override
199 Log.v(TAG, "Detached from an Activity for config changes.");
200 activityPluginBinding = null;
201 }
202
203 @Override
205 Log.v(TAG, "Reconnected to an Activity after config changes.");
206 activityPluginBinding = binding;
207 addExistingListenersToActivityPluginBinding();
208 }
209
210 @Override
212 Log.v(TAG, "Detached from an Activity.");
213 activityPluginBinding = null;
214 }
215
216 private void addExistingListenersToActivityPluginBinding() {
217 for (PluginRegistry.RequestPermissionsResultListener listener :
218 requestPermissionsResultListeners) {
219 activityPluginBinding.addRequestPermissionsResultListener(listener);
220 }
221 for (PluginRegistry.ActivityResultListener listener : activityResultListeners) {
222 activityPluginBinding.addActivityResultListener(listener);
223 }
224 for (PluginRegistry.NewIntentListener listener : newIntentListeners) {
225 activityPluginBinding.addOnNewIntentListener(listener);
226 }
227 for (PluginRegistry.UserLeaveHintListener listener : userLeaveHintListeners) {
228 activityPluginBinding.addOnUserLeaveHintListener(listener);
229 }
230 for (PluginRegistry.WindowFocusChangedListener listener : WindowFocusChangedListeners) {
231 activityPluginBinding.addOnWindowFocusChangedListener(listener);
232 }
233 }
234}
static void v(@NonNull String tag, @NonNull String message)
Definition: Log.java:40
String getLookupKeyForAsset(@NonNull String asset)
void onAttachedToActivity(@NonNull ActivityPluginBinding binding)
PluginRegistry.Registrar addRequestPermissionsResultListener(PluginRegistry.RequestPermissionsResultListener listener)
PluginRegistry.Registrar publish(Object value)
void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding)
PluginRegistry.Registrar addWindowFocusChangedListener(PluginRegistry.WindowFocusChangedListener listener)
PluginRegistry.Registrar addUserLeaveHintListener(PluginRegistry.UserLeaveHintListener listener)
PluginRegistry.Registrar addActivityResultListener(PluginRegistry.ActivityResultListener listener)
void onAttachedToEngine(@NonNull FlutterPluginBinding binding)
ShimRegistrar(@NonNull String pluginId, @NonNull Map< String, Object > globalRegistrarMap)
void onDetachedFromEngine(@NonNull FlutterPluginBinding binding)
String lookupKeyForAsset(String asset, String packageName)
PluginRegistry.Registrar addViewDestroyListener( @NonNull PluginRegistry.ViewDestroyListener listener)
PluginRegistry.Registrar addNewIntentListener(PluginRegistry.NewIntentListener listener)
uint8_t value
void addActivityResultListener(@NonNull PluginRegistry.ActivityResultListener listener)
void addOnWindowFocusChangedListener(@NonNull PluginRegistry.WindowFocusChangedListener listener)
void addOnNewIntentListener(@NonNull PluginRegistry.NewIntentListener listener)
void addOnUserLeaveHintListener(@NonNull PluginRegistry.UserLeaveHintListener listener)
void addRequestPermissionsResultListener( @NonNull PluginRegistry.RequestPermissionsResultListener listener)
#define TAG()