Skip to content

Commit

Permalink
feat(android): add chooser
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Feb 10, 2020
1 parent 6923bbb commit ec99ab8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
16 changes: 6 additions & 10 deletions android.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.

import {
NativeModules,
DeviceEventEmitter,
Platform,
NativeAppEventEmitter,
} from 'react-native'
import { NativeModules, Platform } from 'react-native'

const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
const RNFetchBlob = NativeModules.RNFetchBlob

/**
* Send an intent to open the file.
* @param {string} path Path of the file to be open.
* @param {string} mime MIME type string
* @param {string} title for chooser, if not set the chooser won't be displayed (see https://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent,%20java.lang.CharSequence))
* @return {Promise}
*/
function actionViewIntent(path:string, mime:string = 'text/plain') {
function actionViewIntent(path, mime, chooserTitle) {
if(Platform.OS === 'android')
return RNFetchBlob.actionViewIntent(path, mime)
return RNFetchBlob.actionViewIntent(path, mime, chooserTitle)
else
return Promise.reject('RNFetchBlob.android.actionViewIntent only supports Android.')
}

function getContentIntent(mime:string) {
function getContentIntent(mime) {
if(Platform.OS === 'android')
return RNFetchBlob.getContentIntent(mime)
else
Expand Down
9 changes: 8 additions & 1 deletion android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import okhttp3.OkHttpClient;
import okhttp3.JavaNetCookieJar;

import javax.annotation.Nullable;
import java.io.File;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void run() {
}

@ReactMethod
public void actionViewIntent(String path, String mime, final Promise promise) {
public void actionViewIntent(String path, String mime, @Nullable String chooserTitle, final Promise promise) {
try {
Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(),
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
Expand All @@ -115,6 +116,9 @@ public void actionViewIntent(String path, String mime, final Promise promise) {
// Create the intent with data and type
Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(uriForFile, mime);
if (chooserTitle != null) {
intent = Intent.createChooser(intent, chooserTitle);
}

// Set flag to give temporary permission to external app to use FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Expand All @@ -130,6 +134,9 @@ public void actionViewIntent(String path, String mime, final Promise promise) {
} else {
Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (chooserTitle != null) {
intent = Intent.createChooser(intent, chooserTitle);
}

this.getReactApplicationContext().startActivity(intent);
}
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,9 @@ export interface AndroidApi {
* App to handle the file. For example, open Gallery app to view an image, or install APK.
* @param path Path of the file to be opened.
* @param mime Basically system will open an app according to this MIME type.
* @param chooserTitle title for chooser, if not set the chooser won't be displayed (see [Android docs](https://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent,%20java.lang.CharSequence)))
*/
actionViewIntent(path: string, mime: string): Promise<any>;
actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<any>;

/**
*
Expand Down

0 comments on commit ec99ab8

Please sign in to comment.