1
1
package com .RNFetchBlob ;
2
2
3
3
import android .app .DownloadManager ;
4
+ import android .content .BroadcastReceiver ;
4
5
import android .content .Context ;
6
+ import android .content .Intent ;
7
+ import android .content .IntentFilter ;
8
+ import android .database .Cursor ;
5
9
import android .net .Uri ;
6
10
7
11
import com .facebook .react .bridge .Callback ;
15
19
import com .loopj .android .http .MySSLSocketFactory ;
16
20
17
21
import java .io .File ;
18
- import java .nio .charset .Charset ;
19
22
import java .security .KeyStore ;
20
23
21
24
import cz .msebera .android .httpclient .HttpEntity ;
22
- import cz .msebera .android .httpclient .entity .AbstractHttpEntity ;
23
25
import cz .msebera .android .httpclient .entity .ByteArrayEntity ;
24
26
import cz .msebera .android .httpclient .entity .ContentType ;
25
27
import cz .msebera .android .httpclient .entity .FileEntity ;
28
30
/**
29
31
* Created by wkh237 on 2016/6/21.
30
32
*/
31
- public class RNFetchBlobReq implements Runnable {
33
+ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
32
34
33
35
final String filePathPrefix = "RNFetchBlob-file://" ;
34
36
ReactApplicationContext ctx ;
@@ -40,6 +42,7 @@ public class RNFetchBlobReq implements Runnable{
40
42
ReadableMap headers ;
41
43
Callback callback ;
42
44
HttpEntity entity ;
45
+ long downloadManagerId ;
43
46
AsyncHttpClient req ;
44
47
String type ;
45
48
@@ -82,10 +85,13 @@ public void run() {
82
85
if (options .addAndroidDownloads .getBoolean ("useDownloadManager" )) {
83
86
Uri uri = Uri .parse (url );
84
87
DownloadManager .Request req = new DownloadManager .Request (uri );
85
- if (options .path != null ) {
86
- Uri dest = null ;
87
- dest = Uri .parse (options .path );
88
- req .setDestinationUri (dest );
88
+ req .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED );
89
+
90
+ if (options .addAndroidDownloads .hasKey ("title" )) {
91
+ req .setTitle (options .addAndroidDownloads .getString ("title" ));
92
+ }
93
+ if (options .addAndroidDownloads .hasKey ("description" )) {
94
+ req .setDescription (options .addAndroidDownloads .getString ("description" ));
89
95
}
90
96
// set headers
91
97
ReadableMapKeySetIterator it = headers .keySetIterator ();
@@ -94,7 +100,8 @@ public void run() {
94
100
req .addRequestHeader (key , headers .getString (key ));
95
101
}
96
102
DownloadManager dm = (DownloadManager ) ctx .getSystemService (Context .DOWNLOAD_SERVICE );
97
- dm .enqueue (req );
103
+ downloadManagerId = dm .enqueue (req );
104
+ ctx .registerReceiver (this , new IntentFilter (DownloadManager .ACTION_DOWNLOAD_COMPLETE ));
98
105
return ;
99
106
}
100
107
@@ -230,4 +237,28 @@ void buildEntity(String body) {
230
237
}
231
238
232
239
}
240
+
241
+ @ Override
242
+ public void onReceive (Context context , Intent intent ) {
243
+ String action = intent .getAction ();
244
+ if (DownloadManager .ACTION_DOWNLOAD_COMPLETE .equals (action )) {
245
+ long id = intent .getExtras ().getLong (DownloadManager .EXTRA_DOWNLOAD_ID );
246
+ if (id == this .downloadManagerId ) {
247
+ DownloadManager .Query query = new DownloadManager .Query ();
248
+ query .setFilterById (downloadManagerId );
249
+ DownloadManager dm = (DownloadManager ) ctx .getSystemService (Context .DOWNLOAD_SERVICE );
250
+ dm .query (query );
251
+ Cursor c = dm .query (query );
252
+ if (c .moveToFirst ()) {
253
+ String contentUri = c .getString (c .getColumnIndex (DownloadManager .COLUMN_LOCAL_URI ));
254
+ Uri uri = Uri .parse (contentUri );
255
+ Cursor cursor = ctx .getContentResolver ().query (uri , new String [] { android .provider .MediaStore .Images .ImageColumns .DATA }, null , null , null );
256
+ cursor .moveToFirst ();
257
+ String filePath = cursor .getString (0 );
258
+ cursor .close ();
259
+ this .callback .invoke (null , filePath );
260
+ }
261
+ }
262
+ }
263
+ }
233
264
}
0 commit comments