1
1
package com .baseflow .googleapiavailability ;
2
2
3
- import android .app .Activity ;
4
3
import android .content .Context ;
5
-
6
- import androidx .annotation .IntDef ;
7
-
8
- import com .google .android .gms .common .ConnectionResult ;
9
- import com .google .android .gms .common .GoogleApiAvailability ;
10
-
11
- import java .lang .annotation .Retention ;
12
- import java .lang .annotation .RetentionPolicy ;
13
-
4
+ import androidx .annotation .NonNull ;
14
5
import io .flutter .embedding .engine .plugins .FlutterPlugin ;
15
6
import io .flutter .embedding .engine .plugins .activity .ActivityAware ;
16
7
import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
17
8
import io .flutter .plugin .common .BinaryMessenger ;
18
- import io .flutter .plugin .common .MethodCall ;
19
9
import io .flutter .plugin .common .MethodChannel ;
20
- import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
21
- import io .flutter .plugin .common .MethodChannel .Result ;
22
10
import io .flutter .plugin .common .PluginRegistry .Registrar ;
11
+ import io .flutter .plugin .common .PluginRegistry .ViewDestroyListener ;
12
+ import io .flutter .view .FlutterNativeView ;
23
13
24
14
/**
25
15
* GoogleApiAvailabilityPlugin
26
16
*/
27
- public class GoogleApiAvailabilityPlugin implements MethodCallHandler , FlutterPlugin , ActivityAware {
28
- private static final int REQUEST_GOOGLE_PLAY_SERVICES = 1000 ;
29
-
30
- //GOOGLE_PLAY_SERVICES_AVAILABILITY
31
- private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS = 0 ;
32
- private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING = 1 ;
33
- private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING = 2 ;
34
- private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED = 3 ;
35
- private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED = 4 ;
36
- private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID = 5 ;
37
- private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_NOT_AVAILABLE_ON_PLATFORM = 6 ;
38
- private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN = 7 ;
17
+ public class GoogleApiAvailabilityPlugin implements FlutterPlugin , ActivityAware {
39
18
40
- @ Retention (RetentionPolicy .SOURCE )
41
- @ IntDef ({
42
- GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS ,
43
- GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING ,
44
- GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING ,
45
- GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED ,
46
- GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED ,
47
- GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID ,
48
- GOOGLE_PLAY_SERVICES_AVAILABILITY_NOT_AVAILABLE_ON_PLATFORM ,
49
- GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN ,
50
- })
51
- private @interface GooglePlayServicesAvailability {
52
- }
53
-
54
- private Context context ;
55
-
56
-
57
- public void setContext (Context newContext ) {
58
- context = newContext ;
59
- }
19
+ private MethodChannel channel ;
20
+ private MethodCallHandlerImpl methodCallHandler ;
60
21
61
22
@ Override
62
23
public void onAttachedToActivity (ActivityPluginBinding binding ) {
63
- setContext (binding .getActivity ());
24
+ methodCallHandler . setActivity (binding .getActivity ());
64
25
}
65
26
66
27
@ Override
67
- public void onDetachedFromActivityForConfigChanges () {
68
- setContext (null );
28
+ public void onDetachedFromActivity () {
29
+ methodCallHandler . setActivity (null );
69
30
}
70
31
71
32
@ Override
72
- public void onReattachedToActivityForConfigChanges (ActivityPluginBinding binding ) {
73
- setContext (binding .getActivity ());
33
+ public void onReattachedToActivityForConfigChanges (@ NonNull ActivityPluginBinding binding ) {
34
+ methodCallHandler . setActivity (binding .getActivity ());
74
35
}
75
36
76
37
@ Override
77
- public void onDetachedFromActivity () {
78
- setContext (null );
38
+ public void onDetachedFromActivityForConfigChanges () {
39
+ methodCallHandler . setActivity (null );
79
40
}
80
41
81
42
@ Override
82
43
public void onAttachedToEngine (FlutterPluginBinding binding ) {
83
- this . registerPlugin (null , binding .getBinaryMessenger ());
44
+ registerPlugin (null , binding .getBinaryMessenger ());
84
45
}
85
46
86
47
@ 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 );
48
+ public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
49
+ unregisterPlugin ();
93
50
}
94
51
95
52
public static void registerWith (Registrar registrar ) {
96
53
final GoogleApiAvailabilityPlugin plugin = new GoogleApiAvailabilityPlugin ();
97
54
plugin .registerPlugin (registrar .context (), registrar .messenger ());
98
- }
55
+ plugin . methodCallHandler . setActivity ( registrar . activity ());
99
56
100
- @ Override
101
- public void onMethodCall (MethodCall call , Result result ) {
102
- if (call .method .equals ("checkPlayServicesAvailability" )) {
103
- final Boolean showDialog = call .argument ("showDialog" );
104
- GoogleApiAvailability googleApiAvailability = GoogleApiAvailability .getInstance ();
105
- final int connectionResult = googleApiAvailability .isGooglePlayServicesAvailable (context );
106
-
107
-
108
- if (context instanceof Activity ) {
109
- Activity activity = (Activity ) context ;
110
- if (showDialog != null && showDialog ) {
111
- googleApiAvailability .showErrorDialogFragment (activity , connectionResult , REQUEST_GOOGLE_PLAY_SERVICES );
112
- }
57
+ registrar .addViewDestroyListener (new ViewDestroyListener () {
58
+ @ Override
59
+ public boolean onViewDestroy (FlutterNativeView view ) {
60
+ plugin .unregisterPlugin ();
61
+ return false ;
113
62
}
63
+ });
64
+ }
114
65
115
- final int availability = toPlayServiceAvailability (connectionResult );
116
- result .success (availability );
117
- } else {
118
- result .notImplemented ();
119
- }
66
+ private void registerPlugin (Context context , BinaryMessenger messenger ) {
67
+ methodCallHandler = new MethodCallHandlerImpl (context );
68
+ channel = new MethodChannel (messenger , "flutter.baseflow.com/google_api_availability/methods" );
69
+ channel .setMethodCallHandler (methodCallHandler );
120
70
}
121
71
122
- @ GooglePlayServicesAvailability
123
- private int toPlayServiceAvailability (int connectionResult ) {
124
- switch (connectionResult ) {
125
- case ConnectionResult .SUCCESS :
126
- return GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS ;
127
- case ConnectionResult .SERVICE_MISSING :
128
- return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING ;
129
- case ConnectionResult .SERVICE_UPDATING :
130
- return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING ;
131
- case ConnectionResult .SERVICE_VERSION_UPDATE_REQUIRED :
132
- return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED ;
133
- case ConnectionResult .SERVICE_DISABLED :
134
- return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED ;
135
- case ConnectionResult .SERVICE_INVALID :
136
- return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID ;
137
- default :
138
- return GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN ;
139
- }
72
+ private void unregisterPlugin () {
73
+ channel .setMethodCallHandler (null );
74
+ channel = null ;
140
75
}
141
76
}
0 commit comments