88
99import com .google .android .gms .common .ConnectionResult ;
1010import com .google .android .gms .common .GoogleApiAvailability ;
11+ import com .google .android .gms .tasks .Task ;
1112
1213import java .util .List ;
1314
@@ -22,7 +23,7 @@ interface SuccessCallback {
2223
2324 @ FunctionalInterface
2425 interface MakeGooglePlayServicesAvailableCallback {
25- void onSuccess (boolean makeGooglePlayServicesAvailable );
26+ void onSuccess ();
2627 }
2728
2829 @ FunctionalInterface
@@ -37,7 +38,7 @@ interface isUserResolvableCallback {
3738
3839 @ FunctionalInterface
3940 interface showErrorNotificationCallback {
40- void onSuccess (boolean showErrorNotificationCallback );
41+ void onSuccess (Void v );
4142 }
4243
4344 @ FunctionalInterface
@@ -51,37 +52,45 @@ interface ErrorCallback {
5152 }
5253
5354 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." );
5758 return ;
5859 }
5960
6061 final int connectionResult = googleApiAvailability
6162 .isGooglePlayServicesAvailable (applicationContext );
6263
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+ }
6674 }
6775
6876 successCallback .onSuccess (GoogleApiAvailabilityConstants .toPlayServiceAvailability (connectionResult ));
6977 }
7078
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." );
7482 errorCallback .onError ("GoogleApiAvailability.makeGooglePlayServicesAvailable" , "Android Activity cannot be null." );
7583 return ;
7684 }
7785
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 ());
8089 }
8190
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." );
8594 errorCallback .onError ("GoogleApiAvailability.getErrorString" , "Android context cannot be null." );
8695 return ;
8796 }
@@ -91,9 +100,9 @@ void getErrorString(Context applicationContext, getErrorStringCallback successCa
91100 successCallback .onSuccess (errorString );
92101 }
93102
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." );
97106 errorCallback .onError ("GoogleApiAvailability.isUserResolvable" , "Android context cannot be null." );
98107 return ;
99108 }
@@ -104,9 +113,9 @@ void isUserResolvable(Context applicationContext, isUserResolvableCallback succe
104113 successCallback .onSuccess (googleApiAvailability .isUserResolvableError (connectionResult ));
105114 }
106115
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." );
110119 errorCallback .onError ("GoogleApiAvailability.showErrorNotification" , "Android context cannot be null." );
111120 return ;
112121 }
@@ -116,20 +125,12 @@ void showErrorNotification(Context applicationContext, showErrorNotificationCall
116125
117126 googleApiAvailability .showErrorNotification (applicationContext , connectionResult );
118127
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 );
128129 }
129130
130131 void showErrorDialogFragment (Context applicationContext , Activity activity , showErrorDialogFragmentCallback successCallback , ErrorCallback errorCallback ) {
131132 if (applicationContext == null ) {
132- Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
133+ Log .e (GoogleApiAvailabilityConstants .LOG_TAG , "Context cannot be null." );
133134 errorCallback .onError ("GoogleApiAvailability.showErrorDialogFragment" , "Android context cannot be null." );
134135 return ;
135136 }
0 commit comments