Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntentAndroid.openChooserWithOptions implements #5476

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Libraries/Components/Intent/IntentAndroid.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,33 @@ class IntentAndroid {
IntentAndroidModule.getInitialURL(callback);
}

/**
* Open a chooser dialog to send data to other apps.
*
* Refer http://developer.android.com/intl/ko/training/sharing/send.html
*
* @param {object} Options - the data to send
* @param {string} Title - the title of the chooser dialog
* @example
* var options = {
* type: 'text/plain', //default
* subject: 'React Native',
* text: 'http://facebook.github.io/react-native/'
* }
*/
static openChooserWithOptions(options: Object, title: string) {
invariant(
typeof options === 'object',
'A valid option object is required'
);
invariant(
typeof title === 'string',
'Invalid Title: should be a string. Was: ' + title
);
IntentAndroidModule.openChooserWithOptions(options, title);
}


static _validateURL(url: string) {
invariant(
typeof url === 'string',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,42 @@ public void canOpenURL(String url, Callback callback) {
throw new JSApplicationIllegalArgumentException(
"Could not check if URL '" + url + "' can be opened: " + e.getMessage());
}
}

/**
* Open a chooser dialog to send data to other apps.
*
* Refer http://developer.android.com/intl/ko/training/sharing/send.html
*
* @param options the data to send
* @param title the title of the chooser dialog
*/
@ReactMethod
public void openChooserWithOptions(ReadableMap options, String title) {
if (options == null) {
throw new JSApplicationIllegalArgumentException("Invalid Options");
}

Activity currentActivity = getCurrentActivity();
Intent intent = new Intent(Intent.ACTION_SEND);
if(options.hasKey("type")) {
intent.setTypeAndNormalize(options.getString("type"));
} else {
intent.setTypeAndNormalize("text/plain");
}

if(options.hasKey("subject")) {
intent.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
}
if(options.hasKey("text")) {
intent.putExtra(Intent.EXTRA_TEXT, options.getString("text"));
}

if (currentActivity != null) {
currentActivity.startActivity(Intent.createChooser(intent, title));
} else {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getReactApplicationContext().startActivity(Intent.createChooser(intent, title));
}
}
}