Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 221287d

Browse files
committed
Add Android API getContentIntent
1 parent 913070c commit 221287d

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

android.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ function actionViewIntent(path:string, mime:string = 'text/plain') {
2424
return Promise.reject('RNFetchBlob.actionViewIntent only supports Android.')
2525
}
2626

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+
2734

2835
export default {
29-
actionViewIntent
36+
actionViewIntent,
37+
getContentIntent
3038
}

android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

+36
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.RNFetchBlob;
22

3+
import android.app.Activity;
34
import android.content.Intent;
45
import android.net.Uri;
6+
import android.util.Log;
57

68
import com.RNFetchBlob.Utils.RNFBCookieJar;
9+
import com.facebook.react.bridge.ActivityEventListener;
710
import com.facebook.react.bridge.Callback;
811
import com.facebook.react.bridge.LifecycleEventListener;
912
import com.facebook.react.bridge.Promise;
@@ -15,11 +18,16 @@
1518
import com.facebook.react.bridge.WritableArray;
1619
import com.facebook.react.bridge.WritableMap;
1720

21+
import java.util.HashMap;
1822
import java.util.Map;
23+
import java.util.UUID;
1924
import java.util.concurrent.LinkedBlockingQueue;
2025
import java.util.concurrent.ThreadPoolExecutor;
2126
import java.util.concurrent.TimeUnit;
2227

28+
import static android.app.Activity.RESULT_OK;
29+
import static com.RNFetchBlob.RNFetchBlobConst.GET_CONTENT_INTENT;
30+
2331
public class RNFetchBlob extends ReactContextBaseJavaModule {
2432

2533
static ReactApplicationContext RCTContext;
@@ -28,12 +36,28 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
2836
static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>();
2937
static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
3038
static public boolean ActionViewVisible = false;
39+
static HashMap<Integer, Promise> promiseTable = new HashMap<>();
3140

3241
public RNFetchBlob(ReactApplicationContext reactContext) {
3342

3443
super(reactContext);
3544

3645
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+
});
3761
}
3862

3963
@Override
@@ -322,4 +346,16 @@ public void fetchBlobForm(ReadableMap options, String taskId, String method, Str
322346
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run();
323347
}
324348

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+
325361
}

android/src/main/java/com/RNFetchBlob/RNFetchBlobConst.java

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public class RNFetchBlobConst {
1313
public static final String RNFB_RESPONSE_BASE64 = "base64";
1414
public static final String RNFB_RESPONSE_UTF8 = "utf8";
1515
public static final String RNFB_RESPONSE_PATH = "path";
16+
public static final Integer GET_CONTENT_INTENT = 99900;
1617

1718
}

0 commit comments

Comments
 (0)