From 55ded480701ee5a1fcab8f297ffdd3ed050235ff Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 13 Jul 2022 11:24:27 +0800 Subject: [PATCH] android: avoid create View in non-ui thread --- .../pub/doric/engine/DoricWebShellJSExecutor.java | 15 ++++++++------- .../pub/doric/engine/DoricWebViewJSExecutor.java | 12 ++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSExecutor.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSExecutor.java index de12aa4a..354a75d2 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSExecutor.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSExecutor.java @@ -20,6 +20,7 @@ import android.content.Context; import android.os.Build; import android.os.Handler; import android.os.HandlerThread; +import android.os.Looper; import android.text.TextUtils; import android.util.Base64; import android.webkit.ConsoleMessage; @@ -53,7 +54,6 @@ import java.util.concurrent.atomic.AtomicInteger; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; -import pub.doric.BuildConfig; import pub.doric.async.SettableFuture; import pub.doric.utils.DoricLog; import pub.doric.utils.DoricUtils; @@ -314,23 +314,24 @@ public class DoricWebShellJSExecutor implements IDoricJSE { return new WebResourceResponse("text/javascript", "utf-8", inputStream); } }); - readyFuture = new SettableFuture<>(); + HandlerThread webViewHandlerThread = new HandlerThread("DoricWebViewJSExecutor"); webViewHandlerThread.start(); - this.handler = new Handler(webViewHandlerThread.getLooper()); - this.handler.post(new Runnable() { + readyFuture = new SettableFuture<>(); + handler = new Handler(Looper.getMainLooper()); + handler.post(new Runnable() { @Override public void run() { webView = new WebView(context.getApplicationContext()); -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && BuildConfig.DEBUG) { -// WebView.setWebContentsDebuggingEnabled(true); -// } WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webView.setWebChromeClient(new DoricWebChromeClient()); webView.setWebViewClient(new DoricWebViewClient()); WebViewCallback webViewCallback = new WebViewCallback(); webView.addJavascriptInterface(webViewCallback, "NativeClient"); +// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && BuildConfig.DEBUG) { +// WebView.setWebContentsDebuggingEnabled(true); +// } webView.loadUrl(shellUrl + "doric-web.html"); } }); diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java index fb98ccc9..1b8e24c6 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java @@ -21,6 +21,7 @@ import android.content.Context; import android.os.Build; import android.os.Handler; import android.os.HandlerThread; +import android.os.Looper; import android.text.TextUtils; import android.util.Base64; import android.webkit.ConsoleMessage; @@ -45,7 +46,6 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; -import pub.doric.BuildConfig; import pub.doric.async.SettableFuture; import pub.doric.utils.DoricLog; @@ -213,10 +213,13 @@ public class DoricWebViewJSExecutor implements IDoricJSE { public DoricWebViewJSExecutor(final Context context) { HandlerThread webViewHandlerThread = new HandlerThread("DoricWebViewJSExecutor"); webViewHandlerThread.start(); - this.handler = new Handler(webViewHandlerThread.getLooper()); + this.handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { +// if (BuildConfig.DEBUG) { +// WebView.setWebContentsDebuggingEnabled(true); +// } webView = new WebView(context.getApplicationContext()); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); @@ -224,16 +227,13 @@ public class DoricWebViewJSExecutor implements IDoricJSE { webView.loadUrl("about:blank"); WebViewCallback webViewCallback = new WebViewCallback(); webView.addJavascriptInterface(webViewCallback, "NativeClient"); -// if (BuildConfig.DEBUG) { -// WebView.setWebContentsDebuggingEnabled(true); -// } } }); } @Override public String loadJS(final String script, String source) { - handler.post(new Runnable() { + handler.post( new Runnable() { @Override public void run() { webView.evaluateJavascript(script, null);