Skip to content

Commit b18b979

Browse files
feat(android): Download event (apache#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 <[email protected]> Co-authored-by: Shaikh Amaan FM <[email protected]>
1 parent ac16f78 commit b18b979

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ The object returned from a call to `cordova.InAppBrowser.open` when the target i
221221
- __exit__: event fires when the `InAppBrowser` window is closed.
222222
- __beforeload__: event fires when the `InAppBrowser` decides whether to load an URL or not (only with option `beforeload` set).
223223
- __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview.
224+
- __download__: _(Android Only)_ event fires when the `InAppBrowser` loads a URL that leads in downloading of a file.
224225

225226
- __callback__: the function that executes when the event fires. The function is passed an `InAppBrowserEvent` object as a parameter.
226227

@@ -321,20 +322,43 @@ function messageCallBack(params){
321322
}
322323

323324
```
325+
#### Download event Example
326+
327+
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
328+
329+
- **type** _it contains the String value "download" always_
330+
- **url** _The url that leaded to the downloading of file. Basically, the download link of file_
331+
- **userAgent** _User Agent of the webview_
332+
- **contentDisposition** _If the url contains "content-disposition" header, then this property holds the value of that field else this field is empty_
333+
- **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_
334+
- **mimetype** _The MIME type of the file_
335+
336+
```
337+
338+
function downloadListener(params){
339+
var url = params.url;
340+
var mimetype = params.mimetype;
341+
342+
var xhr = new XMLHttpRequest();
343+
xhr.open("GET", params.url);
344+
xhr.onload = function() {
345+
var content = xhr.responseText;
346+
};
347+
xhr.send();
348+
349+
}
350+
351+
```
352+
324353

325354
### InAppBrowserEvent Properties
326355

327356
- __type__: the eventname, either `loadstart`, `loadstop`, `loaderror`, `message` or `exit`. _(String)_
328-
329357
- __url__: the URL that was loaded. _(String)_
330-
331358
- __code__: the error code, only in the case of `loaderror`. _(Number)_
332-
333359
- __message__: the error message, only in the case of `loaderror`. _(String)_
334-
335360
- __data__: the message contents , only in the case of `message`. A stringified JSON object. _(String)_
336361

337-
338362
### Supported Platforms
339363

340364
- Android
@@ -371,6 +395,7 @@ function messageCallBack(params){
371395
- __loaderror__: event fires when the `InAppBrowser` encounters an error loading a URL.
372396
- __exit__: event fires when the `InAppBrowser` window is closed.
373397
- __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview.
398+
- __download__: _(Android only)_ event fires when the `InAppBrowser` loads a URL that leads in downloading of a file.
374399

375400
- __callback__: the function to execute when the event fires.
376401
The function is passed an `InAppBrowserEvent` object.

src/android/InAppBrowser.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Licensed to the Apache Software Foundation (ASF) under one
5454
import android.webkit.WebResourceResponse;
5555
import android.webkit.WebSettings;
5656
import android.webkit.WebView;
57+
import android.webkit.DownloadListener;
5758
import android.webkit.WebViewClient;
5859
import android.widget.EditText;
5960
import android.widget.ImageButton;
@@ -97,6 +98,7 @@ public class InAppBrowser extends CordovaPlugin {
9798
private static final String LOAD_START_EVENT = "loadstart";
9899
private static final String LOAD_STOP_EVENT = "loadstop";
99100
private static final String LOAD_ERROR_EVENT = "loaderror";
101+
private static final String DOWNLOAD_EVENT = "download";
100102
private static final String MESSAGE_EVENT = "message";
101103
private static final String CLEAR_ALL_CACHE = "clearcache";
102104
private static final String CLEAR_SESSION_CACHE = "clearsessioncache";
@@ -272,6 +274,7 @@ public void run() {
272274
((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false;
273275
}
274276
inAppWebView.loadUrl(url);
277+
275278
}
276279
});
277280
}
@@ -913,7 +916,6 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
913916
View footerClose = createCloseButton(7);
914917
footer.addView(footerClose);
915918

916-
917919
// WebView
918920
inAppWebView = new WebView(cordova.getActivity());
919921
inAppWebView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
@@ -946,6 +948,30 @@ public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePath
946948
settings.setJavaScriptCanOpenWindowsAutomatically(true);
947949
settings.setBuiltInZoomControls(showZoomControls);
948950
settings.setPluginState(android.webkit.WebSettings.PluginState.ON);
951+
952+
// download event
953+
954+
inAppWebView.setDownloadListener(
955+
new DownloadListener(){
956+
public void onDownloadStart(
957+
String url, String userAgent, String contentDisposition, String mimetype, long contentLength
958+
){
959+
try{
960+
JSONObject succObj = new JSONObject();
961+
succObj.put("type", DOWNLOAD_EVENT);
962+
succObj.put("url",url);
963+
succObj.put("userAgent",userAgent);
964+
succObj.put("contentDisposition",contentDisposition);
965+
succObj.put("mimetype",mimetype);
966+
succObj.put("contentLength",contentLength);
967+
sendUpdate(succObj, true);
968+
}
969+
catch(Exception e){
970+
LOG.e(LOG_TAG,e.getMessage());
971+
}
972+
}
973+
}
974+
);
949975

950976
// Add postMessage interface
951977
class JsObject {

www/inappbrowser.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
loaderror: channel.create('loaderror'),
3434
exit: channel.create('exit'),
3535
customscheme: channel.create('customscheme'),
36-
message: channel.create('message')
36+
message: channel.create('message'),
37+
download: channel.create('download')
3738
};
3839
}
3940

@@ -89,6 +90,10 @@
8990
} else {
9091
throw new Error('insertCSS requires exactly one of code or file to be specified');
9192
}
93+
},
94+
95+
addDownloadListener: function (success, error) {
96+
exec(success, error, 'InAppBrowser', 'downloadListener');
9297
}
9398
};
9499

0 commit comments

Comments
 (0)