Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Add ability to cancel android DownloadManager fetches #502

Merged
merged 1 commit into from
Aug 31, 2017
Merged
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 android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ enum ResponseFormat {
}

public static HashMap<String, Call> taskTable = new HashMap<>();
public static HashMap<String, Long> androidDownloadManagerTaskTable = new HashMap<>();
static HashMap<String, RNFetchBlobProgressConfig> progressReport = new HashMap<>();
static HashMap<String, RNFetchBlobProgressConfig> uploadProgressReport = new HashMap<>();
static ConnectionPool pool = new ConnectionPool();
Expand Down Expand Up @@ -135,6 +136,13 @@ public static void cancelTask(String taskId) {
call.cancel();
taskTable.remove(taskId);
}

if (androidDownloadManagerTaskTable.containsKey(taskId)) {
long downloadManagerIdForTaskId = androidDownloadManagerTaskTable.get(taskId).longValue();
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
dm.remove(downloadManagerIdForTaskId);
}
}

@Override
Expand Down Expand Up @@ -172,6 +180,7 @@ public void run() {
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
downloadManagerId = dm.enqueue(req);
androidDownloadManagerTaskTable.put(taskId, Long.valueOf(downloadManagerId));
appCtx.registerReceiver(this, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
return;
}
Expand Down Expand Up @@ -438,6 +447,8 @@ public void onResponse(Call call, Response response) throws IOException {
private void releaseTaskResource() {
if(taskTable.containsKey(taskId))
taskTable.remove(taskId);
if(androidDownloadManagerTaskTable.containsKey(taskId))
androidDownloadManagerTaskTable.remove(taskId);
if(uploadProgressReport.containsKey(taskId))
uploadProgressReport.remove(taskId);
if(progressReport.containsKey(taskId))
Expand Down Expand Up @@ -635,6 +646,8 @@ public void onReceive(Context context, Intent intent) {
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
if (id == this.downloadManagerId) {
releaseTaskResource(); // remove task ID from task map

DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadManagerId);
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
Expand Down