This is an automated email from the ASF dual-hosted git repository. normanbreau pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/cordova-plugin-inappbrowser.git
The following commit(s) were added to refs/heads/master by this push: new b18b979 feat(android): Download event (#1019) b18b979 is described below commit b18b9794a8f4e79dfe308a7d83c4aee694b252b3 Author: Norman Breau <nor...@nbsolutions.ca> AuthorDate: Fri Sep 15 00:23:22 2023 -0300 feat(android): Download event (#1019) * feat(android): Added download event * android typos, whitespaces and whiteline corrected * Update README.md * fix: removed added whitespace trail --------- Co-authored-by: Shaikh Amaan FM <thisisamaa...@gmail.com> Co-authored-by: Shaikh Amaan FM <53618794+shaikh-amaan...@users.noreply.github.com> --- README.md | 35 ++++++++++++++++++++++++++++++----- src/android/InAppBrowser.java | 28 +++++++++++++++++++++++++++- www/inappbrowser.js | 7 ++++++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ed196cf..6703f5c 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,7 @@ The object returned from a call to `cordova.InAppBrowser.open` when the target i - __exit__: event fires when the `InAppBrowser` window is closed. - __beforeload__: event fires when the `InAppBrowser` decides whether to load an URL or not (only with option `beforeload` set). - __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview. + - __download__: _(Android Only)_ event fires when the `InAppBrowser` loads a URL that leads in downloading of a file. - __callback__: the function that executes when the event fires. The function is passed an `InAppBrowserEvent` object as a parameter. @@ -321,20 +322,43 @@ function messageCallBack(params){ } ``` +#### Download event Example + +Whenever the InAppBrowser receives or locates to a url which leads in downloading a file, the callback assigned to the "download" event is called. The parameter passed to this callback is an object with the the following properties + +- **type** _it contains the String value "download" always_ +- **url** _The url that leaded to the downloading of file. Basically, the download link of file_ +- **userAgent** _User Agent of the webview_ +- **contentDisposition** _If the url contains "content-disposition" header, then this property holds the value of that field else this field is empty_ +- **contentLength** _If the link of the file allows to obtain file size then this property holds the file size else it contains int value 0_ +- **mimetype** _The MIME type of the file_ + +``` + +function downloadListener(params){ + var url = params.url; + var mimetype = params.mimetype; + + var xhr = new XMLHttpRequest(); + xhr.open("GET", params.url); + xhr.onload = function() { + var content = xhr.responseText; + }; + xhr.send(); + +} + +``` + ### InAppBrowserEvent Properties - __type__: the eventname, either `loadstart`, `loadstop`, `loaderror`, `message` or `exit`. _(String)_ - - __url__: the URL that was loaded. _(String)_ - - __code__: the error code, only in the case of `loaderror`. _(Number)_ - - __message__: the error message, only in the case of `loaderror`. _(String)_ - - __data__: the message contents , only in the case of `message`. A stringified JSON object. _(String)_ - ### Supported Platforms - Android @@ -371,6 +395,7 @@ function messageCallBack(params){ - __loaderror__: event fires when the `InAppBrowser` encounters an error loading a URL. - __exit__: event fires when the `InAppBrowser` window is closed. - __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview. + - __download__: _(Android only)_ event fires when the `InAppBrowser` loads a URL that leads in downloading of a file. - __callback__: the function to execute when the event fires. The function is passed an `InAppBrowserEvent` object. diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index 76dc150..eed5950 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -54,6 +54,7 @@ import android.webkit.WebResourceRequest; import android.webkit.WebResourceResponse; import android.webkit.WebSettings; import android.webkit.WebView; +import android.webkit.DownloadListener; import android.webkit.WebViewClient; import android.widget.EditText; import android.widget.ImageButton; @@ -97,6 +98,7 @@ public class InAppBrowser extends CordovaPlugin { private static final String LOAD_START_EVENT = "loadstart"; private static final String LOAD_STOP_EVENT = "loadstop"; private static final String LOAD_ERROR_EVENT = "loaderror"; + private static final String DOWNLOAD_EVENT = "download"; private static final String MESSAGE_EVENT = "message"; private static final String CLEAR_ALL_CACHE = "clearcache"; private static final String CLEAR_SESSION_CACHE = "clearsessioncache"; @@ -272,6 +274,7 @@ public class InAppBrowser extends CordovaPlugin { ((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false; } inAppWebView.loadUrl(url); + } }); } @@ -913,7 +916,6 @@ public class InAppBrowser extends CordovaPlugin { View footerClose = createCloseButton(7); footer.addView(footerClose); - // WebView inAppWebView = new WebView(cordova.getActivity()); inAppWebView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); @@ -946,6 +948,30 @@ public class InAppBrowser extends CordovaPlugin { settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setBuiltInZoomControls(showZoomControls); settings.setPluginState(android.webkit.WebSettings.PluginState.ON); + + // download event + + inAppWebView.setDownloadListener( + new DownloadListener(){ + public void onDownloadStart( + String url, String userAgent, String contentDisposition, String mimetype, long contentLength + ){ + try{ + JSONObject succObj = new JSONObject(); + succObj.put("type", DOWNLOAD_EVENT); + succObj.put("url",url); + succObj.put("userAgent",userAgent); + succObj.put("contentDisposition",contentDisposition); + succObj.put("mimetype",mimetype); + succObj.put("contentLength",contentLength); + sendUpdate(succObj, true); + } + catch(Exception e){ + LOG.e(LOG_TAG,e.getMessage()); + } + } + } + ); // Add postMessage interface class JsObject { diff --git a/www/inappbrowser.js b/www/inappbrowser.js index 3dcab21..889eee9 100644 --- a/www/inappbrowser.js +++ b/www/inappbrowser.js @@ -33,7 +33,8 @@ loaderror: channel.create('loaderror'), exit: channel.create('exit'), customscheme: channel.create('customscheme'), - message: channel.create('message') + message: channel.create('message'), + download: channel.create('download') }; } @@ -89,6 +90,10 @@ } else { throw new Error('insertCSS requires exactly one of code or file to be specified'); } + }, + + addDownloadListener: function (success, error) { + exec(success, error, 'InAppBrowser', 'downloadListener'); } }; --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org