Ver código fonte

Add Android API `getContentIntent`

Ben Hsieh 7 anos atrás
pai
commit
221287da17

+ 9
- 1
android.js Ver arquivo

@@ -24,7 +24,15 @@ function actionViewIntent(path:string, mime:string = 'text/plain') {
24 24
     return Promise.reject('RNFetchBlob.actionViewIntent only supports Android.')
25 25
 }
26 26
 
27
+function getContentIntent(mime:string) {
28
+  if(Platform.OS === 'android')
29
+    return RNFetchBlob.getContentIntent(mime)
30
+  else
31
+    return Promise.reject('RNFetchBlob.getContentIntent only supports Android.')
32
+}
33
+
27 34
 
28 35
 export default {
29
-  actionViewIntent
36
+  actionViewIntent,
37
+  getContentIntent
30 38
 }

+ 36
- 0
android/src/main/java/com/RNFetchBlob/RNFetchBlob.java Ver arquivo

@@ -1,9 +1,12 @@
1 1
 package com.RNFetchBlob;
2 2
 
3
+import android.app.Activity;
3 4
 import android.content.Intent;
4 5
 import android.net.Uri;
6
+import android.util.Log;
5 7
 
6 8
 import com.RNFetchBlob.Utils.RNFBCookieJar;
9
+import com.facebook.react.bridge.ActivityEventListener;
7 10
 import com.facebook.react.bridge.Callback;
8 11
 import com.facebook.react.bridge.LifecycleEventListener;
9 12
 import com.facebook.react.bridge.Promise;
@@ -15,11 +18,16 @@ import com.facebook.react.bridge.ReadableMap;
15 18
 import com.facebook.react.bridge.WritableArray;
16 19
 import com.facebook.react.bridge.WritableMap;
17 20
 
21
+import java.util.HashMap;
18 22
 import java.util.Map;
23
+import java.util.UUID;
19 24
 import java.util.concurrent.LinkedBlockingQueue;
20 25
 import java.util.concurrent.ThreadPoolExecutor;
21 26
 import java.util.concurrent.TimeUnit;
22 27
 
28
+import static android.app.Activity.RESULT_OK;
29
+import static com.RNFetchBlob.RNFetchBlobConst.GET_CONTENT_INTENT;
30
+
23 31
 public class RNFetchBlob extends ReactContextBaseJavaModule {
24 32
 
25 33
     static ReactApplicationContext RCTContext;
@@ -28,12 +36,28 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
28 36
     static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>();
29 37
     static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
30 38
     static public boolean ActionViewVisible = false;
39
+    static HashMap<Integer, Promise> promiseTable = new HashMap<>();
31 40
 
32 41
     public RNFetchBlob(ReactApplicationContext reactContext) {
33 42
 
34 43
         super(reactContext);
35 44
 
36 45
         RCTContext = reactContext;
46
+        reactContext.addActivityEventListener(new ActivityEventListener() {
47
+            @Override
48
+            public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
49
+                if(requestCode == GET_CONTENT_INTENT && resultCode == RESULT_OK) {
50
+                    Uri d = data.getData();
51
+                    promiseTable.get(GET_CONTENT_INTENT).resolve(d.toString());
52
+                    promiseTable.remove(GET_CONTENT_INTENT);
53
+                }
54
+            }
55
+
56
+            @Override
57
+            public void onNewIntent(Intent intent) {
58
+
59
+            }
60
+        });
37 61
     }
38 62
 
39 63
     @Override
@@ -322,4 +346,16 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
322 346
         new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run();
323 347
     }
324 348
 
349
+    @ReactMethod
350
+    public void getContentIntent(String mime, Promise promise) {
351
+        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
352
+        if(mime != null)
353
+            i.setType(mime);
354
+        else
355
+            i.setType("*/*");
356
+        promiseTable.put(GET_CONTENT_INTENT, promise);
357
+        this.getReactApplicationContext().startActivityForResult(i, GET_CONTENT_INTENT, null);
358
+
359
+    }
360
+
325 361
 }

+ 1
- 0
android/src/main/java/com/RNFetchBlob/RNFetchBlobConst.java Ver arquivo

@@ -13,5 +13,6 @@ public class RNFetchBlobConst {
13 13
     public static final String RNFB_RESPONSE_BASE64 = "base64";
14 14
     public static final String RNFB_RESPONSE_UTF8  = "utf8";
15 15
     public static final String RNFB_RESPONSE_PATH  = "path";
16
+    public static final Integer GET_CONTENT_INTENT = 99900;
16 17
 
17 18
 }