Skip to content

Commit b8f1c6e

Browse files
authored
fix(email): android query email client availability (#428)
1 parent d7fa13a commit b8f1c6e

File tree

5 files changed

+48
-35
lines changed

5 files changed

+48
-35
lines changed

apps/demo-angular/src/plugin-demos/email.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<ScrollView class="h-full">
44
<StackLayout>
55
<Button text="Test email" (tap)="demoShared.testIt()" class="btn btn-primary"></Button>
6+
<Button text="Is available?" (tap)="demoShared.checkAvailability()" class="btn btn-primary"></Button>
67
</StackLayout>
78
</ScrollView>
89
</StackLayout>

apps/demo/src/plugin-demos/email.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ScrollView class="h-full">
88
<StackLayout>
99
<Button text="Test email" tap="{{ testIt }}" class="btn btn-primary"/>
10+
<Button text="Is available?" tap="{{ checkAvailability }}" class="btn btn-primary"/>
1011

1112
</StackLayout>
1213
</ScrollView>

packages/email/index.android.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, File, Folder } from '@nativescript/core';
1+
import { Application, File, Folder, Utils } from '@nativescript/core';
22

33
(function () {
44
_cleanAttachmentFolder();
@@ -12,7 +12,8 @@ const FileProviderPackageName = useAndroidX() ? global.androidx.core.content : (
1212
const _determineAvailability = function () {
1313
const uri = android.net.Uri.fromParts('mailto', '', null);
1414
const intent = new android.content.Intent(android.content.Intent.ACTION_SENDTO, uri);
15-
const packageManager = Application.android.context.getPackageManager();
15+
intent.addCategory(android.content.Intent.CATEGORY_DEFAULT);
16+
const packageManager = Utils.android.getApplicationContext().getPackageManager();
1617
const nrOfMailApps = packageManager.queryIntentActivities(intent, 0).size();
1718
return nrOfMailApps > 0;
1819
};
@@ -31,11 +32,8 @@ export function available() {
3132
export function compose(arg) {
3233
return new Promise(function (resolve, reject) {
3334
try {
34-
if (!_determineAvailability()) {
35-
reject('No mail available');
36-
}
37-
3835
const mail = new android.content.Intent(android.content.Intent.ACTION_SENDTO);
36+
3937
if (arg.body) {
4038
const htmlPattern = java.util.regex.Pattern.compile('.*\\<[^>]+>.*', java.util.regex.Pattern.DOTALL);
4139
if (htmlPattern.matcher(arg.body).matches()) {

packages/email/platforms/android/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@
1212
android:resource="@xml/provider_paths"/>
1313
</provider>
1414
</application>
15+
16+
<queries>
17+
<intent>
18+
<action android:name="android.intent.action.SENDTO"/>
19+
<category android:name="android.intent.category.DEFAULT"/>
20+
<data android:scheme="mailto"/>
21+
</intent>
22+
</queries>
1523
</manifest>

tools/demo/email/index.ts

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
import { DemoSharedBase } from '../utils';
2-
import { compose } from '@nativescript/email';
2+
import { available, compose } from '@nativescript/email';
33

44
export class DemoSharedEmail extends DemoSharedBase {
55
testIt() {
6-
compose({
7-
subject: 'Yo',
8-
body: 'Hello <strong>dude</strong> :)',
9-
to: ['eddyverbruggen@gmail.com', 'to@person2.com'],
10-
cc: ['ccperson@somewhere.com'],
11-
bcc: ['eddy@combidesk.com', 'eddy@x-services.nl'],
12-
attachments: [
13-
{
14-
fileName: 'arrow1.png',
15-
path:
16-
'base64://iVBORw0KGgoAAAANSUhEUgAAABYAAAAoCAYAAAD6xArmAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAHGlET1QAAAACAAAAAAAAABQAAAAoAAAAFAAAABQAAAB5EsHiAAAAAEVJREFUSA1iYKAimDhxYjwIU9FIBgaQgZMmTfoPwlOmTJGniuHIhlLNxaOGwiNqNEypkwlGk9RokoIUfaM5ijo5Clh9AAAAAP//ksWFvgAAAEFJREFUY5g4cWL8pEmT/oMwiM1ATTBqONbQHA2W0WDBGgJYBUdTy2iwYA0BrILDI7VMmTJFHqv3yBUEBQsIg/QDAJNpcv6v+k1ZAAAAAElFTkSuQmCC',
17-
mimeType: 'image/png',
18-
},
19-
// {
20-
// fileName: 'logo.png',
21-
// path: logoPath,
22-
// mimeType: 'image/png',
23-
// },
24-
],
25-
}).then(
26-
function () {
27-
console.log('Email composer closed');
28-
},
29-
function (err) {
30-
console.log('Error: ' + err);
31-
}
32-
);
33-
}
6+
compose({
7+
subject: 'Yo',
8+
body: 'Hello <strong>dude</strong> :)',
9+
to: ['eddyverbruggen@gmail.com', 'to@person2.com'],
10+
cc: ['ccperson@somewhere.com'],
11+
bcc: ['eddy@combidesk.com', 'eddy@x-services.nl'],
12+
attachments: [
13+
{
14+
fileName: 'arrow1.png',
15+
path: 'base64://iVBORw0KGgoAAAANSUhEUgAAABYAAAAoCAYAAAD6xArmAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAHGlET1QAAAACAAAAAAAAABQAAAAoAAAAFAAAABQAAAB5EsHiAAAAAEVJREFUSA1iYKAimDhxYjwIU9FIBgaQgZMmTfoPwlOmTJGniuHIhlLNxaOGwiNqNEypkwlGk9RokoIUfaM5ijo5Clh9AAAAAP//ksWFvgAAAEFJREFUY5g4cWL8pEmT/oMwiM1ATTBqONbQHA2W0WDBGgJYBUdTy2iwYA0BrILDI7VMmTJFHqv3yBUEBQsIg/QDAJNpcv6v+k1ZAAAAAElFTkSuQmCC',
16+
mimeType: 'image/png',
17+
},
18+
// {
19+
// fileName: 'logo.png',
20+
// path: logoPath,
21+
// mimeType: 'image/png',
22+
// },
23+
],
24+
}).then(
25+
function () {
26+
console.log('Email composer closed');
27+
},
28+
function (err) {
29+
console.log('Error: ' + err);
30+
}
31+
);
32+
}
33+
34+
checkAvailability() {
35+
available()
36+
.then((yes) => alert('Email is available? ' + yes))
37+
.catch((err) => console.error('Failed to check email availability', err));
38+
}
3439
}

0 commit comments

Comments
 (0)