11package com .baseflow .googleapiavailability ;
22
33import android .app .Activity ;
4+ import android .app .Dialog ;
5+ import android .app .PendingIntent ;
46import android .content .Context ;
57import android .util .Log ;
68
9+ import com .google .android .gms .common .ConnectionResult ;
710import com .google .android .gms .common .GoogleApiAvailability ;
811
12+ import java .util .List ;
13+
914public class GoogleApiAvailabilityManager {
1015
1116 GoogleApiAvailability googleApiAvailability = GoogleApiAvailability .getInstance ();
@@ -16,19 +21,34 @@ interface SuccessCallback {
1621 }
1722
1823 @ FunctionalInterface
19- interface MakeGooglePlayServicesAvailable {
24+ interface MakeGooglePlayServicesAvailableCallback {
2025 void onSuccess (boolean makeGooglePlayServicesAvailable );
2126 }
2227
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+
2343 @ FunctionalInterface
2444 interface ErrorCallback {
2545 void onError (String errorCode , String errorDescription );
2646 }
2747
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." );
3252 return ;
3353 }
3454
@@ -37,13 +57,13 @@ void checkPlayServicesAvailability(Boolean showDialog, Context applicationContex
3757
3858 if (showDialog != null && showDialog ) {
3959 googleApiAvailability
40- .showErrorDialogFragment (( Activity ) applicationContext , connectionResult , GoogleApiAvailabilityConstants .REQUEST_GOOGLE_PLAY_SERVICES );
60+ .showErrorDialogFragment (activity , connectionResult , GoogleApiAvailabilityConstants .REQUEST_GOOGLE_PLAY_SERVICES );
4161 }
4262
4363 successCallback .onSuccess (GoogleApiAvailabilityConstants .toPlayServiceAvailability (connectionResult ));
4464 }
4565
46- void makeGooglePlayServicesAvailable (Activity activity , MakeGooglePlayServicesAvailable successCallback , ErrorCallback errorCallback ){
66+ void makeGooglePlayServicesAvailable (Activity activity , MakeGooglePlayServicesAvailableCallback successCallback , ErrorCallback errorCallback ){
4767 if (activity == null ){
4868 Log .d (GoogleApiAvailabilityConstants .LOG_TAG , "Activity cannot be null." );
4969 errorCallback .onError ("GoogleApiAvailability.makeGooglePlayServicesAvailable" , "Android Activity cannot be null." );
@@ -53,4 +73,52 @@ void makeGooglePlayServicesAvailable(Activity activity, MakeGooglePlayServicesAv
5373 final boolean status = googleApiAvailability .makeGooglePlayServicesAvailable (activity ).isSuccessful ();
5474 successCallback .onSuccess (status );
5575 }
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+ }
56124}
0 commit comments