1
1
package com .baseflow .googleapiavailability ;
2
2
3
3
import android .app .Activity ;
4
+ import android .app .Dialog ;
5
+ import android .app .PendingIntent ;
4
6
import android .content .Context ;
5
7
import android .util .Log ;
6
8
9
+ import com .google .android .gms .common .ConnectionResult ;
7
10
import com .google .android .gms .common .GoogleApiAvailability ;
8
11
12
+ import java .util .List ;
13
+
9
14
public class GoogleApiAvailabilityManager {
10
15
11
16
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability .getInstance ();
@@ -16,19 +21,34 @@ interface SuccessCallback {
16
21
}
17
22
18
23
@ FunctionalInterface
19
- interface MakeGooglePlayServicesAvailable {
24
+ interface MakeGooglePlayServicesAvailableCallback {
20
25
void onSuccess (boolean makeGooglePlayServicesAvailable );
21
26
}
22
27
28
+ @ FunctionalInterface
29
+ interface getErrorStringCallback {
30
+ void onSuccess (String errorString );
31
+ }
32
+
33
+ @ FunctionalInterface
34
+ interface isUserResolvableCallback {
35
+ void onSuccess (boolean isUserResolvable );
36
+ }
37
+
38
+ @ FunctionalInterface
39
+ interface showErrorNotificationCallback {
40
+ void onSuccess (boolean showErrorNotificationCallback );
41
+ }
42
+
23
43
@ FunctionalInterface
24
44
interface ErrorCallback {
25
45
void onError (String errorCode , String errorDescription );
26
46
}
27
47
28
- void checkPlayServicesAvailability (Boolean showDialog , Context applicationContext , SuccessCallback successCallback , ErrorCallback errorCallback ) {
29
- if (applicationContext == null ) {
30
- Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
31
- errorCallback .onError ("GoogleApiAvailability.GoogleApiAvailabilityManager" , "Android context cannot be null." );
48
+ void checkPlayServicesAvailability (Boolean showDialog , Activity activity , Context applicationContext , SuccessCallback successCallback , ErrorCallback errorCallback ) {
49
+ if (applicationContext == null || activity == null ) {
50
+ Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context and/or activity cannot be null." );
51
+ errorCallback .onError ("GoogleApiAvailability.GoogleApiAvailabilityManager" , "Android context and/or activity cannot be null." );
32
52
return ;
33
53
}
34
54
@@ -37,13 +57,13 @@ void checkPlayServicesAvailability(Boolean showDialog, Context applicationContex
37
57
38
58
if (showDialog != null && showDialog ) {
39
59
googleApiAvailability
40
- .showErrorDialogFragment (( Activity ) applicationContext , connectionResult , GoogleApiAvailabilityConstants .REQUEST_GOOGLE_PLAY_SERVICES );
60
+ .showErrorDialogFragment (activity , connectionResult , GoogleApiAvailabilityConstants .REQUEST_GOOGLE_PLAY_SERVICES );
41
61
}
42
62
43
63
successCallback .onSuccess (GoogleApiAvailabilityConstants .toPlayServiceAvailability (connectionResult ));
44
64
}
45
65
46
- void makeGooglePlayServicesAvailable (Activity activity , MakeGooglePlayServicesAvailable successCallback , ErrorCallback errorCallback ){
66
+ void makeGooglePlayServicesAvailable (Activity activity , MakeGooglePlayServicesAvailableCallback successCallback , ErrorCallback errorCallback ){
47
67
if (activity == null ){
48
68
Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Activity cannot be null." );
49
69
errorCallback .onError ("GoogleApiAvailability.makeGooglePlayServicesAvailable" , "Android Activity cannot be null." );
@@ -53,4 +73,52 @@ void makeGooglePlayServicesAvailable(Activity activity, MakeGooglePlayServicesAv
53
73
final boolean status = googleApiAvailability .makeGooglePlayServicesAvailable (activity ).isSuccessful ();
54
74
successCallback .onSuccess (status );
55
75
}
76
+
77
+ void getErrorString (Context applicationContext , getErrorStringCallback successCallback , ErrorCallback errorCallback ){
78
+ if (applicationContext == null ){
79
+ Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
80
+ errorCallback .onError ("GoogleApiAvailability.getErrorString" , "Android context cannot be null." );
81
+ return ;
82
+ }
83
+
84
+ final String errorString = googleApiAvailability .getErrorString (googleApiAvailability .isGooglePlayServicesAvailable (applicationContext ));
85
+
86
+ successCallback .onSuccess (errorString );
87
+ }
88
+
89
+ void isUserResolvable (Context applicationContext , isUserResolvableCallback successCallback , ErrorCallback errorCallback ){
90
+ if (applicationContext == null ){
91
+ Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
92
+ errorCallback .onError ("GoogleApiAvailability.isUserResolvable" , "Android context cannot be null." );
93
+ return ;
94
+ }
95
+
96
+ final int connectionResult = googleApiAvailability
97
+ .isGooglePlayServicesAvailable (applicationContext );
98
+
99
+ successCallback .onSuccess (googleApiAvailability .isUserResolvableError (connectionResult ));
100
+ }
101
+
102
+ void showErrorNotification (Context applicationContext , showErrorNotificationCallback successCallback , ErrorCallback errorCallback ){
103
+ if (applicationContext == null ){
104
+ Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
105
+ errorCallback .onError ("GoogleApiAvailability.showErrorNotification" , "Android context cannot be null." );
106
+ return ;
107
+ }
108
+
109
+ final int connectionResult = googleApiAvailability
110
+ .isGooglePlayServicesAvailable (applicationContext );
111
+
112
+ googleApiAvailability .showErrorNotification (applicationContext , connectionResult );
113
+
114
+ if (connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED ||
115
+ connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID ||
116
+ connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING ||
117
+ connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING ||
118
+ connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED ){
119
+ successCallback .onSuccess (true );
120
+ }
121
+
122
+ successCallback .onSuccess (false );
123
+ }
56
124
}
0 commit comments