-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathFirestackModule.java
173 lines (148 loc) · 5.71 KB
/
FirestackModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package io.fullstack.firestack;
import android.content.Context;
import android.util.Log;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.ReactContext;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.ServerValue;
class FirestackModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
private static final String TAG = "Firestack";
private Context context;
private ReactContext mReactContext;
private FirebaseApp app;
enum ConfigurationValues {
API_KEY("apiKey", "APIKey") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setApiKey(value);
}
},
APPLICATION_ID("applicationID", "applicationId") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setApplicationId(value);
}
},
GCM_SENDER_ID("gcmSenderID", "GCMSenderID") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setGcmSenderId(value);
}
},
STORAGE_BUCKET("storageBucket") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setStorageBucket(value);
}
}, DATABASE_URL("databaseUrl", "databaseURL") {
@Override
protected void doApply(FirebaseOptions.Builder builder, String value) {
builder.setDatabaseUrl(value);
}
};
ConfigurationValues(String... keys) {
this.keys = Arrays.asList(keys);
}
public void apply(ReadableMap params, FirebaseOptions.Builder builder) {
String value = lookUp(params);
Log.d(TAG, "Setting " + this.name() + " from params to: " + value);
doApply(builder, value);
}
private String lookUp(ReadableMap params) {
for (String key : keys) {
if (params.hasKey(key)) {
return params.getString(key);
}
}
return "";
}
protected abstract void doApply(FirebaseOptions.Builder builder, String value);
private final List<String> keys;
}
public FirestackModule(ReactApplicationContext reactContext, Context context) {
super(reactContext);
this.context = context;
mReactContext = reactContext;
Log.d(TAG, "New instance");
}
@Override
public String getName() {
return TAG;
}
@ReactMethod
public void configureWithOptions(final ReadableMap params, @Nullable final Callback onComplete) {
Log.i(TAG, "configureWithOptions");
try {
app = retrieveOrConfigure(params);
WritableMap resp = Arguments.createMap();
resp.putString("msg", "success");
onComplete.invoke(null, resp);
} catch (Exception ex) {
Log.e(TAG, "ERROR configureWithOptions");
Log.e(TAG, ex.getMessage());
WritableMap resp = Arguments.createMap();
resp.putString("msg", ex.getMessage());
onComplete.invoke(resp);
}
}
private FirebaseApp retrieveOrConfigure(ReadableMap params) {
if (FirebaseApp.getApps(context).size() > 0) {
return FirebaseApp.getInstance();
}
return configure(params);
}
@NonNull
private FirebaseApp configure(ReadableMap params) {
Log.i(TAG, "Configuring app");
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
for (ConfigurationValues configurationValue : ConfigurationValues.values()) {
configurationValue.apply(params, builder);
}
FirebaseApp firebaseApp = FirebaseApp.initializeApp(this.context, builder.build());
Log.i(TAG, "Configured");
return firebaseApp;
}
@ReactMethod
public void serverValue(@Nullable final Callback onComplete) {
WritableMap timestampMap = Arguments.createMap();
for (Map.Entry<String, String> entry : ServerValue.TIMESTAMP.entrySet()) {
timestampMap.putString(entry.getKey(), entry.getValue());
}
WritableMap map = Arguments.createMap();
map.putMap("TIMESTAMP", timestampMap);
onComplete.invoke(null, map);
}
// Internal helpers
@Override
public void onHostResume() {
WritableMap params = Arguments.createMap();
params.putBoolean("isForeground", true);
FirestackUtils.sendEvent(mReactContext, "FirestackAppState", params);
}
@Override
public void onHostPause() {
WritableMap params = Arguments.createMap();
params.putBoolean("isForeground", false);
FirestackUtils.sendEvent(mReactContext, "FirestackAppState", params);
}
@Override
public void onHostDestroy() {
}
}