[js][android] Use GoogleApiAvailability to prompt the user to install Google Play Services if it is not installed

This commit is contained in:
Chris Bianca 2017-04-10 09:50:42 +01:00
parent 90546aa7dd
commit baa198ea7d
2 changed files with 27 additions and 3 deletions

View File

@ -3,12 +3,17 @@ package io.invertase.firebase;
import java.util.Map;
import java.util.HashMap;
// android
import android.app.Activity;
// react
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
// play services
import com.google.android.gms.common.ConnectionResult;
@ -27,6 +32,16 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
return TAG;
}
@ReactMethod
public void promptPlayServices() {
GoogleApiAvailability gapi = GoogleApiAvailability.getInstance();
int status = gapi.isGooglePlayServicesAvailable(getReactApplicationContext());
if (status != ConnectionResult.SUCCESS && gapi.isUserResolvableError(status)) {
gapi.getErrorDialog(getCurrentActivity(), status, 2404).show();
}
}
private WritableMap getPlayServicesStatus() {
GoogleApiAvailability gapi = GoogleApiAvailability.getInstance();
final int status = gapi.isGooglePlayServicesAvailable(getReactApplicationContext());

View File

@ -54,7 +54,7 @@ export default class Firebase {
constructor(options: Object = {}) {
this.eventHandlers = {};
this.debug = options.debug || false;
this.options = Object.assign({ errorOnMissingPlayServices: true }, options);
this.options = Object.assign({ errorOnMissingPlayServices: true, promptOnMissingPlayServices: true }, options);
if (this.debug) {
Log.enable(this.debug);
@ -62,8 +62,17 @@ export default class Firebase {
this._log = new Log('firebase');
if (this.options.errorOnMissingPlayServices && !this.googleApiAvailability.isAvailable) {
throw new Error(`Google Play Services is required to run this application but no valid installation was found (Code ${this.googleApiAvailability.status}).`);
if (!this.googleApiAvailability.isAvailable) {
if (this.options.promptOnMissingPlayServices && this.googleApiAvailability.isUserResolvableError) {
FirebaseModule.promptPlayServices();
} else {
const error = `Google Play Services is required to run this application but no valid installation was found (Code ${this.googleApiAvailability.status}).`;
if (this.options.errorOnMissingPlayServices) {
throw new Error(error);
} else {
console.warn(error);
}
}
}
this.auth = this._staticsOrInstance('auth', AuthStatics, Auth);