From 32c5103579c0219a12ec8859a8a6f63aed5c4cb4 Mon Sep 17 00:00:00 2001 From: Errietta Kostala Date: Sat, 26 Dec 2015 12:56:50 +0000 Subject: [PATCH 1/6] Fix exception when cancelling dl within app --- src/android/BackgroundDownload.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index 2e7fb07..a8f5243 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; /** @@ -357,7 +358,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 +406,4 @@ public void copyTempFileToActualFile(Download curDownload) { curDownload.getCallbackContextDownloadStart().error("Cannot copy from temporary path to actual path"); } } -} \ No newline at end of file +} From 7a25d523a772546e04defb8c30c2b9330e767f1d Mon Sep 17 00:00:00 2001 From: Errietta Kostala Date: Sat, 26 Dec 2015 15:45:13 +0000 Subject: [PATCH 2/6] Call CleanUp when cancelling download --- src/android/BackgroundDownload.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index a8f5243..e90a63a 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -297,8 +297,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(); } From 63c30bc166e7c85bc9f742a95faeac123bb53e30 Mon Sep 17 00:00:00 2001 From: Errietta Kostala Date: Sat, 26 Dec 2015 21:03:07 +0000 Subject: [PATCH 3/6] Update BackgroundDownload.java --- src/android/BackgroundDownload.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index e90a63a..fbefb73 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -172,7 +172,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. From 5eb1db3cc27c3c823eeb8cd1f3de55c4b56f14a0 Mon Sep 17 00:00:00 2001 From: Errietta Kostala Date: Sat, 26 Dec 2015 21:03:56 +0000 Subject: [PATCH 4/6] Update BackgroundDownloader.js --- www/BackgroundDownloader.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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; From 1e92dffeac9d46f96f241f3a133275213f394bbd Mon Sep 17 00:00:00 2001 From: Errietta Kostala Date: Sat, 26 Dec 2015 21:05:58 +0000 Subject: [PATCH 5/6] Update DownloadOperation.js --- www/DownloadOperation.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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; From 79e585ca2046acef7483ee03adcfd4726a6000c5 Mon Sep 17 00:00:00 2001 From: Errietta Kostala Date: Thu, 31 Dec 2015 22:16:36 +0000 Subject: [PATCH 6/6] Re-attach progress listener for app restart --- src/android/BackgroundDownload.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index fbefb73..ddc043a 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -125,7 +125,7 @@ public void setTimerProgressUpdate(Timer TimerProgressUpdate) { this.timerProgressUpdate = TimerProgressUpdate; }; } - + HashMap activDownloads = new HashMap(); @Override @@ -155,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); @@ -412,3 +421,4 @@ public void copyTempFileToActualFile(Download curDownload) { } } } +