Browse Source

#21 Add support for android Downloads app

Ben Hsieh 8 years ago
parent
commit
8f05bb58ba

+ 6
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java View File

@@ -2,6 +2,8 @@ package com.RNFetchBlob;
2 2
 
3 3
 import com.facebook.react.bridge.ReadableMap;
4 4
 
5
+import java.util.HashMap;
6
+
5 7
 /**
6 8
  * Created by wkh237 on 2016/5/29.
7 9
  */
@@ -10,6 +12,7 @@ public class RNFetchBlobConfig {
10 12
     public Boolean fileCache;
11 13
     public String path;
12 14
     public String appendExt;
15
+    public ReadableMap addAndroidDownloads;
13 16
 
14 17
     RNFetchBlobConfig(ReadableMap options) {
15 18
         if(options == null)
@@ -17,7 +20,9 @@ public class RNFetchBlobConfig {
17 20
         this.fileCache = options.hasKey("fileCache") ? options.getBoolean("fileCache") : false;
18 21
         this.path = options.hasKey("path") ? options.getString("path") : null;
19 22
         this.appendExt = options.hasKey("appendExt") ? options.getString("appendExt") : "";
20
-
23
+        if(options.hasKey("addAndroidDownloads")) {
24
+            this.addAndroidDownloads = options.getMap("addAndroidDownloads");
25
+        }
21 26
     }
22 27
 
23 28
 }

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

@@ -1,9 +1,13 @@
1 1
 package com.RNFetchBlob;
2 2
 
3
+import android.app.DownloadManager;
4
+import android.content.Context;
5
+
3 6
 import com.facebook.react.bridge.Arguments;
4 7
 import com.facebook.react.bridge.Callback;
5 8
 import com.facebook.react.bridge.ReactApplicationContext;
6 9
 import com.facebook.react.bridge.ReactContext;
10
+import com.facebook.react.bridge.ReadableMap;
7 11
 import com.facebook.react.bridge.WritableMap;
8 12
 import com.facebook.react.modules.core.DeviceEventManagerModule;
9 13
 import com.loopj.android.http.FileAsyncHttpResponseHandler;
@@ -61,6 +65,26 @@ public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
61 65
 
62 66
     @Override
63 67
     public void onSuccess(int statusCode, Header[] headers, File file) {
68
+        ReadableMap notifyConfig = mConfig.addAndroidDownloads;
69
+
70
+        // Download manager settings
71
+        if(notifyConfig != null ) {
72
+            String title = "", desc = "", mime = "text/plain";
73
+            boolean scannable = false, notification = false;
74
+            if(notifyConfig.hasKey("title"))
75
+                title = mConfig.addAndroidDownloads.getString("title");
76
+            if(notifyConfig.hasKey("description"))
77
+                desc = notifyConfig.getString("description");
78
+            if(notifyConfig.hasKey("mime"))
79
+                mime = notifyConfig.getString("mime");
80
+            if(notifyConfig.hasKey("mediaScannable"))
81
+                scannable = notifyConfig.getBoolean("mediaScannable");
82
+            if(notifyConfig.hasKey("notification"))
83
+                notification = notifyConfig.getBoolean("notification");
84
+            DownloadManager dm = (DownloadManager)mCtx.getSystemService(mCtx.DOWNLOAD_SERVICE);
85
+            dm.addCompletedDownload(title, desc, scannable, mime, file.getAbsolutePath(), file.length(), notification);
86
+        }
87
+
64 88
         this.onResponse.invoke(null, file.getAbsolutePath());
65 89
     }
66 90
 }