cordova-plugin-appice 2.0.9 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +0 -2
  2. package/example/config.xml +59 -59
  3. package/example/package.json +38 -38
  4. package/example/res/ai_android.pem +40 -40
  5. package/example/www/css/index.css +116 -116
  6. package/example/www/index.html +62 -62
  7. package/example/www/js/index.js +111 -102
  8. package/package.json +20 -2
  9. package/plugin.xml +193 -128
  10. package/scripts/BeforeAndroidBuilt.js +165 -102
  11. package/scripts/BeforeIosBuilt.js +35 -35
  12. package/scripts/androidAfterPluginAdd.js +67 -67
  13. package/scripts/androidAfterPluginRm.js +112 -112
  14. package/scripts/iOSAfterPluginAdd.js +58 -58
  15. package/scripts/iOSAfterPluginRm.js +30 -31
  16. package/src/android/AppICEFCMPush.java +26 -0
  17. package/src/android/AppICEMFPPush.java +67 -0
  18. package/{libcordova/src/main/java/com/appice/cordova → src/android}/AppICEPlugin.java +984 -968
  19. package/src/android/AppICEPushHandler.java +163 -0
  20. package/{libcordova/src/main/java/com/appice/cordova → src/android}/CampaignCampsReceiver.java +94 -95
  21. package/{libcordova/src/main/java/com/appice/cordova → src/android}/NotificationEventService.java +64 -55
  22. package/src/build.gradle +4 -2
  23. package/src/ios/AppDelegate+AppICEPlugin.m +178 -89
  24. package/src/ios/AppICEPlugin.h +3 -0
  25. package/src/ios/AppICEPlugin.m +55 -10
  26. package/www/AppICE.js +261 -228
  27. package/libcordova/android-release-aar.gradle +0 -63
  28. package/libcordova/build.gradle +0 -50
  29. package/libcordova/cordova.jar +0 -0
  30. package/libcordova/proguard-rules.pro +0 -21
  31. package/libcordova/src/main/AndroidManifest.xml +0 -24
  32. package/libcordova/src/main/res/values/strings.xml +0 -3
  33. package/src/firebase/modified/modified.iml +0 -11
  34. package/src/firebase/original/original.iml +0 -11
