forked from react-native-webrtc/react-native-callkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (143 loc) · 4.43 KB
/
index.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
import { NativeModules, Platform, Alert } from 'react-native';
import { listeners } from './actions'
const RNCallKeepModule = NativeModules.RNCallKeep;
const isIOS = Platform.OS === 'ios';
const supportConnectionService = !isIOS && Platform.Version >= 23;
class RNCallKeep {
constructor() {
this._callkitEventHandlers = new Map();
}
addEventListener = (type, handler) => {
const listener = listeners[type](handler);
this._callkitEventHandlers.set(handler, listener);
};
removeEventListener = (type, handler) => {
const listener = this._callkitEventHandlers.get(handler);
if (!listener) {
return;
}
listener.remove();
this._callkitEventHandlers.delete(handler);
};
setup = async (options) => {
if (!isIOS) {
return this._setupAndroid(options.android);
}
return this._setupIOS(options.ios);
};
hasDefaultPhoneAccount = async (options) => {
if (!isIOS) {
return this._hasDefaultPhoneAccount(options);
}
return;
};
displayIncomingCall = (uuid, handle, localizedCallerName, handleType = 'number', hasVideo = false) => {
if (!isIOS) {
RNCallKeepModule.displayIncomingCall(handle, localizedCallerName);
return;
}
RNCallKeepModule.displayIncomingCall(uuid, handle, handleType, hasVideo, localizedCallerName);
};
startCall = (uuid, handle, handleType = 'number', hasVideo = false, contactIdentifier) => {
if (!isIOS) {
RNCallKeepModule.startCall(handle, contactIdentifier);
return;
}
RNCallKeepModule.startCall(uuid, handle, handleType, hasVideo, contactIdentifier);
};
reportConnectedOutgoingCallWithUUID = (uuid) => {
RNCallKeepModule.reportConnectedOutgoingCallWithUUID(uuid);
};
endCall = (uuid) => {
isIOS ? RNCallKeepModule.endCall(uuid) : RNCallKeepModule.endCall();
};
endAllCalls = () => {
isIOS ? RNCallKeepModule.endAllCalls() : RNCallKeepModule.endCall();
};
supportConnectionService = () => supportConnectionService;
hasPhoneAccount = async () =>
isIOS ? true : await RNCallKeepModule.hasPhoneAccount();
setMutedCall = (uuid, muted) => {
if (!isIOS) {
// Can't mute on Android
return;
}
RNCallKeepModule.setMutedCall(uuid, muted);
};
checkIfBusy = () =>
Platform.OS === 'ios'
? RNCallKeepModule.checkIfBusy()
: Promise.reject('RNCallKeep.checkIfBusy was called from unsupported OS');
checkSpeaker = () =>
Platform.OS === 'ios'
? RNCallKeepModule.checkSpeaker()
: Promise.reject('RNCallKeep.checkSpeaker was called from unsupported OS');
setAvailable = (state) => {
if (isIOS) {
return;
}
// Tell android that we are able to make outgoing calls
RNCallKeepModule.setAvailable(state);
};
setCurrentCallActive = () => {
if (isIOS) {
return;
}
RNCallKeepModule.setCurrentCallActive();
};
_setupIOS = async (options) => new Promise((resolve, reject) => {
if (!options.appName) {
reject('RNCallKeep.setup: option "appName" is required');
}
if (typeof options.appName !== 'string') {
reject('RNCallKeep.setup: option "appName" should be of type "string"');
}
resolve(RNCallKeepModule.setup(options));
});
_setupAndroid = async (options) => {
const hasAccount = await RNCallKeepModule.checkPhoneAccountPermission();
const shouldOpenAccounts = await this._alert(options, hasAccount);
if (shouldOpenAccounts) {
RNCallKeepModule.openPhoneAccounts();
}
};
_hasDefaultPhoneAccount = async (options) => {
const hasDefault = await RNCallKeepModule.checkDefaultPhoneAccount();
const shouldOpenAccounts = await this._alert(options, hasDefault);
if (shouldOpenAccounts) {
RNCallKeepModule.openPhoneAccountSettings();
}
};
_alert = async (options, condition) => new Promise((resolve, reject) => {
if (condition) {
return resolve(false);
}
Alert.alert(
options.alertTitle,
options.alertDescription,
[
{
text: options.cancelButton,
onPress: reject,
style: 'cancel',
},
{ text: options.okButton,
onPress: () => resolve(true)
},
],
{ cancelable: true },
);
});
backToForeground() {
if (isIOS) {
return;
}
NativeModules.RNCallKeep.backToForeground();
}
/*
static holdCall(uuid, onHold) {
RNCallKeepModule.setHeldCall(uuid, onHold);
}
*/
}
export default new RNCallKeep();