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

Small improvements #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
var downloadManager = require('cordova-plugin-android-downloadmanager.DownloadManager')
```

-- OR --

```js
var downloadManager = cordova.plugins.DownloadManager
```

## API

Please refer to [`DownloadManager`](https://developer.android.com/reference/android/app/DownloadManager.html)
Expand Down Expand Up @@ -101,6 +107,13 @@ Note: Don't include any `file://` prefix in the path.
npm install cordova-plugin-android-downloadmanager
```

-- OR --

```sh
cordova plugin add --save cordova-plugin-android-downloadmanager
```


## License

[ISC](LICENSE)
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"version": "0.5.0",
"description": "Plugin to expose Android's DownloadManager in Javascript",
"main": "www/index.js",
"dependencies": {
"nanoassert": "^1.1.0"
},
"devDependencies": {
"sync-cordova-xml": "^0.3.0"
},
Expand Down
3 changes: 3 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<repo>git+https://github.com/emilbayes/cordova-plugin-android-downloadmanager.git</repo>
<issue>https://github.com/emilbayes/cordova-plugin-android-downloadmanager/issues</issue>
<platform name="android">
<js-module src="www/index.js" name="DownloadManagerPlugin">
<clobbers target="cordova.plugins.DownloadManagerPlugin" />
</js-module>
<source-file src="src/com/github/emilbayes/downloadmanager/DownloadManagerPlugin.java" target-dir="src/com/github/emilbayes/downloadmanager"/>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="DownloadManagerPlugin">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ private static JSONArray JSONFromCursor(Cursor cursor) throws JSONException {
rowObject.put("title", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)));
rowObject.put("description", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION)));
rowObject.put("mediaType", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE)));
rowObject.put("localFilename", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)));

if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
rowObject.put("localFilename", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)));
}

rowObject.put("localUri", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
rowObject.put("mediaproviderUri", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIAPROVIDER_URI)));
rowObject.put("uri", cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI)));
Expand Down
21 changes: 12 additions & 9 deletions www/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
var assert = require('nanoassert')
giordanocardillo marked this conversation as resolved.
Show resolved Hide resolved
module.exports = new DownloadManager()

function DownloadManager () {
function DownloadManager() {
}

DownloadManager.prototype.enqueue = function(req, cb) {
if (typeof req === 'string') req = {uri: req}
function assert(assertion, message) {
if (!assertion) throw new Error(message || 'AssertionError')
}

DownloadManager.prototype.enqueue = function (req, cb) {
if (typeof req === 'string') req = { uri: req }
if (req.url) req.uri = req.url

assert(typeof req.uri === 'string', 'req.uri must be string')
Expand All @@ -27,7 +30,7 @@ DownloadManager.prototype.enqueue = function(req, cb) {
exec('enqueue', [req], cb)
}

DownloadManager.prototype.query = function(filter, cb) {
DownloadManager.prototype.query = function (filter, cb) {
if (typeof filter === 'function') {
cb = filter
filter = null
Expand All @@ -40,7 +43,7 @@ DownloadManager.prototype.query = function(filter, cb) {
exec('query', [filter], cb)
}

DownloadManager.prototype.remove = function(ids, cb) {
DownloadManager.prototype.remove = function (ids, cb) {
assert(Array.isArray(ids), 'ids must be array')
assert(cb == null ? true : typeof cb === 'function', 'cb must be function')

Expand All @@ -55,16 +58,16 @@ function exec (method, args, cb) {
if (cb == null) cb = noop
cordova.exec(onsuccess, onerror, 'DownloadManagerPlugin', method, args || [])

function onsuccess () {
function onsuccess() {
var args = Array.prototype.slice.call(arguments)
args.unshift(null)
cb.apply(null, args)
}

function onerror (err) {
function onerror(err) {
err = (err instanceof Error) ? err : new Error(err)
cb(err)
}
}

function noop () {}
function noop() { }