11package com .RNFetchBlob ;
22
33import android .app .DownloadManager ;
4+ import android .content .BroadcastReceiver ;
45import android .content .Context ;
6+ import android .content .Intent ;
7+ import android .content .IntentFilter ;
8+ import android .database .Cursor ;
59import android .net .Uri ;
610
711import com .facebook .react .bridge .Callback ;
1519import com .loopj .android .http .MySSLSocketFactory ;
1620
1721import java .io .File ;
18- import java .nio .charset .Charset ;
1922import java .security .KeyStore ;
2023
2124import cz .msebera .android .httpclient .HttpEntity ;
22- import cz .msebera .android .httpclient .entity .AbstractHttpEntity ;
2325import cz .msebera .android .httpclient .entity .ByteArrayEntity ;
2426import cz .msebera .android .httpclient .entity .ContentType ;
2527import cz .msebera .android .httpclient .entity .FileEntity ;
2830/**
2931 * Created by wkh237 on 2016/6/21.
3032 */
31- public class RNFetchBlobReq implements Runnable {
33+ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
3234
3335 final String filePathPrefix = "RNFetchBlob-file://" ;
3436 ReactApplicationContext ctx ;
@@ -40,6 +42,7 @@ public class RNFetchBlobReq implements Runnable{
4042 ReadableMap headers ;
4143 Callback callback ;
4244 HttpEntity entity ;
45+ long downloadManagerId ;
4346 AsyncHttpClient req ;
4447 String type ;
4548
@@ -82,10 +85,13 @@ public void run() {
8285 if (options .addAndroidDownloads .getBoolean ("useDownloadManager" )) {
8386 Uri uri = Uri .parse (url );
8487 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" ));
8995 }
9096 // set headers
9197 ReadableMapKeySetIterator it = headers .keySetIterator ();
@@ -94,7 +100,8 @@ public void run() {
94100 req .addRequestHeader (key , headers .getString (key ));
95101 }
96102 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 ));
98105 return ;
99106 }
100107
@@ -230,4 +237,28 @@ void buildEntity(String body) {
230237 }
231238
232239 }
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+ }
233264}
0 commit comments