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