11package com .RNFetchBlob ;
22
33import android .app .Activity ;
4+ import android .app .DownloadManager ;
45import android .content .Intent ;
56import android .net .Uri ;
67import android .support .annotation .Nullable ;
1718import com .facebook .react .bridge .ReadableArray ;
1819import com .facebook .react .bridge .ReadableMap ;
1920import com .facebook .react .bridge .WritableMap ;
21+ import com .facebook .react .modules .network .ForwardingCookieHandler ;
22+ import com .facebook .react .modules .network .CookieJarContainer ;
23+ import com .facebook .react .modules .network .OkHttpClientProvider ;
24+ import okhttp3 .OkHttpClient ;
25+ import okhttp3 .JavaNetCookieJar ;
2026
2127import java .util .HashMap ;
2228import java .util .Map ;
2935
3036public class RNFetchBlob extends ReactContextBaseJavaModule {
3137
38+ // Cookies
39+ private final ForwardingCookieHandler mCookieHandler ;
40+ private final CookieJarContainer mCookieJarContainer ;
41+ private final OkHttpClient mClient ;
42+
3243 static ReactApplicationContext RCTContext ;
3344 static LinkedBlockingQueue <Runnable > taskQueue = new LinkedBlockingQueue <>();
3445 static ThreadPoolExecutor threadPool = new ThreadPoolExecutor (5 , 10 , 5000 , TimeUnit .MILLISECONDS , taskQueue );
@@ -41,6 +52,11 @@ public RNFetchBlob(ReactApplicationContext reactContext) {
4152
4253 super (reactContext );
4354
55+ mClient = OkHttpClientProvider .getOkHttpClient ();
56+ mCookieHandler = new ForwardingCookieHandler (reactContext );
57+ mCookieJarContainer = (CookieJarContainer ) mClient .cookieJar ();
58+ mCookieJarContainer .setCookieJar (new JavaNetCookieJar (mCookieHandler ));
59+
4460 RCTContext = reactContext ;
4561 reactContext .addActivityEventListener (new ActivityEventListener () {
4662 @ Override
@@ -251,35 +267,6 @@ public void run() {
251267
252268 }
253269
254- @ ReactMethod
255- /**
256- * Get cookies belongs specific host.
257- * @param host String domain name.
258- */
259- public void getCookies (String domain , Promise promise ) {
260- try {
261- WritableMap cookies = RNFBCookieJar .getCookies (domain );
262- promise .resolve (cookies );
263- } catch (Exception err ) {
264- promise .reject ("RNFetchBlob.getCookies" , err .getMessage ());
265- }
266- }
267-
268- @ ReactMethod
269- /**
270- * Remove cookies for specific domain
271- * @param domain String of the domain
272- * @param promise JSC promise injected by RN
273- */
274- public void removeCookies (String domain , Promise promise ) {
275- try {
276- RNFBCookieJar .removeCookies (domain );
277- promise .resolve (null );
278- } catch (Exception err ) {
279- promise .reject ("RNFetchBlob.removeCookies" , err .getMessage ());
280- }
281- }
282-
283270 @ ReactMethod
284271 /**
285272 * @param path Stream file path
@@ -370,12 +357,12 @@ public void enableUploadProgressReport(String taskId, int interval, int count) {
370357
371358 @ ReactMethod
372359 public void fetchBlob (ReadableMap options , String taskId , String method , String url , ReadableMap headers , String body , final Callback callback ) {
373- new RNFetchBlobReq (options , taskId , method , url , headers , body , null , callback ).run ();
374- }
360+ new RNFetchBlobReq (options , taskId , method , url , headers , body , null , mClient , callback ).run ();
361+ }
375362
376363 @ ReactMethod
377364 public void fetchBlobForm (ReadableMap options , String taskId , String method , String url , ReadableMap headers , ReadableArray body , final Callback callback ) {
378- new RNFetchBlobReq (options , taskId , method , url , headers , null , body , callback ).run ();
365+ new RNFetchBlobReq (options , taskId , method , url , headers , null , body , mClient , callback ).run ();
379366 }
380367
381368 @ ReactMethod
@@ -411,4 +398,31 @@ public void getContentIntent(String mime, Promise promise) {
411398
412399 }
413400
401+ @ ReactMethod
402+ public void addCompleteDownload (ReadableMap config , Promise promise ) {
403+ DownloadManager dm = (DownloadManager ) RNFetchBlob .RCTContext .getSystemService (RNFetchBlob .RCTContext .DOWNLOAD_SERVICE );
404+ String path = RNFetchBlobFS .normalizePath (config .getString ("path" ));
405+ if (path == null ) {
406+ promise .reject ("RNFetchblob.addCompleteDownload can not resolve URI:" + config .getString ("path" ), "RNFetchblob.addCompleteDownload can not resolve URI:" + path );
407+ return ;
408+ }
409+ try {
410+ WritableMap stat = RNFetchBlobFS .statFile (path );
411+ dm .addCompletedDownload (
412+ config .hasKey ("title" ) ? config .getString ("title" ) : "" ,
413+ config .hasKey ("description" ) ? config .getString ("description" ) : "" ,
414+ true ,
415+ config .hasKey ("mime" ) ? config .getString ("mime" ) : null ,
416+ path ,
417+ Long .valueOf (stat .getString ("size" )),
418+ config .hasKey ("showNotification" ) && config .getBoolean ("showNotification" )
419+ );
420+ promise .resolve (null );
421+ }
422+ catch (Exception ex ) {
423+ promise .reject ("RNFetchblob.addCompleteDownload failed" , ex .getStackTrace ().toString ());
424+ }
425+
426+ }
427+
414428}
0 commit comments