Browse Source

Add implementation of #22

Ben Hsieh 8 years ago
parent
commit
ecf7e2cfb0
1 changed files with 26 additions and 0 deletions
  1. 26
    0
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

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

1
 package com.RNFetchBlob;
1
 package com.RNFetchBlob;
2
 
2
 
3
+import android.app.DownloadManager;
4
+import android.content.Context;
3
 import android.net.Uri;
5
 import android.net.Uri;
4
 import android.os.Environment;
6
 import android.os.Environment;
5
 
7
 
159
 
161
 
160
         RNFetchBlobConfig config = new RNFetchBlobConfig(options);
162
         RNFetchBlobConfig config = new RNFetchBlobConfig(options);
161
 
163
 
164
+        // use download manager instead of default HTTP implementation
165
+        if(config.addAndroidDownloads != null && config.addAndroidDownloads.hasKey("useDownloadManager")) {
166
+
167
+            if(config.addAndroidDownloads.getBoolean("useDownloadManager")) {
168
+                Uri uri = Uri.parse(url);
169
+                DownloadManager.Request req = new DownloadManager.Request(uri);
170
+                if(config.path != null) {
171
+                    Uri dest = null;
172
+                    dest = Uri.parse(config.path);
173
+                    req.setDestinationUri(dest);
174
+                }
175
+                // set headers
176
+                ReadableMapKeySetIterator it = headers.keySetIterator();
177
+                while (it.hasNextKey()) {
178
+                    String key = it.nextKey();
179
+                    req.addRequestHeader(key, headers.getString(key));
180
+                }
181
+                DownloadManager dm = (DownloadManager) this.getReactApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
182
+                dm.enqueue(req);
183
+                return;
184
+            }
185
+
186
+        }
187
+
162
         try {
188
         try {
163
             AsyncHttpClient req = new AsyncHttpClient();
189
             AsyncHttpClient req = new AsyncHttpClient();
164
 
190