@aigens/aigens-sdk-core 0.5.5 → 5.0.0

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.
@@ -13,6 +13,7 @@ import android.content.pm.PackageInfo;
13
13
  import android.content.pm.PackageManager;
14
14
  import android.net.Uri;
15
15
  import android.os.Build;
16
+ import android.os.Bundle;
16
17
  import android.os.Handler;
17
18
  import android.util.ArrayMap;
18
19
  import android.util.Log;
@@ -38,10 +39,13 @@ import com.getcapacitor.annotation.CapacitorPlugin;
38
39
  import com.getcapacitor.annotation.Permission;
39
40
  import com.getcapacitor.annotation.PermissionCallback;
40
41
 
42
+ import org.json.JSONArray;
41
43
  import org.json.JSONException;
44
+ import org.json.JSONObject;
42
45
 
43
46
  import java.io.Serializable;
44
47
  import java.lang.reflect.Field;
48
+ import java.lang.reflect.Method;
45
49
  import java.util.HashMap;
46
50
  import java.util.Iterator;
47
51
  import java.util.List;
@@ -52,16 +56,23 @@ import java.util.TimeZone;
52
56
  @CapacitorPlugin(
53
57
  name = "Core",
54
58
  permissions = {
55
- @Permission(strings = { Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR }, alias = CorePlugin.CALENDAR_PERMISSION),
59
+ @Permission(strings = {Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR}, alias = CorePlugin.CALENDAR_PERMISSION),
56
60
  }
57
61
  )
