Skip to content

Commit

Permalink
move logic over to MailManager
Browse files Browse the repository at this point in the history
  • Loading branch information
siddarthkay committed Apr 24, 2024
1 parent 33a6a4f commit 08f36a0
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 307 deletions.
6 changes: 6 additions & 0 deletions ios/StatusIm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,14 @@
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Status-StatusIm-StatusImTests/Pods-Status-StatusIm-StatusImTests-resources.sh",
"${PODS_CONFIGURATION_BUILD_DIR}/RNImageCropPicker/QBImagePicker.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/RNPermissions/RNPermissionsPrivacyInfo.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/React-Core/RCTI18nStrings.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/TOCropViewController/TOCropViewControllerBundle.bundle",
);
name = "[CP] Copy Pods Resources";
outputPaths = (
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/QBImagePicker.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/RNPermissionsPrivacyInfo.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/RCTI18nStrings.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/TOCropViewControllerBundle.bundle",
);
Expand Down Expand Up @@ -668,12 +670,14 @@
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Status-StatusIm/Pods-Status-StatusIm-resources.sh",
"${PODS_CONFIGURATION_BUILD_DIR}/RNImageCropPicker/QBImagePicker.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/RNPermissions/RNPermissionsPrivacyInfo.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/React-Core/RCTI18nStrings.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/TOCropViewController/TOCropViewControllerBundle.bundle",
);
name = "[CP] Copy Pods Resources";
outputPaths = (
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/QBImagePicker.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/RNPermissionsPrivacyInfo.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/RCTI18nStrings.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/TOCropViewControllerBundle.bundle",
);
Expand All @@ -690,12 +694,14 @@
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Status-StatusImPR/Pods-Status-StatusImPR-resources.sh",
"${PODS_CONFIGURATION_BUILD_DIR}/RNImageCropPicker/QBImagePicker.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/RNPermissions/RNPermissionsPrivacyInfo.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/React-Core/RCTI18nStrings.bundle",
"${PODS_CONFIGURATION_BUILD_DIR}/TOCropViewController/TOCropViewControllerBundle.bundle",
);
name = "[CP] Copy Pods Resources";
outputPaths = (
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/QBImagePicker.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/RNPermissionsPrivacyInfo.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/RCTI18nStrings.bundle",
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/TOCropViewControllerBundle.bundle",
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package im.status.ethereum.module

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.Callback
import android.util.Log
import android.content.Intent
import android.net.Uri
import android.content.pm.PackageManager
import java.io.File
import androidx.core.content.FileProvider
import android.text.Html

class MailManager(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

private val utils = Utils(reactContext)

override fun getName() = "MailManager"

@ReactMethod
fun mail(options: ReadableMap, callback: Callback) {
Log.d(TAG, "attempting to send email")
val i = Intent(Intent.ACTION_SEND_MULTIPLE)
val selectorIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
i.selector = selectorIntent

if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"))
}

if (options.hasKey("body") && !options.isNull("body")) {
val body = options.getString("body")
if (options.hasKey("isHTML") && options.getBoolean("isHTML")) {
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body))
} else {
i.putExtra(Intent.EXTRA_TEXT, body)
}
}

if (options.hasKey("recipients") && !options.isNull("recipients")) {
val recipients = options.getArray("recipients")
i.putExtra(Intent.EXTRA_EMAIL, this.utils.readableArrayToStringArray(recipients!!))
}

if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) {
val ccRecipients = options.getArray("ccRecipients")
i.putExtra(Intent.EXTRA_CC, this.utils.readableArrayToStringArray(ccRecipients!!))
}

if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
val bccRecipients = options.getArray("bccRecipients")
i.putExtra(Intent.EXTRA_BCC, this.utils.readableArrayToStringArray(bccRecipients!!))
}

