|
| 1 | +import { Application, File, Folder } from "@nativescript/core"; |
| 2 | + |
| 3 | +(function () { |
| 4 | + _cleanAttachmentFolder(); |
| 5 | +})(); |
| 6 | + |
| 7 | +const _determineAvailability = function () { |
| 8 | + const uri = android.net.Uri.fromParts("mailto", "", null); |
| 9 | + const intent = new android.content.Intent(android.content.Intent.ACTION_SENDTO, uri); |
| 10 | + const packageManager = Application.android.context.getPackageManager(); |
| 11 | + const nrOfMailApps = packageManager.queryIntentActivities(intent, 0).size(); |
| 12 | + return nrOfMailApps > 0; |
| 13 | +}; |
| 14 | + |
| 15 | +export function available() { |
| 16 | + return new Promise(function (resolve, reject) { |
| 17 | + try { |
| 18 | + resolve(_determineAvailability()); |
| 19 | + } catch (ex) { |
| 20 | + console.log("Error in email.available: " + ex); |
| 21 | + reject(ex); |
| 22 | + } |
| 23 | + }); |
| 24 | +}; |
| 25 | + |
| 26 | +export function compose(arg) { |
| 27 | + return new Promise(function (resolve, reject) { |
| 28 | + try { |
| 29 | + |
| 30 | + if (!_determineAvailability()) { |
| 31 | + reject("No mail available"); |
| 32 | + } |
| 33 | + |
| 34 | + const mail = new android.content.Intent(android.content.Intent.ACTION_SENDTO); |
| 35 | + if (arg.body) { |
| 36 | + const htmlPattern = java.util.regex.Pattern.compile(".*\\<[^>]+>.*", java.util.regex.Pattern.DOTALL); |
| 37 | + if (htmlPattern.matcher(arg.body).matches()) { |
| 38 | + mail.putExtra(android.content.Intent.EXTRA_TEXT, android.text.Html.fromHtml(arg.body)); |
| 39 | + mail.setType("text/html"); |
| 40 | + } else { |
| 41 | + mail.putExtra(android.content.Intent.EXTRA_TEXT, arg.body); |
| 42 | + mail.setType("text/plain"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (arg.subject) { |
| 47 | + mail.putExtra(android.content.Intent.EXTRA_SUBJECT, arg.subject); |
| 48 | + } |
| 49 | + if (arg.to) { |
| 50 | + mail.putExtra(android.content.Intent.EXTRA_EMAIL, toStringArray(arg.to)); |
| 51 | + } |
| 52 | + if (arg.cc) { |
| 53 | + mail.putExtra(android.content.Intent.EXTRA_CC, toStringArray(arg.cc)); |
| 54 | + } |
| 55 | + if (arg.bcc) { |
| 56 | + mail.putExtra(android.content.Intent.EXTRA_BCC, toStringArray(arg.bcc)); |
| 57 | + } |
| 58 | + |
| 59 | + if (arg.attachments) { |
| 60 | + const uris = new java.util.ArrayList(); |
| 61 | + for (const a in arg.attachments) { |
| 62 | + const attachment = arg.attachments[a]; |
| 63 | + const path = attachment.path; |
| 64 | + const fileName = attachment.fileName; |
| 65 | + const uri = _getUriForPath(path, fileName, Application.android.context); |
| 66 | + |
| 67 | + if (!uri) { |
| 68 | + reject("File not found for path: " + path); |
| 69 | + return; |
| 70 | + } |
| 71 | + uris.add(uri); |
| 72 | + } |
| 73 | + |
| 74 | + if (!uris.isEmpty()) { |
| 75 | + // required for Android 7+ (alternative is using a FileProvider (which is a better solution btw)) |
| 76 | + const builder = new android.os.StrictMode.VmPolicy.Builder(); |
| 77 | + android.os.StrictMode.setVmPolicy(builder.build()); |
| 78 | + |
| 79 | + mail.setAction(android.content.Intent.ACTION_SEND_MULTIPLE); |
| 80 | + mail.setType("message/rfc822"); |
| 81 | + mail.putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, uris); |
| 82 | + } |
| 83 | + } else { |
| 84 | + mail.setData(android.net.Uri.parse("mailto:")); |
| 85 | + } |
| 86 | + |
| 87 | + mail.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK); |
| 88 | + |
| 89 | + // we can wire up an intent receiver but it's always the same resultCode (0, canceled) anyway |
| 90 | + Application.android.context.startActivity(mail); |
| 91 | + resolve(true); |
| 92 | + } catch (ex) { |
| 93 | + console.log("Error in email.compose: " + ex); |
| 94 | + reject(ex); |
| 95 | + } |
| 96 | + }); |
| 97 | +}; |
| 98 | + |
| 99 | +function _getUriForPath(path, fileName, ctx) { |
| 100 | + if (path.indexOf("file:///") === 0) { |
| 101 | + return _getUriForAbsolutePath(path); |
| 102 | + } else if (path.indexOf("file://") === 0) { |
| 103 | + return _getUriForAssetPath(path, fileName, ctx); |
| 104 | + } else if (path.indexOf("base64:") === 0) { |
| 105 | + return _getUriForBase64Content(path, fileName, ctx); |
| 106 | + } else { |
| 107 | + if (path.indexOf(ctx.getPackageName()) > -1) { |
| 108 | + return _getUriForAssetPath(path, fileName, ctx); |
| 109 | + } else { |
| 110 | + return _getUriForAbsolutePath(path); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +function _getUriForAbsolutePath(path) { |
| 116 | + const absPath = path.replace("file://", ""); |
| 117 | + const file = new java.io.File(absPath); |
| 118 | + if (!file.exists()) { |
| 119 | + console.log("File not found: " + file.getAbsolutePath()); |
| 120 | + return null; |
| 121 | + } else { |
| 122 | + return android.net.Uri.fromFile(file); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function _getUriForAssetPath(path, fileName, ctx) { |
| 127 | + path = path.replace("file://", "/"); |
| 128 | + if (!File.exists(path)) { |
| 129 | + console.log("File does not exist: " + path); |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + const localFile = File.fromPath(path); |
| 134 | + const localFileContents = localFile.readSync(function (e) { |
| 135 | + console.log('read file error:', e); |
| 136 | + }); |
| 137 | + |
| 138 | + let cacheFileName = _writeBytesToFile(ctx, fileName, localFileContents); |
| 139 | + if (cacheFileName.indexOf("file://") === -1) { |
| 140 | + cacheFileName = "file://" + cacheFileName; |
| 141 | + } |
| 142 | + return android.net.Uri.parse(cacheFileName); |
| 143 | +} |
| 144 | + |
| 145 | +function _getUriForBase64Content(path, fileName, ctx) { |
| 146 | + const resData = path.substring(path.indexOf("://") + 3); |
| 147 | + let bytes; |
| 148 | + try { |
| 149 | + bytes = android.util.Base64.decode(resData, 0); |
| 150 | + } catch (ex) { |
| 151 | + console.log("Invalid Base64 string: " + resData); |
| 152 | + return android.net.Uri.EMPTY; |
| 153 | + } |
| 154 | + const cacheFileName = _writeBytesToFile(ctx, fileName, bytes); |
| 155 | + |
| 156 | + return android.net.Uri.parse(cacheFileName); |
| 157 | +} |
| 158 | + |
| 159 | +function _writeBytesToFile(ctx, fileName, contents) { |
| 160 | + const dir = ctx.getExternalCacheDir(); |
| 161 | + |
| 162 | + if (dir === null) { |
| 163 | + console.log("Missing external cache dir"); |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + const storage = dir.toString() + "/emailcomposer"; |
| 168 | + let cacheFileName = storage + "/" + fileName; |
| 169 | + |
| 170 | + const toFile = File.fromPath(cacheFileName); |
| 171 | + toFile.writeSync(contents, function (e) { |
| 172 | + console.log('write file error:', e); |
| 173 | + }); |
| 174 | + |
| 175 | + if (cacheFileName.indexOf("file://") === -1) { |
| 176 | + cacheFileName = "file://" + cacheFileName; |
| 177 | + } |
| 178 | + return cacheFileName; |
| 179 | +} |
| 180 | + |
| 181 | +function _cleanAttachmentFolder() { |
| 182 | + |
| 183 | + if (Application.android.context) { |
| 184 | + const dir = Application.android.context.getExternalCacheDir(); |
| 185 | + |
| 186 | + if (dir === null) { |
| 187 | + console.log("Missing external cache dir"); |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + const storage = dir.toString() + "/emailcomposer"; |
| 192 | + const cacheFolder = Folder.fromPath(storage); |
| 193 | + cacheFolder.clear(); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +const toStringArray = function (arg) { |
| 198 | + const arr = java.lang.reflect.Array.newInstance(java.lang.String.class, arg.length); |
| 199 | + for (let i = 0; i < arg.length; i++) { |
| 200 | + arr[i] = arg[i]; |
| 201 | + } |
| 202 | + return arr; |
| 203 | +}; |
0 commit comments