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