Skip to content

Commit 31c9ccf

Browse files
committed
added android play services availability checks - will throw a js error if not available, still need to add the makePlayServicesAvailable functionality
1 parent 2b90c0b commit 31c9ccf

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

android/src/main/java/io/fullstack/firestack/FirestackModule.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.fullstack.firestack;
22

3+
import java.util.HashMap;
34
import java.util.Map;
45

56
import android.util.Log;
@@ -17,7 +18,8 @@
1718
import com.facebook.react.bridge.ReactContext;
1819

1920
import com.google.android.gms.common.ConnectionResult;
20-
//import com.google.android.gms.common.GooglePlayServicesUtil;
21+
import com.google.android.gms.common.GoogleApiAvailability;
22+
2123
import com.google.firebase.FirebaseApp;
2224
import com.google.firebase.FirebaseOptions;
2325
import com.google.firebase.database.ServerValue;
@@ -27,7 +29,7 @@ interface KeySetterFn {
2729
}
2830

2931
@SuppressWarnings("WeakerAccess")
30-
class FirestackModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
32+
public class FirestackModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
3133
private static final String TAG = "Firestack";
3234
private Context context;
3335
private ReactContext mReactContext;
@@ -46,18 +48,20 @@ public String getName() {
4648
return TAG;
4749
}
4850

49-
// private static Boolean hasValidPlayServices() {
50-
// final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.context);
51-
// if (status != ConnectionResult.SUCCESS) {
52-
// Log.e(TAG, GooglePlayServicesUtil.getErrorString(status));
53-
// Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 1);
54-
// dialog.show();
55-
// return false;
56-
// } else {
57-
// Log.i(TAG, GooglePlayServicesUtil.getErrorString(status));
58-
// return true;
59-
// }
60-
// }
51+
private WritableMap getPlayServicesStatus() {
52+
GoogleApiAvailability gapi = GoogleApiAvailability.getInstance();
53+
final int status = gapi.isGooglePlayServicesAvailable(getReactApplicationContext());
54+
WritableMap result = Arguments.createMap();
55+
result.putInt("status", status);
56+
if (status == ConnectionResult.SUCCESS) {
57+
result.putBoolean("isAvailable", true);
58+
} else {
59+
result.putBoolean("isAvailable", false);
60+
result.putBoolean("isUserResolvableError", gapi.isUserResolvableError(status));
61+
result.putString("error", gapi.getErrorString(status));
62+
}
63+
return result;
64+
}
6165

6266
@ReactMethod
6367
public void configureWithOptions(final ReadableMap params, @Nullable final Callback onComplete) {
@@ -198,4 +202,11 @@ public void onHostPause() {
198202
public void onHostDestroy() {
199203

200204
}
205+
206+
@Override
207+
public Map<String, Object> getConstants() {
208+
final Map<String, Object> constants = new HashMap<>();
209+
constants.put("googleApiAvailability", getPlayServicesStatus());
210+
return constants;
211+
}
201212
}

lib/firestack.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const instances = { default: null };
2222
const FirestackModule = NativeModules.Firestack;
2323
const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
2424

25+
type GoogleApiAvailabilityType = {
26+
status: number,
27+
isAvailable: boolean,
28+
isUserResolvableError?: boolean,
29+
error?: string
30+
};
31+
2532
/**
2633
* @class Firestack
2734
*/
@@ -31,10 +38,10 @@ export default class Firestack extends Singleton {
3138
*
3239
* @param options
3340
*/
34-
constructor(options: Object) {
41+
constructor(options: Object = {}) {
3542
const instance = super(options);
3643

37-
instance.options = options || {};
44+
instance.options = Object.assign({ errorOnMissingPlayServices: true }, options);
3845
instance._debug = instance.options.debug || false;
3946

4047
Log.enable(instance._debug);
@@ -53,6 +60,10 @@ export default class Firestack extends Singleton {
5360
instance.configurePromise = instance.configure(instance.options);
5461

5562
instance._auth = new Auth(instance, instance.options);
63+
64+
if (instance.options.errorOnMissingPlayServices && !this.googleApiAvailability.isAvailable) {
65+
throw new Error(`Google Play Services is required to run this application but no valid installation was found (Code ${this.googleApiAvailability.status}).`);
66+
}
5667
}
5768

5869
_db: ?Object;
@@ -179,6 +190,15 @@ export default class Firestack extends Singleton {
179190
return Object.keys(instances);
180191
}
181192

193+
/**
194+
* Returns androids GoogleApiAvailability status and message if available.
195+
* @returns {GoogleApiAvailabilityType|{isAvailable: boolean, status: number}}
196+
*/
197+
get googleApiAvailability(): GoogleApiAvailabilityType {
198+
// if not available then return a fake object for ios - saves doing platform specific logic.
199+
return FirestackModule.googleApiAvailability || { isAvailable: true, status: 0 };
200+
}
201+
182202
/**
183203
* Logger
184204
*/

0 commit comments

Comments
 (0)