|
| 1 | +package com.baseflow.googleapiavailability; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Dialog; |
| 5 | +import android.app.PendingIntent; |
| 6 | +import android.content.Context; |
| 7 | +import android.util.Log; |
| 8 | + |
| 9 | +import com.google.android.gms.common.ConnectionResult; |
| 10 | +import com.google.android.gms.common.GoogleApiAvailability; |
| 11 | +import com.google.android.gms.tasks.Task; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class GoogleApiAvailabilityManager { |
| 16 | + |
| 17 | + GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); |
| 18 | + |
| 19 | + @FunctionalInterface |
| 20 | + interface SuccessCallback { |
| 21 | + void onSuccess(int connectionResult); |
| 22 | + } |
| 23 | + |
| 24 | + @FunctionalInterface |
| 25 | + interface MakeGooglePlayServicesAvailableCallback { |
| 26 | + void onSuccess(Void v); |
| 27 | + } |
| 28 | + |
| 29 | + @FunctionalInterface |
| 30 | + interface getErrorStringCallback { |
| 31 | + void onSuccess(String errorString); |
| 32 | + } |
| 33 | + |
| 34 | + @FunctionalInterface |
| 35 | + interface isUserResolvableCallback { |
| 36 | + void onSuccess(boolean isUserResolvable); |
| 37 | + } |
| 38 | + |
| 39 | + @FunctionalInterface |
| 40 | + interface showErrorNotificationCallback { |
| 41 | + void onSuccess(Void v); |
| 42 | + } |
| 43 | + |
| 44 | + @FunctionalInterface |
| 45 | + interface showErrorDialogFragmentCallback { |
| 46 | + void onSuccess(boolean showErrorDialogFragmentCallback); |
| 47 | + } |
| 48 | + |
| 49 | + @FunctionalInterface |
| 50 | + interface ErrorCallback { |
| 51 | + void onError(String errorCode, String errorDescription); |
| 52 | + } |
| 53 | + |
| 54 | + void checkPlayServicesAvailability(Boolean showDialog, Activity activity, Context applicationContext, SuccessCallback successCallback, ErrorCallback errorCallback) { |
| 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."); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + final int connectionResult = googleApiAvailability |
| 62 | + .isGooglePlayServicesAvailable(applicationContext); |
| 63 | + |
| 64 | + if (activity == null) { |
| 65 | + if (showDialog != null && showDialog) { |
| 66 | + // Only log warning when `showDialog` property was `true`. |
| 67 | + Log.w(GoogleApiAvailabilityConstants.LOG_TAG, "Unable to show dialog as `Activity` is not available."); |
| 68 | + } |
| 69 | + showDialog = false; |
| 70 | + } |
| 71 | + |
| 72 | + if (showDialog != null && showDialog) { |
| 73 | + googleApiAvailability |
| 74 | + .showErrorDialogFragment(activity, connectionResult, GoogleApiAvailabilityConstants.REQUEST_GOOGLE_PLAY_SERVICES); |
| 75 | + } |
| 76 | + |
| 77 | + successCallback.onSuccess(GoogleApiAvailabilityConstants.toPlayServiceAvailability(connectionResult)); |
| 78 | + } |
| 79 | + |
| 80 | + void makeGooglePlayServicesAvailable(Activity activity, MakeGooglePlayServicesAvailableCallback successCallback, ErrorCallback errorCallback) { |
| 81 | + if (activity == null) { |
| 82 | + Log.e(GoogleApiAvailabilityConstants.LOG_TAG, "Activity cannot be null."); |
| 83 | + errorCallback.onError("GoogleApiAvailability.makeGooglePlayServicesAvailable", "Android Activity cannot be null."); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + googleApiAvailability.makeGooglePlayServicesAvailable(activity) |
| 88 | + .addOnFailureListener((Exception e) -> errorCallback.onError("GoogleApiAvailability.makeGooglePlayServicesAvailable", e.getMessage())) |
| 89 | + .addOnSuccessListener((Void t) -> successCallback.onSuccess(null)); |
| 90 | + } |
| 91 | + |
| 92 | + void getErrorString(Context applicationContext, getErrorStringCallback successCallback, ErrorCallback errorCallback) { |
| 93 | + if (applicationContext == null) { |
| 94 | + Log.e(GoogleApiAvailabilityConstants.LOG_TAG, "Context cannot be null."); |
| 95 | + errorCallback.onError("GoogleApiAvailability.getErrorString", "Android context cannot be null."); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + final String errorString = googleApiAvailability.getErrorString(googleApiAvailability.isGooglePlayServicesAvailable(applicationContext)); |
| 100 | + |
| 101 | + successCallback.onSuccess(errorString); |
| 102 | + } |
| 103 | + |
| 104 | + void isUserResolvable(Context applicationContext, isUserResolvableCallback successCallback, ErrorCallback errorCallback) { |
| 105 | + if (applicationContext == null) { |
| 106 | + Log.e(GoogleApiAvailabilityConstants.LOG_TAG, "Context cannot be null."); |
| 107 | + errorCallback.onError("GoogleApiAvailability.isUserResolvable", "Android context cannot be null."); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + final int connectionResult = googleApiAvailability |
| 112 | + .isGooglePlayServicesAvailable(applicationContext); |
| 113 | + |
| 114 | + successCallback.onSuccess(googleApiAvailability.isUserResolvableError(connectionResult)); |
| 115 | + } |
| 116 | + |
| 117 | + void showErrorNotification(Context applicationContext, showErrorNotificationCallback successCallback, ErrorCallback errorCallback) { |
| 118 | + if (applicationContext == null) { |
| 119 | + Log.e(GoogleApiAvailabilityConstants.LOG_TAG, "Context cannot be null."); |
| 120 | + errorCallback.onError("GoogleApiAvailability.showErrorNotification", "Android context cannot be null."); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + final int connectionResult = googleApiAvailability |
| 125 | + .isGooglePlayServicesAvailable(applicationContext); |
| 126 | + |
| 127 | + googleApiAvailability.showErrorNotification(applicationContext, connectionResult); |
| 128 | + |
| 129 | + successCallback.onSuccess(null); |
| 130 | + } |
| 131 | + |
| 132 | + void showErrorDialogFragment(Context applicationContext, Activity activity, showErrorDialogFragmentCallback successCallback, ErrorCallback errorCallback) { |
| 133 | + if (applicationContext == null) { |
| 134 | + Log.e(GoogleApiAvailabilityConstants.LOG_TAG, "Context cannot be null."); |
| 135 | + errorCallback.onError("GoogleApiAvailability.showErrorDialogFragment", "Android context cannot be null."); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + final int errorCode = googleApiAvailability |
| 140 | + .isGooglePlayServicesAvailable(applicationContext); |
| 141 | + |
| 142 | + if (errorCode != GoogleApiAvailabilityConstants.GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS) { |
| 143 | + googleApiAvailability.showErrorDialogFragment(activity, errorCode, GoogleApiAvailabilityConstants.REQUEST_GOOGLE_PLAY_SERVICES); |
| 144 | + successCallback.onSuccess(true); |
| 145 | + } |
| 146 | + |
| 147 | + successCallback.onSuccess(false); |
| 148 | + } |
| 149 | +} |
0 commit comments