feat:fix when android excute js on same thread,cause stucked

This commit is contained in:
pengfei.zhou 2021-11-05 16:31:38 +08:00 committed by osborn
parent 5c74729fbc
commit 7282146e14
8 changed files with 50 additions and 22 deletions

View File

@ -74,7 +74,7 @@ public class MainActivity extends AppCompatActivity {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
DoricDev.getInstance(); // DoricDev.getInstance();
} }
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

View File

@ -46,7 +46,7 @@ public class MyApplication extends Application {
DoricSingleton.getInstance().setEnvironmentValue(map); DoricSingleton.getInstance().setEnvironmentValue(map);
} }
}, intentFilter); }, intentFilter);
DoricNativeDriver.getInstance(); // DoricNativeDriver.getInstance();
Doric.enablePerformance(true); Doric.enablePerformance(true);
Doric.enableRenderSnapshot(true); Doric.enableRenderSnapshot(true);
} }

View File

@ -100,8 +100,11 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
protected void initJSEngine() { protected void initJSEngine() {
mDoricJSE = new DoricWebViewJSExecutor(Doric.application()); mDoricJSE = new DoricWebViewJSExecutor(Doric.application());
//mDoricJSE = new DoricNativeJSExecutor();
if (mDoricJSE instanceof DoricWebViewJSExecutor) {
loadBuiltinJS("doric-web.js"); loadBuiltinJS("doric-web.js");
} }
}
public void setEnvironmentValue(Map<String, Object> values) { public void setEnvironmentValue(Map<String, Object> values) {
mEnvironmentMap.putAll(values); mEnvironmentMap.putAll(values);

View File

@ -17,6 +17,9 @@ package pub.doric.engine;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.text.TextUtils; import android.text.TextUtils;
import android.webkit.ConsoleMessage; import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
@ -48,8 +51,9 @@ import pub.doric.utils.DoricLog;
* @CreateDate: 2021/11/3 * @CreateDate: 2021/11/3
*/ */
public class DoricWebViewJSExecutor implements IDoricJSE { public class DoricWebViewJSExecutor implements IDoricJSE {
private final WebView webView; private WebView webView;
private final Map<String, JavaFunction> globalFunctions = new HashMap<>(); private final Map<String, JavaFunction> globalFunctions = new HashMap<>();
private final Handler handler;
private static Object unwrapJSObject(JSONObject jsonObject) { private static Object unwrapJSObject(JSONObject jsonObject) {
String type = jsonObject.optString("type"); String type = jsonObject.optString("type");
@ -127,6 +131,7 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
@JavascriptInterface @JavascriptInterface
public void returnNative(String result) { public void returnNative(String result) {
DoricLog.d("return Native" + result);
if (returnFuture != null) { if (returnFuture != null) {
returnFuture.set(result); returnFuture.set(result);
} }
@ -178,26 +183,39 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
} }
@SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"}) @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
public DoricWebViewJSExecutor(Context context) { public DoricWebViewJSExecutor(final Context context) {
this.webView = new WebView(context.getApplicationContext()); HandlerThread webViewHandlerThread = new HandlerThread("DoricWebViewJSExecutor");
WebSettings webSettings = this.webView.getSettings(); webViewHandlerThread.start();
this.handler = new Handler(webViewHandlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
webView = new WebView(context.getApplicationContext());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true);
this.webView.setWebChromeClient(new DoricWebChromeClient()); webView.setWebChromeClient(new DoricWebChromeClient());
this.webView.loadUrl("about:blank"); webView.loadUrl("about:blank");
WebViewCallback webViewCallback = new WebViewCallback(); WebViewCallback webViewCallback = new WebViewCallback();
this.webView.addJavascriptInterface(webViewCallback, "NativeClient"); webView.addJavascriptInterface(webViewCallback, "NativeClient");
WebView.setWebContentsDebuggingEnabled(true); WebView.setWebContentsDebuggingEnabled(true);
} }
});
}
@Override @Override
public String loadJS(String script, String source) { public String loadJS(final String script, String source) {
this.webView.evaluateJavascript(script, null); handler.post(new Runnable() {
@Override
public void run() {
webView.evaluateJavascript(script, null);
}
});
return null; return null;
} }
@Override @Override
public JSDecoder evaluateJS(String script, String source, boolean hashKey) throws JSRuntimeException { public JSDecoder evaluateJS(String script, String source, boolean hashKey) throws JSRuntimeException {
this.webView.evaluateJavascript(script, null); loadJS(script, source);
return null; return null;
} }
@ -209,7 +227,7 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
@Override @Override
public void injectGlobalJSObject(String name, JavaValue javaValue) { public void injectGlobalJSObject(String name, JavaValue javaValue) {
loadJS(String.format("_injectGlobalObject('%s','%s')", name, javaValue.getValue()), ""); loadJS(String.format("__injectGlobalObject('%s','%s')", name, javaValue.getValue()), "");
} }
@Override @Override
@ -220,13 +238,14 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
jsonArray.put(jsonObject); jsonArray.put(jsonObject);
} }
returnFuture = new SettableFuture<>(); returnFuture = new SettableFuture<>();
String script = String.format( final String script = String.format(
"__invokeMethod('%s','%s','%s')", "__invokeMethod('%s','%s','%s')",
objectName, objectName,
functionName, functionName,
jsonArray.toString()); jsonArray.toString());
loadJS(script, ""); loadJS(script, "");
String result = returnFuture.get(); String result = returnFuture.get();
returnFuture = null;
if (!TextUtils.isEmpty(result)) { if (!TextUtils.isEmpty(result)) {
try { try {
JSONObject jsonObject = new JSONObject(result); JSONObject jsonObject = new JSONObject(result);

View File

@ -93,7 +93,7 @@ function _rawValue(v) {
return undefined; return undefined;
} }
} }
function _injectGlobalObject(name, args) { function __injectGlobalObject(name, args) {
Reflect.set(window, name, JSON.parse(args)); Reflect.set(window, name, JSON.parse(args));
} }
function __injectGlobalFunction(name) { function __injectGlobalFunction(name) {
@ -107,6 +107,7 @@ function __injectGlobalFunction(name) {
}); });
} }
function __invokeMethod(objectName, functionName, stringifiedArgs) { function __invokeMethod(objectName, functionName, stringifiedArgs) {
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
try { try {
const thisObject = Reflect.get(window, objectName); const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName); const thisFunction = Reflect.get(thisObject, functionName);
@ -114,6 +115,7 @@ function __invokeMethod(objectName, functionName, stringifiedArgs) {
const rawArgs = args.map(e => _rawValue(e)); const rawArgs = args.map(e => _rawValue(e));
const ret = Reflect.apply(thisFunction, thisObject, rawArgs); const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : ""; const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : "";
NativeClient.log(`return:${returnVal}`);
NativeClient.returnNative(returnVal); NativeClient.returnNative(returnVal);
} }
catch (e) { catch (e) {

View File

@ -103,7 +103,7 @@ function _rawValue(v: WrappedValue): RawValue {
} }
} }
function _injectGlobalObject(name: string, args: string) { function __injectGlobalObject(name: string, args: string) {
Reflect.set(window, name, JSON.parse(args)); Reflect.set(window, name, JSON.parse(args));
} }
@ -119,6 +119,7 @@ function __injectGlobalFunction(name: string) {
} }
function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string) { function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string) {
// NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`)
try { try {
const thisObject = Reflect.get(window, objectName); const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName); const thisFunction = Reflect.get(thisObject, functionName);
@ -126,6 +127,7 @@ function __invokeMethod(objectName: string, functionName: string, stringifiedArg
const rawArgs = args.map(e => _rawValue(e)); const rawArgs = args.map(e => _rawValue(e));
const ret = Reflect.apply(thisFunction, thisObject, rawArgs); const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : "" const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : ""
// NativeClient.log(`return:${returnVal}`)
NativeClient.returnNative(returnVal) NativeClient.returnNative(returnVal)
} catch (e) { } catch (e) {
NativeClient.log(`error:${e},${(e as any).stack}`) NativeClient.log(`error:${e},${(e as any).stack}`)

View File

@ -23,7 +23,7 @@ declare function _binaryValue(v: RawValue): {
}; };
declare function _wrappedValue(v: RawValue): WrappedValue; declare function _wrappedValue(v: RawValue): WrappedValue;
declare function _rawValue(v: WrappedValue): RawValue; declare function _rawValue(v: WrappedValue): RawValue;
declare function _injectGlobalObject(name: string, args: string): void; declare function __injectGlobalObject(name: string, args: string): void;
declare function __injectGlobalFunction(name: string): void; declare function __injectGlobalFunction(name: string): void;
declare function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string): void; declare function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string): void;
declare function _prepared(): void; declare function _prepared(): void;

