4
4
import android .content .Context ;
5
5
6
6
import androidx .annotation .IntDef ;
7
+ import androidx .annotation .Nullable ;
7
8
8
9
import com .google .android .gms .common .ConnectionResult ;
9
10
import com .google .android .gms .common .GoogleApiAvailability ;
25
26
* GoogleApiAvailabilityPlugin
26
27
*/
27
28
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 ;
39
-
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
- }
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 ());
84
- }
85
-
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
- }
94
-
95
- public static void registerWith (Registrar registrar ) {
96
- final GoogleApiAvailabilityPlugin plugin = new GoogleApiAvailabilityPlugin ();
97
- plugin .registerPlugin (registrar .context (), registrar .messenger ());
98
- }
99
-
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
- }
113
- }
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
+ }
114
87
115
- final int availability = toPlayServiceAvailability (connectionResult );
116
- result .success (availability );
117
- } else {
118
- result .notImplemented ();
88
+ @ Override
89
+ public void onDetachedFromActivityForConfigChanges () {
90
+ activity = null ;
119
91
}
120
- }
121
-
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 ;
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
+ }
139
146
}
140
- }
141
147
}
0 commit comments