android: avoid create View in non-ui thread

This commit is contained in:
pengfei.zhou 2022-07-13 11:24:27 +08:00 committed by osborn
parent 1f567cb928
commit 55ded48070
2 changed files with 14 additions and 13 deletions

View File

@ -20,6 +20,7 @@ import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.Looper;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.webkit.ConsoleMessage; import android.webkit.ConsoleMessage;
@ -53,7 +54,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import pub.doric.BuildConfig;
import pub.doric.async.SettableFuture; import pub.doric.async.SettableFuture;
import pub.doric.utils.DoricLog; import pub.doric.utils.DoricLog;
import pub.doric.utils.DoricUtils; import pub.doric.utils.DoricUtils;
@ -314,23 +314,24 @@ public class DoricWebShellJSExecutor implements IDoricJSE {
return new WebResourceResponse("text/javascript", "utf-8", inputStream); return new WebResourceResponse("text/javascript", "utf-8", inputStream);
} }
}); });
readyFuture = new SettableFuture<>();
HandlerThread webViewHandlerThread = new HandlerThread("DoricWebViewJSExecutor"); HandlerThread webViewHandlerThread = new HandlerThread("DoricWebViewJSExecutor");
webViewHandlerThread.start(); webViewHandlerThread.start();
this.handler = new Handler(webViewHandlerThread.getLooper()); readyFuture = new SettableFuture<>();
this.handler.post(new Runnable() { handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
webView = new WebView(context.getApplicationContext()); webView = new WebView(context.getApplicationContext());
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && BuildConfig.DEBUG) {
// WebView.setWebContentsDebuggingEnabled(true);
// }
WebSettings webSettings = webView.getSettings(); WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new DoricWebChromeClient()); webView.setWebChromeClient(new DoricWebChromeClient());
webView.setWebViewClient(new DoricWebViewClient()); webView.setWebViewClient(new DoricWebViewClient());
WebViewCallback webViewCallback = new WebViewCallback(); WebViewCallback webViewCallback = new WebViewCallback();
webView.addJavascriptInterface(webViewCallback, "NativeClient"); webView.addJavascriptInterface(webViewCallback, "NativeClient");
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && BuildConfig.DEBUG) {
// WebView.setWebContentsDebuggingEnabled(true);
// }
webView.loadUrl(shellUrl + "doric-web.html"); webView.loadUrl(shellUrl + "doric-web.html");
} }
}); });

View File

@ -21,6 +21,7 @@ import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.Looper;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.webkit.ConsoleMessage; import android.webkit.ConsoleMessage;
@ -45,7 +46,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import pub.doric.BuildConfig;
import pub.doric.async.SettableFuture; import pub.doric.async.SettableFuture;
import pub.doric.utils.DoricLog; import pub.doric.utils.DoricLog;
@ -213,10 +213,13 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
public DoricWebViewJSExecutor(final Context context) { public DoricWebViewJSExecutor(final Context context) {
HandlerThread webViewHandlerThread = new HandlerThread("DoricWebViewJSExecutor"); HandlerThread webViewHandlerThread = new HandlerThread("DoricWebViewJSExecutor");
webViewHandlerThread.start(); webViewHandlerThread.start();
this.handler = new Handler(webViewHandlerThread.getLooper()); this.handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() { handler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
// if (BuildConfig.DEBUG) {
// WebView.setWebContentsDebuggingEnabled(true);
// }
webView = new WebView(context.getApplicationContext()); webView = new WebView(context.getApplicationContext());
WebSettings webSettings = webView.getSettings(); WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true);
@ -224,16 +227,13 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
webView.loadUrl("about:blank"); webView.loadUrl("about:blank");
WebViewCallback webViewCallback = new WebViewCallback(); WebViewCallback webViewCallback = new WebViewCallback();
webView.addJavascriptInterface(webViewCallback, "NativeClient"); webView.addJavascriptInterface(webViewCallback, "NativeClient");
// if (BuildConfig.DEBUG) {
// WebView.setWebContentsDebuggingEnabled(true);
// }
} }
}); });
} }
@Override @Override
public String loadJS(final String script, String source) { public String loadJS(final String script, String source) {
handler.post(new Runnable() { handler.post( new Runnable() {
@Override @Override
public void run() { public void run() {
webView.evaluateJavascript(script, null); webView.evaluateJavascript(script, null);