View File

@ -91,7 +91,7 @@ function _rawValue(v) {
return undefined; return undefined;
} }
} }
function _injectGlobalObject(name, args) { function __injectGlobalObject(name, args) {
Reflect.set(window, name, JSON.parse(args)); Reflect.set(window, name, JSON.parse(args));
} }
function __injectGlobalFunction(name) { function __injectGlobalFunction(name) {
@ -105,6 +105,7 @@ function __injectGlobalFunction(name) {
}); });
} }
function __invokeMethod(objectName, functionName, stringifiedArgs) { function __invokeMethod(objectName, functionName, stringifiedArgs) {
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
try { try {
const thisObject = Reflect.get(window, objectName); const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName); const thisFunction = Reflect.get(thisObject, functionName);
@ -112,6 +113,7 @@ function __invokeMethod(objectName, functionName, stringifiedArgs) {
const rawArgs = args.map(e => _rawValue(e)); const rawArgs = args.map(e => _rawValue(e));
const ret = Reflect.apply(thisFunction, thisObject, rawArgs); const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : ""; const returnVal = ret ? JSON.stringify(_wrappedValue(ret)) : "";
NativeClient.log(`return:${returnVal}`);
NativeClient.returnNative(returnVal); NativeClient.returnNative(returnVal);
} }
catch (e) { catch (e) {