From 254ba1a1b84fc3edba09f2bcc78cced58fd61556 Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Mon, 18 Apr 2022 23:57:11 +0200 Subject: [PATCH] using getWebViewLooper API for Android 28+ --- .../JavaScriptBridgeInterface.java | 4 ++-- .../content_blocker/ContentBlockerHandler.java | 4 ++-- .../in_app_webview/InAppWebView.java | 10 +++++++++- .../types/InAppWebViewInterface.java | 2 ++ example/integration_test/webview_flutter_test.dart | 4 ++++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/JavaScriptBridgeInterface.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/JavaScriptBridgeInterface.java index 547badbd..9556cd52 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/JavaScriptBridgeInterface.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/JavaScriptBridgeInterface.java @@ -35,7 +35,7 @@ public class JavaScriptBridgeInterface { return; } - final Handler handler = new Handler(Looper.getMainLooper()); + final Handler handler = new Handler(inAppWebView.getWebViewLooper()); handler.post(new Runnable() { @Override public void run() { @@ -58,7 +58,7 @@ public class JavaScriptBridgeInterface { // java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. // https://github.com/pichillilorenzo/flutter_inappwebview/issues/98 - final Handler handler = new Handler(Looper.getMainLooper()); + final Handler handler = new Handler(inAppWebView.getWebViewLooper()); handler.post(new Runnable() { @Override public void run() { diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/content_blocker/ContentBlockerHandler.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/content_blocker/ContentBlockerHandler.java index aac86a7e..3b9e279b 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/content_blocker/ContentBlockerHandler.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/content_blocker/ContentBlockerHandler.java @@ -97,7 +97,7 @@ public class ContentBlockerHandler { final String[] webViewUrl = new String[1]; if (!trigger.getLoadType().isEmpty() || !trigger.getIfTopUrl().isEmpty() || !trigger.getUnlessTopUrl().isEmpty()) { final CountDownLatch latch = new CountDownLatch(1); - Handler handler = new Handler(Looper.getMainLooper()); + Handler handler = new Handler(webView.getWebViewLooper()); handler.post(new Runnable() { @Override public void run() { @@ -160,7 +160,7 @@ public class ContentBlockerHandler { " d.addEventListener('DOMContentLoaded', function(event) { hide(); }); " + "})(document);"; - final Handler handler = new Handler(Looper.getMainLooper()); + final Handler handler = new Handler(webView.getWebViewLooper()); handler.post(new Runnable() { @Override public void run() { diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/in_app_webview/InAppWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/in_app_webview/InAppWebView.java index c4afe58f..4a4c0567 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/in_app_webview/InAppWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/in_app_webview/InAppWebView.java @@ -133,7 +133,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie public LinearLayout floatingContextMenu = null; @Nullable public Map contextMenu = null; - public Handler mainLooperHandler = new Handler(Looper.getMainLooper()); + public Handler mainLooperHandler = new Handler(getWebViewLooper()); static Handler mHandler = new Handler(); public Runnable checkScrollStoppedTask; @@ -1672,6 +1672,14 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie webMessageListeners.clear(); } + @Override + public Looper getWebViewLooper() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + return super.getWebViewLooper(); + } + return Looper.getMainLooper(); + } + @Override public void postWebMessage(com.pichillilorenzo.flutter_inappwebview.types.WebMessage message, Uri targetOrigin, ValueCallback callback) throws Exception { throw new UnsupportedOperationException(); diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/InAppWebViewInterface.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/InAppWebViewInterface.java index dd20b474..e11e2651 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/InAppWebViewInterface.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/InAppWebViewInterface.java @@ -3,6 +3,7 @@ package com.pichillilorenzo.flutter_inappwebview.types; import android.content.Context; import android.net.Uri; import android.net.http.SslCertificate; +import android.os.Looper; import android.webkit.ValueCallback; import android.webkit.WebMessage; import android.webkit.WebView; @@ -98,4 +99,5 @@ public interface InAppWebViewInterface { void setWebMessageChannels(Map webMessageChannels); void disposeWebMessageChannels(); void disposeWebMessageListeners(); + Looper getWebViewLooper(); } diff --git a/example/integration_test/webview_flutter_test.dart b/example/integration_test/webview_flutter_test.dart index 47e826fc..e4a01c10 100644 --- a/example/integration_test/webview_flutter_test.dart +++ b/example/integration_test/webview_flutter_test.dart @@ -5271,6 +5271,10 @@ setTimeout(function() { expect(await AndroidInAppWebViewController.getCurrentWebViewPackage(), isNotNull); }, skip: !Platform.isAndroid); + + test('setWebContentsDebuggingEnabled', () async { + expect(AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true), completes); + }, skip: !Platform.isAndroid); }, skip: !Platform.isAndroid); group('ios methods', () {