diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index 2e7fb07..ddc043a 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -36,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; +import android.database.CursorIndexOutOfBoundsException; import android.net.Uri; /** @@ -124,7 +125,7 @@ public void setTimerProgressUpdate(Timer TimerProgressUpdate) { this.timerProgressUpdate = TimerProgressUpdate; }; } - + HashMap activDownloads = new HashMap(); @Override @@ -154,7 +155,16 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws Download curDownload = new Download(args.get(0).toString(), args.get(1).toString(), callbackContext); if (activDownloads.containsKey(curDownload.getUriString())) { - return; + if (args.length() >= 4) { + boolean resumeCurrent = args.getBoolean(3); + + if (resumeCurrent) { + StartProgressTracking(curDownload); + return; + } + } else { + return; + } } activDownloads.put(curDownload.getUriString(), curDownload); @@ -171,7 +181,13 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws DownloadManager mgr = (DownloadManager) this.cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(source); - request.setTitle("org.apache.cordova.backgroundDownload plugin"); + String title = "org.apache.cordova.backgroundDownload plugin"; + + if (args.length() >= 3) { + title = args.get(2).toString(); + } + + request.setTitle(title); request.setVisibleInDownloadsUi(false); // hide notification. Not compatible with current android api. @@ -296,8 +312,7 @@ private void stop(JSONArray args, CallbackContext callbackContext) throws JSONEx return; } - DownloadManager mgr = (DownloadManager) cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE); - mgr.remove(curDownload.getDownloadId()); + CleanUp(curDownload); callbackContext.success(); } @@ -357,7 +372,16 @@ public void onReceive(Context context, Intent intent) { Cursor cursor = mgr.query(query); int idxURI = cursor.getColumnIndex(DownloadManager.COLUMN_URI); cursor.moveToFirst(); - String uri = cursor.getString(idxURI); + String uri; + + try { + uri = cursor.getString(idxURI); + } catch (CursorIndexOutOfBoundsException e) { + // Already removed from the list / list is empty due to cancellation + // Can't do anything else since there's no DL to get + // context from. + return; + } Download curDownload = activDownloads.get(uri); @@ -396,4 +420,5 @@ public void copyTempFileToActualFile(Download curDownload) { curDownload.getCallbackContextDownloadStart().error("Cannot copy from temporary path to actual path"); } } -} \ No newline at end of file +} + diff --git a/www/BackgroundDownloader.js b/www/BackgroundDownloader.js index 874d1ed..296c930 100644 --- a/www/BackgroundDownloader.js +++ b/www/BackgroundDownloader.js @@ -34,9 +34,10 @@ var BackgroundDownloader = function() { * * @param {string} uri The location of the resource. * @param {File} resultFile The file that the response will be written to. + * @param {string} appTitle The title of the app, which will be shown in notification */ -BackgroundDownloader.prototype.createDownload = function(uri, resultFile) { - return new DownloadOperation(uri, resultFile); +BackgroundDownloader.prototype.createDownload = function(uri, resultFile, appTitle) { + return new DownloadOperation(uri, resultFile, appTitle); }; -module.exports = BackgroundDownloader; \ No newline at end of file +module.exports = BackgroundDownloader; diff --git a/www/DownloadOperation.js b/www/DownloadOperation.js index 304b748..ac82eab 100644 --- a/www/DownloadOperation.js +++ b/www/DownloadOperation.js @@ -27,8 +27,9 @@ var exec = require('cordova/exec'), * * @param {string} uri The location of the resource. * @param {File} resultFile The file that the response will be written to. + * @param {string} appTitle the title of the app, will be shown in notification */ -var DownloadOperation = function (uri, resultFile) { +var DownloadOperation = function (uri, resultFile, appTitle) { if (uri == null || resultFile == null) { throw new Error("missing or invalid argument"); @@ -36,6 +37,7 @@ var DownloadOperation = function (uri, resultFile) { this.uri = uri; this.resultFile = resultFile; + this.appTitle = appTitle || "org.apache.cordova.backgroundDownload plugin"; }; /** @@ -60,7 +62,7 @@ DownloadOperation.prototype.startAsync = function() { deferral.reject(err); }; - exec(successCallback, errorCallback, "BackgroundDownload", "startAsync", [this.uri, this.resultFile.toURL()]); + exec(successCallback, errorCallback, "BackgroundDownload", "startAsync", [this.uri, this.resultFile.toURL(), this.appTitle]); // custom mechanism to trigger stop when user cancels pending operation deferral.promise.onCancelled = function () { @@ -79,4 +81,4 @@ DownloadOperation.prototype.stop = function() { }; -module.exports = DownloadOperation; \ No newline at end of file +module.exports = DownloadOperation;