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 <thisisamaan.s@gmail.com> Co-authored-by: Shaikh Amaan FM <53618794+shaikh-amaan-fm@users.noreply.github.com>
This commit is contained in:
parent
ac16f78b15
commit
b18b9794a8
35
README.md
35
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.
|
||||
|
@ -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 {
|
||||
|
@ -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');
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user