-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathSerialization.java
More file actions
416 lines (365 loc) · 16.7 KB
/
Serialization.java
File metadata and controls
416 lines (365 loc) · 16.7 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
package com.iterable.reactnative;
import android.util.Log;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.iterable.iterableapi.CommerceItem;
import com.iterable.iterableapi.IterableAction;
import com.iterable.iterableapi.IterableActionContext;
import com.iterable.iterableapi.IterableConfig;
import com.iterable.iterableapi.IterableDataRegion;
import com.iterable.iterableapi.IterableInAppCloseAction;
import com.iterable.iterableapi.IterableInAppDeleteActionType;
import com.iterable.iterableapi.IterableInAppHandler;
import com.iterable.iterableapi.IterableInAppLocation;
import com.iterable.iterableapi.IterableInAppMessage;
import com.iterable.iterableapi.IterableInboxSession;
import com.iterable.iterableapi.IterableLogger;
import com.iterable.iterableapi.RNIterableInternal;
import com.iterable.iterableapi.RetryPolicy;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
class Serialization {
static String TAG = "Serialization";
static IterableInAppLocation getIterableInAppLocationFromInteger(@Nullable Integer location) {
if (location == null || location >= IterableInAppLocation.values().length || location < 0) {
return null;
} else {
return IterableInAppLocation.values()[location];
}
}
static IterableInAppCloseAction getIterableInAppCloseSourceFromInteger(@Nullable Integer location) {
if (location == null || location >= IterableInAppCloseAction.values().length || location < 0) {
return null;
} else {
return IterableInAppCloseAction.values()[location];
}
}
static IterableInAppDeleteActionType getIterableDeleteActionTypeFromInteger(@Nullable Integer actionType) {
if (actionType == null || actionType >= IterableInAppCloseAction.values().length || actionType < 0) {
return null;
} else {
return IterableInAppDeleteActionType.values()[actionType];
}
}
static IterableInAppHandler.InAppResponse getInAppResponse(@Nullable Integer inAppResponseInteger) {
if (inAppResponseInteger == null || inAppResponseInteger >= IterableInAppCloseAction.values().length || inAppResponseInteger < 0) {
return null;
} else {
return IterableInAppHandler.InAppResponse.values()[inAppResponseInteger];
}
}
static List<CommerceItem> commerceItemsFromReadableArray(ReadableArray array) {
ArrayList<CommerceItem> list = new ArrayList<>();
try {
JSONArray commerceItemJsonArray = convertArrayToJson(array);
for (int i = 0; i < commerceItemJsonArray.length(); i++) {
JSONObject item = commerceItemJsonArray.getJSONObject(i);
list.add(commerceItemFromMap(item));
}
} catch (JSONException e) {
IterableLogger.e(TAG, "Failed converting to JSONObject");
}
return list;
}
static CommerceItem commerceItemFromMap(JSONObject itemMap) throws JSONException {
String[] categories = null;
JSONArray categoriesArray = itemMap.optJSONArray("categories");
if (categoriesArray != null) {
for (int i = 0; i < categoriesArray.length(); i++) {
if (categories == null) {
categories = new String[categoriesArray.length()];
}
categories[i] = categoriesArray.getString(i);
}
}
return new CommerceItem(itemMap.getString("id"),
itemMap.getString("name"),
itemMap.getDouble("price"),
itemMap.getInt("quantity"),
itemMap.optString("sku", null),
itemMap.optString("description", null),
itemMap.optString("url", null),
itemMap.optString("imageUrl", null),
categories,
itemMap.optJSONObject("dataFields")
);
}
static JSONObject messageContentToJsonObject(IterableInAppMessage.Content content) {
if (content == null) {
return null;
}
JSONObject messageContent = new JSONObject();
try {
messageContent.put("edgeInsets", content.padding);
messageContent.put("html", content.html);
if (messageContent.length() == 0) {
return null;
}
} catch (JSONException e) {
IterableLogger.e(TAG, "Failed to serialize message content\n" + e.getLocalizedMessage());
return null;
}
return messageContent;
}
static JSONArray serializeInAppMessages(List<IterableInAppMessage> inAppMessages) {
JSONArray inAppMessagesJson = new JSONArray();
for (IterableInAppMessage message : inAppMessages) {
JSONObject messageJson = RNIterableInternal.getInAppMessageJson(message);
inAppMessagesJson.put(messageJson);
}
return inAppMessagesJson;
}
static IterableConfig.Builder getConfigFromReadableMap(ReadableMap iterableContextMap) {
try {
JSONObject iterableContextJSON = convertMapToJson(iterableContextMap);
IterableConfig.Builder configBuilder = new IterableConfig.Builder();
if (iterableContextJSON.has("allowedProtocols")) {
WritableArray allowedProtocolsArray = convertJsonToArray(iterableContextJSON.getJSONArray("allowedProtocols"));
String[] allowedProtocols = new String[allowedProtocolsArray.size()];
for (int i = 0; i < allowedProtocolsArray.size(); i++) {
allowedProtocols[i] = allowedProtocolsArray.getString(i);
}
if (allowedProtocols != null) {
configBuilder.setAllowedProtocols(allowedProtocols);
}
}
if (iterableContextJSON.has("pushIntegrationName")) {
configBuilder.setPushIntegrationName(iterableContextJSON.optString("pushIntegrationName"));
}
if (iterableContextJSON.has("autoPushRegistration")) {
configBuilder.setAutoPushRegistration(iterableContextJSON.optBoolean("autoPushRegistration"));
}
if (iterableContextJSON.has("inAppDisplayInterval")) {
configBuilder.setInAppDisplayInterval(iterableContextJSON.optDouble("inAppDisplayInterval"));
}
if (iterableContextJSON.has("expiringAuthTokenRefreshPeriod")) {
configBuilder.setExpiringAuthTokenRefreshPeriod(iterableContextJSON.optLong("expiringAuthTokenRefreshPeriod"));
}
if (iterableContextJSON.has("useInMemoryStorageForInApps") || iterableContextJSON.has("androidSdkUseInMemoryStorageForInApps")) {
configBuilder.setUseInMemoryStorageForInApps(iterableContextJSON.optBoolean("useInMemoryStorageForInApps"));
}
if (iterableContextJSON.has("logLevel")) {
int logLevel = iterableContextJSON.getInt("logLevel");
switch (logLevel) {
case 1:
//Debug
logLevel = Log.DEBUG;
break;
case 2:
//info
logLevel = Log.VERBOSE;
break;
case 3:
//error
logLevel = Log.ERROR;
break;
default:
logLevel = Log.ERROR;
break;
}
configBuilder.setLogLevel(logLevel);
}
if(iterableContextJSON.has("dataRegion")) {
int dataRegion = iterableContextJSON.getInt("dataRegion");
IterableDataRegion iterableDataRegion = IterableDataRegion.US;
switch (dataRegion) {
case 0:
iterableDataRegion = IterableDataRegion.US;
break;
case 1:
iterableDataRegion = IterableDataRegion.EU;
break;
default:
iterableDataRegion = IterableDataRegion.US;
break;
}
configBuilder.setDataRegion(iterableDataRegion);
}
if (iterableContextJSON.has("retryPolicy")) {
JSONObject retryPolicyJson = iterableContextJSON.getJSONObject("retryPolicy");
int maxRetry = retryPolicyJson.getInt("maxRetry");
long retryInterval = retryPolicyJson.getLong("retryInterval");
String retryBackoff = retryPolicyJson.getString("retryBackoff");
RetryPolicy.Type retryPolicyType = RetryPolicy.Type.LINEAR;
if (retryBackoff.equals("EXPONENTIAL")) {
retryPolicyType = RetryPolicy.Type.EXPONENTIAL;
}
configBuilder.setAuthRetryPolicy(new RetryPolicy(maxRetry, retryInterval, retryPolicyType));
}
return configBuilder;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
static JSONObject actionToJson(IterableAction iterableAction) {
JSONObject actionJson = new JSONObject();
try {
actionJson.put("type", iterableAction.getType());
if (iterableAction.getData() != null && iterableAction.getData() != "") {
actionJson.put("data", iterableAction.getData());
}
if (iterableAction.userInput == null && iterableAction.userInput != "") {
actionJson.put("userInput", iterableAction.userInput);
}
} catch (JSONException e) {
IterableLogger.e(TAG, e.getLocalizedMessage());
}
return actionJson;
}
static JSONObject actionContextToJson(IterableActionContext iterableActionContext) {
JSONObject actionContextJson = new JSONObject();
JSONObject actionJson = Serialization.actionToJson(iterableActionContext.action);
try {
actionContextJson.put("action", actionJson);
actionContextJson.put("source", iterableActionContext.source.ordinal());
} catch (JSONException e) {
IterableLogger.e(TAG, e.getLocalizedMessage());
}
return actionContextJson;
}
static IterableInboxSession.Impression inboxImpressionFromMap(JSONObject impressionMap) throws JSONException {
// Add null check for messageId to prevent NullPointerException
String messageId = impressionMap.optString("messageId", null);
if (messageId == null || messageId.isEmpty()) {
throw new JSONException("messageId is null or empty");
}
return new IterableInboxSession.Impression(messageId,
impressionMap.getBoolean("silentInbox"),
impressionMap.optInt("displayCount", 0),
(float) impressionMap.optDouble("duration", 0)
);
}
static List<IterableInboxSession.Impression> impressionsFromReadableArray(ReadableArray array) {
ArrayList<IterableInboxSession.Impression> list = new ArrayList<>();
try {
JSONArray impressionJsonArray = convertArrayToJson(array);
for (int i = 0; i < impressionJsonArray.length(); i++) {
try {
JSONObject impressionObj = impressionJsonArray.getJSONObject(i);
list.add(inboxImpressionFromMap(impressionObj));
} catch (JSONException e) {
// Skip invalid entries instead of failing completely
IterableLogger.w(TAG, "Skipping invalid impression at index " + i + ": " + e.getLocalizedMessage());
}
}
} catch (JSONException e) {
IterableLogger.e(TAG, "Failed converting to JSONObject");
}
return list;
}
// ---------------------------------------------------------------------------------------
// region React Native JSON conversion methods
// obtained from https://gist.github.com/viperwarp/2beb6bbefcc268dee7ad
static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
WritableMap map = new WritableNativeMap();
Iterator<String> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = jsonObject.get(key);
if (value instanceof JSONObject) {
map.putMap(key, convertJsonToMap((JSONObject) value));
} else if (value instanceof JSONArray) {
map.putArray(key, convertJsonToArray((JSONArray) value));
} else if (value instanceof Boolean) {
map.putBoolean(key, (Boolean) value);
} else if (value instanceof Integer) {
map.putInt(key, (Integer) value);
} else if (value instanceof Double) {
map.putDouble(key, (Double) value);
} else if (value instanceof String) {
map.putString(key, (String) value);
} else {
map.putString(key, value.toString());
}
}
return map;
}
static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSONException {
WritableArray array = new WritableNativeArray();
for (int i = 0; i < jsonArray.length(); i++) {
Object value = jsonArray.get(i);
if (value instanceof JSONObject) {
array.pushMap(convertJsonToMap((JSONObject) value));
} else if (value instanceof JSONArray) {
array.pushArray(convertJsonToArray((JSONArray) value));
} else if (value instanceof Boolean) {
array.pushBoolean((Boolean) value);
} else if (value instanceof Integer) {
array.pushInt((Integer) value);
} else if (value instanceof Double) {
array.pushDouble((Double) value);
} else if (value instanceof String) {
array.pushString((String) value);
} else {
array.pushString(value.toString());
}
}
return array;
}
static JSONObject convertMapToJson(ReadableMap readableMap) throws JSONException {
JSONObject object = new JSONObject();
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
switch (readableMap.getType(key)) {
case Null:
object.put(key, JSONObject.NULL);
break;
case Boolean:
object.put(key, readableMap.getBoolean(key));
break;
case Number:
object.put(key, readableMap.getDouble(key));
break;
case String:
object.put(key, readableMap.getString(key));
break;
case Map:
object.put(key, convertMapToJson(readableMap.getMap(key)));
break;
case Array:
object.put(key, convertArrayToJson(readableMap.getArray(key)));
break;
}
}
return object;
}
static JSONArray convertArrayToJson(ReadableArray readableArray) throws JSONException {
JSONArray array = new JSONArray();
for (int i = 0; i < readableArray.size(); i++) {
switch (readableArray.getType(i)) {
case Null:
break;
case Boolean:
array.put(readableArray.getBoolean(i));
break;
case Number:
array.put(readableArray.getDouble(i));
break;
case String:
array.put(readableArray.getString(i));
break;
case Map:
array.put(convertMapToJson(readableArray.getMap(i)));
break;
case Array:
array.put(convertArrayToJson(readableArray.getArray(i)));
break;
}
}
return array;
}
// ---------------------------------------------------------------------------------------
// endregion
}