mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-23 03:59:18 +08:00
[android][utils] new utils() module - home of all non firebase utilities and library configuration options, including play services availability checks
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
package io.invertase.firebase;
|
||||
|
||||
import android.util.Log;
|
||||
import android.app.Activity;
|
||||
import android.content.IntentSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
|
||||
// react
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.Callback;
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.Callback;
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.bridge.LifecycleEventListener;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
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;
|
||||
@@ -25,7 +26,7 @@ import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.FirebaseOptions;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class RNFirebaseModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
|
||||
public class RNFirebaseModule extends ReactContextBaseJavaModule {
|
||||
private static final String TAG = "RNFirebase";
|
||||
|
||||
public RNFirebaseModule(ReactApplicationContext reactContext) {
|
||||
@@ -42,12 +43,12 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
|
||||
public void initializeApp(String appName, ReadableMap options, Callback callback) {
|
||||
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
|
||||
|
||||
builder.setApplicationId(options.getString("appId"));
|
||||
builder.setGcmSenderId(options.getString("messagingSenderId"));
|
||||
builder.setApiKey(options.getString("apiKey"));
|
||||
builder.setApplicationId(options.getString("appId"));
|
||||
builder.setProjectId(options.getString("projectId"));
|
||||
builder.setDatabaseUrl(options.getString("databaseURL"));
|
||||
builder.setStorageBucket(options.getString("storageBucket"));
|
||||
builder.setGcmSenderId(options.getString("messagingSenderId"));
|
||||
// todo firebase sdk has no client id setter
|
||||
|
||||
FirebaseApp.initializeApp(getReactApplicationContext(), builder.build(), appName);
|
||||
@@ -84,8 +85,9 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
|
||||
result.putBoolean("isAvailable", true);
|
||||
} else {
|
||||
result.putBoolean("isAvailable", false);
|
||||
result.putBoolean("isUserResolvableError", gapi.isUserResolvableError(status));
|
||||
result.putString("error", gapi.getErrorString(status));
|
||||
result.putBoolean("isUserResolvableError", gapi.isUserResolvableError(status));
|
||||
result.putBoolean("hasResolution", new ConnectionResult(status).hasResolution());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -94,7 +96,7 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
|
||||
* Prompt the device user to update play services
|
||||
*/
|
||||
@ReactMethod
|
||||
public void promptPlayServices() {
|
||||
public void promptForPlayServices() {
|
||||
GoogleApiAvailability gapi = GoogleApiAvailability.getInstance();
|
||||
int status = gapi.isGooglePlayServicesAvailable(getReactApplicationContext());
|
||||
|
||||
@@ -106,6 +108,27 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompt the device user to update play services
|
||||
*/
|
||||
@ReactMethod
|
||||
public void resolutionForPlayServices() {
|
||||
int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getReactApplicationContext());
|
||||
ConnectionResult connectionResult = new ConnectionResult(status);
|
||||
|
||||
if (!connectionResult.isSuccess() && connectionResult.hasResolution()) {
|
||||
Activity activity = getCurrentActivity();
|
||||
if (activity != null) {
|
||||
try {
|
||||
connectionResult.startResolutionForResult(activity, status);
|
||||
} catch (IntentSender.SendIntentException error) {
|
||||
Log.d(TAG, "resolutionForPlayServices", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prompt the device user to update play services
|
||||
*/
|
||||
@@ -122,32 +145,16 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHostResume() {
|
||||
// WritableMap params = Arguments.createMap();
|
||||
// params.putBoolean("isForeground", true);
|
||||
// Utils.sendEvent(getReactApplicationContext(), "RNFirebaseAppState", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHostPause() {
|
||||
// WritableMap params = Arguments.createMap();
|
||||
// params.putBoolean("isForeground", false);
|
||||
// Utils.sendEvent(getReactApplicationContext(), "RNFirebaseAppState", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHostDestroy() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getConstants() {
|
||||
FirebaseApp firebaseApp;
|
||||
Map<String, Object> constants = new HashMap<>();
|
||||
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(getReactApplicationContext());
|
||||
List<Map<String, Object>> appMapsList = new ArrayList<Map<String, Object>>();
|
||||
|
||||
Map<String, Object> constants = new HashMap<>();
|
||||
List<Map<String, Object>> appMapsList = new ArrayList<>();
|
||||
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(getReactApplicationContext());
|
||||
|
||||
// TODO no way to get client id currently from app options - firebase sdk issue
|
||||
for (FirebaseApp app : firebaseAppList) {
|
||||
String appName = app.getName();
|
||||
FirebaseOptions appOptions = app.getOptions();
|
||||
@@ -156,16 +163,16 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
|
||||
appProps.put("name", appName);
|
||||
appProps.put("apiKey", appOptions.getApiKey());
|
||||
appProps.put("appId", appOptions.getApplicationId());
|
||||
appProps.put("projectId", appOptions.getProjectId());
|
||||
appProps.put("databaseURL", appOptions.getDatabaseUrl());
|
||||
appProps.put("messagingSenderId", appOptions.getGcmSenderId());
|
||||
appProps.put("projectId", appOptions.getProjectId());
|
||||
appProps.put("storageBucket", appOptions.getStorageBucket());
|
||||
// TODO no way to get client id currently from app options - firebase sdk issue
|
||||
|
||||
appMapsList.add(appProps);
|
||||
}
|
||||
|
||||
constants.put("apps", appMapsList);
|
||||
constants.put("googleApiAvailability", getPlayServicesStatus());
|
||||
constants.put("playServicesAvailability", getPlayServicesStatus());
|
||||
return constants;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user