if (options.hasKey("attachments") && !options.isNull("attachments")) {
val r = options.getArray("attachments")
val length = r?.size() ?: 0

val provider = reactContext.applicationContext.packageName + ".rnmail.provider"
val resolvedIntentActivities = reactContext.packageManager.queryIntentActivities(i,
PackageManager.MATCH_DEFAULT_ONLY)

val uris = ArrayList<Uri>()
for (keyIndex in 0 until length) {
val clip = r?.getMap(keyIndex)
val uri: Uri
if (clip?.hasKey("path") == true && !clip.isNull("path")) {
val path = clip.getString("path")
val file = File(path)
uri = FileProvider.getUriForFile(reactContext, provider, file)
} else if (clip?.hasKey("uri") == true && !clip.isNull("uri")) {
val uriPath = clip.getString("uri")
uri = Uri.parse(uriPath)
} else {
callback.invoke("not_found")
return
}
uris.add(uri)

for (resolvedIntentInfo in resolvedIntentActivities) {
val packageName = resolvedIntentInfo.activityInfo.packageName
reactContext.grantUriPermission(packageName, uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
}

i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
}

val manager = reactContext.packageManager
val list = manager.queryIntentActivities(i, 0)

if (list == null || list.isEmpty()) {
Log.d(TAG, "not_available")
callback.invoke("not_available")
return
}

if (list.size == 1) {
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
try {
reactContext.startActivity(i)
} catch (ex: Exception) {
Log.e(TAG, ex.message!!)
callback.invoke("error")
}
} else {
var chooserTitle = "Send Mail"

if (options.hasKey("customChooserTitle") && !options.isNull("customChooserTitle")) {
chooserTitle = options.getString("customChooserTitle") ?: ""
}

val chooser = Intent.createChooser(i, chooserTitle)
chooser.flags = Intent.FLAG_ACTIVITY_NEW_TASK

try {
reactContext.startActivity(chooser)
} catch (ex: Exception) {
Log.e(TAG, ex.message!!)
callback.invoke("error")
}
}
}

companion object {
private const val TAG = "MailManager"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,113 +84,6 @@ class NetworkManager(private val reactContext: ReactApplicationContext) : ReactC
utils.executeRunnableStatusGoMethod({ Statusgo.recover(rpcParams) }, callback)
}

@ReactMethod
fun mail(options: ReadableMap, callback: Callback) {
Log.d(TAG, "Just starting mail")
val i = Intent(Intent.ACTION_SEND_MULTIPLE)
val selectorIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
i.selector = selectorIntent

if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"))
}

if (options.hasKey("body") && !options.isNull("body")) {
val body = options.getString("body")
if (options.hasKey("isHTML") && options.getBoolean("isHTML")) {
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body))
} else {
i.putExtra(Intent.EXTRA_TEXT, body)
}
}

if (options.hasKey("recipients") && !options.isNull("recipients")) {
val recipients = options.getArray("recipients")
i.putExtra(Intent.EXTRA_EMAIL, this.utils.readableArrayToStringArray(recipients!!))
}

if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) {
val ccRecipients = options.getArray("ccRecipients")
i.putExtra(Intent.EXTRA_CC, this.utils.readableArrayToStringArray(ccRecipients!!))
}

if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
val bccRecipients = options.getArray("bccRecipients")
i.putExtra(Intent.EXTRA_BCC, this.utils.readableArrayToStringArray(bccRecipients!!))
}

if (options.hasKey("attachments") && !options.isNull("attachments")) {
val r = options.getArray("attachments")
val length = r?.size() ?: 0

val provider = reactContext.applicationContext.packageName + ".rnmail.provider"
val resolvedIntentActivities = reactContext.packageManager.queryIntentActivities(i,
PackageManager.MATCH_DEFAULT_ONLY)

val uris = ArrayList<Uri>()
for (keyIndex in 0 until length) {
val clip = r?.getMap(keyIndex)
val uri: Uri
if (clip?.hasKey("path") == true && !clip.isNull("path")) {
val path = clip.getString("path")
val file = File(path)
uri = FileProvider.getUriForFile(reactContext, provider, file)
} else if (clip?.hasKey("uri") == true && !clip.isNull("uri")) {
val uriPath = clip.getString("uri")
uri = Uri.parse(uriPath)
} else {
callback.invoke("not_found")
return
}
uris.add(uri)

for (resolvedIntentInfo in resolvedIntentActivities) {
val packageName = resolvedIntentInfo.activityInfo.packageName
reactContext.grantUriPermission(packageName, uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
}

i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
}

val manager = reactContext.packageManager
val list = manager.queryIntentActivities(i, 0)

if (list == null || list.isEmpty()) {
Log.d(TAG, "not_available")
callback.invoke("not_available")
return
}

if (list.size == 1) {
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
try {
reactContext.startActivity(i)
} catch (ex: Exception) {
Log.e(TAG, ex.message!!)
callback.invoke("error")
}
} else {
var chooserTitle = "Send Mail"

if (options.hasKey("customChooserTitle") && !options.isNull("customChooserTitle")) {
chooserTitle = options.getString("customChooserTitle") ?: ""
}

val chooser = Intent.createChooser(i, chooserTitle)
chooser.flags = Intent.FLAG_ACTIVITY_NEW_TASK

try {
reactContext.startActivity(chooser)
} catch (ex: Exception) {
Log.e(TAG, ex.message!!)
callback.invoke("error")
}
}
}

companion object {
private const val TAG = "NetworkManager"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class StatusPackage(private val rootedDevice: Boolean) : ReactPackage {
add(LogManager(reactContext))
add(Utils(reactContext))
add(NetworkManager(reactContext))
add(MailManager(reactContext))
add(RNSelectableTextInputModule(reactContext))
}

Expand Down
9 changes: 9 additions & 0 deletions modules/react-native-status/ios/RCTStatus/MailManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <sys/utsname.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <React/RCTBridgeModule.h>
#import "RCTLog.h"

@interface MailManager : NSObject <RCTBridgeModule, MFMailComposeViewControllerDelegate>

@end
Loading

0 comments on commit 08f36a0

Please sign in to comment.