58
62
  public class CorePlugin extends Plugin {
59
63
 
60
64
  static final String CALENDAR_PERMISSION = "calendarPermission";
65
+
61
66
  public interface CoreListener {
62
67
  public boolean isInterceptedUrl(String url, WebView view, Activity activity);
63
68
  }
69
+
70
+ public interface AnalyticsListener {
71
+ public void logEvent(String name, Bundle parameters);
72
+ }
73
+
64
74
  static public CoreListener coreListener;
75
+ static public AnalyticsListener analyticsListener;
65
76
  public static final int REQUEST_CODE_OPEN_URL = 100001;
66
77
 
67
78
  public static boolean DEBUG = false;
@@ -69,6 +80,7 @@ public class CorePlugin extends Plugin {
69
80
  private static Map member;
70
81
  private static Map deeplink;
71
82
  private static boolean ENVIRONMENT_PRODUCTION = true;
83
+
72
84
  private void debug(Object msg) {
73
85
 
74
86
  if (DEBUG) {
@@ -243,6 +255,128 @@ public class CorePlugin extends Plugin {
243
255
 
244
256
  }
245
257
 
258
+ private static Method logEventMethod;
259
+ private static Object analyticsInstance;
260
+
261
+ public static void initFirebaseAnalytics(Context context) {
262
+ try {
263
+ if (logEventMethod != null && analyticsInstance != null) return;
264
+ Class<?> clazz = Class.forName("com.google.firebase.analytics.FirebaseAnalytics");
265
+ Method getInstance = clazz.getMethod("getInstance", Context.class);
266
+ logEventMethod = clazz.getMethod("logEvent", String.class, Bundle.class);
267
+ analyticsInstance = getInstance.invoke(null, context);
268
+ } catch (Exception e) {
269
+ Log.e("CorePlugin", "initFirebaseAnalytics Error", e);
270
+ }
271
+ }
272
+
273
+ public static void trackEvent(Context context, String eventName, Bundle params) {
274
+ CorePlugin.initFirebaseAnalytics(context);
275
+ if (logEventMethod == null || analyticsInstance == null) {
276
+ Log.i("CorePlugin", "FirebaseAnalytics trackEvent failure");
277
+ return;
278
+ }
279
+ try {
280
+ logEventMethod.invoke(analyticsInstance, eventName, params);
281
+ Log.i("CorePlugin", "trackEvent successful");
282
+ } catch (Exception e) {
283
+ Log.e("CorePlugin", "FirebaseAnalytics trackEvent Error", e);
284
+ }
285
+ }
286
+
287
+ private void callTracking(String name, Bundle bundle) {
288
+ if (CorePlugin.analyticsListener != null) {
289
+ CorePlugin.analyticsListener.logEvent(name, bundle);
290
+ return;
291
+ }
292
+
293
+ CorePlugin.trackEvent(getContext(), name, bundle);
294
+ }
295
+
296
+ private static Bundle convertJsonToBundle(JSONObject json) {
297
+ Bundle bundle = new Bundle();
298
+ if (json == null || json.length() == 0) return bundle;
299
+
300
+ Iterator<String> iterator = json.keys();
301
+ while (iterator.hasNext()) {
302
+ String key = (String) iterator.next();
303
+ try {
304
+ Object value = json.get(key);
305
+ if (value == null);
306
+ else if (value instanceof String) bundle.putString(key, (String) value);
307
+ else if (value instanceof Boolean) bundle.putBoolean(key, (Boolean) value);
308
+ else if (value instanceof Integer) bundle.putInt(key, (Integer) value);
309
+ else if (value instanceof Long) bundle.putLong(key, (Long) value);
310
+ else if (value instanceof Float) bundle.putFloat(key, (Float) value);
311
+ else if (value instanceof Double) bundle.putDouble(key, (Double) value);
312
+ else if (value instanceof JSONObject) bundle.putBundle(key, CorePlugin.convertJsonToBundle((JSONObject) value));
313
+ else if (value instanceof JSONArray) {
314
+ JSONArray array = (JSONArray) value;
315
+ Object first = array.length() == 0 ? null : (Object) array.get(0);
316
+ if (first == null);
317
+ else if (first instanceof JSONObject) {
318
+ Bundle[] items = new Bundle[array.length()];
319
+ for (int i = 0; i < array.length(); i++) items[i] = CorePlugin.convertJsonToBundle(array.getJSONObject(i));
320
+ bundle.putParcelableArray(key, items);
321
+ } else if (first instanceof String) {
322
+ String[] items = new String[array.length()];
323
+ for (int i = 0; i < array.length(); i++) items[i] = array.getString(i);
324
+ bundle.putStringArray(key, items);
325
+ } else if (first instanceof Integer || first instanceof Float || first instanceof Double) {
326
+ float[] items = new float[array.length()];
327
+ for (int i = 0; i < array.length(); i++) {
328
+ items[i] = ((Number) array.get(i)).floatValue();
329
+ }
330
+ bundle.putFloatArray(key, items);
331
+ }
332
+ }
333
+ } catch (ClassCastException | JSONException e) {
334
+ e.printStackTrace();
335
+ }
336
+ }
337
+
338
+ return bundle;
339
+ }
340
+
341
+ @PluginMethod
342
+ public void setScreenName(PluginCall call) {
343
+
344
+ try {
345
+ String screenName = call.getString("name", null);
346
+
347
+ if (screenName == null) {
348
+ call.resolve();
349
+ return;
350
+ }
351
+ bridge.getActivity().runOnUiThread(new Runnable() {
352
+ @Override
353
+ public void run() {
354
+ Bundle bundle = new Bundle();
355
+ bundle.putString("screen_name", screenName);
356
+ callTracking("screen_view", bundle);
357
+ }
358
+ });
359
+ call.resolve();
360
+ } catch (Exception ex) {
361
+ call.reject(ex.getLocalizedMessage());
362
+ }
363
+ }
364
+
365
+ @PluginMethod
366
+ public void logEvent(PluginCall call) {
367
+ String name = call.getString("name", null);
368
+
369
+ if (name == null) {
370
+ call.resolve();
371
+ return;
372
+ }
373
+
374
+ JSONObject params = call.getData().getJSObject("params");
375
+ Bundle bundle = params != null ? CorePlugin.convertJsonToBundle(params) : null;
376
+ callTracking(name, bundle);
377
+ call.resolve();
378
+ }
379
+
246
380
  private static PluginCall dismissCall;
247
381
  @PluginMethod
248
382
  public void getFinishData(PluginCall call) {
@@ -405,7 +539,7 @@ public class CorePlugin extends Plugin {
405
539
  try {
406
540
  if (areNotificationsEnabled) {
407
541
  object.put("display", "granted");
408
- }else {
542
+ } else {
409
543
  object.put("display", "denied");
410
544
  }
411
545
  } catch (Exception e) {
@@ -441,7 +575,7 @@ public class CorePlugin extends Plugin {
441
575
  endTime + TimeZone.getDefault().getOffset(endTime));
442
576
  }
443
577
  calIntent.putExtra("eventTimezone", "TIMEZONE_UTC");
444
- }else {
578
+ } else {
445
579
  if (beginTime != null) {
446
580
  calIntent.putExtra("beginTime", beginTime);
447
581
  }
@@ -472,6 +606,7 @@ public class CorePlugin extends Plugin {
472
606
  call.resolve(ret);
473
607
  // Do something with the result data
474
608
  }
609
+
475
610
  @PluginMethod()
476
611
  public void addCalendar(PluginCall call) {
477
612
  if (getPermissionState(CorePlugin.CALENDAR_PERMISSION) != PermissionState.GRANTED) {
@@ -504,7 +639,7 @@ public class CorePlugin extends Plugin {
504
639
  int rounded = (int) Math.round(value * 100);
505
640
  settings.setTextZoom(rounded);
506
641
  });
507
- }catch (Exception e) {
642
+ } catch (Exception e) {
508
643
  e.printStackTrace();
509
644
  }
510
645
 
@@ -512,6 +647,7 @@ public class CorePlugin extends Plugin {
512
647
  call.resolve();
513
648
 
514
649
  }
650
+
515
651
  public JSObject _readClipboard() {
516
652
  JSObject object = new JSObject();
517
653
  try {
@@ -20,6 +20,10 @@ CAP_PLUGIN(CorePlugin, "Core",
20
20
  CAP_PLUGIN_METHOD(readClipboard, CAPPluginReturnPromise);
21
21
  CAP_PLUGIN_METHOD(addCalendar, CAPPluginReturnPromise);
22
22
  CAP_PLUGIN_METHOD(makeHKFPSPayment, CAPPluginReturnPromise);
23
+ CAP_PLUGIN_METHOD(setScreenName, CAPPluginReturnPromise);
24
+ CAP_PLUGIN_METHOD(logEvent, CAPPluginReturnPromise);
25
+
26
+
23
27
 
24
28
 
25
29
  )
@@ -12,6 +12,10 @@ import EventKitUI
12
12
  func isInterceptedUrl(url: URL, webview: WKWebView) -> Bool
13
13
  }
14
14
 
15
+ @objc public protocol AnalyticsDelegate: AnyObject {
16
+ func logEvent(_ name: String, parameters: Dictionary<String, Any>?)
17
+ }
18
+
15
19
  var aigensDebug = false;
16
20
  /**
17
21
  * Please read the Capacitor iOS Plugin Development Guide
@@ -21,6 +25,7 @@ var aigensDebug = false;
21
25
  public class CorePlugin: CAPPlugin {
22
26
 
23
27
  static public var coreDelegate: CoreDelegate?
28
+ static public var analyticsDelegate: AnalyticsDelegate?
24
29
 
25
30
  public static let shared = CorePlugin()
26
31
  private let implementation = Core()
@@ -239,6 +244,80 @@ public class CorePlugin: CAPPlugin {
239
244
 
240
245
  }
241
246
 
247
+ @objc func setScreenName(_ call: CAPPluginCall) {
248
+ if let screenName = call.getString("name") {
249
+ DispatchQueue.main.async {
250
+ self.trackEvent("screen_view", parameters: ["screen_name": screenName])
251
+ }
252
+ call.resolve()
253
+ } else {
254
+ call.resolve()
255
+ }
256
+
257
+ }
258
+
259
+ private func _logEvent(name: String, params: [String: Any]) -> Bool {
260
+
261
+ guard let analyticsClass = NSClassFromString("FirebaseAnalytics.FIRAnalytics") ?? NSClassFromString("FirebaseAnalytics.Analytics") ?? NSClassFromString("FIRAnalytics") ?? NSClassFromString("Analytics") else {
262
+ return false
263
+ }
264
+
265
+ let selector = NSSelectorFromString("logEventWithName:parameters:")
266
+
267
+ guard analyticsClass.responds(to: selector) else { return false }
268
+
269
+ let arguments: [Any] = [name, params]
270
+
271
+ _ = analyticsClass.perform(selector, with: arguments, afterDelay: 0)
272
+ print("CorePlugin logEvent")
273
+ return true
274
+ }
275
+
276
+ private func trackEvent(_ name: String, parameters: Dictionary<String, Any>?) {
277
+ if let del = CorePlugin.analyticsDelegate {
278
+ del.logEvent(name, parameters: parameters)
279
+ return;
280
+ }
281
+ _ = _logEvent(name: name, params: parameters ?? [:])
282
+ }
283
+
284
+ @objc func logEvent(_ call: CAPPluginCall) {
285
+ guard let name = call.getString("name"), !name.isEmpty else {
286
+ call.reject("Event name is required and can't be empty")
287
+ return
288
+ }
289
+
290
+ /// logEvent() expects `nil` when there are no parameters
291
+ guard var params = call.getObject("params"), !params.isEmpty else {
292
+ trackEvent(name, parameters: nil)
293
+ call.resolve()
294
+ return
295
+ }
296
+
297
+ /// FirebaseAnalytics silently converts any item quantity that is not an
298
+ /// integer to zero, this includes any NSNumber or string value passed
299
+ /// as an option to CAPPluginCall.
300
+ if var items = params["items"] as? NSArray as? [[String:Any]] {
301
+ for (idx, item) in items.enumerated() {
302
+ if let quantity = item["quantity"] {
303
+ guard let intVal = quantity as? Int else {
304
+ call.resolve()
305
+ return
306
+ }
307
+ items[idx]["quantity"] = intVal
308
+ }
309
+ }
310
+ params["items"] = items
311
+ }
312
+
313
+ if let extendSession = params["extendSession"] as? NSNumber, extendSession == 1 {
314
+ params["extendSession"] = true
315
+ }
316
+ trackEvent(name, parameters: params)
317
+ call.resolve()
318
+
319
+ }
320
+
242
321
  @objc func getMember(_ call: CAPPluginCall) {
243
322
 
244
323
  call.resolve([
@@ -592,5 +671,3 @@ extension CorePlugin: EKEventEditViewDelegate {
592
671
  }
593
672
  }
594
673
  #endif
595
-
596
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigens/aigens-sdk-core",
3
- "version": "0.5.5",
3
+ "version": "5.0.0",
4
4
  "description": "Aigens Order.Place Core Plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",