3
3
import android .app .Activity ;
4
4
import android .content .Context ;
5
5
6
+ import androidx .annotation .IntDef ;
7
+
6
8
import com .google .android .gms .common .ConnectionResult ;
7
9
import com .google .android .gms .common .GoogleApiAvailability ;
8
10
9
11
import java .lang .annotation .Retention ;
10
12
import java .lang .annotation .RetentionPolicy ;
11
13
12
- import androidx .annotation .IntDef ;
14
+ import io .flutter .embedding .engine .plugins .FlutterPlugin ;
15
+ import io .flutter .embedding .engine .plugins .activity .ActivityAware ;
16
+ import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
17
+ import io .flutter .plugin .common .BinaryMessenger ;
13
18
import io .flutter .plugin .common .MethodCall ;
14
19
import io .flutter .plugin .common .MethodChannel ;
15
20
import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
19
24
/**
20
25
* GoogleApiAvailabilityPlugin
21
26
*/
22
- public class GoogleApiAvailabilityPlugin implements MethodCallHandler {
27
+ public class GoogleApiAvailabilityPlugin implements MethodCallHandler , FlutterPlugin , ActivityAware {
23
28
private static final int REQUEST_GOOGLE_PLAY_SERVICES = 1000 ;
24
29
25
30
//GOOGLE_PLAY_SERVICES_AVAILABILITY
@@ -46,15 +51,50 @@ public class GoogleApiAvailabilityPlugin implements MethodCallHandler {
46
51
private @interface GooglePlayServicesAvailability {
47
52
}
48
53
49
- public static void registerWith (Registrar registrar ) {
50
- final MethodChannel channel = new MethodChannel (registrar .messenger (), "flutter.baseflow.com/google_api_availability/methods" );
51
- channel .setMethodCallHandler (new GoogleApiAvailabilityPlugin (registrar .context ()));
54
+ private Context context ;
55
+
56
+
57
+ public void setContext (Context newContext ) {
58
+ context = newContext ;
59
+ }
60
+
61
+ @ Override
62
+ public void onAttachedToActivity (ActivityPluginBinding binding ) {
63
+ setContext (binding .getActivity ());
64
+ }
65
+
66
+ @ Override
67
+ public void onDetachedFromActivityForConfigChanges () {
68
+ setContext (null );
69
+ }
70
+
71
+ @ Override
72
+ public void onReattachedToActivityForConfigChanges (ActivityPluginBinding binding ) {
73
+ setContext (binding .getActivity ());
74
+ }
75
+
76
+ @ Override
77
+ public void onDetachedFromActivity () {
78
+ setContext (null );
79
+ }
80
+
81
+ @ Override
82
+ public void onAttachedToEngine (FlutterPluginBinding binding ) {
83
+ this .registerPlugin (null , binding .getBinaryMessenger ());
52
84
}
53
85
54
- private final Context context ;
86
+ @ Override
87
+ public void onDetachedFromEngine (FlutterPluginBinding binding ) {}
88
+
89
+ private void registerPlugin (Context context , BinaryMessenger messenger ) {
90
+ final MethodChannel channel = new MethodChannel (messenger , "flutter.baseflow.com/google_api_availability/methods" );
91
+ if (context != null ) setContext (context );
92
+ channel .setMethodCallHandler (this );
93
+ }
55
94
56
- private GoogleApiAvailabilityPlugin (Context context ) {
57
- this .context = context ;
95
+ public static void registerWith (Registrar registrar ) {
96
+ final GoogleApiAvailabilityPlugin plugin = new GoogleApiAvailabilityPlugin ();
97
+ plugin .registerPlugin (registrar .context (), registrar .messenger ());
58
98
}
59
99
60
100
@ Override
@@ -98,4 +138,4 @@ private int toPlayServiceAvailability(int connectionResult) {
98
138
return GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN ;
99
139
}
100
140
}
101
- }
141
+ }
0 commit comments