android:webview add some api

This commit is contained in:
pengfei.zhou
2021-11-05 11:59:54 +08:00
committed by osborn
parent 780188e145
commit 196497f3bd
8 changed files with 369 additions and 57 deletions

View File

@@ -25,10 +25,18 @@ import android.webkit.WebSettings;
import android.webkit.WebView;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSRuntimeException;
import com.github.pengfeizhou.jscore.JavaFunction;
import com.github.pengfeizhou.jscore.JavaValue;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import pub.doric.utils.DoricLog;
@@ -39,11 +47,72 @@ import pub.doric.utils.DoricLog;
*/
public class DoricWebViewJSExecutor implements IDoricJSE {
private final WebView webView;
private final Map<String, JavaFunction> globalFunctions = new HashMap<>();
private static Object unwrapJSObject(JSONObject jsonObject) {
String type = jsonObject.optString("type");
switch (type) {
case "number":
return jsonObject.optDouble("value");
case "string":
return jsonObject.optString("value");
case "boolean":
return jsonObject.optBoolean("value");
case "object":
return jsonObject.optJSONObject("value");
case "array":
return jsonObject.optJSONArray("value");
default:
return JSONObject.NULL;
}
}
private static final String WRAPPED_NULL = new JSONBuilder().put("type", "null").toString();
public class WebViewCallback {
@JavascriptInterface
public void callNative(int command, String arguments) {
public String callNative(String name, String arguments) {
JavaFunction javaFunction = globalFunctions.get(name);
if (javaFunction == null) {
return WRAPPED_NULL;
}
try {
JSONArray jsonArray = new JSONArray(arguments);
int length = jsonArray.length();
JSDecoder[] decoders = new JSDecoder[length];
for (int i = 0; i < length; i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
Object object = unwrapJSObject(jsonObject);
decoders[i] = new JavaJSDecoder(object);
}
JavaValue javaValue = javaFunction.exec(decoders);
if (javaValue.getType() == 0) {
return WRAPPED_NULL;
}
if (javaValue.getType() == 1) {
Double value = Double.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "number").put("value", value).toString();
}
if (javaValue.getType() == 2) {
Boolean value = Boolean.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "boolean").put("value", value).toString();
}
if (javaValue.getType() == 3) {
String value = String.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "string").put("value", value).toString();
}
if (javaValue.getType() == 4) {
String value = String.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "object").put("value", value).toString();
}
if (javaValue.getType() == 5) {
String value = String.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "array").put("value", value).toString();
}
} catch (JSONException e) {
e.printStackTrace();
}
return WRAPPED_NULL;
}
}
@@ -73,16 +142,16 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
WebSettings webSettings = this.webView.getSettings();
webSettings.setJavaScriptEnabled(true);
this.webView.setWebChromeClient(new DoricWebChromeClient());
this.webView.loadUrl("https://m.baidu.com");
this.webView.loadUrl("javascript:alert(\"11111\")");
this.webView.loadUrl("about:blank");
WebViewCallback webViewCallback = new WebViewCallback();
this.webView.addJavascriptInterface(webViewCallback, "callNative");
this.webView.addJavascriptInterface(webViewCallback, "NativeClient");
WebView.setWebContentsDebuggingEnabled(true);
}
@Override
public String loadJS(String script, String source) {
this.webView.evaluateJavascript(script, null);
return script;
return null;
}
@Override
@@ -93,7 +162,7 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
@Override
public void injectGlobalJSFunction(String name, JavaFunction javaFunction) {
globalFunctions.put(name, javaFunction);
}
@Override

View File

@@ -0,0 +1,132 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.engine;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSBoolean;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSNull;
import com.github.pengfeizhou.jscore.JSNumber;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSString;
import com.github.pengfeizhou.jscore.JSValue;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Iterator;
/**
* @Description: This is for java runtime.
* @Author: pengfei.zhou
* @CreateDate: 2021/11/5
*/
public class JavaJSDecoder extends JSDecoder {
private final Object value;
private static final JSNull JS_NULL = new JSNull();
public JavaJSDecoder(Object object) {
super(null);
this.value = object;
}
@Override
public boolean readBool() throws ArchiveException {
return (boolean) this.value;
}
@Override
public String readString() throws ArchiveException {
return (String) this.value;
}
@Override
public Double readNumber() {
return (Double) this.value;
}
@Override
public boolean isBool() {
return this.value instanceof Boolean;
}
@Override
public boolean isNULL() {
return this.value == JSONObject.NULL;
}
@Override
public boolean isString() {
return this.value instanceof String;
}
@Override
public boolean isNumber() {
return this.value instanceof Number;
}
@Override
public boolean isArray() {
return this.value instanceof JSONArray;
}
@Override
public boolean isObject() {
return this.value instanceof JSONObject;
}
@Override
public JSValue decode() throws ArchiveException {
if (isBool()) {
return new JSBoolean((Boolean) this.value);
}
if (isString()) {
return new JSString((String) this.value);
}
if (isNumber()) {
return new JSNumber(((Number) this.value).doubleValue());
}
if (isObject()) {
JSONObject jsonObject = (JSONObject) this.value;
Iterator<String> it = jsonObject.keys();
JSObject jsObject = new JSObject();
while (it.hasNext()) {
String key = it.next();
Object o = jsonObject.opt(key);
JavaJSDecoder jsDecoder = new JavaJSDecoder(o);
JSValue jsValue = jsDecoder.decode();
jsObject.setProperty(key, jsValue);
}
return jsObject;
}
if (isArray()) {
JSONArray jsonArray = (JSONArray) this.value;
int length = jsonArray.length();
JSArray jsArray = new JSArray(length);
for (int i = 0; i < length; i++) {
Object o = jsonArray.opt(i);
JavaJSDecoder jsDecoder = new JavaJSDecoder(o);
JSValue jsValue = jsDecoder.decode();
jsArray.put(i, jsValue);
}
return jsArray;
}
return JS_NULL;
}
}