From eb05983e0c4a29aa6aba8ba38c9bea86afb1c2db Mon Sep 17 00:00:00 2001 From: Martijn de Haan Date: Fri, 13 Jan 2017 10:07:06 +0100 Subject: [PATCH] Implement Mailer#canSend --- README.md | 26 ++++- RNMail/RNMail.m | 5 + .../java/com/chirag/RNMail/RNMailModule.java | 97 +++++++++++-------- 3 files changed, 88 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 5937da7..1fd741d 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,9 @@ public class MainApplication extends Application implements ReactApplication { 4. Whenever you want to use it within React code now you can: `var Mailer = require('NativeModules').RNMail;` -## Example +## Example 1 +Show a mail dialog. + ```javascript var Mailer = require('NativeModules').RNMail; @@ -139,5 +141,27 @@ var MailExampleApp = React.createClass({ On Android, the `callback` will only be called if an `error` occurs. The `event` argument is unused! +## Example 2 +Check if the user has an email account configured. + +```js +var Mailer = require('NativeModules').RNMail; + +Mailer.canSend((error, canSend) => { + if (error) { + // + } + else { + if (canSend) { + // User can send a mail + } + else { + // User cannot send a mail + } + } +}); +} +``` + ## Here is how it looks: ![Demo gif](https://github.com/chirag04/react-native-mail/blob/master/screenshot.png) diff --git a/RNMail/RNMail.m b/RNMail/RNMail.m index b997652..5111d8f 100644 --- a/RNMail/RNMail.m +++ b/RNMail/RNMail.m @@ -23,6 +23,11 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_MODULE() +RCT_EXPORT_METHOD(canSend:(RCTResponseSenderBlock)callback) +{ + callback(@[[NSNull null], @([MFMailComposeViewController canSendMail])]); +} + RCT_EXPORT_METHOD(mail:(NSDictionary *)options callback: (RCTResponseSenderBlock)callback) { diff --git a/android/src/main/java/com/chirag/RNMail/RNMailModule.java b/android/src/main/java/com/chirag/RNMail/RNMailModule.java index af50d0c..86a676d 100644 --- a/android/src/main/java/com/chirag/RNMail/RNMailModule.java +++ b/android/src/main/java/com/chirag/RNMail/RNMailModule.java @@ -4,6 +4,7 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; +import android.support.annotation.Nullable; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -50,53 +51,71 @@ private String[] readableArrayToStringArray(ReadableArray r) { return strArray; } - @ReactMethod - public void mail(ReadableMap options, Callback callback) { - Intent i = new Intent(Intent.ACTION_SENDTO); - i.setData(Uri.parse("mailto:")); - - if (options.hasKey("subject") && !options.isNull("subject")) { - i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject")); - } + private Intent createIntent(@Nullable ReadableMap options) { + Intent intent = new Intent(Intent.ACTION_SENDTO); + intent.setData(Uri.parse("mailto:")); + + if (options != null) { + if (options.hasKey("subject") && !options.isNull("subject")) { + intent.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject")); + } + + if (options.hasKey("body") && !options.isNull("body")) { + intent.putExtra(Intent.EXTRA_TEXT, options.getString("body")); + } + + if (options.hasKey("recipients") && !options.isNull("recipients")) { + ReadableArray recipients = options.getArray("recipients"); + intent.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients)); + } + + if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) { + ReadableArray ccRecipients = options.getArray("ccRecipients"); + intent.putExtra(Intent.EXTRA_CC, readableArrayToStringArray(ccRecipients)); + } + + if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) { + ReadableArray bccRecipients = options.getArray("bccRecipients"); + intent.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients)); + } + + if (options.hasKey("attachment") && !options.isNull("attachment")) { + ReadableMap attachment = options.getMap("attachment"); + if (attachment.hasKey("path") && !attachment.isNull("path")) { + String path = attachment.getString("path"); + File file = new File(path); + Uri p = Uri.fromFile(file); + intent.putExtra(Intent.EXTRA_STREAM, p); + } + } + } - if (options.hasKey("body") && !options.isNull("body")) { - i.putExtra(Intent.EXTRA_TEXT, options.getString("body")); - } + return intent; + } - if (options.hasKey("recipients") && !options.isNull("recipients")) { - ReadableArray recipients = options.getArray("recipients"); - i.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients)); - } + private int countIntentActivities(Intent intent) { + PackageManager manager = reactContext.getPackageManager(); + List list = manager.queryIntentActivities(intent, 0); - if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) { - ReadableArray ccRecipients = options.getArray("ccRecipients"); - i.putExtra(Intent.EXTRA_CC, readableArrayToStringArray(ccRecipients)); - } + return list.size(); + } - if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) { - ReadableArray bccRecipients = options.getArray("bccRecipients"); - i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients)); - } + @ReactMethod + public void canSend(Callback callback) { + Intent intent = createIntent(null); + boolean canSendMail = countIntentActivities(intent) > 0; - if (options.hasKey("attachment") && !options.isNull("attachment")) { - ReadableMap attachment = options.getMap("attachment"); - if (attachment.hasKey("path") && !attachment.isNull("path")) { - String path = attachment.getString("path"); - File file = new File(path); - Uri p = Uri.fromFile(file); - i.putExtra(Intent.EXTRA_STREAM, p); - } - } + callback.invoke(null, canSendMail); + } - PackageManager manager = reactContext.getPackageManager(); - List list = manager.queryIntentActivities(i, 0); + @ReactMethod + public void mail(ReadableMap options, Callback callback) { + Intent i = createIntent(options); + int numberOfActivities = countIntentActivities(i); - if (list == null || list.size() == 0) { + if (numberOfActivities == 0) { callback.invoke("not_available"); - return; - } - - if (list.size() == 1) { + } else if (numberOfActivities == 1) { i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { reactContext.startActivity(i);