@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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/capacitor/build.gradle +94 -0
  3. package/capacitor/lint-baseline.xml +136 -0
  4. package/capacitor/lint.xml +9 -0
  5. package/capacitor/proguard-rules.pro +28 -0
  6. package/capacitor/src/main/AndroidManifest.xml +3 -0
  7. package/capacitor/src/main/assets/native-bridge.js +1029 -0
  8. package/capacitor/src/main/java/com/getcapacitor/AndroidProtocolHandler.java +94 -0
  9. package/capacitor/src/main/java/com/getcapacitor/App.java +61 -0
  10. package/capacitor/src/main/java/com/getcapacitor/AppUUID.java +65 -0
  11. package/capacitor/src/main/java/com/getcapacitor/Bridge.java +1627 -0
  12. package/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java +212 -0
  13. package/capacitor/src/main/java/com/getcapacitor/BridgeWebChromeClient.java +467 -0
  14. package/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java +105 -0
  15. package/capacitor/src/main/java/com/getcapacitor/CapConfig.java +709 -0
  16. package/capacitor/src/main/java/com/getcapacitor/CapacitorWebView.java +57 -0
  17. package/capacitor/src/main/java/com/getcapacitor/FileUtils.java +303 -0
  18. package/capacitor/src/main/java/com/getcapacitor/InvalidPluginException.java +8 -0
  19. package/capacitor/src/main/java/com/getcapacitor/InvalidPluginMethodException.java +16 -0
  20. package/capacitor/src/main/java/com/getcapacitor/JSArray.java +51 -0
  21. package/capacitor/src/main/java/com/getcapacitor/JSExport.java +208 -0
  22. package/capacitor/src/main/java/com/getcapacitor/JSExportException.java +16 -0
  23. package/capacitor/src/main/java/com/getcapacitor/JSInjector.java +127 -0
  24. package/capacitor/src/main/java/com/getcapacitor/JSObject.java +164 -0
  25. package/capacitor/src/main/java/com/getcapacitor/JSValue.java +65 -0
  26. package/capacitor/src/main/java/com/getcapacitor/Logger.java +103 -0
  27. package/capacitor/src/main/java/com/getcapacitor/MessageHandler.java +157 -0
  28. package/capacitor/src/main/java/com/getcapacitor/NativePlugin.java +37 -0
  29. package/capacitor/src/main/java/com/getcapacitor/PermissionState.java +31 -0
  30. package/capacitor/src/main/java/com/getcapacitor/Plugin.java +1050 -0
  31. package/capacitor/src/main/java/com/getcapacitor/PluginCall.java +394 -0
  32. package/capacitor/src/main/java/com/getcapacitor/PluginConfig.java +116 -0
  33. package/capacitor/src/main/java/com/getcapacitor/PluginHandle.java +160 -0
  34. package/capacitor/src/main/java/com/getcapacitor/PluginInvocationException.java +16 -0
  35. package/capacitor/src/main/java/com/getcapacitor/PluginLoadException.java +19 -0
  36. package/capacitor/src/main/java/com/getcapacitor/PluginManager.java +56 -0
  37. package/capacitor/src/main/java/com/getcapacitor/PluginMethod.java +15 -0
  38. package/capacitor/src/main/java/com/getcapacitor/PluginMethodHandle.java +33 -0
  39. package/capacitor/src/main/java/com/getcapacitor/PluginResult.java +84 -0
  40. package/capacitor/src/main/java/com/getcapacitor/ProcessedRoute.java +37 -0
  41. package/capacitor/src/main/java/com/getcapacitor/RouteProcessor.java +8 -0
  42. package/capacitor/src/main/java/com/getcapacitor/ServerPath.java +25 -0
  43. package/capacitor/src/main/java/com/getcapacitor/UriMatcher.java +184 -0
  44. package/capacitor/src/main/java/com/getcapacitor/WebViewListener.java +57 -0
  45. package/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +758 -0
  46. package/capacitor/src/main/java/com/getcapacitor/annotation/ActivityCallback.java +10 -0
  47. package/capacitor/src/main/java/com/getcapacitor/annotation/CapacitorPlugin.java +35 -0
  48. package/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java +22 -0
  49. package/capacitor/src/main/java/com/getcapacitor/annotation/PermissionCallback.java +10 -0
  50. package/capacitor/src/main/java/com/getcapacitor/cordova/CapacitorCordovaCookieManager.java +42 -0
  51. package/capacitor/src/main/java/com/getcapacitor/cordova/MockCordovaInterfaceImpl.java +39 -0
  52. package/capacitor/src/main/java/com/getcapacitor/cordova/MockCordovaWebViewImpl.java +282 -0
  53. package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookieManager.java +239 -0
  54. package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookies.java +119 -0
  55. package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java +119 -0
  56. package/capacitor/src/main/java/com/getcapacitor/plugin/SystemBars.java +220 -0
  57. package/capacitor/src/main/java/com/getcapacitor/plugin/WebView.java +48 -0
  58. package/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java +358 -0
  59. package/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java +473 -0
  60. package/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java +465 -0
  61. package/capacitor/src/main/java/com/getcapacitor/plugin/util/ICapacitorHttpUrlConnection.java +15 -0
  62. package/capacitor/src/main/java/com/getcapacitor/plugin/util/MimeType.java +17 -0
  63. package/capacitor/src/main/java/com/getcapacitor/util/HostMask.java +123 -0
  64. package/capacitor/src/main/java/com/getcapacitor/util/InternalUtils.java +27 -0
  65. package/capacitor/src/main/java/com/getcapacitor/util/JSONUtils.java +166 -0
  66. package/capacitor/src/main/java/com/getcapacitor/util/PermissionHelper.java +114 -0
  67. package/capacitor/src/main/java/com/getcapacitor/util/WebColor.java +28 -0
  68. package/capacitor/src/main/res/layout/capacitor_bridge_layout_main.xml +15 -0
  69. package/capacitor/src/main/res/layout/no_webview.xml +14 -0
  70. package/capacitor/src/main/res/values/colors.xml +6 -0
  71. package/capacitor/src/main/res/values/strings.xml +3 -0
  72. package/capacitor/src/main/res/values/styles.xml +6 -0
  73. package/package.json +31 -0
