cordova-plugin-appice 2.0.4 → 2.0.6

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.
@@ -0,0 +1,969 @@
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
+
969
+ }