-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathWebengageBridge.java
More file actions
549 lines (484 loc) · 21.5 KB
/
WebengageBridge.java
File metadata and controls
549 lines (484 loc) · 21.5 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package com.webengage;
/**
* Created by uzma on 10/25/17.
*/
import android.net.Uri;
import android.content.Context;
import android.util.Log;
import com.webengage.sdk.android.Logger;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.webengage.sdk.android.Analytics;
import com.webengage.sdk.android.UserProfile;
import com.webengage.sdk.android.WebEngage;
import com.webengage.sdk.android.actions.render.InAppNotificationData;
import com.webengage.sdk.android.actions.render.PushNotificationData;
import com.webengage.sdk.android.callbacks.InAppNotificationCallbacks;
import com.webengage.sdk.android.callbacks.PushNotificationCallbacks;
import com.webengage.sdk.android.utils.Gender;
import com.webengage.sdk.android.Channel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.annotation.Nullable;
//WebEngageBridge singelton
public class WebengageBridge extends ReactContextBaseJavaModule implements PushNotificationCallbacks,
InAppNotificationCallbacks {
private static final String TAG = "webengageBridge";
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
private static final int DATE_FORMAT_LENGTH = DATE_FORMAT.replaceAll("'", "").length();
private static int listenerCount = 0;
private static volatile WebengageBridge INSTANCE = null;
private static final Object lock = new Object();
private static final HashMap<String, WritableMap> queuedMap = new HashMap<>();
private ReactApplicationContext reactApplicationContext;
//no parameter initialization to be called from application class during react native initialization
public static WebengageBridge getInstance() {
Logger.d(TAG, "getInstance without context: ");
if (INSTANCE == null) {
synchronized (lock) {
INSTANCE = new WebengageBridge(null);
}
}
return INSTANCE;
}
//to be called during setting up package list
public static WebengageBridge getInstance(ReactApplicationContext reactContext) {
Logger.d(TAG, "getInstance: " + reactContext);
if (INSTANCE == null) {
synchronized (lock) {
INSTANCE = new WebengageBridge(reactContext);
}
}
return INSTANCE;
}
public void setReactNativeContext(ReactApplicationContext context) {
listenerCount = 0;
reactApplicationContext = context;
}
private WebengageBridge(ReactApplicationContext reactContext) {
super(reactContext);
Log.d(TAG, "MyLogs Constructor called");
WebEngage.registerPushNotificationCallback(this);
WebEngage.registerInAppNotificationCallback(this);
listenerCount = 0;
}
@ReactMethod
public void updateListenerCount() {
listenerCount++;
Logger.e(TAG, "updateListenerCount: " + listenerCount);
synchronized (lock) {
if (listenerCount > 0) {
HashMap<String, WritableMap> map = new HashMap<>();
map.putAll(queuedMap);
for (Map.Entry<String, WritableMap> entry : map.entrySet()) {
sendEvent(reactApplicationContext, entry.getKey(), entry.getValue());
queuedMap.remove(entry.getKey());
Logger.d(TAG, "Sending queued event: " + entry.getKey());
}
}
}
}
@Override
public String getName() {
return TAG;
}
@ReactMethod
public void init(boolean autoRegister) {
WebEngage.registerPushNotificationCallback(this);
WebEngage.registerInAppNotificationCallback(this);
}
private static Date getDate(String value) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat.parse(value);
} catch (Throwable t) {
}
return null;
}
@ReactMethod
public void trackEventWithName(String name) {
Analytics weAnalytics = WebEngage.get().analytics();
weAnalytics.track(name);
}
@ReactMethod
public void trackEventWithNameAndData(String name, ReadableMap values) {
Map<String, Object> map = recursivelyDeconstructReadableMap(values);
WebEngage.get().analytics().track(name, map);
}
@ReactMethod
public void screenNavigated(String name) {
WebEngage.get().analytics().screenNavigated(name);
}
@ReactMethod
public void screenNavigatedWithData(String name, ReadableMap userData) {
WebEngage.get().analytics().screenNavigated(name, recursivelyDeconstructReadableMap(userData));
}
@ReactMethod
public void login(String userIdentifier) {
WebEngage.get().user().login(userIdentifier);
}
@ReactMethod
public void setAttribute(ReadableMap readableMap) throws JSONException {
JSONObject jsonObject = new JSONObject();
Map<String, Object> hashMap = recursivelyDeconstructReadableMap(readableMap);
Logger.d(TAG, "Setting user attributes: " + hashMap);
WebEngage.get().user().setAttributes(hashMap);
}
@ReactMethod
public void deleteAttribute(String attributeName) {
WebEngage.get().user().deleteAttribute(attributeName);
}
@ReactMethod
public void deleteAttributes(ReadableArray attributeNames) {
int n = attributeNames.size();
List<String> result = new ArrayList<String>(n);
for (int i = 0; i < n; i++) {
ReadableType indexType = attributeNames.getType(i);
if (indexType == ReadableType.String) {
result.add(attributeNames.getString(i));
} else {
Logger.e(TAG, "Invalid data type at index " + i + ", key must be String.");
}
}
WebEngage.get().user().deleteAttributes(result);
}
@ReactMethod
public void setEmail(String email) {
WebEngage.get().user().setEmail(email);
}
@ReactMethod
public void setHashedEmail(String hashedEmail) {
WebEngage.get().user().setHashedEmail(hashedEmail);
}
@ReactMethod
public void setPhone(String phone) {
WebEngage.get().user().setPhoneNumber(phone);
}
@ReactMethod
public void setHashedPhone(String hashedPhone) {
WebEngage.get().user().setHashedPhoneNumber(hashedPhone);
}
@ReactMethod
public void setBirthDateString(String birthDateString) {
WebEngage.get().user().setBirthDate(birthDateString);
}
@ReactMethod
public void setGender(String gender) {
WebEngage.get().user().setGender(Gender.valueByString(gender));
}
@ReactMethod
public void setFirstName(String name) {
WebEngage.get().user().setFirstName(name);
}
@ReactMethod
public void setLastName(String name) {
WebEngage.get().user().setLastName(name);
}
@ReactMethod
public void setCompany(String company) {
WebEngage.get().user().setCompany(company);
}
@ReactMethod
public void setDevicePushOptIn(Boolean state) {
WebEngage.get().user().setDevicePushOptIn(state);
}
@ReactMethod
public void setOptIn(String channel, boolean status) {
if ("push".equalsIgnoreCase(channel)) {
WebEngage.get().user().setOptIn(Channel.PUSH, status);
} else if ("sms".equalsIgnoreCase(channel)) {
WebEngage.get().user().setOptIn(Channel.SMS, status);
} else if ("email".equalsIgnoreCase(channel)) {
WebEngage.get().user().setOptIn(Channel.EMAIL, status);
} else if ("in_app".equalsIgnoreCase(channel)) {
WebEngage.get().user().setOptIn(Channel.IN_APP, status);
} else if ("whatsapp".equalsIgnoreCase(channel)) {
WebEngage.get().user().setOptIn(Channel.WHATSAPP, status);
} else {
Logger.e(TAG, "Invalid channel: " + channel + ". Must be one of [push, sms, email, in_app, whatsapp].");
}
}
@ReactMethod
public void logout() {
WebEngage.get().user().logout();
}
@ReactMethod
public void addListener(String eventName) {
// Keep: Required for RN built in Event Emitter Calls.
}
@ReactMethod
public void removeListeners(Integer count) {
// Keep: Required for RN built in Event Emitter Calls.
}
private Map<String, Object> recursivelyDeconstructReadableMap(ReadableMap readableMap) {
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
Map<String, Object> deconstructedMap = new HashMap<>();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType type = readableMap.getType(key);
switch (type) {
case Null:
deconstructedMap.put(key, null);
break;
case Boolean:
deconstructedMap.put(key, readableMap.getBoolean(key));
break;
case Number:
deconstructedMap.put(key, readableMap.getDouble(key));
break;
case String:
String value = readableMap.getString(key);
if (value.length() == DATE_FORMAT_LENGTH) {
Date date = getDate(value);
if (date != null) {
deconstructedMap.put(key, date);
} else {
deconstructedMap.put(key, value);
}
} else {
deconstructedMap.put(key, value);
}
break;
case Map:
Map<String, Object> nestedMap = recursivelyDeconstructReadableMap(readableMap.getMap(key));
deconstructedMap.put(key, nestedMap);
break;
case Array:
List<Object> nestedList = recursivelyDeconstructReadableArray(readableMap.getArray(key));
deconstructedMap.put(key, nestedList);
break;
default:
Logger.e(TAG, "Could not convert object with key: " + key);
}
}
return deconstructedMap;
}
private List<Object> recursivelyDeconstructReadableArray(ReadableArray readableArray) {
List<Object> deconstructedList = new ArrayList<>(readableArray.size());
for (int i = 0; i < readableArray.size(); i++) {
ReadableType indexType = readableArray.getType(i);
switch (indexType) {
case Null:
deconstructedList.add(i, null);
break;
case Boolean:
deconstructedList.add(i, readableArray.getBoolean(i));
break;
case Number:
deconstructedList.add(i, readableArray.getDouble(i));
break;
case String:
String value = readableArray.getString(i);
if (value.length() == DATE_FORMAT_LENGTH) {
Date date = getDate(value);
if (date != null) {
deconstructedList.add(i, date);
} else {
deconstructedList.add(i, value);
}
} else {
deconstructedList.add(i, value);
}
break;
case Map:
deconstructedList.add(i, recursivelyDeconstructReadableMap(readableArray.getMap(i)));
break;
case Array:
deconstructedList.add(i, recursivelyDeconstructReadableArray(readableArray.getArray(i)));
break;
default:
Logger.e(TAG, "Could not convert object at index " + i);
}
}
return deconstructedList;
}
public static void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
Logger.d(TAG, " sendEvent: " + eventName + " context: " + reactContext);
if (listenerCount > 0 && reactContext != null) {
if (reactContext.hasCatalystInstance()) {
Logger.d(TAG, "Bridge hasActiveCatalystInstance");
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
} else {
Logger.d(TAG, "QUEUEING event: " + eventName);
queuedMap.put(eventName, params);
}
} else {
Logger.d(TAG, "QUEUEING event: " + eventName);
queuedMap.put(eventName, params);
}
}
private ReadableMap convertJsonObjectToReadable(JSONObject jsonObject) {
return convertJsonObjectToWriteable(jsonObject);
}
public static WritableMap convertJsonObjectToWriteable(JSONObject jsonObj) {
WritableMap map = Arguments.createMap();
Iterator<String> it = jsonObj.keys();
while (it.hasNext()) {
String key = it.next();
Object obj = null;
try {
obj = jsonObj.get(key);
} catch (JSONException jsonException) {
Logger.e(TAG, "Key " + key + " should exist in " + String.valueOf(jsonObj), jsonException);
} catch (Exception e) {
Logger.e(TAG, "Exception while getting value for " + key, e);
}
if (obj instanceof JSONObject)
map.putMap(key, convertJsonObjectToWriteable((JSONObject) obj));
else if (obj instanceof JSONArray)
map.putArray(key, convertJsonArrayToWriteable((JSONArray) obj));
else if (obj instanceof String)
map.putString(key, (String) obj);
else if (obj instanceof Double)
map.putDouble(key, (Double) obj);
else if (obj instanceof Integer)
map.putInt(key, (Integer) obj);
else if (obj instanceof Boolean)
map.putBoolean(key, (Boolean) obj);
else if (obj == null || (obj == JSONObject.NULL))
map.putNull(key);
else
Logger.e(TAG, "Unrecognized value for " + key + ": " + String.valueOf(obj));
}
return map;
}
public static WritableArray convertJsonArrayToWriteable(JSONArray jsonArr) {
WritableArray arr = Arguments.createArray();
for (int i = 0; i < jsonArr.length(); i++) {
Object obj = null;
try {
obj = jsonArr.get(i);
} catch (JSONException jsonException) {
Logger.e(TAG, i + " should be within bounds of array " + String.valueOf(jsonArr), jsonException);
}
if (obj instanceof JSONObject)
arr.pushMap(convertJsonObjectToWriteable((JSONObject) obj));
else if (obj instanceof JSONArray)
arr.pushArray(convertJsonArrayToWriteable((JSONArray) obj));
else if (obj instanceof String)
arr.pushString((String) obj);
else if (obj instanceof Double)
arr.pushDouble((Double) obj);
else if (obj instanceof Integer)
arr.pushInt((Integer) obj);
else if (obj instanceof Boolean)
arr.pushBoolean((Boolean) obj);
else if (obj == null)
arr.pushNull();
else
Logger.e(TAG, "Unrecognized object: " + String.valueOf(obj));
}
return arr;
}
@Override
public PushNotificationData onPushNotificationReceived(Context context, PushNotificationData pushNotificationData) {
WritableMap map = Arguments.fromBundle(pushNotificationData.getCustomData());
map.putMap("userData", convertJsonObjectToWriteable(pushNotificationData.getPushPayloadJSON()));
map.putString("deeplink", pushNotificationData.getPrimeCallToAction().getAction());
sendEvent(reactApplicationContext, "pushNotificationReceived", map);
return pushNotificationData;
}
@Override
public void onPushNotificationShown(Context context, PushNotificationData pushNotificationData) {
WritableMap map = Arguments.fromBundle(pushNotificationData.getCustomData());
map.putMap("userData", convertJsonObjectToWriteable(pushNotificationData.getPushPayloadJSON()));
map.putString("deeplink", pushNotificationData.getPrimeCallToAction().getAction());
sendEvent(reactApplicationContext, "pushNotificationShown", map);
}
@Override
public boolean onPushNotificationClicked(Context context, PushNotificationData pushNotificationData) {
WritableMap map = Arguments.fromBundle(pushNotificationData.getCustomData());
map.putMap("userData", convertJsonObjectToWriteable(pushNotificationData.getPushPayloadJSON()));
map.putString("deeplink", pushNotificationData.getPrimeCallToAction().getAction());
sendEvent(reactApplicationContext, "pushNotificationClicked", map);
return false;
}
@Override
public void onPushNotificationDismissed(Context context, PushNotificationData pushNotificationData) {
WritableMap map = Arguments.fromBundle(pushNotificationData.getCustomData());
map.putMap("userData", convertJsonObjectToWriteable(pushNotificationData.getPushPayloadJSON()));
map.putString("deeplink", pushNotificationData.getPrimeCallToAction().getAction());
sendEvent(reactApplicationContext, "pushNotificationDismissed", map);
}
@Override
public boolean onPushNotificationActionClicked(Context context, PushNotificationData pushNotificationData, String buttonId) {
WritableMap map = Arguments.fromBundle(pushNotificationData.getCustomData());
map.putMap("userData", convertJsonObjectToWriteable(pushNotificationData.getPushPayloadJSON()));
map.putString("deeplink", pushNotificationData.getCallToActionById(buttonId).getAction());
sendEvent(reactApplicationContext, "pushNotificationClicked", map);
return false;
}
@Override
public InAppNotificationData onInAppNotificationPrepared(Context context, InAppNotificationData inAppNotificationData) {
sendEvent(reactApplicationContext, "notificationPrepared", convertJsonObjectToWriteable(inAppNotificationData.getData()));
return inAppNotificationData;
}
@Override
public void onInAppNotificationShown(Context context, InAppNotificationData inAppNotificationData) {
WritableMap map = convertJsonObjectToWriteable(inAppNotificationData.getData());
Logger.d(TAG, "in-app notification data: " + map);
sendEvent(reactApplicationContext, "notificationShown", map);
}
@Override
public boolean onInAppNotificationClicked(Context context, InAppNotificationData inAppNotificationData, String actionId) {
Logger.d(TAG, "action id: " + actionId);
JSONObject jsonObject = inAppNotificationData.getData();
String actionLink = null;
try {
JSONArray actions = jsonObject.isNull("actions") ? null : jsonObject.getJSONArray("actions");
if (actions != null) {
for (int i = 0; i < actions.length(); i++) {
JSONObject action = actions.getJSONObject(i);
String actionEId = action.isNull("actionEId") ? null : action.optString("actionEId");
if (actionEId != null && actionEId.equals(actionId)) {
actionLink = action.isNull("actionLink") ? null : action.getString("actionLink");
break;
}
}
List<String> params = null;
try {
params = Uri.parse(actionLink).getPathSegments();
} catch (Exception e) {
}
if (params != null && params.size() > 1) {
actionLink = params.get(1);
}
Logger.d(TAG, "action link: " + actionLink);
}
} catch (JSONException e) {
Logger.e(TAG, "JSONException while getting action link from in-app notification data", e);
}
WritableMap map = convertJsonObjectToWriteable(jsonObject);
map.putString("deepLink", actionLink);
map.putString("clickId", actionId);
sendEvent(reactApplicationContext, "notificationClicked", map);
return false;
}
@Override
public void onInAppNotificationDismissed(Context context, InAppNotificationData inAppNotificationData) {
sendEvent(reactApplicationContext, "notificationDismissed", convertJsonObjectToWriteable(inAppNotificationData.getData()));
}
}