Ver código fonte

Add android action view intent API for #75

Ben Hsieh 8 anos atrás
pai
commit
bb76484c9b

+ 31
- 0
src/android.js Ver arquivo

@@ -0,0 +1,31 @@
1
+// Copyright 2016 wkh237@github. All rights reserved.
2
+// Use of this source code is governed by a MIT-style license that can be
3
+// found in the LICENSE file.
4
+// @flow
5
+
6
+import {
7
+  NativeModules,
8
+  DeviceEventEmitter,
9
+  Platform,
10
+  NativeAppEventEmitter,
11
+} from 'react-native'
12
+
13
+const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
14
+
15
+/**
16
+ * Send an intent to open the file.
17
+ * @param  {string]} path Path of the file to be open.
18
+ * @param  {string} mime MIME type string
19
+ * @return {Promise}
20
+ */
21
+function actionViewIntent(path:string, mime = 'text/plain':string) {
22
+  if(Platform.OS === 'android')
23
+    return RNFetchBlob.actionViewIntent(path, mime)
24
+  else
25
+    return Promise.reject('RNFetchBlob.actionViewIntent only supports Android.')
26
+}
27
+
28
+
29
+export default {
30
+  actionViewIntent
31
+}

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

@@ -1,5 +1,8 @@
1 1
 package com.RNFetchBlob;
2 2
 
3
+import android.content.Intent;
4
+import android.net.Uri;
5
+
3 6
 import com.facebook.react.bridge.Callback;
4 7
 import com.facebook.react.bridge.Promise;
5 8
 import com.facebook.react.bridge.ReactApplicationContext;
@@ -46,6 +49,20 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
46 49
 
47 50
     }
48 51
 
52
+    @ReactMethod
53
+    public void actionViewIntent(String path, String mime, Promise promise) {
54
+        try {
55
+            Intent intent= new Intent(Intent.ACTION_VIEW)
56
+                    .setDataAndType(Uri.parse("file://" + path), mime);
57
+            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
58
+
59
+            this.getReactApplicationContext().startActivity(intent);
60
+            promise.resolve(null);
61
+        } catch(Exception ex) {
62
+            promise.reject(ex.getLocalizedMessage());
63
+        }
64
+    }
65
+
49 66
     @ReactMethod
50 67
     public void createFileASCII(final String path, final ReadableArray dataArray, final Callback callback) {
51 68
         threadPool.execute(new Runnable() {

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

@@ -562,7 +562,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
562 562
                         cursor.moveToFirst();
563 563
                         String filePath = cursor.getString(0);
564 564
                         cursor.close();
565
-                        this.callback.invoke(null, null, filePath);
565
+                        this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
566 566
                     }
567 567
                     else
568 568
                         this.callback.invoke(null, null, null);

+ 3
- 0
src/index.js Ver arquivo

@@ -20,6 +20,7 @@ import fs from './fs'
20 20
 import getUUID from './utils/uuid'
21 21
 import base64 from 'base-64'
22 22
 import polyfill from './polyfill'
23
+import android from './android'
23 24
 const {
24 25
   RNFetchBlobSession,
25 26
   readStream,
@@ -36,6 +37,7 @@ const {
36 37
   cp
37 38
 } = fs
38 39
 
40
+
39 41
 const Blob = polyfill.Blob
40 42
 const emitter = DeviceEventEmitter
41 43
 const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
@@ -383,6 +385,7 @@ class FetchBlobResponse {
383 385
 export default {
384 386
   fetch,
385 387
   base64,
388
+  android,
386 389
   config,
387 390
   session,
388 391
   fs,