From 71673dab5c21cbc322db01ae981d4f70050f422b Mon Sep 17 00:00:00 2001 From: William Schurman Date: Tue, 29 Aug 2017 13:16:35 -0700 Subject: [PATCH] Add ability to cancel android DownloadManager fetches 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...) --- .../main/java/com/RNFetchBlob/RNFetchBlobReq.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java index 8a81a832e..5e66e48a6 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java @@ -79,6 +79,7 @@ enum ResponseFormat { } public static HashMap taskTable = new HashMap<>(); + public static HashMap androidDownloadManagerTaskTable = new HashMap<>(); static HashMap progressReport = new HashMap<>(); static HashMap uploadProgressReport = new HashMap<>(); static ConnectionPool pool = new ConnectionPool(); @@ -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 @@ -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; } @@ -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)) @@ -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);