@@ -0,0 +1,127 @@
1
+ package com.getcapacitor;
2
+
3
+ import java.io.ByteArrayInputStream;
4
+ import java.io.InputStream;
5
+ import java.io.InputStreamReader;
6
+ import java.io.Reader;
7
+ import java.nio.charset.StandardCharsets;
8
+
9
+ /**
10
+ * JSInject is responsible for returning Capacitor's core
11
+ * runtime JS and any plugin JS back into HTML page responses
12
+ * to the client.
13
+ */
14
+ class JSInjector {
15
+
16
+ private String globalJS;
17
+ private String bridgeJS;
18
+ private String pluginJS;
19
+ private String cordovaJS;
20
+ private String cordovaPluginsJS;
21
+ private String cordovaPluginsFileJS;
22
+ private String localUrlJS;
23
+ private String miscJS;
24
+
25
+ public JSInjector(
26
+ String globalJS,
27
+ String bridgeJS,
28
+ String pluginJS,
29
+ String cordovaJS,
30
+ String cordovaPluginsJS,
31
+ String cordovaPluginsFileJS,
32
+ String localUrlJS
33
+ ) {
34
+ this(globalJS, bridgeJS, pluginJS, cordovaJS, cordovaPluginsJS, cordovaPluginsFileJS, localUrlJS, null);
35
+ }
36
+
37
+ public JSInjector(
38
+ String globalJS,
39
+ String bridgeJS,
40
+ String pluginJS,
41
+ String cordovaJS,
42
+ String cordovaPluginsJS,
43
+ String cordovaPluginsFileJS,
44
+ String localUrlJS,
45
+ String miscJS
46
+ ) {
47
+ this.globalJS = globalJS;
48
+ this.bridgeJS = bridgeJS;
49
+ this.pluginJS = pluginJS;
50
+ this.cordovaJS = cordovaJS;
51
+ this.cordovaPluginsJS = cordovaPluginsJS;
52
+ this.cordovaPluginsFileJS = cordovaPluginsFileJS;
53
+ this.localUrlJS = localUrlJS;
54
+ this.miscJS = miscJS;
55
+ }
56
+
57
+ /**
58
+ * Generates injectable JS content.
59
+ * This may be used in other forms of injecting that aren't using an InputStream.
60
+ * @return
61
+ */
62
+ public String getScriptString() {
63
+ String scriptString =
64
+ globalJS +
65
+ "\n\n" +
66
+ localUrlJS +
67
+ "\n\n" +
68
+ bridgeJS +
69
+ "\n\n" +
70
+ pluginJS +
71
+ "\n\n" +
72
+ cordovaJS +
73
+ "\n\n" +
74
+ cordovaPluginsFileJS +
75
+ "\n\n" +
76
+ cordovaPluginsJS;
77
+
78
+ if (miscJS != null) {
79
+ scriptString += "\n\n" + miscJS;
80
+ }
81
+
82
+ return scriptString;
83
+ }
84
+
85
+ /**
86
+ * Given an InputStream from the web server, prepend it with
87
+ * our JS stream
88
+ * @param responseStream
89
+ * @return
90
+ */
91
+ public InputStream getInjectedStream(InputStream responseStream) {
92
+ String js = "<script type=\"text/javascript\">" + getScriptString() + "</script>";
93
+ String html = this.readAssetStream(responseStream);
94
+
95
+ // Insert the js string at the position after <head> or before </head> using StringBuilder
96
+ StringBuilder modifiedHtml = new StringBuilder(html);
97
+ if (html.contains("<head>")) {
98
+ modifiedHtml.insert(html.indexOf("<head>") + "<head>".length(), "\n" + js + "\n");
99
+ html = modifiedHtml.toString();
100
+ } else if (html.contains("</head>")) {
101
+ modifiedHtml.insert(html.indexOf("</head>"), "\n" + js + "\n");
102
+ html = modifiedHtml.toString();
103
+ } else {
104
+ Logger.error("Unable to inject Capacitor, Plugins won't work");
105
+ }
106
+ return new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8));
107
+ }
108
+
109
+ private String readAssetStream(InputStream stream) {
110
+ try {
111
+ final int bufferSize = 1024;
112
+ final char[] buffer = new char[bufferSize];
113
+ final StringBuilder out = new StringBuilder();
114
+ Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8);
115
+ for (;;) {
116
+ int rsz = in.read(buffer, 0, buffer.length);
117
+ if (rsz < 0) break;
118
+ out.append(buffer, 0, rsz);
119
+ }
120
+ return out.toString();
121
+ } catch (Exception e) {
122
+ Logger.error("Unable to process HTML asset file. This is a fatal error", e);
123
+ }
124
+
125
+ return "";
126
+ }
127
+ }
@@ -0,0 +1,164 @@
1
+ package com.getcapacitor;
2
+
3
+ import androidx.annotation.Nullable;
4
+ import java.util.ArrayList;
5
+ import java.util.Iterator;
6
+ import java.util.List;
7
+ import org.json.JSONException;
8
+ import org.json.JSONObject;
9
+
10
+ /**
11
+ * A wrapper around JSONObject that isn't afraid to do simple
12
+ * JSON put operations without having to throw an exception
13
+ * for every little thing jeez
14
+ */
15
+ public class JSObject extends JSONObject {
16
+
17
+ public JSObject() {
18
+ super();
19
+ }
20
+
21
+ public JSObject(String json) throws JSONException {
22
+ super(json);
23
+ }
24
+
25
+ public JSObject(JSONObject obj, String[] names) throws JSONException {
26
+ super(obj, names);
27
+ }
28
+
29
+ /**
30
+ * Convert a pathetic JSONObject into a JSObject
31
+ * @param obj
32
+ */
33
+ public static JSObject fromJSONObject(JSONObject obj) throws JSONException {
34
+ Iterator<String> keysIter = obj.keys();
35
+ List<String> keys = new ArrayList<>();
36
+ while (keysIter.hasNext()) {
37
+ keys.add(keysIter.next());
38
+ }
39
+
40
+ return new JSObject(obj, keys.toArray(new String[keys.size()]));
41
+ }
42
+
43
+ @Override
44
+ @Nullable
45
+ public String getString(String key) {
46
+ return getString(key, null);
47
+ }
48
+
49
+ @Nullable
50
+ public String getString(String key, @Nullable String defaultValue) {
51
+ try {
52
+ String value = super.getString(key);
53
+ if (!super.isNull(key)) {
54
+ return value;
55
+ }
56
+ } catch (JSONException ex) {}
57
+ return defaultValue;
58
+ }
59
+
60
+ @Nullable
61
+ public Integer getInteger(String key) {
62
+ return getInteger(key, null);
63
+ }
64
+
65
+ @Nullable
66
+ public Integer getInteger(String key, @Nullable Integer defaultValue) {
67
+ try {
68
+ return super.getInt(key);
69
+ } catch (JSONException e) {}
70
+ return defaultValue;
71
+ }
72
+
73
+ @Nullable
74
+ public Boolean getBoolean(String key, @Nullable Boolean defaultValue) {
75
+ try {
76
+ return super.getBoolean(key);
77
+ } catch (JSONException e) {}
78
+ return defaultValue;
79
+ }
80
+
81
+ /**
82
+ * Fetch boolean from jsonObject
83
+ */
84
+ @Nullable
85
+ public Boolean getBool(String key) {
86
+ return getBoolean(key, null);
87
+ }
88
+
89
+ @Nullable
90
+ public JSObject getJSObject(String name) {
91
+ try {
92
+ return getJSObject(name, null);
93
+ } catch (JSONException e) {}
94
+ return null;
95
+ }
96
+
97
+ @Nullable
98
+ public JSObject getJSObject(String name, @Nullable JSObject defaultValue) throws JSONException {
99
+ try {
100
+ Object obj = get(name);
101
+ if (obj instanceof JSONObject) {
102
+ Iterator<String> keysIter = ((JSONObject) obj).keys();
103
+ List<String> keys = new ArrayList<>();
104
+ while (keysIter.hasNext()) {
105
+ keys.add(keysIter.next());
106
+ }
107
+
108
+ return new JSObject((JSONObject) obj, keys.toArray(new String[keys.size()]));
109
+ }
110
+ } catch (JSONException ex) {}
111
+ return defaultValue;
112
+ }
113
+
114
+ @Override
115
+ public JSObject put(String key, boolean value) {
116
+ try {
117
+ super.put(key, value);
118
+ } catch (JSONException ex) {}
119
+ return this;
120
+ }
121
+
122
+ @Override
123
+ public JSObject put(String key, int value) {
124
+ try {
125
+ super.put(key, value);
126
+ } catch (JSONException ex) {}
127
+ return this;
128
+ }
129
+
130
+ @Override
131
+ public JSObject put(String key, long value) {
132
+ try {
133
+ super.put(key, value);
134
+ } catch (JSONException ex) {}
135
+ return this;
136
+ }
137
+
138
+ @Override
139
+ public JSObject put(String key, double value) {
140
+ try {
141
+ super.put(key, value);
142
+ } catch (JSONException ex) {}
143
+ return this;
144
+ }
145
+
146
+ @Override
147
+ public JSObject put(String key, Object value) {
148
+ try {
149
+ super.put(key, value);
150
+ } catch (JSONException ex) {}
151
+ return this;
152
+ }
153
+
154
+ public JSObject put(String key, String value) {
155
+ try {
156
+ super.put(key, value);
157
+ } catch (JSONException ex) {}
158
+ return this;
159
+ }
160
+
161
+ public JSObject putSafe(String key, Object value) throws JSONException {
162
+ return (JSObject) super.put(key, value);
163
+ }
164
+ }
@@ -0,0 +1,65 @@
1
+ package com.getcapacitor;
2
+
3
+ import org.json.JSONException;
4
+
5
+ /**
6
+ * Represents a single user-data value of any type on the capacitor PluginCall object.
7
+ */
8
+ public class JSValue {
9
+
10
+ private final Object value;
11
+
12
+ /**
13
+ * @param call The capacitor plugin call, used for accessing the value safely.
14
+ * @param name The name of the property to access.
15
+ */
16
+ public JSValue(PluginCall call, String name) {
17
+ this.value = this.toValue(call, name);
18
+ }
19
+
20
+ /**
21
+ * Returns the coerced but uncasted underlying value.
22
+ */
23
+ public Object getValue() {
24
+ return this.value;
25
+ }
26
+
27
+ @Override
28
+ public String toString() {
29
+ return this.getValue().toString();
30
+ }
31
+
32
+ /**
33
+ * Returns the underlying value as a JSObject, or throwing if it cannot.
34
+ *
35
+ * @throws JSONException If the underlying value is not a JSObject.
36
+ */
37
+ public JSObject toJSObject() throws JSONException {
38
+ if (this.value instanceof JSObject) return (JSObject) this.value;
39
+ throw new JSONException("JSValue could not be coerced to JSObject.");
40
+ }
41
+
42
+ /**
43
+ * Returns the underlying value as a JSArray, or throwing if it cannot.
44
+ *
45
+ * @throws JSONException If the underlying value is not a JSArray.
46
+ */
47
+ public JSArray toJSArray() throws JSONException {
48
+ if (this.value instanceof JSArray) return (JSArray) this.value;
49
+ throw new JSONException("JSValue could not be coerced to JSArray.");
50
+ }
51
+
52
+ /**
53
+ * Returns the underlying value this object represents, coercing it into a capacitor-friendly object if supported.
54
+ */
55
+ private Object toValue(PluginCall call, String name) {
56
+ Object value = null;
57
+ value = call.getArray(name, null);
58
+ if (value != null) return value;
59
+ value = call.getObject(name, null);
60
+ if (value != null) return value;
61
+ value = call.getString(name, null);
62
+ if (value != null) return value;
63
+ return call.getData().opt(name);
64
+ }
65
+ }
@@ -0,0 +1,103 @@
1
+ package com.getcapacitor;
2
+
3
+ import android.text.TextUtils;
4
+ import android.util.Log;
5
+
6
+ public class Logger {
7
+
8
+ public static final String LOG_TAG_CORE = "Capacitor";
9
+ public static CapConfig config;
10
+
11
+ private static Logger instance;
12
+
13
+ private static Logger getInstance() {
14
+ if (instance == null) {
15
+ instance = new Logger();
16
+ }
17
+ return instance;
18
+ }
19
+
20
+ public static void init(CapConfig config) {
21
+ Logger.getInstance().loadConfig(config);
22
+ }
23
+
24
+ private void loadConfig(CapConfig config) {
25
+ Logger.config = config;
26
+ }
27
+
28
+ public static String tags(String... subtags) {
29
+ if (subtags != null && subtags.length > 0) {
30
+ return LOG_TAG_CORE + "/" + TextUtils.join("/", subtags);
31
+ }
32
+
33
+ return LOG_TAG_CORE;
34
+ }
35
+
36
+ public static void verbose(String message) {
37
+ verbose(LOG_TAG_CORE, message);
38
+ }
39
+
40
+ public static void verbose(String tag, String message) {
41
+ if (!shouldLog()) {
42
+ return;
43
+ }
44
+
45
+ Log.v(tag, message);
46
+ }
47
+
48
+ public static void debug(String message) {
49
+ debug(LOG_TAG_CORE, message);
50
+ }
51
+
52
+ public static void debug(String tag, String message) {
53
+ if (!shouldLog()) {
54
+ return;
55
+ }
56
+
57
+ Log.d(tag, message);
58
+ }
59
+
60
+ public static void info(String message) {
61
+ info(LOG_TAG_CORE, message);
62
+ }
63
+
64
+ public static void info(String tag, String message) {
65
+ if (!shouldLog()) {
66
+ return;
67
+ }
68
+
69
+ Log.i(tag, message);
70
+ }
71
+
72
+ public static void warn(String message) {
73
+ warn(LOG_TAG_CORE, message);
74
+ }
75
+
76
+ public static void warn(String tag, String message) {
77
+ if (!shouldLog()) {
78
+ return;
79
+ }
80
+
81
+ Log.w(tag, message);
82
+ }
83
+
84
+ public static void error(String message) {
85
+ error(LOG_TAG_CORE, message, null);
86
+ }
87
+
88
+ public static void error(String message, Throwable e) {
89
+ error(LOG_TAG_CORE, message, e);
90
+ }
91
+
92
+ public static void error(String tag, String message, Throwable e) {
93
+ if (!shouldLog()) {
94
+ return;
95
+ }
96
+
97
+ Log.e(tag, message, e);
98
+ }
99
+
100
+ public static boolean shouldLog() {
101
+ return config == null || config.isLoggingEnabled();
102
+ }
103
+ }
@@ -0,0 +1,157 @@
1
+ package com.getcapacitor;
2
+
3
+ import android.webkit.JavascriptInterface;
4
+ import android.webkit.WebView;
5
+ import androidx.webkit.JavaScriptReplyProxy;
6
+ import androidx.webkit.WebViewCompat;
7
+ import androidx.webkit.WebViewFeature;
8
+ import org.apache.cordova.PluginManager;
9
+
10
+ /**
11
+ * MessageHandler handles messages from the WebView, dispatching them
12
+ * to plugins.
13
+ */
14
+ public class MessageHandler {
15
+
16
+ private Bridge bridge;
17
+ private WebView webView;
18
+ private PluginManager cordovaPluginManager;
19
+ private JavaScriptReplyProxy javaScriptReplyProxy;
20
+
21
+ public MessageHandler(Bridge bridge, WebView webView, PluginManager cordovaPluginManager) {
22
+ this.bridge = bridge;
23
+ this.webView = webView;
24
+ this.cordovaPluginManager = cordovaPluginManager;
25
+
26
+ if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) && !bridge.getConfig().isUsingLegacyBridge()) {
27
+ WebViewCompat.WebMessageListener capListener = (view, message, sourceOrigin, isMainFrame, replyProxy) -> {
28
+ if (isMainFrame) {
29
+ postMessage(message.getData());
30
+ javaScriptReplyProxy = replyProxy;
31
+ } else {
32
+ Logger.warn("Plugin execution is allowed in Main Frame only");
33
+ }
34
+ };
35
+ try {
36
+ WebViewCompat.addWebMessageListener(webView, "androidBridge", bridge.getAllowedOriginRules(), capListener);
37
+ } catch (Exception ex) {
38
+ webView.addJavascriptInterface(this, "androidBridge");
39
+ }
40
+ } else {
41
+ webView.addJavascriptInterface(this, "androidBridge");
42
+ }
43
+ }
44
+
45
+ /**
46
+ * The main message handler that will be called from JavaScript
47
+ * to send a message to the native bridge.
48
+ * @param jsonStr
49
+ */
50
+ @JavascriptInterface
51
+ @SuppressWarnings("unused")
52
+ public void postMessage(String jsonStr) {
53
+ try {
54
+ JSObject postData = new JSObject(jsonStr);
55
+
56
+ String type = postData.getString("type");
57
+
58
+ boolean typeIsNotNull = type != null;
59
+ boolean isCordovaPlugin = typeIsNotNull && type.equals("cordova");
60
+ boolean isJavaScriptError = typeIsNotNull && type.equals("js.error");
61
+
62
+ String callbackId = postData.getString("callbackId");
63
+
64
+ if (isCordovaPlugin) {
65
+ String service = postData.getString("service");
66
+ String action = postData.getString("action");
67
+ String actionArgs = postData.getString("actionArgs");
68
+
69
+ Logger.verbose(
70
+ Logger.tags("Plugin"),
71
+ "To native (Cordova plugin): callbackId: " +
72
+ callbackId +
73
+ ", service: " +
74
+ service +
75
+ ", action: " +
76
+ action +
77
+ ", actionArgs: " +
78
+ actionArgs
79
+ );
80
+
81
+ this.callCordovaPluginMethod(callbackId, service, action, actionArgs);
82
+ } else if (isJavaScriptError) {
83
+ Logger.error("JavaScript Error: " + jsonStr);
84
+ } else {
85
+ String pluginId = postData.getString("pluginId");
86
+ String methodName = postData.getString("methodName");
87
+ JSObject methodData = postData.getJSObject("options", new JSObject());
88
+
89
+ Logger.verbose(
90
+ Logger.tags("Plugin"),
91
+ "To native (Capacitor plugin): callbackId: " + callbackId + ", pluginId: " + pluginId + ", methodName: " + methodName
92
+ );
93
+
94
+ this.callPluginMethod(callbackId, pluginId, methodName, methodData);
95
+ }
96
+ } catch (Exception ex) {
97
+ Logger.error("Post message error:", ex);
98
+ }
99
+ }
100
+
101
+ public void sendResponseMessage(PluginCall call, PluginResult successResult, PluginResult errorResult) {
102
+ try {
103
+ PluginResult data = new PluginResult();
104
+ data.put("save", call.isKeptAlive());
105
+ data.put("callbackId", call.getCallbackId());
106
+ data.put("pluginId", call.getPluginId());
107
+ data.put("methodName", call.getMethodName());
108
+
109
+ boolean pluginResultInError = errorResult != null;
110
+ if (pluginResultInError) {
111
+ data.put("success", false);
112
+ data.put("error", errorResult);
113
+ Logger.debug("Sending plugin error: " + data.toString());
114
+ } else {
115
+ data.put("success", true);
116
+ if (successResult != null) {
117
+ data.put("data", successResult);
118
+ }
119
+ }
120
+
121
+ boolean isValidCallbackId = !call.getCallbackId().equals(PluginCall.CALLBACK_ID_DANGLING);
122
+ if (isValidCallbackId) {
123
+ if (bridge.getConfig().isUsingLegacyBridge()) {
124
+ legacySendResponseMessage(data);
125
+ } else if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) && javaScriptReplyProxy != null) {
126
+ javaScriptReplyProxy.postMessage(data.toString());
127
+ } else {
128
+ legacySendResponseMessage(data);
129
+ }
130
+ } else {
131
+ bridge.getApp().fireRestoredResult(data);
132
+ }
133
+ } catch (Exception ex) {
134
+ Logger.error("sendResponseMessage: error: " + ex);
135
+ }
136
+ if (!call.isKeptAlive()) {
137
+ call.release(bridge);
138
+ }
139
+ }
140
+
141
+ private void legacySendResponseMessage(PluginResult data) {
142
+ final String runScript = "window.Capacitor.fromNative(" + data.toString() + ")";
143
+ final WebView webView = this.webView;
144
+ webView.post(() -> webView.evaluateJavascript(runScript, null));
145
+ }
146
+
147
+ private void callPluginMethod(String callbackId, String pluginId, String methodName, JSObject methodData) {
148
+ PluginCall call = new PluginCall(this, pluginId, callbackId, methodName, methodData);
149
+ bridge.callPluginMethod(pluginId, methodName, call);
150
+ }
151
+
152
+ private void callCordovaPluginMethod(String callbackId, String service, String action, String actionArgs) {
153
+ bridge.execute(() -> {
154
+ cordovaPluginManager.exec(service, action, callbackId, actionArgs);
155
+ });
156
+ }
157
+ }
@@ -0,0 +1,37 @@
1
+ package com.getcapacitor;
2
+
3
+ import com.getcapacitor.annotation.CapacitorPlugin;
4
+ import java.lang.annotation.Retention;
5
+ import java.lang.annotation.RetentionPolicy;
6
+
7
+ /**
8
+ * Base annotation for all Plugins
9
+ * @deprecated
10
+ * <p> Use {@link CapacitorPlugin} instead
11
+ */
12
+ @Retention(RetentionPolicy.RUNTIME)
13
+ @Deprecated
14
+ public @interface NativePlugin {
15
+ /**
16
+ * Request codes this plugin uses and responds to, in order to tie
17
+ * Android events back the plugin to handle
18
+ */
19
+ int[] requestCodes() default {};
20
+
21
+ /**
22
+ * Permissions this plugin needs, in order to make permission requests
23
+ * easy if the plugin only needs basic permission prompting
24
+ */
25
+ String[] permissions() default {};
26
+
27
+ /**
28
+ * The request code to use when automatically requesting permissions
29
+ */
30
+ int permissionRequestCode() default 9000;
31
+
32
+ /**
33
+ * A custom name for the plugin, otherwise uses the
34
+ * simple class name.
35
+ */
36
+ String name() default "";
37
+ }
@@ -0,0 +1,31 @@
1
+ package com.getcapacitor;
2
+
3
+ import java.util.Locale;
4
+
5
+ /**
6
+ * Represents the state of a permission
7
+ *
8
+ * @since 3.0.0
9
+ */
10
+ public enum PermissionState {
11
+ GRANTED("granted"),
12
+ DENIED("denied"),
13
+ PROMPT("prompt"),
14
+ PROMPT_WITH_RATIONALE("prompt-with-rationale");
15
+
16
+ private String state;
17
+
18
+ PermissionState(String state) {
19
+ this.state = state;
20
+ }
21
+
22
+ @Override
23
+ public String toString() {
24
+ return state;
25
+ }
26
+
27
+ public static PermissionState byState(String state) {
28
+ state = state.toUpperCase(Locale.ROOT).replace('-', '_');
29
+ return valueOf(state);
30
+ }
31
+ }