11package com .github .cloudwebrtc .flutter_callkeep ;
22
3+ import android .app .Activity ;
4+ import android .content .Context ;
35import androidx .annotation .NonNull ;
46
57import io .flutter .embedding .engine .plugins .FlutterPlugin ;
68import io .flutter .embedding .engine .plugins .activity .ActivityAware ;
79import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
10+ import io .flutter .plugin .common .BinaryMessenger ;
811import io .flutter .plugin .common .MethodCall ;
912import io .flutter .plugin .common .MethodChannel ;
1013import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
1114import io .flutter .plugin .common .MethodChannel .Result ;
15+ import io .flutter .plugin .common .PluginRegistry .Registrar ;
1216import io .wazo .callkeep .CallKeepModule ;
1317
1418/** FlutterCallkeepPlugin */
@@ -20,11 +24,44 @@ public class FlutterCallkeepPlugin implements FlutterPlugin, MethodCallHandler,
2024 private MethodChannel channel ;
2125 private CallKeepModule callKeep ;
2226
27+ /**
28+ * Plugin registration.
29+ */
30+ public static void registerWith (Registrar registrar ) {
31+ final FlutterCallkeepPlugin plugin = new FlutterCallkeepPlugin ();
32+
33+ plugin .startListening (registrar .context (), registrar .messenger ());
34+
35+ if (registrar .activeContext () instanceof Activity ) {
36+ plugin .setActivity ((Activity ) registrar .activeContext ());
37+ }
38+
39+ registrar .addViewDestroyListener (view -> {
40+ plugin .stopListening ();
41+ return false ;
42+ });
43+ }
44+
45+ private void setActivity (@ NonNull Activity activity ) {
46+ callKeep .setActivity (activity );
47+ }
48+
49+ private void startListening (final Context context , BinaryMessenger messenger ) {
50+ channel = new MethodChannel (messenger , "FlutterCallKeep.Method" );
51+ channel .setMethodCallHandler (this );
52+ callKeep = new CallKeepModule (context , messenger );
53+ }
54+
55+ private void stopListening () {
56+ channel .setMethodCallHandler (null );
57+ channel = null ;
58+ callKeep .dispose ();
59+ callKeep = null ;
60+ }
61+
2362 @ Override
2463 public void onAttachedToEngine (@ NonNull FlutterPluginBinding flutterPluginBinding ) {
25- channel = new MethodChannel (flutterPluginBinding .getBinaryMessenger (), "FlutterCallKeep.Method" );
26- channel .setMethodCallHandler (this );
27- callKeep = new CallKeepModule (flutterPluginBinding .getApplicationContext (), flutterPluginBinding );
64+ startListening (flutterPluginBinding .getApplicationContext (), flutterPluginBinding .getBinaryMessenger ());
2865 }
2966
3067 @ Override
@@ -36,7 +73,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
3673
3774 @ Override
3875 public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
39- channel . setMethodCallHandler ( null );
76+ stopListening ( );
4077 }
4178
4279 @ Override
0 commit comments