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