Skip to content
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
39 changes: 32 additions & 7 deletions src/android/BackgroundDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -124,7 +125,7 @@ public void setTimerProgressUpdate(Timer TimerProgressUpdate) {
this.timerProgressUpdate = TimerProgressUpdate;
};
}

HashMap<String, Download> activDownloads = new HashMap<String, Download>();

@Override
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -396,4 +420,5 @@ public void copyTempFileToActualFile(Download curDownload) {
curDownload.getCallbackContextDownloadStart().error("Cannot copy from temporary path to actual path");
}
}
}
}

7 changes: 4 additions & 3 deletions www/BackgroundDownloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
module.exports = BackgroundDownloader;
8 changes: 5 additions & 3 deletions www/DownloadOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ 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");
}

this.uri = uri;
this.resultFile = resultFile;
this.appTitle = appTitle || "org.apache.cordova.backgroundDownload plugin";
};

/**
Expand All @@ -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 () {
Expand All @@ -79,4 +81,4 @@ DownloadOperation.prototype.stop = function() {

};

module.exports = DownloadOperation;
module.exports = DownloadOperation;