Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added download event #743

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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_
breautek marked this conversation as resolved.
Show resolved Hide resolved
- **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
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 28 additions & 2 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Licensed to the Apache Software Foundation (ASF) under one
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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -272,6 +274,7 @@ public void run() {
((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false;
}
inAppWebView.loadUrl(url);

}
});
}
Expand Down Expand Up @@ -913,7 +916,6 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
View footerClose = createCloseButton(7);
footer.addView(footerClose);


// WebView
inAppWebView = new WebView(cordova.getActivity());
inAppWebView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Expand Down Expand Up @@ -946,6 +948,30 @@ public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePath
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 {
Expand Down Expand Up @@ -1067,7 +1093,7 @@ private void sendUpdate(JSONObject obj, boolean keepCallback) {
private void sendUpdate(JSONObject obj, boolean keepCallback, PluginResult.Status status) {
if (callbackContext != null) {
PluginResult result = new PluginResult(status, obj);
result.setKeepCallback(keepCallback);
result.setKeepCallback(keepCallback);
shaikh-amaan-fm marked this conversation as resolved.
Show resolved Hide resolved
callbackContext.sendPluginResult(result);
if (!keepCallback) {
callbackContext = null;
Expand Down
15 changes: 8 additions & 7 deletions www/inappbrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@

function InAppBrowser () {
this.channels = {
beforeload: channel.create('beforeload'),
loadstart: channel.create('loadstart'),
loadstop: channel.create('loadstop'),
loaderror: channel.create('loaderror'),
exit: channel.create('exit'),
customscheme: channel.create('customscheme'),
message: channel.create('message')
'beforeload': channel.create('beforeload'),

Check failure on line 30 in www/inappbrowser.js

View workflow job for this annotation

GitHub Actions / Lint Test

Unnecessarily quoted property 'beforeload' found
'loadstart': channel.create('loadstart'),

Check failure on line 31 in www/inappbrowser.js

View workflow job for this annotation

GitHub Actions / Lint Test

Unnecessarily quoted property 'loadstart' found
'loadstop': channel.create('loadstop'),

Check failure on line 32 in www/inappbrowser.js

View workflow job for this annotation

GitHub Actions / Lint Test

Unnecessarily quoted property 'loadstop' found
'loaderror': channel.create('loaderror'),

Check failure on line 33 in www/inappbrowser.js

View workflow job for this annotation

GitHub Actions / Lint Test

Unnecessarily quoted property 'loaderror' found
'exit': channel.create('exit'),

Check failure on line 34 in www/inappbrowser.js

View workflow job for this annotation

GitHub Actions / Lint Test

Unnecessarily quoted property 'exit' found
'customscheme': channel.create('customscheme'),

Check failure on line 35 in www/inappbrowser.js

View workflow job for this annotation

GitHub Actions / Lint Test

Unnecessarily quoted property 'customscheme' found
'message': channel.create('message'),

Check failure on line 36 in www/inappbrowser.js

View workflow job for this annotation

GitHub Actions / Lint Test

Unnecessarily quoted property 'message' found
'download': channel.create('download')

Check failure on line 37 in www/inappbrowser.js

View workflow job for this annotation

GitHub Actions / Lint Test

Unnecessarily quoted property 'download' found
};
}

Expand Down
Loading