1
1
package org .blankapp .flutterplugins .flutter_flipperkit ;
2
2
3
- import android .app .Activity ;
4
3
import android .content .Context ;
5
4
5
+ import androidx .annotation .NonNull ;
6
+
6
7
import com .facebook .flipper .android .AndroidFlipperClient ;
7
8
import com .facebook .flipper .android .utils .FlipperUtils ;
8
9
import com .facebook .flipper .core .FlipperClient ;
22
23
import java .util .Map ;
23
24
import java .util .Set ;
24
25
26
+ import io .flutter .embedding .engine .plugins .FlutterPlugin ;
27
+ import io .flutter .plugin .common .BinaryMessenger ;
25
28
import io .flutter .plugin .common .EventChannel ;
26
29
import io .flutter .plugin .common .MethodCall ;
27
30
import io .flutter .plugin .common .MethodChannel ;
32
35
/**
33
36
* FlutterFlipperkitPlugin
34
37
*/
35
- public class FlutterFlipperkitPlugin implements MethodCallHandler , EventChannel .StreamHandler {
36
- private EventChannel .EventSink eventSink ;
38
+ public class FlutterFlipperkitPlugin implements FlutterPlugin , MethodCallHandler , EventChannel .StreamHandler {
39
+ private static final String CHANNEL_NAME = "flutter_flipperkit" ;
40
+ private static final String EVENT_CHANNEL_NAME = "flutter_flipperkit/event_channel" ;
37
41
38
42
private Context context ;
43
+ private MethodChannel channel ;
44
+ private EventChannel eventChannel ;
45
+
46
+ private EventChannel .EventSink eventSink ;
47
+
39
48
private FlipperClient flipperClient ;
40
49
private NetworkFlipperPlugin networkFlipperPlugin ;
41
50
private SharedPreferencesFlipperPlugin sharedPreferencesFlipperPlugin ;
42
51
43
52
private FlipperDatabaseBrowserPlugin flipperDatabaseBrowserPlugin ;
44
53
private FlipperReduxInspectorPlugin flipperReduxInspectorPlugin ;
45
54
46
- public FlutterFlipperkitPlugin (Context context ) {
47
- this .context = context ;
48
- SoLoader .init (context .getApplicationContext (), false );
49
- if (BuildConfig .DEBUG && FlipperUtils .shouldEnableFlipper (context )) {
50
- flipperClient = AndroidFlipperClient .getInstance (context );
51
- networkFlipperPlugin = new NetworkFlipperPlugin ();
52
- sharedPreferencesFlipperPlugin = new SharedPreferencesFlipperPlugin (context , "FlutterSharedPreferences" );
53
-
54
- flipperDatabaseBrowserPlugin = new FlipperDatabaseBrowserPlugin ();
55
- flipperReduxInspectorPlugin = new FlipperReduxInspectorPlugin ();
56
- }
55
+ @ Override
56
+ public void onAttachedToEngine (@ NonNull FlutterPluginBinding flutterPluginBinding ) {
57
+ this .setupChannel (flutterPluginBinding .getBinaryMessenger (), flutterPluginBinding .getApplicationContext ());
57
58
}
58
59
59
- /**
60
- * Plugin registration.
61
- */
60
+ // This static function is optional and equivalent to onAttachedToEngine. It supports the old
61
+ // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
62
+ // plugin registration via this function while apps migrate to use the new Android APIs
63
+ // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
64
+ //
65
+ // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
66
+ // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
67
+ // depending on the user's project. onAttachedToEngine or registerWith must both be defined
68
+ // in the same class.
62
69
public static void registerWith (Registrar registrar ) {
63
- final FlutterFlipperkitPlugin flipperkitPlugin = new FlutterFlipperkitPlugin (registrar .activeContext ());
64
- final MethodChannel channel = new MethodChannel (registrar .messenger (), "flutter_flipperkit" );
65
- channel .setMethodCallHandler (flipperkitPlugin );
66
-
67
- final EventChannel eventChannel = new EventChannel (registrar .messenger (), "flutter_flipperkit/event_channel" );
68
- eventChannel .setStreamHandler (flipperkitPlugin );
70
+ final FlutterFlipperkitPlugin plugin = new FlutterFlipperkitPlugin ();
71
+ plugin .setupChannel (registrar .messenger (), registrar .activeContext ());
69
72
}
70
73
71
74
@ Override
72
- public void onMethodCall (MethodCall call , Result result ) {
75
+ public void onMethodCall (@ NonNull MethodCall call , @ NonNull Result result ) {
73
76
if (!FlipperUtils .shouldEnableFlipper (context )) {
74
77
result .success (true );
75
78
return ;
@@ -200,15 +203,17 @@ private byte[] convertBody(MethodCall call) {
200
203
201
204
if (argBody instanceof HashMap ) {
202
205
bodyString = new JSONObject ((HashMap ) argBody ).toString ();
203
- } else if (argBody instanceof ArrayList ) {
206
+ } else if (argBody instanceof ArrayList ) {
204
207
bodyString = new JSONArray ((ArrayList ) argBody ).toString ();
205
208
}
206
- } catch (ClassCastException e ) { }
209
+ } catch (ClassCastException e ) {
210
+ }
207
211
208
212
if (bodyString == null ) {
209
213
try {
210
214
bodyString = call .argument ("body" );
211
- } catch (NullPointerException e ) { }
215
+ } catch (NullPointerException e ) {
216
+ }
212
217
}
213
218
}
214
219
@@ -217,7 +222,7 @@ private byte[] convertBody(MethodCall call) {
217
222
218
223
private List <NetworkReporter .Header > convertHeader (MethodCall call ) {
219
224
Map <String , Object > argHeaders = call .argument ("headers" );
220
- List <NetworkReporter .Header > list = new ArrayList <>();;
225
+ List <NetworkReporter .Header > list = new ArrayList <>();
221
226
222
227
if (argHeaders != null ) {
223
228
Set <String > keys = argHeaders .keySet ();
@@ -228,7 +233,7 @@ private List<NetworkReporter.Header> convertHeader(MethodCall call) {
228
233
if (value instanceof ArrayList ) {
229
234
List values = (ArrayList ) value ;
230
235
StringBuilder builder = new StringBuilder ();
231
- for (Object obj : values ) {
236
+ for (Object obj : values ) {
232
237
builder .append (obj );
233
238
}
234
239
valueString = builder .toString ();
@@ -244,11 +249,42 @@ private List<NetworkReporter.Header> convertHeader(MethodCall call) {
244
249
@ Override
245
250
public void onListen (Object args , EventChannel .EventSink eventSink ) {
246
251
this .eventSink = eventSink ;
247
- flipperDatabaseBrowserPlugin .setEventSink (eventSink );
252
+ flipperDatabaseBrowserPlugin .setEventSink (this . eventSink );
248
253
}
249
254
250
255
@ Override
251
256
public void onCancel (Object args ) {
252
257
this .eventSink = null ;
253
258
}
259
+
260
+ @ Override
261
+ public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
262
+ this .teardownChannel ();
263
+ }
264
+
265
+ private void setupChannel (BinaryMessenger messenger , Context context ) {
266
+ this .context = context ;
267
+ SoLoader .init (context .getApplicationContext (), false );
268
+ if (BuildConfig .DEBUG && FlipperUtils .shouldEnableFlipper (context )) {
269
+ flipperClient = AndroidFlipperClient .getInstance (context );
270
+ networkFlipperPlugin = new NetworkFlipperPlugin ();
271
+ sharedPreferencesFlipperPlugin = new SharedPreferencesFlipperPlugin (context , "FlutterSharedPreferences" );
272
+
273
+ flipperDatabaseBrowserPlugin = new FlipperDatabaseBrowserPlugin ();
274
+ flipperReduxInspectorPlugin = new FlipperReduxInspectorPlugin ();
275
+ }
276
+
277
+ this .channel = new MethodChannel (messenger , CHANNEL_NAME );
278
+ this .channel .setMethodCallHandler (this );
279
+
280
+ this .eventChannel = new EventChannel (messenger , EVENT_CHANNEL_NAME );
281
+ this .eventChannel .setStreamHandler (this );
282
+ }
283
+
284
+ private void teardownChannel () {
285
+ this .channel .setMethodCallHandler (null );
286
+ this .channel = null ;
287
+ this .eventChannel .setStreamHandler (null );
288
+ this .eventChannel = null ;
289
+ }
254
290
}
0 commit comments