@@ -1,969 +1,985 @@
1
- //
2
- // AppICEPlugin.java
3
- //
4
- // AppICE, 08/11/16.
5
- //
6
- // AppICE analytics Plugin for Cordova
7
- // www.appice.io
8
- //
9
-
10
- package com.appice.cordova;
11
-
12
- import android.content.ComponentName;
13
- import android.content.Context;
14
- import android.content.Intent;
15
- import android.content.pm.PackageManager;
16
- import android.content.pm.ResolveInfo;
17
- import android.util.DisplayMetrics;
18
- import android.view.View;
19
-
20
- //import org.apache.cordova.CallbackContext;
21
- //import org.apache.cordova.CordovaInterface;
22
- //import org.apache.cordova.CordovaPlugin;
23
- //import org.apache.cordova.CordovaWebView;
24
- //import org.apache.cordova.PluginResult;
25
- import org.apache.cordova.CallbackContext;
26
- import org.apache.cordova.CordovaInterface;
27
- import org.apache.cordova.CordovaPlugin;
28
- import org.apache.cordova.CordovaWebView;
29
- import org.apache.cordova.PluginResult;
30
- import org.json.JSONArray;
31
- import org.json.JSONObject;
32
-
33
- import java.lang.annotation.Retention;
34
- import java.lang.reflect.Method;
35
- import java.util.ArrayList;
36
- import java.util.Arrays;
37
- import java.util.Collections;
38
- import java.util.HashMap;
39
- import java.util.Iterator;
40
- import java.util.List;
41
- import java.util.Map;
42
-
43
- import semusi.activitysdk.Api;
44
- import semusi.activitysdk.ContextData;
45
- import semusi.activitysdk.ContextSdk;
46
- import semusi.activitysdk.SdkConfig;
47
- import semusi.activitysdk.User;
48
-
49
- import static java.lang.annotation.RetentionPolicy.RUNTIME;
50
-
51
- public class AppICEPlugin extends CordovaPlugin {
52
- private static final String TAG = "AppICE_CP";
53
-
54
- HashMap<String, CallbackContext> callbackIds = new HashMap<String, CallbackContext>();
55
-
56
- private static final Map<String, Method> exportedMethods;
57
-
58
- @Retention(RUNTIME)
59
- @interface CordovaMethod {
60
-
61
- }
62
-
63
- static {
64
- HashMap<String, Method> methods = new HashMap<String, Method>();
65
-
66
- final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(AppICEPlugin.class.getDeclaredMethods()));
67
- for (final Method method : allMethods) {
68
- if (method.isAnnotationPresent(CordovaMethod.class)) {
69
- methods.put(method.getName(), method);
70
- }
71
- }
72
-
73
- exportedMethods = methods;
74
- }
75
-
76
- @Override
77
- public void initialize(CordovaInterface cordova, CordovaWebView webView) {
78
- super.initialize(cordova, webView);
79
-
80
- onNewIntent(cordova.getActivity().getIntent());
81
- }
82
-
83
- @CordovaMethod
84
- private void validateIntegration(JSONArray data, CallbackContext callbackContext) {
85
- try {
86
- Context ctx = cordova.getActivity().getApplicationContext();
87
- PackageManager pm = ctx.getPackageManager();
88
-
89
- Intent receiverIntent = new Intent();
90
- receiverIntent.setClass(ctx, CampaignCampsReceiver.class);
91
- List<ResolveInfo> receivers = pm.queryBroadcastReceivers(receiverIntent, 0);
92
- if (receivers == null || receivers.size() <= 0)
93
- callbackContext.error("Missing Receiver entry in AndroidManifest : CampaignCampsReceiver");
94
-
95
- Intent serviceIntent = new Intent();
96
- serviceIntent.setClass(ctx, NotificationEventService.class);
97
- ResolveInfo services = pm.resolveService(serviceIntent, 0);
98
- if (services == null)
99
- callbackContext.error("Missing Service entry in AndroidManifest : NotificationEventService");
100
-
101
- ContextSdk sdk = new ContextSdk(ctx);
102
- if (sdk.getAppId() == null || sdk.getAppId().length() <= 0 || sdk.getAppId().trim().length() <= 0) {
103
- callbackContext.error("Missing Meta-data entry : AppID");
104
- }
105
-
106
- if (sdk.getAppKey() == null || sdk.getAppKey().length() <= 0 || sdk.getAppKey().trim().length() <= 0) {
107
- callbackContext.error("Missing Meta-data entry : AppKey");
108
- return;
109
- }
110
-
111
- if (sdk.getApiKey() == null || sdk.getApiKey().length() <= 0 || sdk.getApiKey().trim().length() <= 0) {
112
- callbackContext.error("Missing Meta-data entry : ApiKey");
113
- return;
114
- }
115
- } catch (Throwable e) {
116
- callbackContext.error(e.getMessage());
117
- return;
118
- }
119
-
120
- callbackContext.success("");
121
- return;
122
- }
123
-
124
- // private void validateCordovaIntegration(Context ctx) {
125
- // try {
126
- // PackageManager pm = ctx.getPackageManager();
127
- //
128
- // Intent receiverIntent = new Intent();
129
- // receiverIntent.setClass(ctx, CampaignCampsReceiver.class);
130
- // List<ResolveInfo> receivers = pm.queryBroadcastReceivers(receiverIntent, 0);
131
- // if (receivers == null || receivers.size() <= 0)
132
- // Toast.makeText(ctx, "Missing Receiver entry in AndroidManifest : CampaignCampsReceiver", Toast.LENGTH_LONG).show();
133
- //
134
- // Intent serviceIntent = new Intent();
135
- // serviceIntent.setClass(ctx, NotificationEventService.class);
136
- // ResolveInfo services = pm.resolveService(serviceIntent, 0);
137
- // if (services == null)
138
- // Toast.makeText(ctx, "Missing Service entry in AndroidManifest : NotificationEventService", Toast.LENGTH_LONG).show();
139
- //
140
- // } catch (Throwable e) {
141
- // e.printStackTrace();
142
- // }
143
- // }
144
-
145
- @CordovaMethod
146
- private void startContext(JSONArray data, CallbackContext callbackContext) {
147
- try {
148
- //validateCordovaIntegration(cordova.getActivity().getApplicationContext());
149
-
150
- SdkConfig config = new SdkConfig();
151
-
152
- // Init sdk with your config
153
- Api.startContext(cordova.getActivity().getApplicationContext(), config);
154
-
155
- callbackContext.success();
156
- } catch (Throwable e) {
157
- callbackContext.error(e.getMessage());
158
- }
159
- }
160
-
161
- @CordovaMethod
162
- private void initSdk(JSONArray data, CallbackContext callbackContext) {
163
- try {
164
- //validateCordovaIntegration(cordova.getActivity().getApplicationContext());
165
-
166
- ArrayList<String> listCert = new ArrayList<>();
167
- SdkConfig config = new SdkConfig();
168
-
169
- String appId = "", appKey = "", apiKey = "", region = "", baseUrl = "";
170
- try {
171
- JSONObject root = data.getJSONObject(0);
172
-
173
- appId = root.optString("appID");
174
- appKey = root.optString("appKey");
175
- apiKey = root.optString("apiKey");
176
- region = root.optString("region");
177
- baseUrl = root.optString("baseUrl");
178
-
179
- if (root.has("certs")){
180
- JSONArray arr = root.optJSONArray("certs");
181
- if (arr != null){
182
- for (int i = 0; i < arr.length(); i++) {
183
- listCert.add(arr.get(i).toString());
184
- }
185
- }
186
- }
187
- } catch (Throwable e) {
188
- }
189
-
190
- config.setAnalyticsTrackingAllowedState(true);
191
-
192
- // Init sdk with your config
193
- Api.initSdk(appId, appKey, apiKey, region, baseUrl, listCert, cordova.getActivity().getApplicationContext());
194
-
195
- Api.startContext(cordova.getActivity().getApplicationContext(), config);
196
-
197
- callbackContext.success();
198
-
199
- } catch (Throwable e) {
200
- callbackContext.error(e.getMessage());
201
- }
202
- }
203
-
204
- @CordovaMethod
205
- private void setUserId(JSONArray data, CallbackContext callbackContext){
206
- ArrayList<String> userIdList = new ArrayList<>();
207
- try {
208
- JSONObject object = data.getJSONObject(0) ;
209
- JSONArray arr = object.optJSONArray("userID");
210
- if (arr != null) {
211
- for (int i = 0; i < arr.length(); i++) {
212
- userIdList.add((String) arr.get(i));
213
- }
214
-
215
- if (userIdList != null) {
216
- try {
217
- String[] userIds = userIdList.toArray(new String[userIdList.size()]);
218
- ContextSdk.setUser(userIds, cordova.getActivity().getApplicationContext());
219
- callbackContext.success();
220
- } catch (Throwable e) {
221
- callbackContext.error(e.getMessage());
222
- }
223
- }
224
- }
225
- } catch (Throwable e) {
226
- callbackContext.error(e.getMessage());
227
- }
228
- }
229
-
230
- @CordovaMethod
231
- private void stopContext(JSONArray data, CallbackContext callbackContext) {
232
- // Stop sdk
233
- try {
234
- Api.stopContext(cordova.getActivity().getApplicationContext());
235
- callbackContext.success();
236
- } catch (Throwable e) {
237
- callbackContext.error(e.getMessage());
238
- }
239
- }
240
-
241
- @CordovaMethod
242
- private void isSemusiSensing(JSONArray data, CallbackContext callbackContext) {
243
- // Check whether appice sdk is running or not
244
- try {
245
- boolean key = ContextSdk.isSemusiSensing(cordova.getActivity().getApplicationContext());
246
- if (key)
247
- callbackContext.success(1);
248
- else
249
- callbackContext.success(0);
250
- } catch (Throwable e) {
251
- callbackContext.error(e.getMessage());
252
- }
253
- }
254
-
255
- @CordovaMethod
256
- private void setAsTestDevice(JSONArray data, CallbackContext callbackContext) {
257
- // Set current device is marked as test device
258
- try {
259
- ContextSdk.setAsTestDevice(true, cordova.getActivity().getApplicationContext());
260
- callbackContext.success();
261
- } catch (Throwable e) {
262
- callbackContext.error(e.getMessage());
263
- }
264
- }
265
-
266
- @CordovaMethod
267
- private void removeAsTestDevice(JSONArray data, CallbackContext callbackContext) {
268
- // Remove current device is marked as test device
269
- try {
270
- ContextSdk.setAsTestDevice(false, cordova.getActivity().getApplicationContext());
271
- callbackContext.success();
272
- } catch (Throwable e) {
273
- callbackContext.error(e.getMessage());
274
- }
275
- }
276
-
277
- @CordovaMethod
278
- private void getIsTestDevice(JSONArray data, CallbackContext callbackContext) {
279
- // Check whether current device is marked as test device
280
- try {
281
- boolean key = ContextSdk.getIsTestDevice(cordova.getActivity().getApplicationContext());
282
- if (key)
283
- callbackContext.success(1);
284
- else
285
- callbackContext.success(0);
286
- } catch (Throwable e) {
287
- callbackContext.error(e.getMessage());
288
- }
289
- }
290
-
291
- @CordovaMethod
292
- private void openPlayServiceUpdate(JSONArray data, CallbackContext callbackContext) {
293
- // Open up google play service udpate UI for user
294
- try {
295
- ContextSdk.openPlayServiceUpdate(cordova.getActivity().getApplicationContext());
296
- callbackContext.success();
297
- } catch (Throwable e) {
298
- callbackContext.error(e.getMessage());
299
- }
300
- }
301
-
302
- @CordovaMethod
303
- private void getSdkVersion(JSONArray data, CallbackContext callbackContext) {
304
- // Get sdk version as string value
305
- try {
306
- String key = ContextSdk.getSdkVersion();
307
- callbackContext.success(key);
308
- } catch (Throwable e) {
309
- callbackContext.error(e.getMessage());
310
- }
311
- }
312
-
313
- @CordovaMethod
314
- private void getSdkIntVersion(JSONArray data, CallbackContext callbackContext) {
315
- // Get sdk version as int value
316
- try {
317
- int key = ContextSdk.getSdkIntVersion();
318
- callbackContext.success(key);
319
- } catch (Throwable e) {
320
- callbackContext.error(e.getMessage());
321
- }
322
- }
323
-
324
- @CordovaMethod
325
- private void setDeviceId(JSONArray data, CallbackContext callbackContext) {
326
- // Set new deviceId in system
327
- try {
328
- String deviceID = (String) data.getJSONObject(0).get("deviceID");
329
- if (deviceID != null && deviceID.length() > 0) {
330
- ContextSdk.setDeviceId(cordova.getActivity().getApplicationContext(), deviceID);
331
- callbackContext.success();
332
- }
333
- } catch (Throwable e) {
334
- callbackContext.error(e.getMessage());
335
- }
336
- }
337
-
338
- @CordovaMethod
339
- private void getDeviceId(JSONArray data, CallbackContext callbackContext) {
340
- // Gather existing device-id from system
341
- try {
342
- String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getDeviceId();
343
- callbackContext.success(key);
344
- } catch (Throwable e) {
345
- callbackContext.error(e.getMessage());
346
- }
347
- }
348
-
349
- @CordovaMethod
350
- private void getAndroidId(JSONArray data, CallbackContext callbackContext) {
351
- // Gather existing android-id from system
352
- try {
353
- String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getAndroidId();
354
- callbackContext.success(key);
355
- } catch (Throwable e) {
356
- callbackContext.error(e.getMessage());
357
- }
358
- }
359
-
360
- @CordovaMethod
361
- private void getAppKey(JSONArray data, CallbackContext callbackContext) {
362
- // Gather existing app-key from system
363
- try {
364
- String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getAppKey();
365
- callbackContext.success(key);
366
- } catch (Throwable e) {
367
- callbackContext.error(e.getMessage());
368
- }
369
- }
370
-
371
- @CordovaMethod
372
- private void getApiKey(JSONArray data, CallbackContext callbackContext) {
373
- // Gather existing api-key from system
374
- try {
375
- String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getApiKey();
376
- callbackContext.success(key);
377
- } catch (Throwable e) {
378
- callbackContext.error(e.getMessage());
379
- }
380
- }
381
-
382
- @CordovaMethod
383
- private void getAppId(JSONArray data, CallbackContext callbackContext) {
384
- // Gather existing app-id from system
385
- try {
386
- String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getAppId();
387
- callbackContext.success(key);
388
- } catch (Throwable e) {
389
- callbackContext.error(e.getMessage());
390
- }
391
- }
392
-
393
- @CordovaMethod
394
- private void getCurrentContext(JSONArray data, CallbackContext callbackContext) {
395
- // Get user current context from system
396
- try {
397
- ContextData userData = new ContextSdk(cordova.getActivity().getApplicationContext()).getCurrentContext();
398
- callbackContext.success();
399
- } catch (Throwable e) {
400
- callbackContext.error(e.getMessage());
401
- }
402
- }
403
-
404
- @CordovaMethod
405
- private void setAlias(JSONArray data, CallbackContext callbackContext) {
406
- // Set new deviceId in system
407
- try {
408
- String alias = (String) data.getJSONObject(0).get("alias");
409
- if (alias != null && alias.length() > 0) {
410
- ContextSdk.setAlias(alias, cordova.getActivity().getApplicationContext());
411
- callbackContext.success();
412
- }
413
- } catch (Throwable e) {
414
- callbackContext.error(e.getMessage());
415
- }
416
- }
417
-
418
- @CordovaMethod
419
- private void getAlias(JSONArray data, CallbackContext callbackContext) {
420
- // Get alias value from system
421
- try {
422
- String key = ContextSdk.getAlias(cordova.getActivity().getApplicationContext());
423
- callbackContext.success(key);
424
- } catch (Throwable e) {
425
- callbackContext.error(e.getMessage());
426
- }
427
- }
428
-
429
- @CordovaMethod
430
- private void setUser(JSONArray data, CallbackContext callbackContext) {
431
- // Set user in system
432
- try {
433
- User userinfo = ContextSdk.getUser(cordova.getActivity().getApplicationContext());
434
-
435
- String name = (String) data.getJSONObject(0).get("name");
436
- if (name != null && name.length() > 0) {
437
- userinfo.setName(name);
438
- }
439
- String phone = (String) data.getJSONObject(0).get("phone");
440
- if (phone != null && phone.length() > 0) {
441
- userinfo.setPhone(phone);
442
- }
443
- String email = (String) data.getJSONObject(0).get("email");
444
- if (email != null && email.length() > 0) {
445
- userinfo.setEmail(email);
446
- }
447
- ContextSdk.setUser(userinfo, cordova.getActivity().getApplicationContext());
448
-
449
- callbackContext.success();
450
- } catch (Throwable e) {
451
- callbackContext.error(e.getMessage());
452
- }
453
- }
454
-
455
- @CordovaMethod
456
- private void getUser(JSONArray data, CallbackContext callbackContext) {
457
- // Get current configured user
458
- try {
459
- User userInfo = ContextSdk.getUser(cordova.getActivity().getApplicationContext());
460
- callbackContext.success(userInfo.toString());
461
- } catch (Throwable e) {
462
- callbackContext.error(e.getMessage());
463
- }
464
- }
465
-
466
- @CordovaMethod
467
- private void setChildId(JSONArray data, CallbackContext callbackContext) {
468
- // Set new child-id in system
469
- try {
470
- String childID = (String) data.getJSONObject(0).get("childID");
471
- if (childID != null && childID.length() > 0) {
472
- ContextSdk.setChildId(childID, cordova.getActivity().getApplicationContext());
473
- callbackContext.success();
474
- }
475
- } catch (Throwable e) {
476
- callbackContext.error(e.getMessage());
477
- }
478
- }
479
-
480
- @CordovaMethod
481
- private void getChildId(JSONArray data, CallbackContext callbackContext) {
482
- // Get child-id value from system
483
- try {
484
- String key = ContextSdk.getChildId(cordova.getActivity().getApplicationContext());
485
- callbackContext.success(key);
486
- } catch (Throwable e) {
487
- callbackContext.error(e.getMessage());
488
- }
489
- }
490
-
491
- @CordovaMethod
492
- private void setReferrer(JSONArray data, CallbackContext callbackContext) {
493
- // Set new referrer in system
494
- try {
495
- String key = (String) data.getJSONObject(0).get("referrer");
496
- if (key != null && key.length() > 0) {
497
- ContextSdk.setReferrer(key, cordova.getActivity().getApplicationContext());
498
- callbackContext.success();
499
- }
500
- } catch (Throwable e) {
501
- callbackContext.error(e.getMessage());
502
- }
503
- }
504
-
505
- @CordovaMethod
506
- private void getReferrer(JSONArray data, CallbackContext callbackContext) {
507
- // Get referrer value from system
508
- try {
509
- String key = ContextSdk.getReferrer(cordova.getActivity().getApplicationContext());
510
- callbackContext.success(key);
511
- } catch (Throwable e) {
512
- callbackContext.error(e.getMessage());
513
- }
514
- }
515
-
516
- @CordovaMethod
517
- private void setInstallReferrer(JSONArray data, CallbackContext callbackContext) {
518
- // Set new install referrer in system
519
- try {
520
- String key = (String) data.getJSONObject(0).get("installRef");
521
- if (key != null && key.length() > 0) {
522
- ContextSdk.setInstallReferrer(key, cordova.getActivity().getApplicationContext());
523
- callbackContext.success();
524
- }
525
- } catch (Throwable e) {
526
- callbackContext.error(e.getMessage());
527
- }
528
- }
529
-
530
- @CordovaMethod
531
- private void getInstallReferrer(JSONArray data, CallbackContext callbackContext) {
532
- // Get install referrer value from system
533
- try {
534
- String key = ContextSdk.getInstallReferrer(cordova.getActivity().getApplicationContext());
535
- callbackContext.success(key);
536
- } catch (Throwable e) {
537
- callbackContext.error(e.getMessage());
538
- }
539
- }
540
-
541
- @CordovaMethod
542
- private void setInstaller(JSONArray data, CallbackContext callbackContext) {
543
- // Set new installer in system
544
- try {
545
- String key = (String) data.getJSONObject(0).get("installer");
546
- if (key != null && key.length() > 0) {
547
- ContextSdk.setInstaller(key, cordova.getActivity().getApplicationContext());
548
- callbackContext.success();
549
- }
550
- } catch (Throwable e) {
551
- callbackContext.error(e.getMessage());
552
- }
553
- }
554
-
555
- @CordovaMethod
556
- private void getInstaller(JSONArray data, CallbackContext callbackContext) {
557
- // Get installer value from system
558
- try {
559
- String key = ContextSdk.getInstaller(cordova.getActivity().getApplicationContext());
560
- callbackContext.success(key);
561
- } catch (Throwable e) {
562
- callbackContext.error(e.getMessage());
563
- }
564
- }
565
-
566
- @CordovaMethod
567
- private void getGCMSenderId(JSONArray data, CallbackContext callbackContext) {
568
- // Get gcm sender id
569
- try {
570
- String key = "GCM-Deprecated";
571
- callbackContext.success(key);
572
- } catch (Throwable e) {
573
- callbackContext.error(e.getMessage());
574
- }
575
- }
576
-
577
- @CordovaMethod
578
- private void getDeviceToken(JSONArray data, CallbackContext callbackContext) {
579
- // Get gcm sender id
580
- try {
581
- String registrationId = "";
582
- callbackContext.success(registrationId);
583
- } catch (Throwable e) {
584
- callbackContext.error(e.getMessage());
585
- }
586
- }
587
-
588
- @CordovaMethod
589
- private void setCustomVariable(JSONArray data, CallbackContext callbackContext) {
590
- // Set custom variable in system
591
- try {
592
- String key = (String) data.getJSONObject(0).get("key");
593
- Object value = data.getJSONObject(0).get("value");
594
- if (key != null && key.length() > 0 && value != null) {
595
- Class valueClass = value.getClass();
596
- if (valueClass == Integer.class)
597
- ContextSdk.setCustomVariable(key, (Integer) value, cordova.getActivity().getApplicationContext());
598
- else if (valueClass == Float.class || valueClass == Double.class)
599
- ContextSdk.setCustomVariable(key, (Float) value, cordova.getActivity().getApplicationContext());
600
- else if (valueClass == Long.class)
601
- ContextSdk.setCustomVariable(key, (Long) value, cordova.getActivity().getApplicationContext());
602
- else if (valueClass == String.class)
603
- ContextSdk.setCustomVariable(key, (String) value, cordova.getActivity().getApplicationContext());
604
- else if (valueClass == Boolean.class)
605
- ContextSdk.setCustomVariable(key, (Boolean) value, cordova.getActivity().getApplicationContext());
606
-
607
- callbackContext.success();
608
- }
609
- } catch (Throwable e) {
610
- callbackContext.error(e.getMessage());
611
- }
612
- }
613
-
614
- @CordovaMethod
615
- private void getCustomVariable(JSONArray data, CallbackContext callbackContext) {
616
- // Get custom variable value from system
617
- try {
618
- String key = (String) data.getJSONObject(0).get("key");
619
- Object value = ContextSdk.getCustomVariable(key, cordova.getActivity().getApplicationContext());
620
- callbackContext.success((String) value);
621
- } catch (Throwable e) {
622
- callbackContext.error(e.getMessage());
623
- }
624
- }
625
-
626
- @CordovaMethod
627
- private void removeCustomVariable(JSONArray data, CallbackContext callbackContext) {
628
- // Get remove custom variable value from system
629
- try {
630
- String key = (String) data.getJSONObject(0).get("key");
631
- ContextSdk.removeCustomVariable(key, cordova.getActivity().getApplicationContext());
632
- callbackContext.success();
633
- } catch (Throwable e) {
634
- callbackContext.error(e.getMessage());
635
- }
636
- }
637
-
638
- @CordovaMethod
639
- private void tagEvent(JSONArray data, CallbackContext callbackContext) {
640
- // Set event in system
641
- try {
642
- String key = (String) data.getJSONObject(0).get("key");
643
- if (key != null && key.length() > 0) {
644
- HashMap<String, Object> mapData = new HashMap<String, Object>();
645
- try {
646
- JSONObject hashData = (JSONObject) data.getJSONObject(0).get("data");
647
- if (hashData != null && hashData.length() > 0) {
648
- Iterator<String> keys = hashData.keys();
649
- while (keys.hasNext()) {
650
- String dataKey = keys.next();
651
- if (dataKey != null && dataKey.length() > 0) {
652
- mapData.put(dataKey, hashData.get(dataKey));
653
- }
654
- }
655
- }
656
- } catch (Throwable e) {
657
- }
658
-
659
- ContextSdk.tagEventObj(key, mapData, cordova.getActivity().getApplicationContext());
660
- callbackContext.success();
661
- }
662
- } catch (Throwable e) {
663
- callbackContext.error(e.getMessage());
664
- }
665
- }
666
-
667
- @CordovaMethod
668
- private void setSmallIcon(JSONArray data, CallbackContext callbackContext) {
669
- // Set small icon in system
670
- try {
671
- String key = (String) data.getJSONObject(0).get("icon");
672
- if (key != null && key.length() > 0) {
673
- ContextSdk.setSmallIcon(key, cordova.getActivity().getApplicationContext());
674
- }
675
- callbackContext.success();
676
- } catch (Throwable e) {
677
- callbackContext.error(e.getMessage());
678
- }
679
- }
680
-
681
- @CordovaMethod
682
- private void setSessionTimeout(JSONArray data, CallbackContext callbackContext) {
683
- // Set session timeout in system
684
- try {
685
- int key = data.getJSONObject(0).getInt("timeout");
686
- ContextSdk.setSessionTimeout(key, cordova.getActivity().getApplicationContext());
687
-
688
- callbackContext.success();
689
- } catch (Throwable e) {
690
- callbackContext.error(e.getMessage());
691
- }
692
- }
693
-
694
- @CordovaMethod
695
- private void getSessionTimeout(JSONArray data, CallbackContext callbackContext) {
696
- // Get installer value from system
697
- try {
698
- int key = ContextSdk.getSessionTimeout(cordova.getActivity().getApplicationContext());
699
- callbackContext.success(key);
700
- } catch (Throwable e) {
701
- callbackContext.error(e.getMessage());
702
- }
703
- }
704
-
705
- @CordovaMethod
706
- private void trackTouches(JSONArray data, CallbackContext callbackContext) {
707
- try {
708
- DisplayMetrics dm = new DisplayMetrics();
709
- cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
710
- int screenWidth = dm.widthPixels;
711
- int screenHeight = dm.heightPixels;
712
-
713
- JSONObject root = data.getJSONObject(0);
714
- if (root != null) {
715
- double webWidth = (double) root.optInt("w");
716
- double webHeight = (double) root.optInt("h");
717
-
718
- double wRatio = (screenWidth / webWidth);
719
- double hRatio = (screenHeight / webHeight);
720
-
721
- // store rescaled x-y co-ordinates of touch
722
- double x = root.optDouble("x");
723
- double y = root.optDouble("y");
724
- x = x * wRatio;
725
- y = y * hRatio;
726
- root.put("x", x);
727
- root.put("y", y);
728
-
729
- // store rescaled px-py-pw-ph co-ordinates of touch
730
- double px = root.optDouble("px");
731
- double py = root.optDouble("py");
732
- double pw = root.optDouble("pw");
733
- double ph = root.optDouble("ph");
734
- px = px * wRatio;
735
- py = py * hRatio;
736
- pw = pw * wRatio;
737
- ph = ph * hRatio;
738
- root.put("px", px);
739
- root.put("py", py);
740
- root.put("pw", pw);
741
- root.put("ph", ph);
742
-
743
- // re-store rescaled x-y-w-h co-ordinates of input fld
744
- JSONArray newObjects = new JSONArray();
745
- JSONArray objects = root.optJSONArray("arr");
746
- if (objects != null && objects.length() > 0) {
747
- for (int i = 0; i < objects.length(); i++) {
748
- JSONObject inputFld = objects.optJSONObject(i);
749
- if (inputFld != null && inputFld.length() > 0) {
750
- JSONObject obj = new JSONObject(inputFld.toString());
751
- obj.put("x", inputFld.optDouble("x") * wRatio);
752
- obj.put("y", (inputFld.optDouble("y") * hRatio) + inputFld.optDouble("h"));
753
- obj.put("w", inputFld.optDouble("w") * wRatio);
754
- obj.put("h", inputFld.optDouble("h") * hRatio);
755
- newObjects.put(obj);
756
- }
757
- }
758
- }
759
- root.put("arr", newObjects);
760
- }
761
-
762
- View rootView = cordova.getActivity().getWindow().getDecorView().getRootView();
763
- ContextSdk.trackTouches(root, cordova.getActivity().getApplicationContext(), rootView, cordova.getActivity());
764
-
765
- callbackContext.success();
766
- } catch (Throwable e) {
767
- callbackContext.error(e.getMessage());
768
- }
769
- }
770
-
771
- @CordovaMethod
772
- private void trackSwipes(JSONArray data, CallbackContext callbackContext) {
773
- try {
774
- DisplayMetrics dm = new DisplayMetrics();
775
- cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
776
- int screenWidth = dm.widthPixels;
777
- int screenHeight = dm.heightPixels;
778
-
779
- JSONObject root = data.getJSONObject(0);
780
- if (root != null) {
781
- double webWidth = (double) root.optInt("w");
782
- double webHeight = (double) root.optInt("h");
783
-
784
- double wRatio = (screenWidth / webWidth);
785
- double hRatio = (screenHeight / webHeight);
786
-
787
- // store rescaled x-y co-ordinates of touch
788
- double x1 = root.optDouble("x1");
789
- double y1 = root.optDouble("y1");
790
- x1 = x1 * wRatio;
791
- y1 = y1 * hRatio;
792
- root.put("x1", x1);
793
- root.put("y1", y1);
794
-
795
- // store rescaled x-y co-ordinates of touch
796
- double x2 = root.optDouble("x2");
797
- double y2 = root.optDouble("y2");
798
- x2 = x2 * wRatio;
799
- y2 = y2 * hRatio;
800
- root.put("x2", x2);
801
- root.put("y2", y2);
802
-
803
- // re-store rescaled x-y-w-h co-ordinates of input fld
804
- JSONArray newObjects = new JSONArray();
805
- JSONArray objects = root.optJSONArray("arr");
806
- if (objects != null && objects.length() > 0) {
807
- for (int i = 0; i < objects.length(); i++) {
808
- JSONObject inputFld = objects.optJSONObject(i);
809
- if (inputFld != null && inputFld.length() > 0) {
810
- JSONObject obj = new JSONObject(inputFld.toString());
811
- obj.put("x", inputFld.optDouble("x") * wRatio);
812
- obj.put("y", (inputFld.optDouble("y") * hRatio) + inputFld.optDouble("h"));
813
- obj.put("w", inputFld.optDouble("w") * wRatio);
814
- obj.put("h", inputFld.optDouble("h") * hRatio);
815
- newObjects.put(obj);
816
- }
817
- }
818
- }
819
- root.put("arr", newObjects);
820
- }
821
-
822
- View rootView = cordova.getActivity().getWindow().getDecorView().getRootView();
823
- ContextSdk.trackSwipes(root, cordova.getActivity().getApplicationContext(), rootView, cordova.getActivity());
824
-
825
- callbackContext.success();
826
- } catch (Throwable e) {
827
- callbackContext.error(e.getMessage());
828
- }
829
- }
830
-
831
- @CordovaMethod
832
- private void trackScreens(JSONArray data, CallbackContext callbackContext) {
833
- try {
834
- DisplayMetrics dm = new DisplayMetrics();
835
- cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
836
- int screenWidth = dm.widthPixels;
837
- int screenHeight = dm.heightPixels;
838
-
839
- JSONObject root = data.getJSONObject(0);
840
- if (root != null) {
841
- double webWidth = (double) root.optInt("w");
842
- double webHeight = (double) root.optInt("h");
843
-
844
- double wRatio = (screenWidth / webWidth);
845
- double hRatio = (screenHeight / webHeight);
846
-
847
- // re-store rescaled x-y-w-h co-ordinates of input fld
848
- JSONArray newObjects = new JSONArray();
849
- JSONArray objects = root.optJSONArray("arr");
850
- if (objects != null && objects.length() > 0) {
851
- for (int i = 0; i < objects.length(); i++) {
852
- JSONObject inputFld = objects.optJSONObject(i);
853
- if (inputFld != null && inputFld.length() > 0) {
854
- JSONObject obj = new JSONObject(inputFld.toString());
855
- obj.put("x", inputFld.optDouble("x") * wRatio);
856
- obj.put("y", (inputFld.optDouble("y") * hRatio) + inputFld.optDouble("h"));
857
- obj.put("w", inputFld.optDouble("w") * wRatio);
858
- obj.put("h", inputFld.optDouble("h") * hRatio);
859
- newObjects.put(obj);
860
- }
861
- }
862
- }
863
- root.put("arr", newObjects);
864
- }
865
-
866
- View rootView = cordova.getActivity().getWindow().getDecorView().getRootView();
867
- ContextSdk.trackScreens(root, cordova.getActivity().getApplicationContext(), rootView, cordova.getActivity());
868
-
869
- callbackContext.success();
870
- } catch (Throwable e) {
871
- callbackContext.error(e.getMessage());
872
- }
873
- }
874
-
875
- @Override
876
- public boolean execute(String action, JSONArray data, CallbackContext callbackId) {
877
- Method method = exportedMethods.get(action);
878
- if (method == null) {
879
- return false;
880
- }
881
-
882
- try {
883
- Boolean result = (Boolean) method.invoke(this, data, callbackId);
884
- return true;
885
- } catch (Throwable e) {
886
- return false;
887
- }
888
- }
889
-
890
- public static void sendNotification(JSONObject bundle, Context context) {
891
- if (!AppICEPlugin.hasNotificationsCallback() && AppICEPlugin.inBackground()) {
892
- String packageName = context.getPackageName();
893
- if (AppICEPlugin.notificationStack == null) {
894
- AppICEPlugin.notificationStack = new ArrayList<JSONObject>();
895
- }
896
- notificationStack.add(bundle);
897
-
898
- /* start the main activity, if not running */
899
- Intent intent = new Intent("android.intent.action.MAIN");
900
- intent.setComponent(new ComponentName(packageName, packageName + ".MainActivity"));
901
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
902
- intent.putExtra("cdvStartInBackground", true);
903
- context.startActivity(intent);
904
-
905
- return;
906
- }
907
-
908
- try {
909
- final CallbackContext callbackContext = AppICEPlugin.notificationCallbackContext;
910
- if (callbackContext != null && bundle != null) {
911
-
912
- PluginResult pluginresult = new PluginResult(PluginResult.Status.OK, bundle);
913
- pluginresult.setKeepCallback(true);
914
- callbackContext.sendPluginResult(pluginresult);
915
-
916
-
917
- } else if (callbackContext == null) {
918
- String packageName = context.getPackageName();
919
- if (AppICEPlugin.notificationStack == null) {
920
- AppICEPlugin.notificationStack = new ArrayList<JSONObject>();
921
- }
922
- notificationStack.add(bundle);
923
- }
924
- } catch (Throwable e) {
925
- }
926
- }
927
-
928
- private static boolean inBackground = true;
929
- private static ArrayList<JSONObject> notificationStack = null;
930
- private static CallbackContext notificationCallbackContext;
931
-
932
- @Override
933
- public void onPause(boolean multitasking) {
934
- AppICEPlugin.inBackground = true;
935
- }
936
-
937
- @Override
938
- public void onResume(boolean multitasking) {
939
- AppICEPlugin.inBackground = false;
940
- }
941
-
942
- @Override
943
- public void onReset() {
944
- AppICEPlugin.notificationCallbackContext = null;
945
- }
946
-
947
- public static boolean inBackground() {
948
- return AppICEPlugin.inBackground;
949
- }
950
-
951
- public static boolean hasNotificationsCallback() {
952
- return AppICEPlugin.notificationCallbackContext != null;
953
- }
954
-
955
- @CordovaMethod
956
- private void onNotificationOpen(JSONArray data, final CallbackContext callbackContext) {
957
- try {
958
- AppICEPlugin.notificationCallbackContext = callbackContext;
959
- if (AppICEPlugin.notificationStack != null) {
960
- for (JSONObject bundle : AppICEPlugin.notificationStack) {
961
- AppICEPlugin.sendNotification(bundle, this.cordova.getActivity().getApplicationContext());
962
- }
963
- AppICEPlugin.notificationStack.clear();
964
- }
965
- } catch (Throwable e) {
966
- }
967
- }
968
-
1
+ package com.appice.cordova;
2
+
3
+ import android.content.Context;
4
+ import android.content.Intent;
5
+ import android.content.pm.PackageManager;
6
+ import android.content.pm.ResolveInfo;
7
+ import android.net.Uri;
8
+ import android.os.Build;
9
+ import android.os.Bundle;
10
+ import android.os.Handler;
11
+ import android.os.Looper;
12
+ import android.util.DisplayMetrics;
13
+ import android.util.Log;
14
+ import android.view.View;
15
+
16
+ import org.apache.cordova.CallbackContext;
17
+ import org.apache.cordova.CordovaInterface;
18
+ import org.apache.cordova.CordovaPlugin;
19
+ import org.apache.cordova.CordovaWebView;
20
+ import org.apache.cordova.PluginResult;
21
+ import org.json.JSONArray;
22
+ import org.json.JSONObject;
23
+
24
+ import java.lang.annotation.Retention;
25
+ import java.lang.reflect.Method;
26
+ import java.util.ArrayList;
27
+ import java.util.Arrays;
28
+ import java.util.HashMap;
29
+ import java.util.Iterator;
30
+ import java.util.List;
31
+ import java.util.Map;
32
+ import java.util.Timer;
33
+ import java.util.TimerTask;
34
+
35
+ import semusi.activitysdk.Api;
36
+ import semusi.activitysdk.ContextData;
37
+ import semusi.activitysdk.ContextSdk;
38
+ import semusi.activitysdk.SdkConfig;
39
+ import semusi.activitysdk.User;
40
+ import static java.lang.annotation.RetentionPolicy.RUNTIME;
41
+
42
+ public class AppICEPlugin extends CordovaPlugin {
43
+
44
+ private static final String TAG = AppICEPlugin.class.getSimpleName();
45
+ private static final Map<String, Method> exportedMethods;
46
+ private static boolean inBackground = true;
47
+ private static CallbackContext notificationCallbackContext;
48
+ private static CallbackContext callbackContext;
49
+
50
+ @Retention(RUNTIME)
51
+ @interface CordovaMethod { }
52
+
53
+ static {
54
+ HashMap<String, Method> methods = new HashMap<String, Method>();
55
+
56
+ final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(AppICEPlugin.class.getDeclaredMethods()));
57
+ for (final Method method : allMethods) {
58
+ if (method.isAnnotationPresent(CordovaMethod.class)) {
59
+ methods.put(method.getName(), method);
60
+ }
61
+ }
62
+ exportedMethods = methods;
63
+ }
64
+
65
+ @Override
66
+ public void onPause(boolean multitasking) {
67
+ AppICEPlugin.inBackground = true;
68
+ }
69
+
70
+ @Override
71
+ public void onResume(boolean multitasking) {
72
+ AppICEPlugin.inBackground = false;
73
+ }
74
+
75
+ @Override
76
+ public void onReset() {
77
+ AppICEPlugin.notificationCallbackContext = null;
78
+ }
79
+
80
+ public static boolean inBackground() {
81
+ return AppICEPlugin.inBackground;
82
+ }
83
+
84
+ public static boolean hasNotificationsCallback() {
85
+ return AppICEPlugin.notificationCallbackContext != null;
86
+ }
87
+
88
+ @Override
89
+ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
90
+ super.initialize(cordova, webView);
91
+
92
+ onNewIntent(cordova.getActivity().getIntent());
93
+ }
94
+
95
+ public void onNewIntent(Intent intent) {
96
+ if (intent == null) return;
97
+ System.out.println("onNewIntent intent "+intent);
98
+ try {
99
+ if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_VIEW)) {
100
+ Uri data = intent.getData();
101
+ if (data != null) {
102
+ final String json = "{'deeplink':'" + data.toString() + "'}";
103
+
104
+ cordova.getActivity().runOnUiThread(new Runnable() {
105
+ @Override
106
+ public void run() {
107
+ webView.loadUrl("javascript:cordova.fireDocumentEvent('onDeepLink'," + json + ");");
108
+ }
109
+ });
110
+ }
111
+ }
112
+ // push notification
113
+ else {
114
+
115
+ new Timer("AppICE-NotifService-Timer").schedule(new TimerTask() {
116
+ @Override
117
+ public void run() {
118
+
119
+ try{
120
+ String url = "";
121
+ Bundle extras = intent.getExtras();
122
+ System.out.println("onNewIntent bundle "+extras);
123
+ String payload = extras.getString("ai_content");
124
+ boolean isPushNotification = (payload != null && payload.length()>0)?true:false;
125
+ if (isPushNotification) {
126
+ JSONObject data = new JSONObject(payload);
127
+
128
+ try {
129
+ url = data.optString("eurl");
130
+ if (data.has("cdata")) {
131
+ JSONObject cDATA = data.optJSONObject("cdata");
132
+ String test = cDATA.optString("eurl");
133
+ if (test != null && test.length()>0){
134
+ url = test;
135
+ }
136
+ }
137
+ System.out.println("chromium : url "+url);
138
+ if (url != null && url.length()>0){
139
+ final String json = "{'deeplink':'" + url + "'}";
140
+
141
+ webView.getView().post(new Runnable() {
142
+ @Override
143
+ public void run() {
144
+ webView.loadUrl("javascript:cordova.fireDocumentEvent('onDeepLink'," + json + ");");
145
+ }
146
+ });
147
+
148
+ }
149
+ }catch (Throwable t){
150
+ Log.e(TAG, "onNewIntent: customDeeplinkError ", t );
151
+ }
152
+
153
+
154
+ final String json = "{'payload':" + data.toString() + "}";
155
+ webView.getView().post(new Runnable() {
156
+ @Override
157
+ public void run() {
158
+ webView.loadUrl("javascript:cordova.fireDocumentEvent('pushNotificationClicked'," + json + ");");
159
+ }
160
+ });
161
+
162
+ try {
163
+ if (data.has("cdata")) {
164
+ JSONObject cDATA = data.optJSONObject("cdata");
165
+ final String customData = "{'customData':'" + cDATA.toString() + "'}";
166
+
167
+ webView.getView().post(new Runnable() {
168
+ @Override
169
+ public void run() {
170
+ webView.loadUrl("javascript:cordova.fireDocumentEvent('onCustomDataCallback'," + customData + ");");
171
+ }
172
+ });
173
+ }
174
+ }catch (Throwable t){
175
+ Log.e(TAG, "onNewIntent: customDataError ", t);
176
+ }
177
+
178
+ ContextSdk.pushNotificationClicked(payload, cordova.getActivity().getApplicationContext());
179
+ }
180
+
181
+
182
+ }catch (Throwable throwable){}
183
+
184
+ }
185
+ }, 1000);
186
+ }
187
+ }catch (Throwable throwable){
188
+ }
189
+
190
+ }
191
+
192
+
193
+ @Override
194
+ public boolean execute(String action, JSONArray data, CallbackContext callbackId) {
195
+ Method method = exportedMethods.get(action);
196
+ if (method == null) {
197
+ return false;
198
+ }
199
+
200
+ try {
201
+ Boolean result = (Boolean) method.invoke(this, data, callbackId);
202
+ return true;
203
+ } catch (Throwable e) {
204
+ return false;
205
+ }
206
+ }
207
+
208
+ /*====================================
209
+ // SDK CONFIG SETTING APIS
210
+ //===================================*/
211
+ @CordovaMethod
212
+ private void validateIntegration(JSONArray data, CallbackContext callbackContext) {
213
+ try {
214
+ Context ctx = cordova.getActivity().getApplicationContext();
215
+ PackageManager pm = ctx.getPackageManager();
216
+
217
+ Intent receiverIntent = new Intent();
218
+ receiverIntent.setClass(ctx, CampaignCampsReceiver.class);
219
+ List<ResolveInfo> receivers = pm.queryBroadcastReceivers(receiverIntent, 0);
220
+ if (receivers == null || receivers.size() <= 0)
221
+ callbackContext.error("Missing Receiver entry in AndroidManifest : CampaignCampsReceiver");
222
+
223
+ Intent serviceIntent = new Intent();
224
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
225
+ serviceIntent.setClass(ctx, NotificationEventService.class);
226
+ }
227
+ ResolveInfo services = pm.resolveService(serviceIntent, 0);
228
+ if (services == null)
229
+ callbackContext.error("Missing Service entry in AndroidManifest : NotificationEventService");
230
+
231
+ ContextSdk sdk = new ContextSdk(ctx);
232
+ if (sdk.getAppId() == null || sdk.getAppId().length() <= 0 || sdk.getAppId().trim().length() <= 0) {
233
+ callbackContext.error("Missing Meta-data entry : AppID");
234
+ }
235
+
236
+ if (sdk.getAppKey() == null || sdk.getAppKey().length() <= 0 || sdk.getAppKey().trim().length() <= 0) {
237
+ callbackContext.error("Missing Meta-data entry : AppKey");
238
+ }
239
+
240
+ if (sdk.getApiKey() == null || sdk.getApiKey().length() <= 0 || sdk.getApiKey().trim().length() <= 0) {
241
+ callbackContext.error("Missing Meta-data entry : ApiKey");
242
+ }
243
+ } catch (Throwable e) {
244
+ callbackContext.error(e.getMessage());
245
+ }
246
+ callbackContext.success("");
247
+ }
248
+
249
+ @CordovaMethod
250
+ private void startContext(JSONArray data, CallbackContext callbackContext) {
251
+ try {
252
+ SdkConfig config = new SdkConfig();
253
+ Api.startContext(cordova.getActivity().getApplicationContext(), config);
254
+
255
+ callbackContext.success();
256
+ } catch (Throwable e) {
257
+ callbackContext.error(e.getMessage());
258
+ }
259
+ }
260
+
261
+ @CordovaMethod
262
+ private void initSdk(JSONArray data, CallbackContext callbackContext) {
263
+ try {
264
+ ArrayList<String> listCert = new ArrayList<>();
265
+ SdkConfig config = new SdkConfig();
266
+
267
+ String appId = "", appKey = "", apiKey = "", region = "", baseUrl = "";
268
+ try {
269
+ JSONObject root = data.getJSONObject(0);
270
+
271
+ appId = root.optString("appID");
272
+ appKey = root.optString("appKey");
273
+ apiKey = root.optString("apiKey");
274
+ region = root.optString("region");
275
+ baseUrl = root.optString("baseUrl");
276
+
277
+ if (root.has("certs")) {
278
+ JSONArray arr = root.optJSONArray("certs");
279
+ if (arr != null) {
280
+ for (int i = 0; i < arr.length(); i++) {
281
+ listCert.add(arr.get(i).toString());
282
+ }
283
+ }
284
+ }
285
+ } catch (Throwable e) {
286
+ Log.e(TAG, "initSdk: error ", e);
287
+ }
288
+
289
+ config.setAnalyticsTrackingAllowedState(true);
290
+ Api.initSdk(appId, appKey, apiKey, region, baseUrl, listCert, cordova.getActivity().getApplicationContext());
291
+ Api.startContext(cordova.getActivity().getApplicationContext(), config);
292
+
293
+ callbackContext.success();
294
+
295
+ } catch (Throwable e) {
296
+ callbackContext.error(e.getMessage());
297
+ }
298
+ }
299
+
300
+ @CordovaMethod
301
+ private void stopContext(JSONArray data, CallbackContext callbackContext) {
302
+ try {
303
+ Api.stopContext(cordova.getActivity().getApplicationContext());
304
+ callbackContext.success();
305
+ } catch (Throwable e) {
306
+ callbackContext.error(e.getMessage());
307
+ }
308
+ }
309
+
310
+ @CordovaMethod
311
+ private void registerLifeCycle(JSONArray data, CallbackContext callbackContext) {
312
+ try {
313
+ // Get a handler that can be used to post to the main thread
314
+ Handler mainHandler = new Handler(Looper.getMainLooper());
315
+
316
+ Runnable myRunnable = new Runnable() {
317
+ @Override
318
+ public void run() {
319
+ System.out.println("registerLifeCycle reached");
320
+ ContextSdk.registerLifeCycle(cordova.getActivity().getApplicationContext());
321
+ callbackContext.success();
322
+ }
323
+ };
324
+ mainHandler.post(myRunnable);
325
+
326
+ }catch (Throwable t){
327
+ callbackContext.error(t.getMessage());
328
+ }
329
+ }
330
+
331
+ /*=================================
332
+ // GET SDK DETAILS
333
+ //================================ */
334
+ @CordovaMethod
335
+ private void getCurrentContext(JSONArray data, CallbackContext callbackContext) {
336
+ // Get user current context from system
337
+ try {
338
+ ContextData userData = new ContextSdk(cordova.getActivity().getApplicationContext()).getCurrentContext();
339
+ callbackContext.success();
340
+ } catch (Throwable e) {
341
+ callbackContext.error(e.getMessage());
342
+ }
343
+ }
344
+
345
+ @CordovaMethod
346
+ private void getSdkVersion(JSONArray data, CallbackContext callbackContext) {
347
+ // Get sdk version as string value
348
+ try {
349
+ String key = ContextSdk.getSdkVersion();
350
+ callbackContext.success(key);
351
+ } catch (Throwable e) {
352
+ callbackContext.error(e.getMessage());
353
+ }
354
+ }
355
+
356
+ @CordovaMethod
357
+ private void getSdkIntVersion(JSONArray data, CallbackContext callbackContext) {
358
+ // Get sdk version as int value
359
+ try {
360
+ int key = ContextSdk.getSdkIntVersion();
361
+ callbackContext.success(key);
362
+ } catch (Throwable e) {
363
+ callbackContext.error(e.getMessage());
364
+ }
365
+ }
366
+
367
+ /*======================================
368
+ // DEVICE SETTING APIS
369
+ //=====================================*/
370
+ @CordovaMethod
371
+ private void setDeviceId(JSONArray data, CallbackContext callbackContext) {
372
+ // Set new deviceId in system
373
+ try {
374
+ String deviceID = (String) data.getJSONObject(0).get("deviceID");
375
+ if (deviceID != null && deviceID.length() > 0) {
376
+ ContextSdk.setDeviceId(cordova.getActivity().getApplicationContext(), deviceID);
377
+ callbackContext.success();
378
+ }
379
+ } catch (Throwable e) {
380
+ callbackContext.error(e.getMessage());
381
+ }
382
+ }
383
+
384
+ @CordovaMethod
385
+ private void getDeviceId(JSONArray data, CallbackContext callbackContext) {
386
+ // Gather existing device-id from system
387
+ try {
388
+ String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getDeviceId();
389
+ callbackContext.success(key);
390
+ } catch (Throwable e) {
391
+ callbackContext.error(e.getMessage());
392
+ }
393
+ }
394
+
395
+ @CordovaMethod
396
+ private void getAndroidId(JSONArray data, CallbackContext callbackContext) {
397
+ // Gather existing android-id from system
398
+ try {
399
+ String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getAndroidId();
400
+ callbackContext.success(key);
401
+ } catch (Throwable e) {
402
+ callbackContext.error(e.getMessage());
403
+ }
404
+ }
405
+
406
+ /*=============================
407
+ // META DATA SETTING APIS
408
+ //=============================*/
409
+ @CordovaMethod
410
+ private void getAppKey(JSONArray data, CallbackContext callbackContext) {
411
+ // Gather existing app-key from system
412
+ try {
413
+ String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getAppKey();
414
+ callbackContext.success(key);
415
+ } catch (Throwable e) {
416
+ callbackContext.error(e.getMessage());
417
+ }
418
+ }
419
+
420
+ @CordovaMethod
421
+ private void getApiKey(JSONArray data, CallbackContext callbackContext) {
422
+ // Gather existing api-key from system
423
+ try {
424
+ String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getApiKey();
425
+ callbackContext.success(key);
426
+ } catch (Throwable e) {
427
+ callbackContext.error(e.getMessage());
428
+ }
429
+ }
430
+
431
+ @CordovaMethod
432
+ private void getAppId(JSONArray data, CallbackContext callbackContext) {
433
+ // Gather existing app-id from system
434
+ try {
435
+ String key = new ContextSdk(cordova.getActivity().getApplicationContext()).getAppId();
436
+ callbackContext.success(key);
437
+ } catch (Throwable e) {
438
+ callbackContext.error(e.getMessage());
439
+ }
440
+ }
441
+
442
+ //=================================
443
+ // ALIAS SETTING APIS
444
+ //==================================
445
+ @CordovaMethod
446
+ private void setAlias(JSONArray data, CallbackContext callbackContext) {
447
+ // Set new deviceId in system
448
+ try {
449
+ String alias = (String) data.getJSONObject(0).get("alias");
450
+ if (alias != null && alias.length() > 0) {
451
+ ContextSdk.setAlias(alias, cordova.getActivity().getApplicationContext());
452
+ callbackContext.success();
453
+ }
454
+ } catch (Throwable e) {
455
+ callbackContext.error(e.getMessage());
456
+ }
457
+ }
458
+
459
+ @CordovaMethod
460
+ private void getAlias(JSONArray data, CallbackContext callbackContext) {
461
+ // Get alias value from system
462
+ try {
463
+ String key = ContextSdk.getAlias(cordova.getActivity().getApplicationContext());
464
+ callbackContext.success(key);
465
+ } catch (Throwable e) {
466
+ callbackContext.error(e.getMessage());
467
+ }
468
+ }
469
+
470
+ //================================
471
+ // USER PROFILE SETTING APIS
472
+ //================================
473
+ @CordovaMethod
474
+ private void setUserId(JSONArray data, CallbackContext callbackContext) {
475
+ ArrayList<String> userIdList = new ArrayList<>();
476
+ try {
477
+ JSONObject object = data.getJSONObject(0);
478
+ JSONArray arr = object.optJSONArray("userID");
479
+ if (arr != null) {
480
+ for (int i = 0; i < arr.length(); i++) {
481
+ userIdList.add((String) arr.get(i));
482
+ }
483
+
484
+ if (userIdList != null) {
485
+ try {
486
+ String[] userIds = userIdList.toArray(new String[userIdList.size()]);
487
+ ContextSdk.setUser(userIds, cordova.getActivity().getApplicationContext());
488
+ callbackContext.success();
489
+ } catch (Throwable e) {
490
+ callbackContext.error(e.getMessage());
491
+ }
492
+ }
493
+ }
494
+ } catch (Throwable e) {
495
+ callbackContext.error(e.getMessage());
496
+ }
497
+ }
498
+
499
+ @CordovaMethod
500
+ private void setUser(JSONArray data, CallbackContext callbackContext) {
501
+ // Set user in system
502
+ try {
503
+ User userinfo = ContextSdk.getUser(cordova.getActivity().getApplicationContext());
504
+
505
+ String name = (String) data.getJSONObject(0).get("name");
506
+ if (name != null && name.length() > 0) {
507
+ userinfo.setName(name);
508
+ }
509
+ String phone = (String) data.getJSONObject(0).get("phone");
510
+ if (phone != null && phone.length() > 0) {
511
+ userinfo.setPhone(phone);
512
+ }
513
+ String email = (String) data.getJSONObject(0).get("email");
514
+ if (email != null && email.length() > 0) {
515
+ userinfo.setEmail(email);
516
+ }
517
+ ContextSdk.setUser(userinfo, cordova.getActivity().getApplicationContext());
518
+
519
+ callbackContext.success();
520
+ } catch (Throwable e) {
521
+ callbackContext.error(e.getMessage());
522
+ }
523
+ }
524
+
525
+ @CordovaMethod
526
+ private void getUser(JSONArray data, CallbackContext callbackContext) {
527
+ // Get current configured user
528
+ try {
529
+ User userInfo = ContextSdk.getUser(cordova.getActivity().getApplicationContext());
530
+ callbackContext.success(userInfo.toString());
531
+ } catch (Throwable e) {
532
+ callbackContext.error(e.getMessage());
533
+ }
534
+ }
535
+
536
+ //=========================================
537
+ // CUSTOM VARIABLE AND EVENT SETTING APIS
538
+ //==========================================
539
+ @CordovaMethod
540
+ private void setCustomVariable(JSONArray data, CallbackContext callbackContext) {
541
+ // Set custom variable in system
542
+ try {
543
+ String key = (String) data.getJSONObject(0).get("key");
544
+ Object value = data.getJSONObject(0).get("value");
545
+ if (key != null && key.length() > 0 && value != null) {
546
+ Class valueClass = value.getClass();
547
+ if (valueClass == Integer.class)
548
+ ContextSdk.setCustomVariable(key, (Integer) value, cordova.getActivity().getApplicationContext());
549
+ else if (valueClass == Float.class || valueClass == Double.class)
550
+ ContextSdk.setCustomVariable(key, (Float) value, cordova.getActivity().getApplicationContext());
551
+ else if (valueClass == Long.class)
552
+ ContextSdk.setCustomVariable(key, (Long) value, cordova.getActivity().getApplicationContext());
553
+ else if (valueClass == String.class)
554
+ ContextSdk.setCustomVariable(key, (String) value, cordova.getActivity().getApplicationContext());
555
+ else if (valueClass == Boolean.class)
556
+ ContextSdk.setCustomVariable(key, (Boolean) value, cordova.getActivity().getApplicationContext());
557
+
558
+ callbackContext.success();
559
+ }
560
+ } catch (Throwable e) {
561
+ callbackContext.error(e.getMessage());
562
+ }
563
+ }
564
+
565
+ @CordovaMethod
566
+ private void getCustomVariable(JSONArray data, CallbackContext callbackContext) {
567
+ // Get custom variable value from system
568
+ try {
569
+ String key = (String) data.getJSONObject(0).get("key");
570
+ Object value = ContextSdk.getCustomVariable(key, cordova.getActivity().getApplicationContext());
571
+ callbackContext.success((String) value);
572
+ } catch (Throwable e) {
573
+ callbackContext.error(e.getMessage());
574
+ }
575
+ }
576
+
577
+ @CordovaMethod
578
+ private void removeCustomVariable(JSONArray data, CallbackContext callbackContext) {
579
+ // Get remove custom variable value from system
580
+ try {
581
+ String key = (String) data.getJSONObject(0).get("key");
582
+ ContextSdk.removeCustomVariable(key, cordova.getActivity().getApplicationContext());
583
+ callbackContext.success();
584
+ } catch (Throwable e) {
585
+ callbackContext.error(e.getMessage());
586
+ }
587
+ }
588
+
589
+ @CordovaMethod
590
+ private void tagEvent(JSONArray data, CallbackContext callbackContext) {
591
+ // Set event in system
592
+ try {
593
+ String key = (String) data.getJSONObject(0).get("key");
594
+ if (key != null && key.length() > 0) {
595
+ HashMap<String, Object> mapData = new HashMap<String, Object>();
596
+ try {
597
+ JSONObject hashData = (JSONObject) data.getJSONObject(0).get("data");
598
+ if (hashData != null && hashData.length() > 0) {
599
+ Iterator<String> keys = hashData.keys();
600
+ while (keys.hasNext()) {
601
+ String dataKey = keys.next();
602
+ if (dataKey != null && dataKey.length() > 0) {
603
+ mapData.put(dataKey, hashData.get(dataKey));
604
+ }
605
+ }
606
+ }
607
+ } catch (Throwable e) {
608
+ Log.e(TAG, "tagEvent: error ", e);
609
+ }
610
+
611
+ ContextSdk.tagEventObj(key, mapData, cordova.getActivity().getApplicationContext());
612
+ callbackContext.success();
613
+ }
614
+ } catch (Throwable e) {
615
+ callbackContext.error(e.getMessage());
616
+ }
617
+ }
618
+
619
+ //=========================================
620
+ // CHILD KEY SETTING APIS
621
+ //==========================================
622
+
623
+ @CordovaMethod
624
+ private void setChildId(JSONArray data, CallbackContext callbackContext) {
625
+ // Set new child-id in system
626
+ try {
627
+ String childID = (String) data.getJSONObject(0).get("childID");
628
+ if (childID != null && childID.length() > 0) {
629
+ ContextSdk.setChildId(childID, cordova.getActivity().getApplicationContext());
630
+ callbackContext.success();
631
+ }
632
+ } catch (Throwable e) {
633
+ callbackContext.error(e.getMessage());
634
+ }
635
+ }
636
+
637
+ @CordovaMethod
638
+ private void getChildId(JSONArray data, CallbackContext callbackContext) {
639
+ // Get child-id value from system
640
+ try {
641
+ String key = ContextSdk.getChildId(cordova.getActivity().getApplicationContext());
642
+ callbackContext.success(key);
643
+ } catch (Throwable e) {
644
+ callbackContext.error(e.getMessage());
645
+ }
646
+ }
647
+
648
+ //=========================================
649
+ // PLAY STORE SETTING APIS
650
+ //=========================================
651
+ @CordovaMethod
652
+ private void openPlayServiceUpdate(JSONArray data, CallbackContext callbackContext) {
653
+ // Open up google play service update UI for user
654
+ try {
655
+ ContextSdk.openPlayServiceUpdate(cordova.getActivity().getApplicationContext());
656
+ callbackContext.success();
657
+ } catch (Throwable e) {
658
+ callbackContext.error(e.getMessage());
659
+ }
660
+ }
661
+
662
+ //==================================
663
+ // PUSH NOTIFICATION SETTING APIS
664
+ //==================================
665
+ @CordovaMethod
666
+ private void setSmallIcon(JSONArray data, CallbackContext callbackContext) {
667
+ // Set small icon in system
668
+ try {
669
+ String key = (String) data.getJSONObject(0).get("icon");
670
+ if (key != null && key.length() > 0) {
671
+ ContextSdk.setSmallIcon(key, cordova.getActivity().getApplicationContext());
672
+ }
673
+ callbackContext.success();
674
+ } catch (Throwable e) {
675
+ callbackContext.error(e.getMessage());
676
+ }
677
+ }
678
+
679
+ @CordovaMethod
680
+ private void getDeviceToken(JSONArray data, CallbackContext callbackContext) {
681
+ // Get gcm sender id
682
+ try {
683
+ String registrationId = "";
684
+ callbackContext.success(registrationId);
685
+ } catch (Throwable e) {
686
+ callbackContext.error(e.getMessage());
687
+ }
688
+ }
689
+
690
+ @CordovaMethod
691
+ private void onNotificationOpen(JSONArray data, final CallbackContext callbackContext) {
692
+ try {
693
+ this.callbackContext = callbackContext;
694
+ } catch (Throwable e) {
695
+ Log.e(TAG, "onNotificationOpen: error ", e);
696
+ }
697
+ }
698
+
699
+ @CordovaMethod
700
+ private void pushNotificationClicked(JSONArray data, final CallbackContext callbackContext){
701
+ try {
702
+ if(data != null){
703
+ String payload = (String) data.getJSONObject(0).get("pushPayload");
704
+ ContextSdk.pushNotificationClicked(payload, cordova.getActivity().getApplicationContext());
705
+ callbackContext.success();
706
+ }
707
+ } catch (Throwable e) {
708
+ callbackContext.error(e.getMessage());
709
+ Log.e(TAG, "pushNotificationClicked: error ", e);
710
+ }
711
+ }
712
+
713
+ @CordovaMethod
714
+ private void getDeepLinkURL(JSONArray data, final CallbackContext callbackContext){
715
+ try {
716
+ if(data != null){
717
+ String url = "";
718
+ String payload = (String) data.getJSONObject(0).get("pushPayload");
719
+ JSONObject object = new JSONObject(payload);
720
+ url = object.optString("eurl");
721
+ if (object.has("cdata")) {
722
+ JSONObject cDATA = object.optJSONObject("cdata");
723
+ url = cDATA.optString("eurl");
724
+ }
725
+ callbackContext.success(url);
726
+ }
727
+ } catch (Throwable e) {
728
+ callbackContext.error(e.getMessage());
729
+ Log.e(TAG, "pushNotificationClicked: error ", e);
730
+ }
731
+ }
732
+
733
+ public static void sendNotification(JSONObject bundle, Context context) {
734
+ new Thread(new Runnable() {
735
+ @Override
736
+ public void run() {
737
+ try {
738
+ System.out.println("testing send notification "+bundle);
739
+ if (callbackContext != null) {
740
+ String pushPaylaod = bundle.getString("ai_content");
741
+ PluginResult result = new PluginResult(PluginResult.Status.OK, pushPaylaod);
742
+ result.setKeepCallback(true);
743
+ callbackContext.sendPluginResult(result);
744
+ }
745
+ } catch (Throwable t) {
746
+ Log.e(TAG, "run: error ", t);
747
+ }
748
+ }
749
+ }).start();
750
+ }
751
+
752
+ //==================================
753
+ // SESSION SETTING APIS
754
+ //==================================
755
+ @CordovaMethod
756
+ private void setSessionTimeout(JSONArray data, CallbackContext callbackContext) {
757
+ // Set session timeout in system
758
+ try {
759
+ int key = data.getJSONObject(0).getInt("timeout");
760
+ ContextSdk.setSessionTimeout(key, cordova.getActivity().getApplicationContext());
761
+
762
+ callbackContext.success();
763
+ } catch (Throwable e) {
764
+ callbackContext.error(e.getMessage());
765
+ }
766
+ }
767
+
768
+ @CordovaMethod
769
+ private void getSessionTimeout(JSONArray data, CallbackContext callbackContext) {
770
+ // Get installer value from system
771
+ try {
772
+ int key = ContextSdk.getSessionTimeout(cordova.getActivity().getApplicationContext());
773
+ callbackContext.success(key);
774
+ } catch (Throwable e) {
775
+ callbackContext.error(e.getMessage());
776
+ }
777
+ }
778
+
779
+
780
+ //===============================
781
+ // VISUAL PLAYBACK SETTING APIS
782
+ //===============================
783
+
784
+ @CordovaMethod
785
+ private void isSemusiSensing(JSONArray data, CallbackContext callbackContext) {
786
+ try {
787
+ boolean key = ContextSdk.isSemusiSensing(cordova.getActivity().getApplicationContext());
788
+ if (key)
789
+ callbackContext.success(1);
790
+ else
791
+ callbackContext.success(0);
792
+ } catch (Throwable e) {
793
+ callbackContext.error(e.getMessage());
794
+ }
795
+ }
796
+
797
+ @CordovaMethod
798
+ private void trackTouches(JSONArray data, CallbackContext callbackContext) {
799
+ new Thread(new Runnable() {
800
+ @Override
801
+ public void run() {
802
+ try {
803
+ DisplayMetrics dm = new DisplayMetrics();
804
+ cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
805
+ int screenWidth = dm.widthPixels;
806
+ int screenHeight = dm.heightPixels;
807
+
808
+ JSONObject root = data.getJSONObject(0);
809
+ if (root != null) {
810
+ double webWidth = (double) root.optInt("w");
811
+ double webHeight = (double) root.optInt("h");
812
+
813
+ double wRatio = (screenWidth / webWidth);
814
+ double hRatio = (screenHeight / webHeight);
815
+
816
+ // store rescaled x-y co-ordinates of touch
817
+ double x = root.optDouble("x");
818
+ double y = root.optDouble("y");
819
+ x = x * wRatio;
820
+ y = y * hRatio;
821
+ root.put("x", x);
822
+ root.put("y", y);
823
+
824
+ // store rescaled px-py-pw-ph co-ordinates of touch
825
+ double px = root.optDouble("px");
826
+ double py = root.optDouble("py");
827
+ double pw = root.optDouble("pw");
828
+ double ph = root.optDouble("ph");
829
+ px = px * wRatio;
830
+ py = py * hRatio;
831
+ pw = pw * wRatio;
832
+ ph = ph * hRatio;
833
+ root.put("px", px);
834
+ root.put("py", py);
835
+ root.put("pw", pw);
836
+ root.put("ph", ph);
837
+
838
+ // re-store rescaled x-y-w-h co-ordinates of input fld
839
+ JSONArray newObjects = new JSONArray();
840
+ JSONArray objects = root.optJSONArray("arr");
841
+ if (objects != null && objects.length() > 0) {
842
+ for (int i = 0; i < objects.length(); i++) {
843
+ JSONObject inputFld = objects.optJSONObject(i);
844
+ if (inputFld != null && inputFld.length() > 0) {
845
+ JSONObject obj = new JSONObject(inputFld.toString());
846
+ obj.put("x", inputFld.optDouble("x") * wRatio);
847
+ obj.put("y", (inputFld.optDouble("y") * hRatio) + inputFld.optDouble("h"));
848
+ obj.put("w", inputFld.optDouble("w") * wRatio);
849
+ obj.put("h", inputFld.optDouble("h") * hRatio);
850
+ newObjects.put(obj);
851
+ }
852
+ }
853
+ }
854
+ root.put("arr", newObjects);
855
+ }
856
+
857
+ View rootView = cordova.getActivity().getWindow().getDecorView().getRootView();
858
+ ContextSdk.trackTouches(root, cordova.getActivity().getApplicationContext(), rootView, cordova.getActivity());
859
+
860
+ callbackContext.success();
861
+ } catch (Throwable e) {
862
+ callbackContext.error(e.getMessage());
863
+ }
864
+ }
865
+ }).start();
866
+ }
867
+
868
+ @CordovaMethod
869
+ private void trackSwipes(JSONArray data, CallbackContext callbackContext) {
870
+ new Thread(new Runnable() {
871
+ @Override
872
+ public void run() {
873
+ try {
874
+ DisplayMetrics dm = new DisplayMetrics();
875
+ cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
876
+ int screenWidth = dm.widthPixels;
877
+ int screenHeight = dm.heightPixels;
878
+
879
+ JSONObject root = data.getJSONObject(0);
880
+ if (root != null) {
881
+ double webWidth = (double) root.optInt("w");
882
+ double webHeight = (double) root.optInt("h");
883
+
884
+ double wRatio = (screenWidth / webWidth);
885
+ double hRatio = (screenHeight / webHeight);
886
+
887
+ // store rescaled x-y co-ordinates of touch
888
+ double x1 = root.optDouble("x1");
889
+ double y1 = root.optDouble("y1");
890
+ x1 = x1 * wRatio;
891
+ y1 = y1 * hRatio;
892
+ root.put("x1", x1);
893
+ root.put("y1", y1);
894
+
895
+ // store rescaled x-y co-ordinates of touch
896
+ double x2 = root.optDouble("x2");
897
+ double y2 = root.optDouble("y2");
898
+ x2 = x2 * wRatio;
899
+ y2 = y2 * hRatio;
900
+ root.put("x2", x2);
901
+ root.put("y2", y2);
902
+
903
+ // re-store rescaled x-y-w-h co-ordinates of input fld
904
+ JSONArray newObjects = new JSONArray();
905
+ JSONArray objects = root.optJSONArray("arr");
906
+ if (objects != null && objects.length() > 0) {
907
+ for (int i = 0; i < objects.length(); i++) {
908
+ JSONObject inputFld = objects.optJSONObject(i);
909
+ if (inputFld != null && inputFld.length() > 0) {
910
+ JSONObject obj = new JSONObject(inputFld.toString());
911
+ obj.put("x", inputFld.optDouble("x") * wRatio);
912
+ obj.put("y", (inputFld.optDouble("y") * hRatio) + inputFld.optDouble("h"));
913
+ obj.put("w", inputFld.optDouble("w") * wRatio);
914
+ obj.put("h", inputFld.optDouble("h") * hRatio);
915
+ newObjects.put(obj);
916
+ }
917
+ }
918
+ }
919
+ root.put("arr", newObjects);
920
+ }
921
+
922
+ View rootView = cordova.getActivity().getWindow().getDecorView().getRootView();
923
+ ContextSdk.trackSwipes(root, cordova.getActivity().getApplicationContext(), rootView, cordova.getActivity());
924
+
925
+ callbackContext.success();
926
+ } catch (Throwable e) {
927
+ callbackContext.error(e.getMessage());
928
+ }
929
+ }
930
+ }).start();
931
+ }
932
+
933
+ @CordovaMethod
934
+ private void trackScreens(JSONArray data, CallbackContext callbackContext) {
935
+ try {
936
+ new Thread(new Runnable() {
937
+ @Override
938
+ public void run() {
939
+ try {
940
+ DisplayMetrics dm = new DisplayMetrics();
941
+ cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
942
+ int screenWidth = dm.widthPixels;
943
+ int screenHeight = dm.heightPixels;
944
+
945
+ JSONObject root = data.getJSONObject(0);
946
+ if (root != null) {
947
+ double webWidth = (double) root.optInt("w");
948
+ double webHeight = (double) root.optInt("h");
949
+
950
+ double wRatio = (screenWidth / webWidth);
951
+ double hRatio = (screenHeight / webHeight);
952
+
953
+ // re-store rescaled x-y-w-h co-ordinates of input fld
954
+ JSONArray newObjects = new JSONArray();
955
+ JSONArray objects = root.optJSONArray("arr");
956
+ if (objects != null && objects.length() > 0) {
957
+ for (int i = 0; i < objects.length(); i++) {
958
+ JSONObject inputFld = objects.optJSONObject(i);
959
+ if (inputFld != null && inputFld.length() > 0) {
960
+ JSONObject obj = new JSONObject(inputFld.toString());
961
+ obj.put("x", inputFld.optDouble("x") * wRatio);
962
+ obj.put("y", (inputFld.optDouble("y") * hRatio) + inputFld.optDouble("h"));
963
+ obj.put("w", inputFld.optDouble("w") * wRatio);
964
+ obj.put("h", inputFld.optDouble("h") * hRatio);
965
+ newObjects.put(obj);
966
+ }
967
+ }
968
+ }
969
+ root.put("arr", newObjects);
970
+ }
971
+
972
+ View rootView = cordova.getActivity().getWindow().getDecorView().getRootView();
973
+ ContextSdk.trackScreens(root, cordova.getActivity().getApplicationContext(), rootView, cordova.getActivity());
974
+
975
+ callbackContext.success();
976
+ } catch (Throwable e) {
977
+ Log.e(TAG, "trackScreens run: error ", e );
978
+ }
979
+ }
980
+ }).start();
981
+ } catch (Throwable e) {
982
+ callbackContext.error(e.getMessage());
983
+ }
984
+ }
969
985
  }