From 8f43faf832ea63f2555ed9a4c5004227bd4d479b Mon Sep 17 00:00:00 2001 From: ashank96 Date: Thu, 17 Feb 2022 15:00:46 +0530 Subject: [PATCH] expose contentdisposition and contentlength from android --- .../in_app_webview/InAppWebView.java | 4 ++++ example/integration_test/webview_flutter_test.dart | 2 +- lib/src/in_app_browser/in_app_browser.dart | 2 +- lib/src/in_app_webview/headless_in_app_webview.dart | 8 +++++++- lib/src/in_app_webview/in_app_webview.dart | 2 +- lib/src/in_app_webview/in_app_webview_controller.dart | 9 +++++++-- lib/src/in_app_webview/webview.dart | 2 +- 7 files changed, 22 insertions(+), 7 deletions(-) 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 dcde9159..3af8104e 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 @@ -1184,6 +1184,10 @@ final public class InAppWebView extends InputAwareWebView { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Map obj = new HashMap<>(); obj.put("url", url); + obj.put("userAgent", userAgent); + obj.put("contentDisposition", contentDisposition); + obj.put("mimetype", mimetype); + obj.put("contentLength", contentLength); channel.invokeMethod("onDownloadStart", obj); } } diff --git a/example/integration_test/webview_flutter_test.dart b/example/integration_test/webview_flutter_test.dart index ac02f42f..3a79f1ca 100644 --- a/example/integration_test/webview_flutter_test.dart +++ b/example/integration_test/webview_flutter_test.dart @@ -2739,7 +2739,7 @@ void main() { onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, - onDownloadStart: (controller, url) { + onDownloadStart:(controller, url, userAgent, contentDisposition, mimeType, contentLength) { onDownloadStartCompleter.complete(url.toString()); }, ), diff --git a/lib/src/in_app_browser/in_app_browser.dart b/lib/src/in_app_browser/in_app_browser.dart index ff4e2877..3ac507cf 100755 --- a/lib/src/in_app_browser/in_app_browser.dart +++ b/lib/src/in_app_browser/in_app_browser.dart @@ -377,7 +377,7 @@ class InAppBrowser { ///**Official Android API**: https://developer.android.com/reference/android/webkit/WebView#setDownloadListener(android.webkit.DownloadListener) /// ///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455643-webview - void onDownloadStart(Uri url) {} + void onDownloadStart(Uri url, String userAgent, String contentDisposition, String mimeType, int contentLength) {} ///Event fired when the [InAppBrowser] webview finds the `custom-scheme` while loading a resource. Here you can handle the url request and return a [CustomSchemeResponse] to load a specific resource encoded to `base64`. /// diff --git a/lib/src/in_app_webview/headless_in_app_webview.dart b/lib/src/in_app_webview/headless_in_app_webview.dart index f19b5152..e6939550 100644 --- a/lib/src/in_app_webview/headless_in_app_webview.dart +++ b/lib/src/in_app_webview/headless_in_app_webview.dart @@ -299,7 +299,13 @@ class HeadlessInAppWebView implements WebView { void Function(InAppWebViewController controller)? onWindowBlur; @override - void Function(InAppWebViewController controller, Uri url)? onDownloadStart; + void Function( + InAppWebViewController controller, + Uri url, + String userAgent, + String contentDisposition, + String mimeType, + int contentLength)? onDownloadStart; @override void Function(InAppWebViewController controller, int activeMatchOrdinal, diff --git a/lib/src/in_app_webview/in_app_webview.dart b/lib/src/in_app_webview/in_app_webview.dart index 7ef7ee2f..4926f9d0 100755 --- a/lib/src/in_app_webview/in_app_webview.dart +++ b/lib/src/in_app_webview/in_app_webview.dart @@ -208,7 +208,7 @@ class InAppWebView extends StatefulWidget implements WebView { androidOnReceivedTouchIconUrl; @override - final void Function(InAppWebViewController controller, Uri url)? + final void Function(InAppWebViewController controller, Uri url, String userAgent, String contentDisposition, String mimeType, int contentLength)? onDownloadStart; @override diff --git a/lib/src/in_app_webview/in_app_webview_controller.dart b/lib/src/in_app_webview/in_app_webview_controller.dart index d34072bb..fc8090c3 100644 --- a/lib/src/in_app_webview/in_app_webview_controller.dart +++ b/lib/src/in_app_webview/in_app_webview_controller.dart @@ -205,11 +205,16 @@ class InAppWebViewController { if ((_webview != null && _webview!.onDownloadStart != null) || _inAppBrowser != null) { String url = call.arguments["url"]; + String userAgent = call.arguments["userAgent"]; + String contentDisposition = call.arguments["contentDisposition"]; + String mimeType = call.arguments["mimetype"]; + int contentLength = call.arguments["contentLength"] as int; Uri uri = Uri.parse(url); + if (_webview != null && _webview!.onDownloadStart != null) - _webview!.onDownloadStart!(this, uri); + _webview!.onDownloadStart!(this, uri, userAgent, contentDisposition, mimeType, contentLength); else - _inAppBrowser!.onDownloadStart(uri); + _inAppBrowser!.onDownloadStart(uri, userAgent, contentDisposition, mimeType, contentLength); } break; case "onLoadResourceCustomScheme": diff --git a/lib/src/in_app_webview/webview.dart b/lib/src/in_app_webview/webview.dart index c483acf1..710da3c3 100644 --- a/lib/src/in_app_webview/webview.dart +++ b/lib/src/in_app_webview/webview.dart @@ -120,7 +120,7 @@ abstract class WebView { ///**Official Android API**: https://developer.android.com/reference/android/webkit/WebView#setDownloadListener(android.webkit.DownloadListener) /// ///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455643-webview - final void Function(InAppWebViewController controller, Uri url)? + final void Function(InAppWebViewController controller, Uri url, String userAgent, String contentDisposition, String mimeType, int contentLength)? onDownloadStart; ///Event fired when the [WebView] finds the `custom-scheme` while loading a resource. Here you can handle the url request and return a [CustomSchemeResponse] to load a specific resource encoded to `base64`.