capacitor-gleap-plugin 7.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.
@@ -0,0 +1,535 @@
1
+ package com.congrapp.b2c.plugins.gleap;
2
+
3
+ import android.Manifest;
4
+ import android.os.Build;
5
+
6
+ import com.getcapacitor.JSObject;
7
+ import com.getcapacitor.Plugin;
8
+ import com.getcapacitor.PluginCall;
9
+ import com.getcapacitor.PluginMethod;
10
+ import com.getcapacitor.annotation.CapacitorPlugin;
11
+ import com.getcapacitor.annotation.Permission;
12
+
13
+ import java.io.File;
14
+ import java.io.FileOutputStream;
15
+ import java.io.OutputStream;
16
+ import java.util.Base64;
17
+ import java.util.regex.Matcher;
18
+ import java.util.regex.Pattern;
19
+
20
+ import org.json.JSONObject;
21
+
22
+ import io.gleap.ConfigLoadedCallback;
23
+ import io.gleap.CustomActionCallback;
24
+ import io.gleap.FeedbackFlowStartedCallback;
25
+ import io.gleap.FeedbackSendingFailedCallback;
26
+ import io.gleap.FeedbackSentCallback;
27
+ import io.gleap.Gleap;
28
+ import io.gleap.GleapLogLevel;
29
+ import io.gleap.GleapNotInitialisedException;
30
+ import io.gleap.GleapUserProperties;
31
+ import io.gleap.WidgetClosedCallback;
32
+ import io.gleap.WidgetOpenedCallback;
33
+
34
+ @CapacitorPlugin(name = "Gleap", permissions = {
35
+ @Permission(strings = { Manifest.permission.ACCESS_NETWORK_STATE }, alias = "network"),
36
+ @Permission(strings = { Manifest.permission.INTERNET }, alias = "internet"),
37
+ @Permission(strings = { Manifest.permission.WAKE_LOCK }, alias = "wakelock")
38
+ })
39
+ public class GleapPlugin extends Plugin {
40
+ private Gleap implementation;
41
+
42
+ @Override
43
+ public void load() {
44
+ implementation = Gleap.getInstance();
45
+ }
46
+
47
+ @PluginMethod()
48
+ public void initialize(PluginCall call) {
49
+ String api_key = call.getString("API_KEY");
50
+
51
+ // If API_KEY is empty, then pass back error
52
+ if (!call.getData().has("API_KEY")) {
53
+ call.reject("Must provide an API Key");
54
+ return;
55
+ }
56
+
57
+ // Initialize Gleap with API Key
58
+ Gleap.initialize(api_key, getActivity().getApplication());
59
+
60
+ // Build Json object and resolve success
61
+ JSObject ret = new JSObject();
62
+ ret.put("initialized", true);
63
+ call.resolve(ret);
64
+ }
65
+
66
+ @PluginMethod()
67
+ public void identify(PluginCall call) {
68
+ // If userId is empty, then pass back error
69
+ if (!call.getData().has("userId")) {
70
+ call.reject("You must provide an user ID");
71
+ return;
72
+ }
73
+
74
+ GleapUserProperties userProperties = new GleapUserProperties();
75
+ if (call.getData().has("email")) {
76
+ userProperties.setEmail(call.getString("email"));
77
+ }
78
+ if (call.getData().has("name")) {
79
+ userProperties.setName(call.getString("name"));
80
+ }
81
+ if (call.getData().has("phone")) {
82
+ userProperties.setPhoneNumber(call.getString("phone"));
83
+ }
84
+ if (call.getData().has("value")) {
85
+ userProperties.setValue(call.getDouble("value"));
86
+ }
87
+ if (call.getData().has("userHash")) {
88
+ userProperties.setHash(call.getString("userHash"));
89
+ }
90
+
91
+ String userId = call.getString("userId");
92
+ implementation.identifyUser(userId, userProperties);
93
+
94
+ // Build Json object and resolve success
95
+ JSObject ret = new JSObject();
96
+ ret.put("identify", true);
97
+ call.resolve(ret);
98
+ }
99
+
100
+ @PluginMethod()
101
+ public void clearIdentity(PluginCall call) {
102
+
103
+ // Clear User Identity in Gleap
104
+ implementation.clearIdentity();
105
+
106
+ // Build Json object and resolve success
107
+ JSObject ret = new JSObject();
108
+ ret.put("clearIdentity", true);
109
+ call.resolve(ret);
110
+ }
111
+
112
+ @PluginMethod()
113
+ public void setCustomData(PluginCall call) {
114
+ // If key is empty, then pass back error
115
+ if (!call.getData().has("key")) {
116
+ call.reject("Must provide a data key");
117
+ return;
118
+ }
119
+
120
+ // If value is empty, then pass back error
121
+ if (!call.getData().has("value")) {
122
+ call.reject("Must provide a data value");
123
+ return;
124
+ }
125
+
126
+ // Set custom data
127
+ String key = call.getString("key");
128
+ String value = call.getString("value");
129
+ implementation.setCustomData(key, value);
130
+
131
+ // Build Json object and resolve success
132
+ JSObject ret = new JSObject();
133
+ ret.put("setCustomData", true);
134
+ call.resolve(ret);
135
+ }
136
+
137
+ @PluginMethod()
138
+ public void attachCustomData(PluginCall call) {
139
+ // If key is empty, then pass back error
140
+ if (!call.getData().has("data")) {
141
+ call.reject("No data is provided");
142
+ return;
143
+ }
144
+
145
+ // Append custom data
146
+ implementation.attachCustomData(call.getObject("data"));
147
+
148
+ // Build Json object and resolve success
149
+ JSObject ret = new JSObject();
150
+ ret.put("attachedCustomData", true);
151
+ call.resolve(ret);
152
+ }
153
+
154
+ @PluginMethod()
155
+ public void removeCustomData(PluginCall call) {
156
+ // If key is empty, then pass back error
157
+ if (!call.getData().has("key")) {
158
+ call.reject("Must provide a data key");
159
+ return;
160
+ }
161
+
162
+ String key = call.getString("key");
163
+ implementation.removeCustomDataForKey(key);
164
+
165
+ // Build Json object and resolve success
166
+ JSObject ret = new JSObject();
167
+ ret.put("removedCustomData", true);
168
+ call.resolve(ret);
169
+ }
170
+
171
+ @PluginMethod()
172
+ public void clearCustomData(PluginCall call) {
173
+ implementation.clearCustomData();
174
+
175
+ // Build Json object and resolve success
176
+ JSObject ret = new JSObject();
177
+ ret.put("clearedCustomData", true);
178
+ call.resolve(ret);
179
+ }
180
+
181
+ @PluginMethod()
182
+ public void preFillForm(PluginCall call) {
183
+ // If key is empty, then pass back error
184
+ if (!call.getData().has("data")) {
185
+ call.reject("No data is provided");
186
+ return;
187
+ }
188
+
189
+ // Prefill form
190
+ implementation.preFillForm(call.getObject("data"));
191
+
192
+ // Build Json object and resolve success
193
+ JSObject ret = new JSObject();
194
+ ret.put("preFilledForm", true);
195
+ call.resolve(ret);
196
+ }
197
+
198
+ @PluginMethod()
199
+ public void log(PluginCall call) {
200
+ // If logEventSubject is empty, then pass back error
201
+ if (!call.getData().has("message")) {
202
+ call.reject("No log message provided");
203
+ return;
204
+ }
205
+
206
+ String message = call.getString("message");
207
+ String logLevel = call.getString("logLevel", "INFO");
208
+ GleapLogLevel logLevelObj = GleapLogLevel.INFO;
209
+ if (logLevel == "ERROR") {
210
+ logLevelObj = GleapLogLevel.ERROR;
211
+ } else if (logLevel == "WARNING") {
212
+ logLevelObj = GleapLogLevel.WARNING;
213
+ }
214
+
215
+ // Forward log message to native implementation
216
+ implementation.log(message, logLevelObj);
217
+
218
+ // Build Json object and resolve success
219
+ JSObject ret = new JSObject();
220
+ ret.put("logged", true);
221
+ call.resolve(ret);
222
+ }
223
+
224
+ @PluginMethod()
225
+ public void logEvent(PluginCall call) {
226
+ // If logEventSubject is empty, then pass back error
227
+ if (!call.getData().has("name")) {
228
+ call.reject("No event name provided");
229
+ return;
230
+ }
231
+
232
+ String name = call.getString("name");
233
+ // If logEventData is empty call for function without data
234
+ if (!call.getData().has("data")) {
235
+ implementation.logEvent(name);
236
+ } else {
237
+ JSObject eventData = call.getObject("data");
238
+ implementation.logEvent(name, eventData);
239
+ }
240
+
241
+ // Build Json object and resolve success
242
+ JSObject ret = new JSObject();
243
+ ret.put("loggedEvent", true);
244
+ call.resolve(ret);
245
+ }
246
+
247
+ @PluginMethod()
248
+ public void addAttachment(PluginCall call) {
249
+ if (!call.getData().has("base64data")) {
250
+ call.reject("No base64 file data provided");
251
+ return;
252
+ }
253
+
254
+ if (!call.getData().has("name")) {
255
+ call.reject("No file name provided");
256
+ return;
257
+ }
258
+
259
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
260
+ try {
261
+ String fileName = call.getString("name");
262
+ String base64file = call.getString("base64data");
263
+
264
+ if (checkAllowedEndings(fileName)) {
265
+ String[] splittedBase64File = base64file.split(",");
266
+ byte[] data;
267
+ if (splittedBase64File.length == 2) {
268
+ data = Base64.getDecoder().decode(splittedBase64File[1]);
269
+ } else {
270
+ data = Base64.getDecoder().decode(splittedBase64File[0]);
271
+ }
272
+
273
+ String mimetype = extractMimeType(base64file);
274
+ String[] splitted = mimetype.split("/");
275
+ String fileNameConcated = fileName;
276
+ if (splitted.length == 2 && !fileName.contains(".")) {
277
+ fileNameConcated += "." + splitted[1];
278
+ }
279
+
280
+ File file = new File(getActivity().getApplication().getCacheDir() + "/" + fileNameConcated);
281
+ if (!file.exists()) {
282
+ file.createNewFile();
283
+ }
284
+ try (OutputStream stream = new FileOutputStream(file)) {
285
+ stream.write(data);
286
+ } catch (Exception e) {
287
+ e.printStackTrace();
288
+ }
289
+
290
+ if (file.exists()) {
291
+ implementation.addAttachment(file);
292
+ } else {
293
+ System.err.println("Gleap: The file does not exist.");
294
+ }
295
+ }
296
+ } catch (Exception e) {
297
+ e.printStackTrace();
298
+ }
299
+ }
300
+
301
+ // Build Json object and resolve success
302
+ JSObject ret = new JSObject();
303
+ ret.put("attachmentAdded", true);
304
+ call.resolve(ret);
305
+ }
306
+
307
+ @PluginMethod()
308
+ public void removeAllAttachments(PluginCall call) {
309
+ implementation.removeAllAttachments();
310
+
311
+ // Build Json object and resolve success
312
+ JSObject ret = new JSObject();
313
+ ret.put("allAttachmentsRemoved", true);
314
+ call.resolve(ret);
315
+ }
316
+
317
+ @PluginMethod()
318
+ public void disableConsoleLogOverwrite(PluginCall call) {
319
+ // Build Json object and resolve success
320
+ JSObject ret = new JSObject();
321
+ ret.put("consoleLogDisabled", true);
322
+ call.resolve(ret);
323
+ }
324
+
325
+ @PluginMethod()
326
+ public void enableDebugConsoleLog(PluginCall call) {
327
+ // Build Json object and resolve success
328
+ JSObject ret = new JSObject();
329
+ ret.put("debugConsoleLogEnabled", true);
330
+ call.resolve(ret);
331
+ }
332
+
333
+ @PluginMethod()
334
+ public void sendSilentCrashReport(PluginCall call) {
335
+ if (!call.getData().has("description")) {
336
+ call.reject("No description provided");
337
+ return;
338
+ }
339
+
340
+ String description = call.getString("description");
341
+ String severity = call.getString("severity");
342
+
343
+ Gleap.SEVERITY severityObj = Gleap.SEVERITY.HIGH;
344
+ if (severity == "high") {
345
+ severityObj = Gleap.SEVERITY.HIGH;
346
+ } else if (severity == "medium") {
347
+ severityObj = Gleap.SEVERITY.MEDIUM;
348
+ } else {
349
+ severityObj = Gleap.SEVERITY.LOW;
350
+ }
351
+
352
+ JSONObject dataExclusion = call.getObject("dataExclusion");
353
+ if (dataExclusion != null) {
354
+ implementation.sendSilentCrashReport(description, severityObj, dataExclusion);
355
+ } else {
356
+ implementation.sendSilentCrashReport(description, severityObj);
357
+ }
358
+
359
+ // Build Json object and resolve success
360
+ JSObject ret = new JSObject();
361
+ ret.put("sentSilentBugReport", true);
362
+ call.resolve(ret);
363
+ }
364
+
365
+ @PluginMethod()
366
+ public void isOpened(PluginCall call) throws GleapNotInitialisedException {
367
+ // Build Json object and resolve success
368
+ JSObject ret = new JSObject();
369
+ ret.put("isOpened", implementation.isOpened());
370
+ call.resolve(ret);
371
+ }
372
+
373
+ @PluginMethod()
374
+ public void open(PluginCall call) throws GleapNotInitialisedException {
375
+ // Open widget
376
+ implementation.open();
377
+
378
+ // Build Json object and resolve success
379
+ JSObject ret = new JSObject();
380
+ ret.put("openedWidget", true);
381
+ call.resolve(ret);
382
+ }
383
+
384
+ @PluginMethod()
385
+ public void close(PluginCall call) throws GleapNotInitialisedException {
386
+ // Open widget
387
+ implementation.close();
388
+
389
+ // Build Json object and resolve success
390
+ JSObject ret = new JSObject();
391
+ ret.put("closedWidget", true);
392
+ call.resolve(ret);
393
+ }
394
+
395
+ @PluginMethod()
396
+ public void startFeedbackFlow(PluginCall call) throws GleapNotInitialisedException {
397
+ if (!call.getData().has("feedbackFlow")) {
398
+ call.reject("No feedback flow provided");
399
+ return;
400
+ }
401
+
402
+ // Start feedback flow
403
+ String feedbackFlow = call.getString("feedbackFlow");
404
+ implementation.startFeedbackFlow(feedbackFlow);
405
+
406
+ // Build Json object and resolve success
407
+ JSObject ret = new JSObject();
408
+ ret.put("startedFeedbackFlow", true);
409
+ call.resolve(ret);
410
+ }
411
+
412
+ @PluginMethod()
413
+ public void setLanguage(PluginCall call) {
414
+ // If languageCode is empty, then pass back error
415
+ if (!call.getData().has("languageCode")) {
416
+ call.reject("No language provided");
417
+ return;
418
+ }
419
+
420
+ // Pass language
421
+ String languageCode = call.getString("languageCode");
422
+ implementation.setLanguage(languageCode);
423
+
424
+ // Build Json object and resolve success
425
+ JSObject ret = new JSObject();
426
+ ret.put("setLanguage", languageCode);
427
+ call.resolve(ret);
428
+ }
429
+
430
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
431
+ public void setEventCallback(PluginCall call) {
432
+ call.setKeepAlive(true);
433
+
434
+ implementation.setWidgetOpenedCallback(new WidgetOpenedCallback() {
435
+ @Override
436
+ public void invoke() {
437
+ JSObject data = new JSObject();
438
+ data.put("name", "widget-opened");
439
+ call.resolve(data);
440
+ }
441
+ });
442
+
443
+ implementation.setWidgetClosedCallback(new WidgetClosedCallback() {
444
+ @Override
445
+ public void invoke() {
446
+ JSObject data = new JSObject();
447
+ data.put("name", "widget-closed");
448
+ call.resolve(data);
449
+ }
450
+ });
451
+
452
+ implementation.setConfigLoadedCallback(new ConfigLoadedCallback() {
453
+ @Override
454
+ public void configLoaded(JSONObject jsonObject) {
455
+ JSObject data = new JSObject();
456
+ data.put("name", "widget-opened");
457
+ data.put("data", jsonObject);
458
+ call.resolve(data);
459
+ }
460
+ });
461
+
462
+ implementation.setFeedbackSentCallback(new FeedbackSentCallback() {
463
+ @Override
464
+ public void invoke(String message) {
465
+ JSObject data = new JSObject();
466
+ data.put("name", "feedback-sent");
467
+ data.put("data", message);
468
+ call.resolve(data);
469
+ }
470
+ });
471
+
472
+ implementation.setFeedbackSendingFailedCallback(new FeedbackSendingFailedCallback() {
473
+ @Override
474
+ public void invoke(String message) {
475
+ // called when the sending of the feedback failed
476
+ JSObject data = new JSObject();
477
+ data.put("name", "error-while-sending");
478
+ data.put("data", message);
479
+ call.resolve(data);
480
+ }
481
+ });
482
+
483
+ implementation.registerCustomAction(new CustomActionCallback() {
484
+ @Override
485
+ public void invoke(String message) {
486
+ // called when a custom action from the widget is issued
487
+ JSObject data = new JSObject();
488
+ data.put("name", "custom-action-called");
489
+ data.put("data", message);
490
+ call.resolve(data);
491
+ }
492
+ });
493
+
494
+ implementation.setFeedbackFlowStartedCallback(new FeedbackFlowStartedCallback() {
495
+ @Override
496
+ public void invoke(String message) {
497
+ // called when the feedback flow ist started, not only the widget is opened
498
+ JSObject data = new JSObject();
499
+ data.put("name", "flow-started");
500
+ data.put("data", message);
501
+ call.resolve(data);
502
+ }
503
+ });
504
+ }
505
+
506
+ /**
507
+ * Extract the MIME type from a base64 string
508
+ *
509
+ * @param encoded Base64 string
510
+ * @return MIME type string
511
+ */
512
+ private String extractMimeType(final String encoded) {
513
+ final Pattern mime = Pattern.compile("^data:([a-zA-Z0-9]+/[a-zA-Z0-9]+).*,.*");
514
+ final Matcher matcher = mime.matcher(encoded);
515
+ if (!matcher.find())
516
+ return "";
517
+ return matcher.group(1).toLowerCase();
518
+ }
519
+
520
+ private boolean checkAllowedEndings(String fileName) {
521
+ String[] fileType = fileName.split("\\.");
522
+ String[] allowedTypes = {"jpeg", "svg", "png", "mp4", "webp", "xml", "plain", "xml", "json"};
523
+ if (fileType.length <= 1) {
524
+ return false;
525
+ }
526
+ boolean found = false;
527
+ for (String type : allowedTypes) {
528
+ if (type.equals(fileType[1])) {
529
+ found = true;
530
+ }
531
+ }
532
+
533
+ return found;
534
+ }
535
+ }
File without changes