Skip to content

Commit c6f8b5d

Browse files
wschurmanwkh237
authored andcommitted
Add ability to cancel android DownloadManager fetches (#502)
This just requires a bit of bookkeeping that keeps track of the task ID to the download manager ID. Note that the behavior of the download manager remove method is to remove the download and the downloaded file, whether partial or complete. https://developer.android.com/reference/android/app/DownloadManager.html#remove(long...)
1 parent 9abb4eb commit c6f8b5d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+13
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ enum ResponseFormat {
7979
}
8080

8181
public static HashMap<String, Call> taskTable = new HashMap<>();
82+
public static HashMap<String, Long> androidDownloadManagerTaskTable = new HashMap<>();
8283
static HashMap<String, RNFetchBlobProgressConfig> progressReport = new HashMap<>();
8384
static HashMap<String, RNFetchBlobProgressConfig> uploadProgressReport = new HashMap<>();
8485
static ConnectionPool pool = new ConnectionPool();
@@ -135,6 +136,13 @@ public static void cancelTask(String taskId) {
135136
call.cancel();
136137
taskTable.remove(taskId);
137138
}
139+
140+
if (androidDownloadManagerTaskTable.containsKey(taskId)) {
141+
long downloadManagerIdForTaskId = androidDownloadManagerTaskTable.get(taskId).longValue();
142+
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
143+
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
144+
dm.remove(downloadManagerIdForTaskId);
145+
}
138146
}
139147

140148
@Override
@@ -172,6 +180,7 @@ public void run() {
172180
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
173181
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
174182
downloadManagerId = dm.enqueue(req);
183+
androidDownloadManagerTaskTable.put(taskId, Long.valueOf(downloadManagerId));
175184
appCtx.registerReceiver(this, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
176185
return;
177186
}
@@ -438,6 +447,8 @@ public void onResponse(Call call, Response response) throws IOException {
438447
private void releaseTaskResource() {
439448
if(taskTable.containsKey(taskId))
440449
taskTable.remove(taskId);
450+
if(androidDownloadManagerTaskTable.containsKey(taskId))
451+
androidDownloadManagerTaskTable.remove(taskId);
441452
if(uploadProgressReport.containsKey(taskId))
442453
uploadProgressReport.remove(taskId);
443454
if(progressReport.containsKey(taskId))
@@ -635,6 +646,8 @@ public void onReceive(Context context, Intent intent) {
635646
Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
636647
long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
637648
if (id == this.downloadManagerId) {
649+
releaseTaskResource(); // remove task ID from task map
650+
638651
DownloadManager.Query query = new DownloadManager.Query();
639652
query.setFilterById(downloadManagerId);
640653
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);

0 commit comments

Comments
 (0)