From 6d87f97e0e151dd3aca5a30cd742748558f03bf9 Mon Sep 17 00:00:00 2001 From: SangYeob Bono Yu Date: Fri, 22 Jan 2016 14:23:44 +0900 Subject: [PATCH 1/2] IntentAndroid.openChooserWithOptions implements --- .../Intent/IntentAndroid.android.js | 27 ++++++++++++++ .../react/modules/intent/IntentModule.java | 37 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/Libraries/Components/Intent/IntentAndroid.android.js b/Libraries/Components/Intent/IntentAndroid.android.js index 9dad6327bbabe7..fba12b2e59b1ec 100644 --- a/Libraries/Components/Intent/IntentAndroid.android.js +++ b/Libraries/Components/Intent/IntentAndroid.android.js @@ -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', diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java index ce4681264193da..74a4223422fe80 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java @@ -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)); + } } } From d723428f7a9602ccea2c48c262a57ba58ea89520 Mon Sep 17 00:00:00 2001 From: SangYeob Bono Yu Date: Fri, 22 Jan 2016 15:58:26 +0900 Subject: [PATCH 2/2] remove trailing spaces --- Libraries/Components/Intent/IntentAndroid.android.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/Components/Intent/IntentAndroid.android.js b/Libraries/Components/Intent/IntentAndroid.android.js index fba12b2e59b1ec..e08263314709b9 100644 --- a/Libraries/Components/Intent/IntentAndroid.android.js +++ b/Libraries/Components/Intent/IntentAndroid.android.js @@ -113,10 +113,10 @@ class IntentAndroid { * 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 + * @example * var options = { * type: 'text/plain', //default * subject: 'React Native',