1
1
package com .github .cloudwebrtc .flutter_callkeep ;
2
2
3
+ import android .app .Activity ;
4
+ import android .content .Context ;
3
5
import androidx .annotation .NonNull ;
4
6
5
7
import io .flutter .embedding .engine .plugins .FlutterPlugin ;
6
8
import io .flutter .embedding .engine .plugins .activity .ActivityAware ;
7
9
import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
10
+ import io .flutter .plugin .common .BinaryMessenger ;
8
11
import io .flutter .plugin .common .MethodCall ;
9
12
import io .flutter .plugin .common .MethodChannel ;
10
13
import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
11
14
import io .flutter .plugin .common .MethodChannel .Result ;
15
+ import io .flutter .plugin .common .PluginRegistry .Registrar ;
12
16
import io .wazo .callkeep .CallKeepModule ;
13
17
14
18
/** FlutterCallkeepPlugin */
@@ -20,11 +24,44 @@ public class FlutterCallkeepPlugin implements FlutterPlugin, MethodCallHandler,
20
24
private MethodChannel channel ;
21
25
private CallKeepModule callKeep ;
22
26
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
+
23
62
@ Override
24
63
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 ());
28
65
}
29
66
30
67
@ Override
@@ -36,7 +73,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
36
73
37
74
@ Override
38
75
public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
39
- channel . setMethodCallHandler ( null );
76
+ stopListening ( );
40
77
}
41
78
42
79
@ Override
0 commit comments