|
|
@@ -3,9 +3,7 @@ package com.RNFetchBlob;
|
|
3
|
3
|
import android.app.Activity;
|
|
4
|
4
|
import android.content.Intent;
|
|
5
|
5
|
import android.net.Uri;
|
|
6
|
|
-import android.util.Log;
|
|
7
|
6
|
|
|
8
|
|
-import com.RNFetchBlob.Utils.RNFBCookieJar;
|
|
9
|
7
|
import com.facebook.react.bridge.ActivityEventListener;
|
|
10
|
8
|
import com.facebook.react.bridge.Callback;
|
|
11
|
9
|
import com.facebook.react.bridge.LifecycleEventListener;
|
|
|
@@ -15,12 +13,16 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
15
|
13
|
import com.facebook.react.bridge.ReactMethod;
|
|
16
|
14
|
import com.facebook.react.bridge.ReadableArray;
|
|
17
|
15
|
import com.facebook.react.bridge.ReadableMap;
|
|
18
|
|
-import com.facebook.react.bridge.WritableArray;
|
|
19
|
|
-import com.facebook.react.bridge.WritableMap;
|
|
|
16
|
+
|
|
|
17
|
+// Cookies
|
|
|
18
|
+import com.facebook.react.modules.network.ForwardingCookieHandler;
|
|
|
19
|
+import com.facebook.react.modules.network.CookieJarContainer;
|
|
|
20
|
+import com.facebook.react.modules.network.OkHttpClientProvider;
|
|
|
21
|
+import okhttp3.OkHttpClient;
|
|
|
22
|
+import okhttp3.JavaNetCookieJar;
|
|
20
|
23
|
|
|
21
|
24
|
import java.util.HashMap;
|
|
22
|
25
|
import java.util.Map;
|
|
23
|
|
-import java.util.UUID;
|
|
24
|
26
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
25
|
27
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
26
|
28
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -30,6 +32,11 @@ import static com.RNFetchBlob.RNFetchBlobConst.GET_CONTENT_INTENT;
|
|
30
|
32
|
|
|
31
|
33
|
public class RNFetchBlob extends ReactContextBaseJavaModule {
|
|
32
|
34
|
|
|
|
35
|
+ // Cookies
|
|
|
36
|
+ private final ForwardingCookieHandler mCookieHandler;
|
|
|
37
|
+ private final CookieJarContainer mCookieJarContainer;
|
|
|
38
|
+ private final OkHttpClient mClient;
|
|
|
39
|
+
|
|
33
|
40
|
static ReactApplicationContext RCTContext;
|
|
34
|
41
|
static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
|
|
35
|
42
|
static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
|
|
|
@@ -42,6 +49,11 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
|
|
42
|
49
|
|
|
43
|
50
|
super(reactContext);
|
|
44
|
51
|
|
|
|
52
|
+ mClient = OkHttpClientProvider.getOkHttpClient();
|
|
|
53
|
+ mCookieHandler = new ForwardingCookieHandler(reactContext);
|
|
|
54
|
+ mCookieJarContainer = (CookieJarContainer) mClient.cookieJar();
|
|
|
55
|
+ mCookieJarContainer.setCookieJar(new JavaNetCookieJar(mCookieHandler));
|
|
|
56
|
+
|
|
45
|
57
|
RCTContext = reactContext;
|
|
46
|
58
|
reactContext.addActivityEventListener(new ActivityEventListener() {
|
|
47
|
59
|
@Override
|
|
|
@@ -252,35 +264,6 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
|
|
252
|
264
|
|
|
253
|
265
|
}
|
|
254
|
266
|
|
|
255
|
|
- @ReactMethod
|
|
256
|
|
- /**
|
|
257
|
|
- * Get cookies belongs specific host.
|
|
258
|
|
- * @param host String domain name.
|
|
259
|
|
- */
|
|
260
|
|
- public void getCookies(String domain, Promise promise) {
|
|
261
|
|
- try {
|
|
262
|
|
- WritableMap cookies = RNFBCookieJar.getCookies(domain);
|
|
263
|
|
- promise.resolve(cookies);
|
|
264
|
|
- } catch(Exception err) {
|
|
265
|
|
- promise.reject("RNFetchBlob.getCookies", err.getMessage());
|
|
266
|
|
- }
|
|
267
|
|
- }
|
|
268
|
|
-
|
|
269
|
|
- @ReactMethod
|
|
270
|
|
- /**
|
|
271
|
|
- * Remove cookies for specific domain
|
|
272
|
|
- * @param domain String of the domain
|
|
273
|
|
- * @param promise JSC promise injected by RN
|
|
274
|
|
- */
|
|
275
|
|
- public void removeCookies(String domain, Promise promise) {
|
|
276
|
|
- try {
|
|
277
|
|
- RNFBCookieJar.removeCookies(domain);
|
|
278
|
|
- promise.resolve(null);
|
|
279
|
|
- } catch(Exception err) {
|
|
280
|
|
- promise.reject("RNFetchBlob.removeCookies", err.getMessage());
|
|
281
|
|
- }
|
|
282
|
|
- }
|
|
283
|
|
-
|
|
284
|
267
|
@ReactMethod
|
|
285
|
268
|
/**
|
|
286
|
269
|
* @param path Stream file path
|
|
|
@@ -338,12 +321,12 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
|
|
338
|
321
|
|
|
339
|
322
|
@ReactMethod
|
|
340
|
323
|
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
|
|
341
|
|
- new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();
|
|
342
|
|
- }
|
|
|
324
|
+ new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
|
|
|
325
|
+}
|
|
343
|
326
|
|
|
344
|
327
|
@ReactMethod
|
|
345
|
328
|
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
|
|
346
|
|
- new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run();
|
|
|
329
|
+ new RNFetchBlobReq(options, taskId, method, url, headers, null, body, mClient, callback).run();
|
|
347
|
330
|
}
|
|
348
|
331
|
|
|
349
|
332
|
@ReactMethod
|