From 6762b288ba16b4fa2f8734e42714f73076b3185d Mon Sep 17 00:00:00 2001 From: Zaldy Jr Pagaduan Date: Thu, 1 Jun 2023 01:36:50 +0800 Subject: [PATCH] added support for window.open in onCreateWindow --- .../InAppWebViewChromeClient.java | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewChromeClient.java b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewChromeClient.java index de544f972..be94d7cf0 100755 --- a/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewChromeClient.java +++ b/flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewChromeClient.java @@ -31,6 +31,7 @@ import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebView; +import android.webkit.WebViewClient; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.LinearLayout; @@ -625,7 +626,7 @@ public void onCancel(DialogInterface dialog) { } @Override - public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, final Message resultMsg) { + public boolean onCreateWindow(WebView view, final boolean isDialog, final boolean isUserGesture, final Message resultMsg) { int windowId = 0; if (plugin != null && plugin.inAppWebViewManager != null) { plugin.inAppWebViewManager.windowAutoincrementId++; @@ -647,6 +648,64 @@ public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGest } } } + if(result.getType() == WebView.HitTestResult.UNKNOWN_TYPE) { + WebView targetWebView = new WebView(getActivity()); + targetWebView.setWebViewClient(new WebViewClient(){ + @Override + public boolean shouldOverrideUrlLoading (WebView view, String url) { + int windowId = 0; + if (plugin != null && plugin.inAppWebViewManager != null) { + plugin.inAppWebViewManager.windowAutoincrementId++; + windowId = plugin.inAppWebViewManager.windowAutoincrementId; + } + URLRequest request = new URLRequest(url, "GET", null, null); + CreateWindowAction createWindowAction = new CreateWindowAction( + request, + true, + isUserGesture, + false, + windowId, + isDialog + ); + + if (plugin != null && plugin.inAppWebViewManager != null) { + plugin.inAppWebViewManager.windowWebViewMessages.put(windowId, resultMsg); + } + + if (inAppWebView != null && inAppWebView.channelDelegate != null) { + int finalWindowId = windowId; + inAppWebView.channelDelegate.onCreateWindow(createWindowAction, new WebViewChannelDelegate.CreateWindowCallback() { + @Override + public boolean nonNullSuccess(@NonNull Boolean handledByClient) { + return !handledByClient; + } + + @Override + public void defaultBehaviour(@Nullable Boolean handledByClient) { + if (plugin != null && plugin.inAppWebViewManager != null) { + plugin.inAppWebViewManager.windowWebViewMessages.remove(finalWindowId); + } + } + + @Override + public void error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails) { + Log.e(LOG_TAG, errorCode + ", " + ((errorMessage != null) ? errorMessage : "")); + defaultBehaviour(null); + } + }); + + return true; + } + + return true; + + } + }); + WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj; + transport.setWebView(targetWebView); + resultMsg.sendToTarget(); + return true; + } URLRequest request = new URLRequest(url, "GET", null, null); CreateWindowAction createWindowAction = new CreateWindowAction(