|
1 | 1 | package com.RNFetchBlob; |
2 | 2 |
|
| 3 | +import android.app.Activity; |
| 4 | +import android.app.DownloadManager; |
3 | 5 | import android.content.Intent; |
4 | 6 | import android.net.Uri; |
5 | 7 |
|
6 | | -import com.RNFetchBlob.Utils.RNFBCookieJar; |
| 8 | +import com.facebook.react.bridge.ActivityEventListener; |
7 | 9 | import com.facebook.react.bridge.Callback; |
8 | 10 | import com.facebook.react.bridge.LifecycleEventListener; |
9 | 11 | import com.facebook.react.bridge.Promise; |
|
12 | 14 | import com.facebook.react.bridge.ReactMethod; |
13 | 15 | import com.facebook.react.bridge.ReadableArray; |
14 | 16 | import com.facebook.react.bridge.ReadableMap; |
15 | | -import com.facebook.react.bridge.WritableArray; |
| 17 | + |
| 18 | +// Cookies |
16 | 19 | import com.facebook.react.bridge.WritableMap; |
| 20 | +import com.facebook.react.modules.network.ForwardingCookieHandler; |
| 21 | +import com.facebook.react.modules.network.CookieJarContainer; |
| 22 | +import com.facebook.react.modules.network.OkHttpClientProvider; |
| 23 | +import okhttp3.OkHttpClient; |
| 24 | +import okhttp3.JavaNetCookieJar; |
17 | 25 |
|
| 26 | +import java.util.HashMap; |
18 | 27 | import java.util.Map; |
19 | 28 | import java.util.concurrent.LinkedBlockingQueue; |
20 | 29 | import java.util.concurrent.ThreadPoolExecutor; |
21 | 30 | import java.util.concurrent.TimeUnit; |
22 | 31 |
|
| 32 | +import static android.app.Activity.RESULT_OK; |
| 33 | +import static com.RNFetchBlob.RNFetchBlobConst.GET_CONTENT_INTENT; |
| 34 | + |
23 | 35 | public class RNFetchBlob extends ReactContextBaseJavaModule { |
24 | 36 |
|
| 37 | + // Cookies |
| 38 | + private final ForwardingCookieHandler mCookieHandler; |
| 39 | + private final CookieJarContainer mCookieJarContainer; |
| 40 | + private final OkHttpClient mClient; |
| 41 | + |
25 | 42 | static ReactApplicationContext RCTContext; |
26 | 43 | static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>(); |
27 | 44 | static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue); |
28 | 45 | static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>(); |
29 | 46 | static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue); |
30 | 47 | static public boolean ActionViewVisible = false; |
| 48 | + static HashMap<Integer, Promise> promiseTable = new HashMap<>(); |
31 | 49 |
|
32 | 50 | public RNFetchBlob(ReactApplicationContext reactContext) { |
33 | 51 |
|
34 | 52 | super(reactContext); |
35 | 53 |
|
| 54 | + mClient = OkHttpClientProvider.getOkHttpClient(); |
| 55 | + mCookieHandler = new ForwardingCookieHandler(reactContext); |
| 56 | + mCookieJarContainer = (CookieJarContainer) mClient.cookieJar(); |
| 57 | + mCookieJarContainer.setCookieJar(new JavaNetCookieJar(mCookieHandler)); |
| 58 | + |
36 | 59 | RCTContext = reactContext; |
| 60 | + reactContext.addActivityEventListener(new ActivityEventListener() { |
| 61 | + @Override |
| 62 | + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { |
| 63 | + if(requestCode == GET_CONTENT_INTENT && resultCode == RESULT_OK) { |
| 64 | + Uri d = data.getData(); |
| 65 | + promiseTable.get(GET_CONTENT_INTENT).resolve(d.toString()); |
| 66 | + promiseTable.remove(GET_CONTENT_INTENT); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onNewIntent(Intent intent) { |
| 72 | + |
| 73 | + } |
| 74 | + }); |
37 | 75 | } |
38 | 76 |
|
39 | 77 | @Override |
@@ -228,35 +266,6 @@ public void run() { |
228 | 266 |
|
229 | 267 | } |
230 | 268 |
|
231 | | - @ReactMethod |
232 | | - /** |
233 | | - * Get cookies belongs specific host. |
234 | | - * @param host String domain name. |
235 | | - */ |
236 | | - public void getCookies(String domain, Promise promise) { |
237 | | - try { |
238 | | - WritableMap cookies = RNFBCookieJar.getCookies(domain); |
239 | | - promise.resolve(cookies); |
240 | | - } catch(Exception err) { |
241 | | - promise.reject("RNFetchBlob.getCookies", err.getMessage()); |
242 | | - } |
243 | | - } |
244 | | - |
245 | | - @ReactMethod |
246 | | - /** |
247 | | - * Remove cookies for specific domain |
248 | | - * @param domain String of the domain |
249 | | - * @param promise JSC promise injected by RN |
250 | | - */ |
251 | | - public void removeCookies(String domain, Promise promise) { |
252 | | - try { |
253 | | - RNFBCookieJar.removeCookies(domain); |
254 | | - promise.resolve(null); |
255 | | - } catch(Exception err) { |
256 | | - promise.reject("RNFetchBlob.removeCookies", err.getMessage()); |
257 | | - } |
258 | | - } |
259 | | - |
260 | 269 | @ReactMethod |
261 | 270 | /** |
262 | 271 | * @param path Stream file path |
@@ -314,12 +323,51 @@ public void enableUploadProgressReport(String taskId, int interval, int count) { |
314 | 323 |
|
315 | 324 | @ReactMethod |
316 | 325 | public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) { |
317 | | - new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run(); |
318 | | - } |
| 326 | + new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run(); |
| 327 | +} |
319 | 328 |
|
320 | 329 | @ReactMethod |
321 | 330 | public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) { |
322 | | - new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run(); |
| 331 | + new RNFetchBlobReq(options, taskId, method, url, headers, null, body, mClient, callback).run(); |
| 332 | + } |
| 333 | + |
| 334 | + @ReactMethod |
| 335 | + public void getContentIntent(String mime, Promise promise) { |
| 336 | + Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
| 337 | + if(mime != null) |
| 338 | + i.setType(mime); |
| 339 | + else |
| 340 | + i.setType("*/*"); |
| 341 | + promiseTable.put(GET_CONTENT_INTENT, promise); |
| 342 | + this.getReactApplicationContext().startActivityForResult(i, GET_CONTENT_INTENT, null); |
| 343 | + |
| 344 | + } |
| 345 | + |
| 346 | + @ReactMethod |
| 347 | + public void addCompleteDownload (ReadableMap config, Promise promise) { |
| 348 | + DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE); |
| 349 | + String path = RNFetchBlobFS.normalizePath(config.getString("path")); |
| 350 | + if(path == null) { |
| 351 | + promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path); |
| 352 | + return; |
| 353 | + } |
| 354 | + try { |
| 355 | + WritableMap stat = RNFetchBlobFS.statFile(path); |
| 356 | + dm.addCompletedDownload( |
| 357 | + config.hasKey("title") ? config.getString("title") : "", |
| 358 | + config.hasKey("description") ? config.getString("description") : "", |
| 359 | + true, |
| 360 | + config.hasKey("mime") ? config.getString("mime") : null, |
| 361 | + path, |
| 362 | + Long.valueOf(stat.getString("size")), |
| 363 | + config.hasKey("showNotification") && config.getBoolean("showNotification") |
| 364 | + ); |
| 365 | + promise.resolve(null); |
| 366 | + } |
| 367 | + catch(Exception ex) { |
| 368 | + promise.reject("RNFetchblob.addCompleteDownload failed", ex.getStackTrace().toString()); |
| 369 | + } |
| 370 | + |
323 | 371 | } |
324 | 372 |
|
325 | 373 | } |
0 commit comments