-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.android.js
92 lines (92 loc) · 2.99 KB
/
index.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
import { hasPermission, requestPermission } from 'nativescript-permissions';
import { Application } from '@nativescript/core';
import { LocalEventEmitter } from './LocalEventEmitter';
import { SMSEvents, DialEvents } from './interfaces';
export { DialEvents, SMSEvents } from './interfaces';
export const NSPhoneEventEmitter = new LocalEventEmitter();
export function dial(telNum, prompt) {
try {
if (prompt === void 0) {
prompt = true;
}
let intentType = android.content.Intent.ACTION_DIAL;
if (prompt === false) {
const hasPerms = hasPermission(android.Manifest.permission.CALL_PHONE);
if (hasPerms === false) {
return 'Application does not have permission to call directly.';
}
intentType = android.content.Intent.ACTION_CALL;
}
const intent = new android.content.Intent(intentType);
telNum = telNum.replace('#', encodeURIComponent('#'));
intent.setData(android.net.Uri.parse('tel:' + telNum));
const activity =
Application.android.foregroundActivity ||
Application.android.startActivity;
activity.startActivity(intent);
NSPhoneEventEmitter.notify({
eventName: DialEvents.SUCCESS
});
return true;
} catch (ex) {
NSPhoneEventEmitter.notify({
eventName: DialEvents.ERROR,
data: {
error: ex
}
});
return false;
}
}
export function sms(smsNum, messageText) {
try {
if (!Array.isArray(smsNum)) {
smsNum = [smsNum];
}
const SEND_SMS = 1001;
const intent = new android.content.Intent(
android.content.Intent.ACTION_VIEW
);
intent.putExtra('sms_body', messageText);
intent.setType('vnd.android-dir/mms-sms');
intent.setData(android.net.Uri.parse('sms:' + smsNum.join(';')));
const activity =
Application.android.foregroundActivity ||
Application.android.startActivity;
activity.startActivityForResult(intent, SEND_SMS);
const previousResult = activity.onActivityResult;
activity.onActivityResult = (requestCode, resultCode, data) => {
activity.onActivityResult = previousResult;
if (requestCode === SEND_SMS) {
if (resultCode === android.app.Activity.RESULT_OK) {
NSPhoneEventEmitter.notify({
eventName: SMSEvents.SUCCESS
});
} else if (resultCode === android.app.Activity.RESULT_CANCELED) {
NSPhoneEventEmitter.notify({
eventName: SMSEvents.CANCELLED
});
} else {
NSPhoneEventEmitter.notify({
eventName: SMSEvents.UNKNOWN
});
}
} else {
if (typeof previousResult === 'function') {
previousResult(requestCode, resultCode, data);
}
}
};
} catch (error) {
NSPhoneEventEmitter.notify({
eventName: SMSEvents.ERROR,
data: {
error: error
}
});
}
}
export function requestCallPermission(explanation) {
return requestPermission(android.Manifest.permission.CALL_PHONE, explanation);
}
//# sourceMappingURL=index.android.js.map