8
8
9
9
import com .google .android .gms .common .ConnectionResult ;
10
10
import com .google .android .gms .common .GoogleApiAvailability ;
11
+ import com .google .android .gms .tasks .Task ;
11
12
12
13
import java .util .List ;
13
14
@@ -22,7 +23,7 @@ interface SuccessCallback {
22
23
23
24
@ FunctionalInterface
24
25
interface MakeGooglePlayServicesAvailableCallback {
25
- void onSuccess (boolean makeGooglePlayServicesAvailable );
26
+ void onSuccess ();
26
27
}
27
28
28
29
@ FunctionalInterface
@@ -37,7 +38,7 @@ interface isUserResolvableCallback {
37
38
38
39
@ FunctionalInterface
39
40
interface showErrorNotificationCallback {
40
- void onSuccess (boolean showErrorNotificationCallback );
41
+ void onSuccess (Void v );
41
42
}
42
43
43
44
@ FunctionalInterface
@@ -51,37 +52,45 @@ interface ErrorCallback {
51
52
}
52
53
53
54
void checkPlayServicesAvailability (Boolean showDialog , Activity activity , Context applicationContext , SuccessCallback successCallback , ErrorCallback errorCallback ) {
54
- if (applicationContext == null || activity == null ) {
55
- Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context and/or activity cannot be null." );
56
- errorCallback .onError ("GoogleApiAvailability.GoogleApiAvailabilityManager" , "Android context and/or activity cannot be null." );
55
+ if (applicationContext == null ) {
56
+ Log .e (GoogleApiAvailabilityConstants .LOG_TAG , "The `ApplicationContext` cannot be null." );
57
+ errorCallback .onError ("GoogleApiAvailability.GoogleApiAvailabilityManager" , "Android `ApplicationContext` cannot be null." );
57
58
return ;
58
59
}
59
60
60
61
final int connectionResult = googleApiAvailability
61
62
.isGooglePlayServicesAvailable (applicationContext );
62
63
63
- if (showDialog != null && showDialog ) {
64
- googleApiAvailability
65
- .showErrorDialogFragment (activity , connectionResult , GoogleApiAvailabilityConstants .REQUEST_GOOGLE_PLAY_SERVICES );
64
+ if (activity != null ) {
65
+ if (showDialog != null && showDialog ) {
66
+ googleApiAvailability
67
+ .showErrorDialogFragment (activity , connectionResult , GoogleApiAvailabilityConstants .REQUEST_GOOGLE_PLAY_SERVICES );
68
+ }
69
+ } else {
70
+ if (showDialog != null && showDialog ) {
71
+ // Only log warning when `showDialog` property was `true`.
72
+ Log .w (GoogleApiAvailabilityConstants .LOG_TAG , "Unable to show dialog as `Activity` is not available." );
73
+ }
66
74
}
67
75
68
76
successCallback .onSuccess (GoogleApiAvailabilityConstants .toPlayServiceAvailability (connectionResult ));
69
77
}
70
78
71
- void makeGooglePlayServicesAvailable (Activity activity , MakeGooglePlayServicesAvailableCallback successCallback , ErrorCallback errorCallback ){
72
- if (activity == null ){
73
- Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Activity cannot be null." );
79
+ void makeGooglePlayServicesAvailable (Activity activity , MakeGooglePlayServicesAvailableCallback successCallback , ErrorCallback errorCallback ) {
80
+ if (activity == null ) {
81
+ Log .e (GoogleApiAvailabilityConstants .LOG_TAG , "Activity cannot be null." );
74
82
errorCallback .onError ("GoogleApiAvailability.makeGooglePlayServicesAvailable" , "Android Activity cannot be null." );
75
83
return ;
76
84
}
77
85
78
- final boolean status = googleApiAvailability .makeGooglePlayServicesAvailable (activity ).isSuccessful ();
79
- successCallback .onSuccess (status );
86
+ googleApiAvailability .makeGooglePlayServicesAvailable (activity )
87
+ .addOnFailureListener ((Exception e ) -> errorCallback .onError ("GoogleApiAvailability.makeGooglePlayServicesAvailable" , e .getMessage ()))
88
+ .addOnSuccessListener ((Void t ) -> successCallback .onSuccess ());
80
89
}
81
90
82
- void getErrorString (Context applicationContext , getErrorStringCallback successCallback , ErrorCallback errorCallback ){
83
- if (applicationContext == null ){
84
- Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
91
+ void getErrorString (Context applicationContext , getErrorStringCallback successCallback , ErrorCallback errorCallback ) {
92
+ if (applicationContext == null ) {
93
+ Log .e (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
85
94
errorCallback .onError ("GoogleApiAvailability.getErrorString" , "Android context cannot be null." );
86
95
return ;
87
96
}
@@ -91,9 +100,9 @@ void getErrorString(Context applicationContext, getErrorStringCallback successCa
91
100
successCallback .onSuccess (errorString );
92
101
}
93
102
94
- void isUserResolvable (Context applicationContext , isUserResolvableCallback successCallback , ErrorCallback errorCallback ){
95
- if (applicationContext == null ){
96
- Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
103
+ void isUserResolvable (Context applicationContext , isUserResolvableCallback successCallback , ErrorCallback errorCallback ) {
104
+ if (applicationContext == null ) {
105
+ Log .e (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
97
106
errorCallback .onError ("GoogleApiAvailability.isUserResolvable" , "Android context cannot be null." );
98
107
return ;
99
108
}
@@ -104,9 +113,9 @@ void isUserResolvable(Context applicationContext, isUserResolvableCallback succe
104
113
successCallback .onSuccess (googleApiAvailability .isUserResolvableError (connectionResult ));
105
114
}
106
115
107
- void showErrorNotification (Context applicationContext , showErrorNotificationCallback successCallback , ErrorCallback errorCallback ){
108
- if (applicationContext == null ){
109
- Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
116
+ void showErrorNotification (Context applicationContext , showErrorNotificationCallback successCallback , ErrorCallback errorCallback ) {
117
+ if (applicationContext == null ) {
118
+ Log .e (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
110
119
errorCallback .onError ("GoogleApiAvailability.showErrorNotification" , "Android context cannot be null." );
111
120
return ;
112
121
}
@@ -116,20 +125,12 @@ void showErrorNotification(Context applicationContext, showErrorNotificationCall
116
125
117
126
googleApiAvailability .showErrorNotification (applicationContext , connectionResult );
118
127
119
- if (connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED ||
120
- connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID ||
121
- connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING ||
122
- connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING ||
123
- connectionResult == GoogleApiAvailabilityConstants .GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED ){
124
- successCallback .onSuccess (true );
125
- }
126
-
127
- successCallback .onSuccess (false );
128
+ successCallback .onSuccess (null );
128
129
}
129
130
130
131
void showErrorDialogFragment (Context applicationContext , Activity activity , showErrorDialogFragmentCallback successCallback , ErrorCallback errorCallback ) {
131
132
if (applicationContext == null ) {
132
- Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
133
+ Log .e (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
133
134
errorCallback .onError ("GoogleApiAvailability.showErrorDialogFragment" , "Android context cannot be null." );
134
135
return ;
135
136
}
0 commit comments