@capacitor-plus/android 8.0.0-beta.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.
- package/LICENSE +21 -0
- package/capacitor/build.gradle +94 -0
- package/capacitor/lint-baseline.xml +136 -0
- package/capacitor/lint.xml +9 -0
- package/capacitor/proguard-rules.pro +28 -0
- package/capacitor/src/main/AndroidManifest.xml +3 -0
- package/capacitor/src/main/assets/native-bridge.js +1029 -0
- package/capacitor/src/main/java/com/getcapacitor/AndroidProtocolHandler.java +94 -0
- package/capacitor/src/main/java/com/getcapacitor/App.java +61 -0
- package/capacitor/src/main/java/com/getcapacitor/AppUUID.java +65 -0
- package/capacitor/src/main/java/com/getcapacitor/Bridge.java +1627 -0
- package/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java +212 -0
- package/capacitor/src/main/java/com/getcapacitor/BridgeWebChromeClient.java +467 -0
- package/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java +105 -0
- package/capacitor/src/main/java/com/getcapacitor/CapConfig.java +709 -0
- package/capacitor/src/main/java/com/getcapacitor/CapacitorWebView.java +57 -0
- package/capacitor/src/main/java/com/getcapacitor/FileUtils.java +303 -0
- package/capacitor/src/main/java/com/getcapacitor/InvalidPluginException.java +8 -0
- package/capacitor/src/main/java/com/getcapacitor/InvalidPluginMethodException.java +16 -0
- package/capacitor/src/main/java/com/getcapacitor/JSArray.java +51 -0
- package/capacitor/src/main/java/com/getcapacitor/JSExport.java +208 -0
- package/capacitor/src/main/java/com/getcapacitor/JSExportException.java +16 -0
- package/capacitor/src/main/java/com/getcapacitor/JSInjector.java +127 -0
- package/capacitor/src/main/java/com/getcapacitor/JSObject.java +164 -0
- package/capacitor/src/main/java/com/getcapacitor/JSValue.java +65 -0
- package/capacitor/src/main/java/com/getcapacitor/Logger.java +103 -0
- package/capacitor/src/main/java/com/getcapacitor/MessageHandler.java +157 -0
- package/capacitor/src/main/java/com/getcapacitor/NativePlugin.java +37 -0
- package/capacitor/src/main/java/com/getcapacitor/PermissionState.java +31 -0
- package/capacitor/src/main/java/com/getcapacitor/Plugin.java +1050 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginCall.java +394 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginConfig.java +116 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginHandle.java +160 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginInvocationException.java +16 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginLoadException.java +19 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginManager.java +56 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginMethod.java +15 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginMethodHandle.java +33 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginResult.java +84 -0
- package/capacitor/src/main/java/com/getcapacitor/ProcessedRoute.java +37 -0
- package/capacitor/src/main/java/com/getcapacitor/RouteProcessor.java +8 -0
- package/capacitor/src/main/java/com/getcapacitor/ServerPath.java +25 -0
- package/capacitor/src/main/java/com/getcapacitor/UriMatcher.java +184 -0
- package/capacitor/src/main/java/com/getcapacitor/WebViewListener.java +57 -0
- package/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +758 -0
- package/capacitor/src/main/java/com/getcapacitor/annotation/ActivityCallback.java +10 -0
- package/capacitor/src/main/java/com/getcapacitor/annotation/CapacitorPlugin.java +35 -0
- package/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java +22 -0
- package/capacitor/src/main/java/com/getcapacitor/annotation/PermissionCallback.java +10 -0
- package/capacitor/src/main/java/com/getcapacitor/cordova/CapacitorCordovaCookieManager.java +42 -0
- package/capacitor/src/main/java/com/getcapacitor/cordova/MockCordovaInterfaceImpl.java +39 -0
- package/capacitor/src/main/java/com/getcapacitor/cordova/MockCordovaWebViewImpl.java +282 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookieManager.java +239 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookies.java +119 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java +119 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/SystemBars.java +220 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/WebView.java +48 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java +358 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java +473 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java +465 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/ICapacitorHttpUrlConnection.java +15 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/MimeType.java +17 -0
- package/capacitor/src/main/java/com/getcapacitor/util/HostMask.java +123 -0
- package/capacitor/src/main/java/com/getcapacitor/util/InternalUtils.java +27 -0
- package/capacitor/src/main/java/com/getcapacitor/util/JSONUtils.java +166 -0
- package/capacitor/src/main/java/com/getcapacitor/util/PermissionHelper.java +114 -0
- package/capacitor/src/main/java/com/getcapacitor/util/WebColor.java +28 -0
- package/capacitor/src/main/res/layout/capacitor_bridge_layout_main.xml +15 -0
- package/capacitor/src/main/res/layout/no_webview.xml +14 -0
- package/capacitor/src/main/res/values/colors.xml +6 -0
- package/capacitor/src/main/res/values/strings.xml +3 -0
- package/capacitor/src/main/res/values/styles.xml +6 -0
- package/package.json +31 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
package com.getcapacitor.plugin;
|
|
2
|
+
|
|
3
|
+
import android.webkit.JavascriptInterface;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import com.getcapacitor.Plugin;
|
|
6
|
+
import com.getcapacitor.PluginCall;
|
|
7
|
+
import com.getcapacitor.PluginConfig;
|
|
8
|
+
import com.getcapacitor.PluginMethod;
|
|
9
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
10
|
+
import java.io.UnsupportedEncodingException;
|
|
11
|
+
import java.net.CookieHandler;
|
|
12
|
+
import java.net.HttpCookie;
|
|
13
|
+
import java.net.URLDecoder;
|
|
14
|
+
import java.nio.charset.StandardCharsets;
|
|
15
|
+
|
|
16
|
+
@CapacitorPlugin
|
|
17
|
+
public class CapacitorCookies extends Plugin {
|
|
18
|
+
|
|
19
|
+
CapacitorCookieManager cookieManager;
|
|
20
|
+
|
|
21
|
+
@Override
|
|
22
|
+
public void load() {
|
|
23
|
+
this.bridge.getWebView().addJavascriptInterface(this, "CapacitorCookiesAndroidInterface");
|
|
24
|
+
this.cookieManager = new CapacitorCookieManager(null, java.net.CookiePolicy.ACCEPT_ALL, this.bridge);
|
|
25
|
+
this.cookieManager.removeSessionCookies();
|
|
26
|
+
CookieHandler.setDefault(this.cookieManager);
|
|
27
|
+
super.load();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@Override
|
|
31
|
+
protected void handleOnDestroy() {
|
|
32
|
+
super.handleOnDestroy();
|
|
33
|
+
this.cookieManager.removeSessionCookies();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@JavascriptInterface
|
|
37
|
+
public boolean isEnabled() {
|
|
38
|
+
PluginConfig pluginConfig = getBridge().getConfig().getPluginConfiguration("CapacitorCookies");
|
|
39
|
+
return pluginConfig.getBoolean("enabled", false);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@JavascriptInterface
|
|
43
|
+
public void setCookie(String domain, String action) {
|
|
44
|
+
cookieManager.setCookie(domain, action);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@PluginMethod
|
|
48
|
+
public void getCookies(PluginCall call) {
|
|
49
|
+
this.bridge.eval("document.cookie", (value) -> {
|
|
50
|
+
String cookies = value.substring(1, value.length() - 1);
|
|
51
|
+
String[] cookieArray = cookies.split(";");
|
|
52
|
+
|
|
53
|
+
JSObject cookieMap = new JSObject();
|
|
54
|
+
|
|
55
|
+
for (String cookie : cookieArray) {
|
|
56
|
+
if (cookie.length() > 0) {
|
|
57
|
+
String[] keyValue = cookie.split("=", 2);
|
|
58
|
+
|
|
59
|
+
if (keyValue.length == 2) {
|
|
60
|
+
String key = keyValue[0].trim();
|
|
61
|
+
String val = keyValue[1].trim();
|
|
62
|
+
try {
|
|
63
|
+
key = URLDecoder.decode(keyValue[0].trim(), StandardCharsets.UTF_8.name());
|
|
64
|
+
val = URLDecoder.decode(keyValue[1].trim(), StandardCharsets.UTF_8.name());
|
|
65
|
+
} catch (UnsupportedEncodingException ignored) {}
|
|
66
|
+
|
|
67
|
+
cookieMap.put(key, val);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
call.resolve(cookieMap);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@PluginMethod
|
|
77
|
+
public void setCookie(PluginCall call) {
|
|
78
|
+
String key = call.getString("key");
|
|
79
|
+
if (null == key) {
|
|
80
|
+
call.reject("Must provide key");
|
|
81
|
+
}
|
|
82
|
+
String value = call.getString("value");
|
|
83
|
+
if (null == value) {
|
|
84
|
+
call.reject("Must provide value");
|
|
85
|
+
}
|
|
86
|
+
String url = call.getString("url");
|
|
87
|
+
String expires = call.getString("expires", "");
|
|
88
|
+
String path = call.getString("path", "/");
|
|
89
|
+
cookieManager.setCookie(url, key, value, expires, path);
|
|
90
|
+
call.resolve();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
@PluginMethod
|
|
94
|
+
public void deleteCookie(PluginCall call) {
|
|
95
|
+
String key = call.getString("key");
|
|
96
|
+
if (null == key) {
|
|
97
|
+
call.reject("Must provide key");
|
|
98
|
+
}
|
|
99
|
+
String url = call.getString("url");
|
|
100
|
+
cookieManager.setCookie(url, key + "=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
|
|
101
|
+
call.resolve();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@PluginMethod
|
|
105
|
+
public void clearCookies(PluginCall call) {
|
|
106
|
+
String url = call.getString("url");
|
|
107
|
+
HttpCookie[] cookies = cookieManager.getCookies(url);
|
|
108
|
+
for (HttpCookie cookie : cookies) {
|
|
109
|
+
cookieManager.setCookie(url, cookie.getName() + "=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
|
|
110
|
+
}
|
|
111
|
+
call.resolve();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@PluginMethod
|
|
115
|
+
public void clearAllCookies(PluginCall call) {
|
|
116
|
+
cookieManager.removeAllCookies();
|
|
117
|
+
call.resolve();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
package com.getcapacitor.plugin;
|
|
2
|
+
|
|
3
|
+
import android.Manifest;
|
|
4
|
+
import android.webkit.JavascriptInterface;
|
|
5
|
+
import com.getcapacitor.JSObject;
|
|
6
|
+
import com.getcapacitor.Plugin;
|
|
7
|
+
import com.getcapacitor.PluginCall;
|
|
8
|
+
import com.getcapacitor.PluginConfig;
|
|
9
|
+
import com.getcapacitor.PluginMethod;
|
|
10
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
11
|
+
import com.getcapacitor.annotation.Permission;
|
|
12
|
+
import com.getcapacitor.plugin.util.CapacitorHttpUrlConnection;
|
|
13
|
+
import com.getcapacitor.plugin.util.HttpRequestHandler;
|
|
14
|
+
import java.util.Map;
|
|
15
|
+
import java.util.concurrent.ConcurrentHashMap;
|
|
16
|
+
import java.util.concurrent.ExecutorService;
|
|
17
|
+
import java.util.concurrent.Executors;
|
|
18
|
+
|
|
19
|
+
@CapacitorPlugin(
|
|
20
|
+
permissions = {
|
|
21
|
+
@Permission(strings = { Manifest.permission.WRITE_EXTERNAL_STORAGE }, alias = "HttpWrite"),
|
|
22
|
+
@Permission(strings = { Manifest.permission.READ_EXTERNAL_STORAGE }, alias = "HttpRead")
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
public class CapacitorHttp extends Plugin {
|
|
26
|
+
|
|
27
|
+
private final Map<Runnable, PluginCall> activeRequests = new ConcurrentHashMap<>();
|
|
28
|
+
private final ExecutorService executor = Executors.newCachedThreadPool();
|
|
29
|
+
|
|
30
|
+
@Override
|
|
31
|
+
public void load() {
|
|
32
|
+
this.bridge.getWebView().addJavascriptInterface(this, "CapacitorHttpAndroidInterface");
|
|
33
|
+
super.load();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@Override
|
|
37
|
+
protected void handleOnDestroy() {
|
|
38
|
+
super.handleOnDestroy();
|
|
39
|
+
|
|
40
|
+
for (Map.Entry<Runnable, PluginCall> entry : activeRequests.entrySet()) {
|
|
41
|
+
Runnable job = entry.getKey();
|
|
42
|
+
PluginCall call = entry.getValue();
|
|
43
|
+
|
|
44
|
+
if (call.getData().has("activeCapacitorHttpUrlConnection")) {
|
|
45
|
+
try {
|
|
46
|
+
CapacitorHttpUrlConnection connection = (CapacitorHttpUrlConnection) call
|
|
47
|
+
.getData()
|
|
48
|
+
.get("activeCapacitorHttpUrlConnection");
|
|
49
|
+
connection.disconnect();
|
|
50
|
+
call.getData().remove("activeCapacitorHttpUrlConnection");
|
|
51
|
+
} catch (Exception ignored) {}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getBridge().releaseCall(call);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
activeRequests.clear();
|
|
58
|
+
executor.shutdownNow();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private void http(final PluginCall call, final String httpMethod) {
|
|
62
|
+
Runnable asyncHttpCall = new Runnable() {
|
|
63
|
+
@Override
|
|
64
|
+
public void run() {
|
|
65
|
+
try {
|
|
66
|
+
JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge());
|
|
67
|
+
call.resolve(response);
|
|
68
|
+
} catch (Exception e) {
|
|
69
|
+
call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e);
|
|
70
|
+
} finally {
|
|
71
|
+
activeRequests.remove(this);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (!executor.isShutdown()) {
|
|
77
|
+
activeRequests.put(asyncHttpCall, call);
|
|
78
|
+
executor.submit(asyncHttpCall);
|
|
79
|
+
} else {
|
|
80
|
+
call.reject("Failed to execute request - Http Plugin was shutdown");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@JavascriptInterface
|
|
85
|
+
public boolean isEnabled() {
|
|
86
|
+
PluginConfig pluginConfig = getBridge().getConfig().getPluginConfiguration("CapacitorHttp");
|
|
87
|
+
return pluginConfig.getBoolean("enabled", false);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@PluginMethod
|
|
91
|
+
public void request(final PluginCall call) {
|
|
92
|
+
this.http(call, null);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@PluginMethod
|
|
96
|
+
public void get(final PluginCall call) {
|
|
97
|
+
this.http(call, "GET");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@PluginMethod
|
|
101
|
+
public void post(final PluginCall call) {
|
|
102
|
+
this.http(call, "POST");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@PluginMethod
|
|
106
|
+
public void put(final PluginCall call) {
|
|
107
|
+
this.http(call, "PUT");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@PluginMethod
|
|
111
|
+
public void patch(final PluginCall call) {
|
|
112
|
+
this.http(call, "PATCH");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
@PluginMethod
|
|
116
|
+
public void delete(final PluginCall call) {
|
|
117
|
+
this.http(call, "DELETE");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
package com.getcapacitor.plugin;
|
|
2
|
+
|
|
3
|
+
import android.content.pm.PackageInfo;
|
|
4
|
+
import android.content.res.Configuration;
|
|
5
|
+
import android.view.View;
|
|
6
|
+
import android.view.Window;
|
|
7
|
+
import androidx.core.graphics.Insets;
|
|
8
|
+
import androidx.core.view.ViewCompat;
|
|
9
|
+
import androidx.core.view.WindowCompat;
|
|
10
|
+
import androidx.core.view.WindowInsetsCompat;
|
|
11
|
+
import androidx.core.view.WindowInsetsControllerCompat;
|
|
12
|
+
import androidx.webkit.WebViewCompat;
|
|
13
|
+
import com.getcapacitor.Plugin;
|
|
14
|
+
import com.getcapacitor.PluginCall;
|
|
15
|
+
import com.getcapacitor.PluginMethod;
|
|
16
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
17
|
+
import java.util.Locale;
|
|
18
|
+
import java.util.regex.Matcher;
|
|
19
|
+
import java.util.regex.Pattern;
|
|
20
|
+
|
|
21
|
+
@CapacitorPlugin
|
|
22
|
+
public class SystemBars extends Plugin {
|
|
23
|
+
|
|
24
|
+
static final String STYLE_LIGHT = "LIGHT";
|
|
25
|
+
static final String STYLE_DARK = "DARK";
|
|
26
|
+
static final String STYLE_DEFAULT = "DEFAULT";
|
|
27
|
+
static final String BAR_STATUS_BAR = "StatusBar";
|
|
28
|
+
static final String BAR_GESTURE_BAR = "NavigationBar";
|
|
29
|
+
|
|
30
|
+
static final String viewportMetaJSFunction = """
|
|
31
|
+
function capacitorSystemBarsCheckMetaViewport() {
|
|
32
|
+
const meta = document.querySelectorAll("meta[name=viewport]");
|
|
33
|
+
if (meta.length == 0) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
// get the last found meta viewport tag
|
|
37
|
+
const metaContent = meta[meta.length - 1].content;
|
|
38
|
+
return metaContent.includes("viewport-fit=cover");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
capacitorSystemBarsCheckMetaViewport();
|
|
42
|
+
""";
|
|
43
|
+
|
|
44
|
+
@Override
|
|
45
|
+
public void load() {
|
|
46
|
+
super.load();
|
|
47
|
+
initSystemBars();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private boolean hasFixedWebView() {
|
|
51
|
+
PackageInfo packageInfo = WebViewCompat.getCurrentWebViewPackage(bridge.getContext());
|
|
52
|
+
Pattern pattern = Pattern.compile("(\\d+)");
|
|
53
|
+
Matcher matcher = pattern.matcher(packageInfo.versionName);
|
|
54
|
+
|
|
55
|
+
if (!matcher.find()) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
String majorVersionStr = matcher.group(0);
|
|
60
|
+
int majorVersion = Integer.parseInt(majorVersionStr);
|
|
61
|
+
|
|
62
|
+
return majorVersion >= 140;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private void initSystemBars() {
|
|
66
|
+
String style = getConfig().getString("style", STYLE_DEFAULT).toUpperCase(Locale.US);
|
|
67
|
+
boolean hidden = getConfig().getBoolean("hidden", false);
|
|
68
|
+
boolean disableCSSInsets = getConfig().getBoolean("disableInsets", false);
|
|
69
|
+
|
|
70
|
+
this.bridge.getWebView().evaluateJavascript(viewportMetaJSFunction, (res) -> {
|
|
71
|
+
boolean hasMetaViewportCover = res.equals("true");
|
|
72
|
+
if (!disableCSSInsets) {
|
|
73
|
+
setupSafeAreaInsets(this.hasFixedWebView(), hasMetaViewportCover);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
getBridge().executeOnMainThread(() -> {
|
|
78
|
+
setStyle(style, "");
|
|
79
|
+
setHidden(hidden, "");
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@PluginMethod
|
|
84
|
+
public void setStyle(final PluginCall call) {
|
|
85
|
+
String bar = call.getString("bar", "");
|
|
86
|
+
String style = call.getString("style", STYLE_DEFAULT);
|
|
87
|
+
|
|
88
|
+
getBridge().executeOnMainThread(() -> {
|
|
89
|
+
setStyle(style, bar);
|
|
90
|
+
call.resolve();
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@PluginMethod
|
|
95
|
+
public void show(final PluginCall call) {
|
|
96
|
+
String bar = call.getString("bar", "");
|
|
97
|
+
|
|
98
|
+
getBridge().executeOnMainThread(() -> {
|
|
99
|
+
setHidden(false, bar);
|
|
100
|
+
call.resolve();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@PluginMethod
|
|
105
|
+
public void hide(final PluginCall call) {
|
|
106
|
+
String bar = call.getString("bar", "");
|
|
107
|
+
|
|
108
|
+
getBridge().executeOnMainThread(() -> {
|
|
109
|
+
setHidden(true, bar);
|
|
110
|
+
call.resolve();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@PluginMethod
|
|
115
|
+
public void setAnimation(final PluginCall call) {
|
|
116
|
+
call.resolve();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private void setupSafeAreaInsets(boolean hasFixedWebView, boolean hasMetaViewportCover) {
|
|
120
|
+
ViewCompat.setOnApplyWindowInsetsListener((View) getBridge().getWebView().getParent(), (v, insets) -> {
|
|
121
|
+
if (hasFixedWebView && hasMetaViewportCover) {
|
|
122
|
+
return insets;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
Insets safeArea = insets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
|
|
126
|
+
Insets imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime());
|
|
127
|
+
boolean keyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime());
|
|
128
|
+
|
|
129
|
+
int bottomInsets = safeArea.bottom;
|
|
130
|
+
|
|
131
|
+
if (keyboardVisible) {
|
|
132
|
+
// When https://issues.chromium.org/issues/457682720 is fixed and released,
|
|
133
|
+
// add behind a WebView version check
|
|
134
|
+
bottomInsets = imeInsets.bottom - bottomInsets;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
injectSafeAreaCSS(safeArea.top, safeArea.right, bottomInsets, safeArea.left);
|
|
138
|
+
|
|
139
|
+
return WindowInsetsCompat.CONSUMED;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private void injectSafeAreaCSS(int top, int right, int bottom, int left) {
|
|
144
|
+
// Convert pixels to density-independent pixels
|
|
145
|
+
float density = getActivity().getResources().getDisplayMetrics().density;
|
|
146
|
+
float topPx = top / density;
|
|
147
|
+
float rightPx = right / density;
|
|
148
|
+
float bottomPx = bottom / density;
|
|
149
|
+
float leftPx = left / density;
|
|
150
|
+
|
|
151
|
+
// Execute JavaScript to inject the CSS
|
|
152
|
+
getBridge().executeOnMainThread(() -> {
|
|
153
|
+
if (bridge != null && bridge.getWebView() != null) {
|
|
154
|
+
String script = String.format(
|
|
155
|
+
Locale.US,
|
|
156
|
+
"""
|
|
157
|
+
try {
|
|
158
|
+
document.documentElement.style.setProperty("--safe-area-inset-top", "%dpx");
|
|
159
|
+
document.documentElement.style.setProperty("--safe-area-inset-right", "%dpx");
|
|
160
|
+
document.documentElement.style.setProperty("--safe-area-inset-bottom", "%dpx");
|
|
161
|
+
document.documentElement.style.setProperty("--safe-area-inset-left", "%dpx");
|
|
162
|
+
} catch(e) { console.error('Error injecting safe area CSS:', e); }
|
|
163
|
+
""",
|
|
164
|
+
(int) topPx,
|
|
165
|
+
(int) rightPx,
|
|
166
|
+
(int) bottomPx,
|
|
167
|
+
(int) leftPx
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
bridge.getWebView().evaluateJavascript(script, null);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private void setStyle(String style, String bar) {
|
|
176
|
+
if (style.equals(STYLE_DEFAULT)) {
|
|
177
|
+
style = getStyleForTheme();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
Window window = getActivity().getWindow();
|
|
181
|
+
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, window.getDecorView());
|
|
182
|
+
if (bar.isEmpty() || bar.equals(BAR_STATUS_BAR)) {
|
|
183
|
+
windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals(STYLE_DARK));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (bar.isEmpty() || bar.equals(BAR_GESTURE_BAR)) {
|
|
187
|
+
windowInsetsControllerCompat.setAppearanceLightNavigationBars(!style.equals(STYLE_DARK));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private void setHidden(boolean hide, String bar) {
|
|
192
|
+
Window window = getActivity().getWindow();
|
|
193
|
+
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, window.getDecorView());
|
|
194
|
+
|
|
195
|
+
if (hide) {
|
|
196
|
+
if (bar.isEmpty() || bar.equals(BAR_STATUS_BAR)) {
|
|
197
|
+
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.statusBars());
|
|
198
|
+
}
|
|
199
|
+
if (bar.isEmpty() || bar.equals(BAR_GESTURE_BAR)) {
|
|
200
|
+
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.navigationBars());
|
|
201
|
+
}
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (bar.isEmpty() || bar.equals(BAR_STATUS_BAR)) {
|
|
206
|
+
windowInsetsControllerCompat.show(WindowInsetsCompat.Type.systemBars());
|
|
207
|
+
}
|
|
208
|
+
if (bar.isEmpty() || bar.equals(BAR_GESTURE_BAR)) {
|
|
209
|
+
windowInsetsControllerCompat.show(WindowInsetsCompat.Type.navigationBars());
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private String getStyleForTheme() {
|
|
214
|
+
int currentNightMode = getActivity().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
|
|
215
|
+
if (currentNightMode != Configuration.UI_MODE_NIGHT_YES) {
|
|
216
|
+
return STYLE_LIGHT;
|
|
217
|
+
}
|
|
218
|
+
return STYLE_DARK;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
package com.getcapacitor.plugin;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.content.SharedPreferences;
|
|
5
|
+
import com.getcapacitor.JSObject;
|
|
6
|
+
import com.getcapacitor.Plugin;
|
|
7
|
+
import com.getcapacitor.PluginCall;
|
|
8
|
+
import com.getcapacitor.PluginMethod;
|
|
9
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
10
|
+
|
|
11
|
+
@CapacitorPlugin
|
|
12
|
+
public class WebView extends Plugin {
|
|
13
|
+
|
|
14
|
+
public static final String WEBVIEW_PREFS_NAME = "CapWebViewSettings";
|
|
15
|
+
public static final String CAP_SERVER_PATH = "serverBasePath";
|
|
16
|
+
|
|
17
|
+
@PluginMethod
|
|
18
|
+
public void setServerAssetPath(PluginCall call) {
|
|
19
|
+
String path = call.getString("path");
|
|
20
|
+
bridge.setServerAssetPath(path);
|
|
21
|
+
call.resolve();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@PluginMethod
|
|
25
|
+
public void setServerBasePath(PluginCall call) {
|
|
26
|
+
String path = call.getString("path");
|
|
27
|
+
bridge.setServerBasePath(path);
|
|
28
|
+
call.resolve();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@PluginMethod
|
|
32
|
+
public void getServerBasePath(PluginCall call) {
|
|
33
|
+
String path = bridge.getServerBasePath();
|
|
34
|
+
JSObject ret = new JSObject();
|
|
35
|
+
ret.put("path", path);
|
|
36
|
+
call.resolve(ret);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@PluginMethod
|
|
40
|
+
public void persistServerBasePath(PluginCall call) {
|
|
41
|
+
String path = bridge.getServerBasePath();
|
|
42
|
+
SharedPreferences prefs = getContext().getSharedPreferences(WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
|
|
43
|
+
SharedPreferences.Editor editor = prefs.edit();
|
|
44
|
+
editor.putString(CAP_SERVER_PATH, path);
|
|
45
|
+
editor.apply();
|
|
46
|
+
call.resolve();
|
|
47
|
+
}
|
|
48
|
+
}
|