@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,394 @@
|
|
|
1
|
+
package com.getcapacitor;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.Nullable;
|
|
4
|
+
import java.util.ArrayList;
|
|
5
|
+
import java.util.List;
|
|
6
|
+
import org.json.JSONArray;
|
|
7
|
+
import org.json.JSONException;
|
|
8
|
+
import org.json.JSONObject;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wraps a call from the web layer to native
|
|
12
|
+
*/
|
|
13
|
+
public class PluginCall {
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A special callback id that indicates there is no matching callback
|
|
17
|
+
* on the client to associate any PluginCall results back to. This is used
|
|
18
|
+
* in the case of an app resuming with saved instance data, for example.
|
|
19
|
+
*/
|
|
20
|
+
public static final String CALLBACK_ID_DANGLING = "-1";
|
|
21
|
+
|
|
22
|
+
private final MessageHandler msgHandler;
|
|
23
|
+
private final String pluginId;
|
|
24
|
+
private final String callbackId;
|
|
25
|
+
private final String methodName;
|
|
26
|
+
private final JSObject data;
|
|
27
|
+
|
|
28
|
+
private boolean keepAlive = false;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Indicates that this PluginCall was released, and should no longer be used
|
|
32
|
+
*/
|
|
33
|
+
@Deprecated
|
|
34
|
+
private boolean isReleased = false;
|
|
35
|
+
|
|
36
|
+
public PluginCall(MessageHandler msgHandler, String pluginId, String callbackId, String methodName, JSObject data) {
|
|
37
|
+
this.msgHandler = msgHandler;
|
|
38
|
+
this.pluginId = pluginId;
|
|
39
|
+
this.callbackId = callbackId;
|
|
40
|
+
this.methodName = methodName;
|
|
41
|
+
this.data = data;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public void successCallback(PluginResult successResult) {
|
|
45
|
+
if (CALLBACK_ID_DANGLING.equals(this.callbackId)) {
|
|
46
|
+
// don't send back response if the callbackId was "-1"
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.msgHandler.sendResponseMessage(this, successResult, null);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public void resolve(JSObject data) {
|
|
54
|
+
PluginResult result = new PluginResult(data);
|
|
55
|
+
this.msgHandler.sendResponseMessage(this, result, null);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public void resolve() {
|
|
59
|
+
this.msgHandler.sendResponseMessage(this, null, null);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public void errorCallback(String msg) {
|
|
63
|
+
PluginResult errorResult = new PluginResult();
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
errorResult.put("message", msg);
|
|
67
|
+
} catch (Exception jsonEx) {
|
|
68
|
+
Logger.error(Logger.tags("Plugin"), jsonEx.toString(), null);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.msgHandler.sendResponseMessage(this, null, errorResult);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public void reject(String msg, String code, Exception ex, JSObject data) {
|
|
75
|
+
PluginResult errorResult = new PluginResult();
|
|
76
|
+
|
|
77
|
+
if (ex != null) {
|
|
78
|
+
Logger.error(Logger.tags("Plugin"), msg, ex);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
errorResult.put("message", msg);
|
|
83
|
+
errorResult.put("code", code);
|
|
84
|
+
if (null != data) {
|
|
85
|
+
errorResult.put("data", data);
|
|
86
|
+
}
|
|
87
|
+
} catch (Exception jsonEx) {
|
|
88
|
+
Logger.error(Logger.tags("Plugin"), jsonEx.getMessage(), jsonEx);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this.msgHandler.sendResponseMessage(this, null, errorResult);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public void reject(String msg, Exception ex, JSObject data) {
|
|
95
|
+
reject(msg, null, ex, data);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public void reject(String msg, String code, JSObject data) {
|
|
99
|
+
reject(msg, code, null, data);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public void reject(String msg, String code, Exception ex) {
|
|
103
|
+
reject(msg, code, ex, null);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public void reject(String msg, JSObject data) {
|
|
107
|
+
reject(msg, null, null, data);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public void reject(String msg, Exception ex) {
|
|
111
|
+
reject(msg, null, ex, null);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public void reject(String msg, String code) {
|
|
115
|
+
reject(msg, code, null, null);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public void reject(String msg) {
|
|
119
|
+
reject(msg, null, null, null);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public void unimplemented() {
|
|
123
|
+
unimplemented("not implemented");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public void unimplemented(String msg) {
|
|
127
|
+
reject(msg, "UNIMPLEMENTED", null, null);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public void unavailable() {
|
|
131
|
+
unavailable("not available");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public void unavailable(String msg) {
|
|
135
|
+
reject(msg, "UNAVAILABLE", null, null);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public String getPluginId() {
|
|
139
|
+
return this.pluginId;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public String getCallbackId() {
|
|
143
|
+
return this.callbackId;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public String getMethodName() {
|
|
147
|
+
return this.methodName;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public JSObject getData() {
|
|
151
|
+
return this.data;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
@Nullable
|
|
155
|
+
public String getString(String name) {
|
|
156
|
+
return this.getString(name, null);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@Nullable
|
|
160
|
+
public String getString(String name, @Nullable String defaultValue) {
|
|
161
|
+
Object value = this.data.opt(name);
|
|
162
|
+
if (value == null) {
|
|
163
|
+
return defaultValue;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (value instanceof String) {
|
|
167
|
+
return (String) value;
|
|
168
|
+
}
|
|
169
|
+
return defaultValue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
@Nullable
|
|
173
|
+
public Integer getInt(String name) {
|
|
174
|
+
return this.getInt(name, null);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
@Nullable
|
|
178
|
+
public Integer getInt(String name, @Nullable Integer defaultValue) {
|
|
179
|
+
Object value = this.data.opt(name);
|
|
180
|
+
if (value == null) {
|
|
181
|
+
return defaultValue;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (value instanceof Integer) {
|
|
185
|
+
return (Integer) value;
|
|
186
|
+
}
|
|
187
|
+
return defaultValue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
@Nullable
|
|
191
|
+
public Long getLong(String name) {
|
|
192
|
+
return this.getLong(name, null);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
@Nullable
|
|
196
|
+
public Long getLong(String name, @Nullable Long defaultValue) {
|
|
197
|
+
Object value = this.data.opt(name);
|
|
198
|
+
if (value == null) {
|
|
199
|
+
return defaultValue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (value instanceof Long) {
|
|
203
|
+
return (Long) value;
|
|
204
|
+
}
|
|
205
|
+
return defaultValue;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
@Nullable
|
|
209
|
+
public Float getFloat(String name) {
|
|
210
|
+
return this.getFloat(name, null);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
@Nullable
|
|
214
|
+
public Float getFloat(String name, @Nullable Float defaultValue) {
|
|
215
|
+
Object value = this.data.opt(name);
|
|
216
|
+
if (value == null) {
|
|
217
|
+
return defaultValue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (value instanceof Float) {
|
|
221
|
+
return (Float) value;
|
|
222
|
+
}
|
|
223
|
+
if (value instanceof Double) {
|
|
224
|
+
return ((Double) value).floatValue();
|
|
225
|
+
}
|
|
226
|
+
if (value instanceof Integer) {
|
|
227
|
+
return ((Integer) value).floatValue();
|
|
228
|
+
}
|
|
229
|
+
return defaultValue;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
@Nullable
|
|
233
|
+
public Double getDouble(String name) {
|
|
234
|
+
return this.getDouble(name, null);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
@Nullable
|
|
238
|
+
public Double getDouble(String name, @Nullable Double defaultValue) {
|
|
239
|
+
Object value = this.data.opt(name);
|
|
240
|
+
if (value == null) {
|
|
241
|
+
return defaultValue;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (value instanceof Double) {
|
|
245
|
+
return (Double) value;
|
|
246
|
+
}
|
|
247
|
+
if (value instanceof Float) {
|
|
248
|
+
return ((Float) value).doubleValue();
|
|
249
|
+
}
|
|
250
|
+
if (value instanceof Integer) {
|
|
251
|
+
return ((Integer) value).doubleValue();
|
|
252
|
+
}
|
|
253
|
+
return defaultValue;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
@Nullable
|
|
257
|
+
public Boolean getBoolean(String name) {
|
|
258
|
+
return this.getBoolean(name, null);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
@Nullable
|
|
262
|
+
public Boolean getBoolean(String name, @Nullable Boolean defaultValue) {
|
|
263
|
+
Object value = this.data.opt(name);
|
|
264
|
+
if (value == null) {
|
|
265
|
+
return defaultValue;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (value instanceof Boolean) {
|
|
269
|
+
return (Boolean) value;
|
|
270
|
+
}
|
|
271
|
+
return defaultValue;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
public JSObject getObject(String name) {
|
|
275
|
+
return this.getObject(name, null);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
@Nullable
|
|
279
|
+
public JSObject getObject(String name, JSObject defaultValue) {
|
|
280
|
+
Object value = this.data.opt(name);
|
|
281
|
+
if (value == null) {
|
|
282
|
+
return defaultValue;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (value instanceof JSONObject) {
|
|
286
|
+
try {
|
|
287
|
+
return JSObject.fromJSONObject((JSONObject) value);
|
|
288
|
+
} catch (JSONException ex) {
|
|
289
|
+
return defaultValue;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return defaultValue;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
public JSArray getArray(String name) {
|
|
296
|
+
return this.getArray(name, null);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Get a JSONArray and turn it into a JSArray
|
|
301
|
+
* @param name
|
|
302
|
+
* @param defaultValue
|
|
303
|
+
* @return
|
|
304
|
+
*/
|
|
305
|
+
@Nullable
|
|
306
|
+
public JSArray getArray(String name, JSArray defaultValue) {
|
|
307
|
+
Object value = this.data.opt(name);
|
|
308
|
+
if (value == null) {
|
|
309
|
+
return defaultValue;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (value instanceof JSONArray) {
|
|
313
|
+
try {
|
|
314
|
+
JSONArray valueArray = (JSONArray) value;
|
|
315
|
+
List<Object> items = new ArrayList<>();
|
|
316
|
+
for (int i = 0; i < valueArray.length(); i++) {
|
|
317
|
+
items.add(valueArray.get(i));
|
|
318
|
+
}
|
|
319
|
+
return new JSArray(items.toArray());
|
|
320
|
+
} catch (JSONException ex) {
|
|
321
|
+
return defaultValue;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return defaultValue;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* @param name of the option to check
|
|
329
|
+
* @return boolean indicating if the plugin call has an option for the provided name.
|
|
330
|
+
* @deprecated Presence of a key should not be considered significant.
|
|
331
|
+
* Use typed accessors to check the value instead.
|
|
332
|
+
*/
|
|
333
|
+
@Deprecated
|
|
334
|
+
public boolean hasOption(String name) {
|
|
335
|
+
return this.data.has(name);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Indicate that the Bridge should cache this call in order to call
|
|
340
|
+
* it again later. For example, the addListener system uses this to
|
|
341
|
+
* continuously call the call's callback (😆).
|
|
342
|
+
* @deprecated use {@link #setKeepAlive(Boolean)} instead
|
|
343
|
+
*/
|
|
344
|
+
@Deprecated
|
|
345
|
+
public void save() {
|
|
346
|
+
setKeepAlive(true);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Indicate that the Bridge should cache this call in order to call
|
|
351
|
+
* it again later. For example, the addListener system uses this to
|
|
352
|
+
* continuously call the call's callback.
|
|
353
|
+
*
|
|
354
|
+
* @param keepAlive whether to keep the callback saved
|
|
355
|
+
*/
|
|
356
|
+
public void setKeepAlive(Boolean keepAlive) {
|
|
357
|
+
this.keepAlive = keepAlive;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
public void release(Bridge bridge) {
|
|
361
|
+
this.keepAlive = false;
|
|
362
|
+
bridge.releaseCall(this);
|
|
363
|
+
this.isReleased = true;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @deprecated use {@link #isKeptAlive()}
|
|
368
|
+
* @return true if the plugin call is kept alive
|
|
369
|
+
*/
|
|
370
|
+
@Deprecated
|
|
371
|
+
public boolean isSaved() {
|
|
372
|
+
return isKeptAlive();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Gets the keepAlive value of the plugin call
|
|
377
|
+
* @return true if the plugin call is kept alive
|
|
378
|
+
*/
|
|
379
|
+
public boolean isKeptAlive() {
|
|
380
|
+
return keepAlive;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
@Deprecated
|
|
384
|
+
public boolean isReleased() {
|
|
385
|
+
return isReleased;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
class PluginCallDataTypeException extends Exception {
|
|
389
|
+
|
|
390
|
+
PluginCallDataTypeException(String m) {
|
|
391
|
+
super(m);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
package com.getcapacitor;
|
|
2
|
+
|
|
3
|
+
import com.getcapacitor.util.JSONUtils;
|
|
4
|
+
import org.json.JSONObject;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents the configuration options for plugins used by Capacitor
|
|
8
|
+
*/
|
|
9
|
+
public class PluginConfig {
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The object containing plugin config values.
|
|
13
|
+
*/
|
|
14
|
+
private final JSONObject config;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Constructs a PluginsConfig with the provided JSONObject value.
|
|
18
|
+
*
|
|
19
|
+
* @param config A plugin configuration expressed as a JSON Object
|
|
20
|
+
*/
|
|
21
|
+
PluginConfig(JSONObject config) {
|
|
22
|
+
this.config = config;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get a string value for a plugin in the Capacitor config.
|
|
27
|
+
*
|
|
28
|
+
* @param configKey The key of the value to retrieve
|
|
29
|
+
* @return The value from the config, if exists. Null if not
|
|
30
|
+
*/
|
|
31
|
+
public String getString(String configKey) {
|
|
32
|
+
return getString(configKey, null);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Get a string value for a plugin in the Capacitor config.
|
|
37
|
+
*
|
|
38
|
+
* @param configKey The key of the value to retrieve
|
|
39
|
+
* @param defaultValue A default value to return if the key does not exist in the config
|
|
40
|
+
* @return The value from the config, if key exists. Default value returned if not
|
|
41
|
+
*/
|
|
42
|
+
public String getString(String configKey, String defaultValue) {
|
|
43
|
+
return JSONUtils.getString(config, configKey, defaultValue);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Get a boolean value for a plugin in the Capacitor config.
|
|
48
|
+
*
|
|
49
|
+
* @param configKey The key of the value to retrieve
|
|
50
|
+
* @param defaultValue A default value to return if the key does not exist in the config
|
|
51
|
+
* @return The value from the config, if key exists. Default value returned if not
|
|
52
|
+
*/
|
|
53
|
+
public boolean getBoolean(String configKey, boolean defaultValue) {
|
|
54
|
+
return JSONUtils.getBoolean(config, configKey, defaultValue);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Get an integer value for a plugin in the Capacitor config.
|
|
59
|
+
*
|
|
60
|
+
* @param configKey The key of the value to retrieve
|
|
61
|
+
* @param defaultValue A default value to return if the key does not exist in the config
|
|
62
|
+
* @return The value from the config, if key exists. Default value returned if not
|
|
63
|
+
*/
|
|
64
|
+
public int getInt(String configKey, int defaultValue) {
|
|
65
|
+
return JSONUtils.getInt(config, configKey, defaultValue);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get a string array value for a plugin in the Capacitor config.
|
|
70
|
+
*
|
|
71
|
+
* @param configKey The key of the value to retrieve
|
|
72
|
+
* @return The value from the config, if exists. Null if not
|
|
73
|
+
*/
|
|
74
|
+
public String[] getArray(String configKey) {
|
|
75
|
+
return getArray(configKey, null);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get a string array value for a plugin in the Capacitor config.
|
|
80
|
+
*
|
|
81
|
+
* @param configKey The key of the value to retrieve
|
|
82
|
+
* @param defaultValue A default value to return if the key does not exist in the config
|
|
83
|
+
* @return The value from the config, if key exists. Default value returned if not
|
|
84
|
+
*/
|
|
85
|
+
public String[] getArray(String configKey, String[] defaultValue) {
|
|
86
|
+
return JSONUtils.getArray(config, configKey, defaultValue);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get a JSON object value for a plugin in the Capacitor config.
|
|
91
|
+
*
|
|
92
|
+
* @param configKey The key of the value to retrieve
|
|
93
|
+
* @return The value from the config, if exists. Null if not
|
|
94
|
+
*/
|
|
95
|
+
public JSONObject getObject(String configKey) {
|
|
96
|
+
return JSONUtils.getObject(config, configKey);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Check if the PluginConfig is empty.
|
|
101
|
+
*
|
|
102
|
+
* @return true if the plugin config has no entries
|
|
103
|
+
*/
|
|
104
|
+
public boolean isEmpty() {
|
|
105
|
+
return config.length() == 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Gets the JSON Object containing the config of the the provided plugin ID.
|
|
110
|
+
*
|
|
111
|
+
* @return The config for that plugin
|
|
112
|
+
*/
|
|
113
|
+
public JSONObject getConfigJSON() {
|
|
114
|
+
return config;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
package com.getcapacitor;
|
|
2
|
+
|
|
3
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
4
|
+
import java.lang.reflect.InvocationTargetException;
|
|
5
|
+
import java.lang.reflect.Method;
|
|
6
|
+
import java.util.Collection;
|
|
7
|
+
import java.util.HashMap;
|
|
8
|
+
import java.util.Map;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* PluginHandle is an instance of a plugin that has been registered
|
|
12
|
+
* and indexed. Think of it as a Plugin instance with extra metadata goodies
|
|
13
|
+
*/
|
|
14
|
+
public class PluginHandle {
|
|
15
|
+
|
|
16
|
+
private final Bridge bridge;
|
|
17
|
+
private final Class<? extends Plugin> pluginClass;
|
|
18
|
+
|
|
19
|
+
private final Map<String, PluginMethodHandle> pluginMethods = new HashMap<>();
|
|
20
|
+
|
|
21
|
+
private final String pluginId;
|
|
22
|
+
|
|
23
|
+
@SuppressWarnings("deprecation")
|
|
24
|
+
private NativePlugin legacyPluginAnnotation;
|
|
25
|
+
|
|
26
|
+
private CapacitorPlugin pluginAnnotation;
|
|
27
|
+
|
|
28
|
+
private Plugin instance;
|
|
29
|
+
|
|
30
|
+
@SuppressWarnings("deprecation")
|
|
31
|
+
private PluginHandle(Class<? extends Plugin> clazz, Bridge bridge) throws InvalidPluginException {
|
|
32
|
+
this.bridge = bridge;
|
|
33
|
+
this.pluginClass = clazz;
|
|
34
|
+
|
|
35
|
+
CapacitorPlugin pluginAnnotation = pluginClass.getAnnotation(CapacitorPlugin.class);
|
|
36
|
+
if (pluginAnnotation == null) {
|
|
37
|
+
// Check for legacy plugin annotation, @NativePlugin
|
|
38
|
+
NativePlugin legacyPluginAnnotation = pluginClass.getAnnotation(NativePlugin.class);
|
|
39
|
+
if (legacyPluginAnnotation == null) {
|
|
40
|
+
throw new InvalidPluginException("No @CapacitorPlugin annotation found for plugin " + pluginClass.getName());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!legacyPluginAnnotation.name().equals("")) {
|
|
44
|
+
this.pluginId = legacyPluginAnnotation.name();
|
|
45
|
+
} else {
|
|
46
|
+
this.pluginId = pluginClass.getSimpleName();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.legacyPluginAnnotation = legacyPluginAnnotation;
|
|
50
|
+
} else {
|
|
51
|
+
if (!pluginAnnotation.name().equals("")) {
|
|
52
|
+
this.pluginId = pluginAnnotation.name();
|
|
53
|
+
} else {
|
|
54
|
+
this.pluginId = pluginClass.getSimpleName();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.pluginAnnotation = pluginAnnotation;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.indexMethods(clazz);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public PluginHandle(Bridge bridge, Class<? extends Plugin> pluginClass) throws InvalidPluginException, PluginLoadException {
|
|
64
|
+
this(pluginClass, bridge);
|
|
65
|
+
this.load();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public PluginHandle(Bridge bridge, Plugin plugin) throws InvalidPluginException {
|
|
69
|
+
this(plugin.getClass(), bridge);
|
|
70
|
+
this.loadInstance(plugin);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public Class<? extends Plugin> getPluginClass() {
|
|
74
|
+
return pluginClass;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public String getId() {
|
|
78
|
+
return this.pluginId;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@SuppressWarnings("deprecation")
|
|
82
|
+
public NativePlugin getLegacyPluginAnnotation() {
|
|
83
|
+
return this.legacyPluginAnnotation;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public CapacitorPlugin getPluginAnnotation() {
|
|
87
|
+
return this.pluginAnnotation;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public Plugin getInstance() {
|
|
91
|
+
return this.instance;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public Collection<PluginMethodHandle> getMethods() {
|
|
95
|
+
return this.pluginMethods.values();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public Plugin load() throws PluginLoadException {
|
|
99
|
+
if (this.instance != null) {
|
|
100
|
+
return this.instance;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
this.instance = this.pluginClass.getDeclaredConstructor().newInstance();
|
|
105
|
+
return this.loadInstance(instance);
|
|
106
|
+
} catch (Exception ex) {
|
|
107
|
+
throw new PluginLoadException("Unable to load plugin instance. Ensure plugin is publicly accessible");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public Plugin loadInstance(Plugin plugin) {
|
|
112
|
+
this.instance = plugin;
|
|
113
|
+
this.instance.setPluginHandle(this);
|
|
114
|
+
this.instance.setBridge(this.bridge);
|
|
115
|
+
this.instance.load();
|
|
116
|
+
this.instance.initializeActivityLaunchers();
|
|
117
|
+
return this.instance;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Call a method on a plugin.
|
|
122
|
+
* @param methodName the name of the method to call
|
|
123
|
+
* @param call the constructed PluginCall with parameters from the caller
|
|
124
|
+
* @throws InvalidPluginMethodException if no method was found on that plugin
|
|
125
|
+
*/
|
|
126
|
+
public void invoke(String methodName, PluginCall call)
|
|
127
|
+
throws PluginLoadException, InvalidPluginMethodException, InvocationTargetException, IllegalAccessException {
|
|
128
|
+
if (this.instance == null) {
|
|
129
|
+
// Can throw PluginLoadException
|
|
130
|
+
this.load();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
PluginMethodHandle methodMeta = pluginMethods.get(methodName);
|
|
134
|
+
if (methodMeta == null) {
|
|
135
|
+
throw new InvalidPluginMethodException("No method " + methodName + " found for plugin " + pluginClass.getName());
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
methodMeta.getMethod().invoke(this.instance, call);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Index all the known callable methods for a plugin for faster
|
|
143
|
+
* invocation later
|
|
144
|
+
*/
|
|
145
|
+
private void indexMethods(Class<? extends Plugin> plugin) {
|
|
146
|
+
//Method[] methods = pluginClass.getDeclaredMethods();
|
|
147
|
+
Method[] methods = pluginClass.getMethods();
|
|
148
|
+
|
|
149
|
+
for (Method methodReflect : methods) {
|
|
150
|
+
PluginMethod method = methodReflect.getAnnotation(PluginMethod.class);
|
|
151
|
+
|
|
152
|
+
if (method == null) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
PluginMethodHandle methodMeta = new PluginMethodHandle(methodReflect, method);
|
|
157
|
+
pluginMethods.put(methodReflect.getName(), methodMeta);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
package com.getcapacitor;
|
|
2
|
+
|
|
3
|
+
class PluginInvocationException extends Exception {
|
|
4
|
+
|
|
5
|
+
public PluginInvocationException(String s) {
|
|
6
|
+
super(s);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public PluginInvocationException(Throwable t) {
|
|
10
|
+
super(t);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public PluginInvocationException(String s, Throwable t) {
|
|
14
|
+
super(s, t);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
package com.getcapacitor;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Thrown when a plugin fails to instantiate
|
|
5
|
+
*/
|
|
6
|
+
public class PluginLoadException extends Exception {
|
|
7
|
+
|
|
8
|
+
public PluginLoadException(String s) {
|
|
9
|
+
super(s);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public PluginLoadException(Throwable t) {
|
|
13
|
+
super(t);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public PluginLoadException(String s, Throwable t) {
|
|
17
|
+
super(s, t);
|
|
18
|
+
}
|
|
19
|
+
}
|