@aigens/aigens-sdk-core 0.0.17 → 0.0.18
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.
@@ -2,6 +2,7 @@ package com.aigens.sdk;
|
|
2
2
|
|
3
3
|
import android.app.Activity;
|
4
4
|
import android.content.Intent;
|
5
|
+
import android.content.pm.ApplicationInfo;
|
5
6
|
import android.content.res.AssetManager;
|
6
7
|
import android.graphics.Bitmap;
|
7
8
|
import android.graphics.Color;
|
@@ -13,11 +14,10 @@ import android.util.Log;
|
|
13
14
|
import android.view.View;
|
14
15
|
import android.webkit.ServiceWorkerClient;
|
15
16
|
import android.webkit.ServiceWorkerController;
|
16
|
-
import android.webkit.
|
17
|
+
import android.webkit.ValueCallback;
|
17
18
|
import android.webkit.WebResourceRequest;
|
18
19
|
import android.webkit.WebResourceResponse;
|
19
20
|
import android.webkit.WebView;
|
20
|
-
import android.webkit.WebViewClient;
|
21
21
|
import android.widget.Button;
|
22
22
|
|
23
23
|
import com.aigens.sdk.plugins.CorePlugin;
|
@@ -25,9 +25,14 @@ import com.getcapacitor.Bridge;
|
|
25
25
|
import com.getcapacitor.BridgeActivity;
|
26
26
|
import com.getcapacitor.BridgeWebViewClient;
|
27
27
|
import com.getcapacitor.CapConfig;
|
28
|
+
import com.getcapacitor.JSExport;
|
29
|
+
import com.getcapacitor.Logger;
|
30
|
+
import com.getcapacitor.NativePlugin;
|
28
31
|
import com.getcapacitor.Plugin;
|
32
|
+
import com.getcapacitor.PluginHandle;
|
29
33
|
import com.getcapacitor.PluginLoadException;
|
30
34
|
import com.getcapacitor.PluginManager;
|
35
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
31
36
|
|
32
37
|
import org.json.JSONException;
|
33
38
|
import org.json.JSONObject;
|
@@ -37,6 +42,8 @@ import java.io.IOException;
|
|
37
42
|
import java.io.InputStream;
|
38
43
|
import java.io.InputStreamReader;
|
39
44
|
import java.util.ArrayList;
|
45
|
+
import java.util.Collection;
|
46
|
+
import java.util.HashMap;
|
40
47
|
import java.util.List;
|
41
48
|
import java.util.Map;
|
42
49
|
|
@@ -50,6 +57,7 @@ public class WebContainerActivity extends BridgeActivity {
|
|
50
57
|
private Map member;
|
51
58
|
private Map deeplink;
|
52
59
|
private boolean ENVIRONMENT_PRODUCTION = true;
|
60
|
+
private List<Class<? extends Plugin>> allPlugins;
|
53
61
|
private String[] shouldRedirectList = new String[]{
|
54
62
|
"/scan?",
|
55
63
|
"/qr?",
|
@@ -187,7 +195,7 @@ public class WebContainerActivity extends BridgeActivity {
|
|
187
195
|
|
188
196
|
CapConfig cc = new CapConfig(null, config);
|
189
197
|
|
190
|
-
|
198
|
+
allPlugins = new ArrayList<>();
|
191
199
|
|
192
200
|
List<Class<? extends Plugin>> auto = loadPlugins();
|
193
201
|
|
@@ -198,7 +206,7 @@ public class WebContainerActivity extends BridgeActivity {
|
|
198
206
|
if (clsName.endsWith("SplashScreenPlugin")) {
|
199
207
|
//don't add the splash screen plugin so it doesn't show the splash screen again
|
200
208
|
} else {
|
201
|
-
|
209
|
+
allPlugins.add(c);
|
202
210
|
}
|
203
211
|
|
204
212
|
}
|
@@ -210,12 +218,12 @@ public class WebContainerActivity extends BridgeActivity {
|
|
210
218
|
"com.aigens.alipay.AliPayPlugin",
|
211
219
|
"com.aigens.alipayhk.AliPayhkPlugin",
|
212
220
|
"com.aigens.payme.PaymePlugin",
|
213
|
-
},
|
214
|
-
addExtraPlugins(intent.getStringArrayExtra("extraClasspaths"),
|
221
|
+
}, allPlugins);
|
222
|
+
addExtraPlugins(intent.getStringArrayExtra("extraClasspaths"), allPlugins);
|
215
223
|
|
216
|
-
debug("App plugins:" +
|
224
|
+
debug("App plugins:" + allPlugins);
|
217
225
|
|
218
|
-
super.init(currentInstanceState,
|
226
|
+
super.init(currentInstanceState, allPlugins, cc);
|
219
227
|
|
220
228
|
//still have time to override webview client to avoid any intercept
|
221
229
|
|
@@ -225,16 +233,19 @@ public class WebContainerActivity extends BridgeActivity {
|
|
225
233
|
this.bridge.getWebView().setWebViewClient(wvc);
|
226
234
|
this.bridge.getWebView().setWebContentsDebuggingEnabled(true);
|
227
235
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
236
|
+
|
237
|
+
// this.bridge.getWebView().getSettings().setAppCacheEnabled(false);
|
238
|
+
// this.bridge.getWebView().clearCache(true);
|
239
|
+
// this.bridge.getWebView().clearHistory();
|
240
|
+
// this.bridge.getWebView().clearFormData();
|
241
|
+
// this.bridge.getWebView().getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
|
233
242
|
|
234
243
|
initView(intent);
|
235
244
|
initLayout(false, true, false);
|
236
245
|
|
237
|
-
|
246
|
+
// sometime not callback , use Use manual injection js
|
247
|
+
// disableServiceWorker(this.url, this.bridge);
|
248
|
+
|
238
249
|
}
|
239
250
|
|
240
251
|
private void addExtraPlugins(String[] extraClasspaths, List<Class<? extends Plugin>> plugins) {
|
@@ -346,23 +357,92 @@ public class WebContainerActivity extends BridgeActivity {
|
|
346
357
|
return plugins;
|
347
358
|
}
|
348
359
|
|
360
|
+
private boolean isDevMode() {
|
361
|
+
return (this.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
|
362
|
+
}
|
363
|
+
private void mapPlugin(List<Class<? extends Plugin>> allPlugins, Map<String, PluginHandle> plugins) {
|
364
|
+
for (Class<? extends Plugin> pluginClass : allPlugins) {
|
365
|
+
String pluginName;
|
366
|
+
CapacitorPlugin pluginAnnotation = pluginClass.getAnnotation(CapacitorPlugin.class);
|
367
|
+
if (pluginAnnotation == null) {
|
368
|
+
NativePlugin legacyPluginAnnotation = pluginClass.getAnnotation(NativePlugin.class);
|
369
|
+
if (legacyPluginAnnotation == null) {
|
370
|
+
Logger.error("Plugin doesn't have the @CapacitorPlugin annotation. Please add it");
|
371
|
+
continue;
|
372
|
+
}
|
373
|
+
pluginName = legacyPluginAnnotation.name();
|
374
|
+
} else {
|
375
|
+
pluginName = pluginAnnotation.name();
|
376
|
+
}
|
377
|
+
|
378
|
+
String pluginId = pluginClass.getSimpleName();
|
379
|
+
|
380
|
+
// Use the supplied name as the id if available
|
381
|
+
if (!pluginName.equals("")) {
|
382
|
+
pluginId = pluginName;
|
383
|
+
}
|
384
|
+
try {
|
385
|
+
PluginHandle handle = this.bridge.getPlugin(pluginId);
|
386
|
+
if (handle != null) {
|
387
|
+
plugins.put(pluginId, handle);
|
388
|
+
}
|
389
|
+
} catch (Exception ex) {
|
390
|
+
Logger.error(
|
391
|
+
"NativePlugin " +
|
392
|
+
pluginClass.getName() +
|
393
|
+
" is invalid. Ensure the @CapacitorPlugin annotation exists on the plugin class and" +
|
394
|
+
" the class extends Plugin"
|
395
|
+
);
|
396
|
+
}
|
397
|
+
}
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
private String getJSInjectorString() {
|
402
|
+
try {
|
403
|
+
String globalJS = JSExport.getGlobalJS(this, config.isLoggingEnabled(), isDevMode());
|
404
|
+
String bridgeJS = JSExport.getBridgeJS(this);
|
405
|
+
Map<String, PluginHandle> plugins = new HashMap<>();
|
406
|
+
mapPlugin(allPlugins, plugins);
|
407
|
+
String pluginJS = JSExport.getPluginJS(plugins.values());
|
408
|
+
String cordovaJS = JSExport.getCordovaJS(this);
|
409
|
+
String cordovaPluginsJS = JSExport.getCordovaPluginJS(this);
|
410
|
+
String cordovaPluginsFileJS = JSExport.getCordovaPluginsFileJS(this);
|
411
|
+
String localUrlJS = "window.WEBVIEW_SERVER_URL = '" + this.url + "';";
|
412
|
+
return (
|
413
|
+
globalJS +
|
414
|
+
"\n\n" +
|
415
|
+
localUrlJS +
|
416
|
+
"\n\n" +
|
417
|
+
bridgeJS +
|
418
|
+
"\n\n" +
|
419
|
+
pluginJS +
|
420
|
+
"\n\n" +
|
421
|
+
cordovaJS +
|
422
|
+
"\n\n" +
|
423
|
+
cordovaPluginsFileJS +
|
424
|
+
"\n\n" +
|
425
|
+
cordovaPluginsJS
|
426
|
+
);
|
427
|
+
} catch (Exception ex) {
|
428
|
+
Logger.error("Unable to export Capacitor JS. App will not function!", ex);
|
429
|
+
}
|
430
|
+
return "";
|
431
|
+
}
|
432
|
+
|
349
433
|
private static boolean SW_DISABLED = false;
|
350
434
|
|
351
435
|
private static void disableServiceWorker(String originUrl, Bridge bridge) {
|
352
436
|
|
353
437
|
if (SW_DISABLED) return;
|
354
|
-
|
355
438
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
356
439
|
ServiceWorkerController.getInstance().setServiceWorkerClient(new ServiceWorkerClient() {
|
357
440
|
@Override
|
358
441
|
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
// return bridge.getLocalServer().shouldInterceptRequest(request);
|
364
|
-
// }
|
365
|
-
// return null;
|
442
|
+
// don't know why this callback suddenly stopped working
|
443
|
+
if (bridge == null) {
|
444
|
+
return null;
|
445
|
+
}
|
366
446
|
return bridge.getLocalServer().shouldInterceptRequest(request);
|
367
447
|
}
|
368
448
|
});
|
@@ -504,8 +584,9 @@ public class WebContainerActivity extends BridgeActivity {
|
|
504
584
|
String url = request.getUrl().toString();
|
505
585
|
//debug("shouldInterceptRequest", url);
|
506
586
|
|
507
|
-
Log.wtf("shouldInterceptRequestUrl:", url);
|
587
|
+
// Log.wtf("shouldInterceptRequestUrl:", url);
|
508
588
|
|
589
|
+
// call super.shouldInterceptRequest(view, request); at: disableServiceWorker method
|
509
590
|
|
510
591
|
//support redirect from /scan
|
511
592
|
// for (String r: shouldRedirectList) {
|
@@ -513,8 +594,6 @@ public class WebContainerActivity extends BridgeActivity {
|
|
513
594
|
// return null;
|
514
595
|
// }
|
515
596
|
// }
|
516
|
-
|
517
|
-
// call super.shouldInterceptRequest(view, request); at: disableServiceWorker method
|
518
597
|
return null;
|
519
598
|
|
520
599
|
// if(url.indexOf("/scan?") > 0 || url.indexOf("/qr?") > 0 || url.indexOf("//scan") > 0){
|
@@ -545,6 +624,26 @@ public class WebContainerActivity extends BridgeActivity {
|
|
545
624
|
return shouldOverrideUrl(view, url);
|
546
625
|
}
|
547
626
|
|
627
|
+
private void injectJs(String content) {
|
628
|
+
String str = "javascript:(function() { "
|
629
|
+
+ " " //turns to red the background color
|
630
|
+
+ " var script=document.createElement('script'); "
|
631
|
+
+ " script.setAttribute('type','text/javascript'); "
|
632
|
+
+ " script.setAttribute('id', 'test'); "
|
633
|
+
+ " script.innerText='"
|
634
|
+
+ content +
|
635
|
+
"'; "
|
636
|
+
+ " document.getElementsByTagName('head')[0].appendChild(script); "
|
637
|
+
+ "})()";
|
638
|
+
// Log.wtf("injector", content);
|
639
|
+
bridge.getWebView().evaluateJavascript(content, new ValueCallback<String>() {
|
640
|
+
@Override
|
641
|
+
public void onReceiveValue(String value) {
|
642
|
+
// Log.i("onReceiveValue", value);
|
643
|
+
}
|
644
|
+
});
|
645
|
+
// bridge.getWebView().loadUrl(str);
|
646
|
+
}
|
548
647
|
@Override
|
549
648
|
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
550
649
|
|
@@ -554,6 +653,8 @@ public class WebContainerActivity extends BridgeActivity {
|
|
554
653
|
|
555
654
|
//bridge.getWebView().setVisibility(View.VISIBLE);
|
556
655
|
initLayout(true, false, false);
|
656
|
+
|
657
|
+
injectJs(getJSInjectorString());
|
557
658
|
}
|
558
659
|
|
559
660
|
@Override
|