Browse Source

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...)
William Schurman 6 years ago
parent
commit
c6f8b5d581
1 changed files with 13 additions and 0 deletions
  1. 13
    0
      android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 13
- 0
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -79,6 +79,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
79 79
     }
80 80
 
81 81
     public static HashMap<String, Call> taskTable = new HashMap<>();
82
+    public static HashMap<String, Long> androidDownloadManagerTaskTable = new HashMap<>();
82 83
     static HashMap<String, RNFetchBlobProgressConfig> progressReport = new HashMap<>();
83 84
     static HashMap<String, RNFetchBlobProgressConfig> uploadProgressReport = new HashMap<>();
84 85
     static ConnectionPool pool = new ConnectionPool();
@@ -135,6 +136,13 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
135 136
             call.cancel();
136 137
             taskTable.remove(taskId);
137 138
         }
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
+        }
138 146
     }
139 147
 
140 148
     @Override
@@ -172,6 +180,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
172 180
                 Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
173 181
                 DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
174 182
                 downloadManagerId = dm.enqueue(req);
183
+                androidDownloadManagerTaskTable.put(taskId, Long.valueOf(downloadManagerId));
175 184
                 appCtx.registerReceiver(this, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
176 185
                 return;
177 186
             }
@@ -438,6 +447,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
438 447
     private void releaseTaskResource() {
439 448
         if(taskTable.containsKey(taskId))
440 449
             taskTable.remove(taskId);
450
+        if(androidDownloadManagerTaskTable.containsKey(taskId))
451
+            androidDownloadManagerTaskTable.remove(taskId);
441 452
         if(uploadProgressReport.containsKey(taskId))
442 453
             uploadProgressReport.remove(taskId);
443 454
         if(progressReport.containsKey(taskId))
@@ -635,6 +646,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
635 646
             Context appCtx = RNFetchBlob.RCTContext.getApplicationContext();
636 647
             long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
637 648
             if (id == this.downloadManagerId) {
649
+                releaseTaskResource(); // remove task ID from task map
650
+
638 651
                 DownloadManager.Query query = new DownloadManager.Query();
639 652
                 query.setFilterById(downloadManagerId);
640 653
                 DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);