1
1
package com .RNFetchBlob ;
2
2
3
3
import android .app .Activity ;
4
+ import android .app .DownloadManager ;
4
5
import android .content .Intent ;
5
6
import android .net .Uri ;
6
7
import android .support .annotation .Nullable ;
17
18
import com .facebook .react .bridge .ReadableArray ;
18
19
import com .facebook .react .bridge .ReadableMap ;
19
20
import 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 ;
20
26
21
27
import java .util .HashMap ;
22
28
import java .util .Map ;
29
35
30
36
public class RNFetchBlob extends ReactContextBaseJavaModule {
31
37
38
+ // Cookies
39
+ private final ForwardingCookieHandler mCookieHandler ;
40
+ private final CookieJarContainer mCookieJarContainer ;
41
+ private final OkHttpClient mClient ;
42
+
32
43
static ReactApplicationContext RCTContext ;
33
44
static LinkedBlockingQueue <Runnable > taskQueue = new LinkedBlockingQueue <>();
34
45
static ThreadPoolExecutor threadPool = new ThreadPoolExecutor (5 , 10 , 5000 , TimeUnit .MILLISECONDS , taskQueue );
@@ -41,6 +52,11 @@ public RNFetchBlob(ReactApplicationContext reactContext) {
41
52
42
53
super (reactContext );
43
54
55
+ mClient = OkHttpClientProvider .getOkHttpClient ();
56
+ mCookieHandler = new ForwardingCookieHandler (reactContext );
57
+ mCookieJarContainer = (CookieJarContainer ) mClient .cookieJar ();
58
+ mCookieJarContainer .setCookieJar (new JavaNetCookieJar (mCookieHandler ));
59
+
44
60
RCTContext = reactContext ;
45
61
reactContext .addActivityEventListener (new ActivityEventListener () {
46
62
@ Override
@@ -251,35 +267,6 @@ public void run() {
251
267
252
268
}
253
269
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
-
283
270
@ ReactMethod
284
271
/**
285
272
* @param path Stream file path
@@ -370,12 +357,12 @@ public void enableUploadProgressReport(String taskId, int interval, int count) {
370
357
371
358
@ ReactMethod
372
359
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
+ }
375
362
376
363
@ ReactMethod
377
364
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 ();
379
366
}
380
367
381
368
@ ReactMethod
@@ -411,4 +398,31 @@ public void getContentIntent(String mime, Promise promise) {
411
398
412
399
}
413
400
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
+
414
428
}
0 commit comments