forked from react-native-webrtc/react-native-callkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
260 lines (200 loc) · 6.47 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { NativeModules, Platform, Alert } from 'react-native';
import { listeners, emit } from './actions'
const RNCallKeepModule = NativeModules.RNCallKeep;
const isIOS = Platform.OS === 'ios';
const supportConnectionService = !isIOS && Platform.Version >= 23;
const CONSTANTS = {
END_CALL_REASONS: {
FAILED: 1,
REMOTE_ENDED: 2,
UNANSWERED: 3,
ANSWERED_ELSEWHERE: 4,
DECLINED_ELSEWHERE: isIOS ? 5 : 2, // make declined elsewhere link to "Remote ended" on android because that's kinda true
MISSED: isIOS ? 2 : 6 }
};
export { CONSTANTS };
class RNCallKeep {
constructor() {
this._callkeepEventHandlers = new Map();
this.addEventListener('didLoadWithEvents', (events) => {
events.forEach(event => {
emit(event.name, event.data);
});
});
}
addEventListener = (type, handler) => {
const listener = listeners[type](handler);
this._callkeepEventHandlers.set(type, listener);
};
removeEventListener = (type) => {
const listener = this._callkeepEventHandlers.get(type);
if (!listener) {
return;
}
listener.remove();
this._callkeepEventHandlers.delete(type);
};
setup = async (options) => {
if (!isIOS) {
return this._setupAndroid(options.android);
}
return this._setupIOS(options.ios);
};
registerPhoneAccount = () => {
if (isIOS) {
return;
}
RNCallKeepModule.registerPhoneAccount();
};
registerAndroidEvents = () => {
if (isIOS) {
return;
}
RNCallKeepModule.registerEvents();
};
hasDefaultPhoneAccount = async (options) => {
if (!isIOS) {
return this._hasDefaultPhoneAccount(options);
}
return;
};
displayIncomingCall = (uuid, handle, localizedCallerName = '', handleType = 'number', hasVideo = false) => {
if (!isIOS) {
RNCallKeepModule.displayIncomingCall(uuid, handle, localizedCallerName);
return;
}
RNCallKeepModule.displayIncomingCall(uuid, handle, handleType, hasVideo, localizedCallerName);
};
answerIncomingCall = (uuid) => {
if (!isIOS) {
RNCallKeepModule.answerIncomingCall(uuid);
}
};
startCall = (uuid, handle, contactIdentifier, handleType = 'number', hasVideo = false ) => {
if (!isIOS) {
RNCallKeepModule.startCall(uuid, handle, contactIdentifier);
return;
}
RNCallKeepModule.startCall(uuid, handle, contactIdentifier, handleType, hasVideo);
};
reportConnectingOutgoingCallWithUUID = (uuid) => {
//only available on iOS
if (isIOS) {
RNCallKeepModule.reportConnectingOutgoingCallWithUUID(uuid);
}
};
reportConnectedOutgoingCallWithUUID = (uuid) => {
//only available on iOS
if (isIOS) {
RNCallKeepModule.reportConnectedOutgoingCallWithUUID(uuid);
}
};
reportEndCallWithUUID = (uuid, reason) => RNCallKeepModule.reportEndCallWithUUID(uuid, reason);
/*
* Android explicitly states we reject a call
* On iOS we just notify of an endCall
*/
rejectCall = (uuid) => {
if (!isIOS) {
RNCallKeepModule.rejectCall(uuid);
} else {
RNCallKeepModule.endCall(uuid);
}
};
isCallActive = async(uuid) => await RNCallKeepModule.isCallActive(uuid);
endCall = (uuid) => RNCallKeepModule.endCall(uuid);
endAllCalls = () => RNCallKeepModule.endAllCalls();
supportConnectionService = () => supportConnectionService;
hasPhoneAccount = async () =>
isIOS ? true : await RNCallKeepModule.hasPhoneAccount();
hasOutgoingCall = async () =>
isIOS ? null : await RNCallKeepModule.hasOutgoingCall();
setMutedCall = (uuid, shouldMute) => {
RNCallKeepModule.setMutedCall(uuid, shouldMute);
};
sendDTMF = (uuid, key) => RNCallKeepModule.sendDTMF(uuid, key);
checkIfBusy = () =>
isIOS
? RNCallKeepModule.checkIfBusy()
: Promise.reject('RNCallKeep.checkIfBusy was called from unsupported OS');
checkSpeaker = () =>
isIOS
? 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 = (callUUID) => {
if (isIOS) {
return;
}
RNCallKeepModule.setCurrentCallActive(callUUID);
};
updateDisplay = (uuid, displayName, handle) => RNCallKeepModule.updateDisplay(uuid, displayName, handle);
setOnHold = (uuid, shouldHold) => RNCallKeepModule.setOnHold(uuid, shouldHold);
setReachable = () => RNCallKeepModule.setReachable();
// @deprecated
reportUpdatedCall = (uuid, localizedCallerName) => {
console.warn('RNCallKeep.reportUpdatedCall is deprecated, use RNCallKeep.updateDisplay instead');
return isIOS
? RNCallKeepModule.reportUpdatedCall(uuid, localizedCallerName)
: Promise.reject('RNCallKeep.reportUpdatedCall was called from unsupported OS');
};
_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) => {
RNCallKeepModule.setup(options);
const showAccountAlert = await RNCallKeepModule.checkPhoneAccountPermission(options.additionalPermissions || []);
const shouldOpenAccounts = await this._alert(options, showAccountAlert);
if (shouldOpenAccounts) {
RNCallKeepModule.openPhoneAccounts();
return true;
}
return false;
};
_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();
}
}
export default new RNCallKeep();