Skip to content

Commit ba4197a

Browse files
author
Sylvain Boily
authored
Merge pull request #366 from Jerome91410/ios-get-current-calls
feat: add getCalls method for ios only
2 parents 2da7e8b + dbb8a1a commit ba4197a

File tree

4 files changed

+120
-53
lines changed

4 files changed

+120
-53
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,33 @@ RNCallKeep.isCallActive(uuid);
187187
- `uuid`: string
188188
- The `uuid` used for `startCall` or `displayIncomingCall`
189189

190+
191+
### getCalls
192+
193+
_This feature is available only on IOS._
194+
195+
Returns a Promise. The result will be an array with all current calls and their states.
196+
197+
```js
198+
RNCallKeep.getCalls();
199+
200+
response:
201+
[{
202+
callUUID: "E26B14F7-2CDF-48D0-9925-532199AE7C48"
203+
hasConnected: true
204+
hasEnded: false
205+
onHold: false
206+
outgoing: false
207+
}]
208+
```
209+
190210
### displayIncomingCall
191211

192212
Display system UI for incoming calls
193213

194-
````js
214+
```js
195215
RNCallKeep.displayIncomingCall(uuid, handle, localizedCallerName);
196-
````
216+
```
197217

198218
- `uuid`: string
199219
- An `uuid` that should be stored and re-used for `stopCall`.

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ declare module 'react-native-callkeep' {
109109
*/
110110
static isCallActive(uuid: string): Promise<boolean>
111111

112+
static getCalls(): Promise<object>
113+
112114
/**
113115
* @description supportConnectionService method is available only on Android.
114116
*/

index.js

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NativeModules, Platform, Alert } from 'react-native';
22

3-
import { listeners, emit } from './actions'
3+
import { listeners, emit } from './actions';
44

55
const RNCallKeepModule = NativeModules.RNCallKeep;
66
const isIOS = Platform.OS === 'ios';
@@ -13,13 +13,13 @@ const CONSTANTS = {
1313
UNANSWERED: 3,
1414
ANSWERED_ELSEWHERE: 4,
1515
DECLINED_ELSEWHERE: isIOS ? 5 : 2, // make declined elsewhere link to "Remote ended" on android because that's kinda true
16-
MISSED: isIOS ? 2 : 6 }
16+
MISSED: isIOS ? 2 : 6,
17+
},
1718
};
1819

1920
export { CONSTANTS };
2021

2122
class RNCallKeep {
22-
2323
constructor() {
2424
this._callkeepEventHandlers = new Map();
2525
}
@@ -55,7 +55,6 @@ class RNCallKeep {
5555
RNCallKeepModule.registerPhoneAccount();
5656
};
5757

58-
5958
registerAndroidEvents = () => {
6059
if (isIOS) {
6160
return;
@@ -71,7 +70,14 @@ class RNCallKeep {
7170
return;
7271
};
7372

74-
displayIncomingCall = (uuid, handle, localizedCallerName = '', handleType = 'number', hasVideo = false, options = null) => {
73+
displayIncomingCall = (
74+
uuid,
75+
handle,
76+
localizedCallerName = '',
77+
handleType = 'number',
78+
hasVideo = false,
79+
options = null
80+
) => {
7581
if (!isIOS) {
7682
RNCallKeepModule.displayIncomingCall(uuid, handle, localizedCallerName);
7783
return;
@@ -83,7 +89,17 @@ class RNCallKeep {
8389
let supportsGrouping = !!(options?.ios?.supportsGrouping ?? true);
8490
let supportsUngrouping = !!(options?.ios?.supportsUngrouping ?? true);
8591

86-
RNCallKeepModule.displayIncomingCall(uuid, handle, handleType, hasVideo, localizedCallerName, supportsHolding, supportsDTMF, supportsGrouping, supportsUngrouping);
92+
RNCallKeepModule.displayIncomingCall(
93+
uuid,
94+
handle,
95+
handleType,
96+
hasVideo,
97+
localizedCallerName,
98+
supportsHolding,
99+
supportsDTMF,
100+
supportsGrouping,
101+
supportsUngrouping
102+
);
87103
};
88104

89105
answerIncomingCall = (uuid) => {
@@ -92,7 +108,7 @@ class RNCallKeep {
92108
}
93109
};
94110

95-
startCall = (uuid, handle, contactIdentifier, handleType = 'number', hasVideo = false ) => {
111+
startCall = (uuid, handle, contactIdentifier, handleType = 'number', hasVideo = false) => {
96112
if (!isIOS) {
97113
RNCallKeepModule.startCall(uuid, handle, contactIdentifier);
98114
return;
@@ -107,15 +123,15 @@ class RNCallKeep {
107123
}
108124

109125
return RNCallKeepModule.checkPhoneAccountEnabled();
110-
}
126+
};
111127

112128
isConnectionServiceAvailable = async () => {
113129
if (isIOS) {
114130
return true;
115131
}
116132

117133
return RNCallKeepModule.isConnectionServiceAvailable();
118-
}
134+
};
119135

120136
reportConnectingOutgoingCallWithUUID = (uuid) => {
121137
//only available on iOS
@@ -145,19 +161,23 @@ class RNCallKeep {
145161
}
146162
};
147163

148-
isCallActive = async(uuid) => await RNCallKeepModule.isCallActive(uuid);
164+
isCallActive = async (uuid) => await RNCallKeepModule.isCallActive(uuid);
165+
166+
getCalls = () => {
167+
if (isIOS) {
168+
return RNCallKeepModule.getCalls();
169+
}
170+
};
149171

150172
endCall = (uuid) => RNCallKeepModule.endCall(uuid);
151173

152174
endAllCalls = () => RNCallKeepModule.endAllCalls();
153175

154176
supportConnectionService = () => supportConnectionService;
155177

156-
hasPhoneAccount = async () =>
157-
isIOS ? true : await RNCallKeepModule.hasPhoneAccount();
178+
hasPhoneAccount = async () => (isIOS ? true : await RNCallKeepModule.hasPhoneAccount());
158179

159-
hasOutgoingCall = async () =>
160-
isIOS ? null : await RNCallKeepModule.hasOutgoingCall();
180+
hasOutgoingCall = async () => (isIOS ? null : await RNCallKeepModule.hasOutgoingCall());
161181

162182
setMutedCall = (uuid, shouldMute) => {
163183
RNCallKeepModule.setMutedCall(uuid, shouldMute);
@@ -166,14 +186,10 @@ class RNCallKeep {
166186
sendDTMF = (uuid, key) => RNCallKeepModule.sendDTMF(uuid, key);
167187

168188
checkIfBusy = () =>
169-
isIOS
170-
? RNCallKeepModule.checkIfBusy()
171-
: Promise.reject('RNCallKeep.checkIfBusy was called from unsupported OS');
189+
isIOS ? RNCallKeepModule.checkIfBusy() : Promise.reject('RNCallKeep.checkIfBusy was called from unsupported OS');
172190

173191
checkSpeaker = () =>
174-
isIOS
175-
? RNCallKeepModule.checkSpeaker()
176-
: Promise.reject('RNCallKeep.checkSpeaker was called from unsupported OS');
192+
isIOS ? RNCallKeepModule.checkSpeaker() : Promise.reject('RNCallKeep.checkSpeaker was called from unsupported OS');
177193

178194
setAvailable = (state) => {
179195
if (isIOS) {
@@ -220,7 +236,7 @@ class RNCallKeep {
220236
if (options && options.ios) {
221237
iosOptions = {
222238
...options.ios,
223-
}
239+
};
224240
}
225241
RNCallKeepModule.updateDisplay(uuid, displayName, handle, iosOptions);
226242
};
@@ -238,16 +254,17 @@ class RNCallKeep {
238254
: Promise.reject('RNCallKeep.reportUpdatedCall was called from unsupported OS');
239255
};
240256

241-
_setupIOS = async (options) => new Promise((resolve, reject) => {
242-
if (!options.appName) {
243-
reject('RNCallKeep.setup: option "appName" is required');
244-
}
245-
if (typeof options.appName !== 'string') {
246-
reject('RNCallKeep.setup: option "appName" should be of type "string"');
247-
}
257+
_setupIOS = async (options) =>
258+
new Promise((resolve, reject) => {
259+
if (!options.appName) {
260+
reject('RNCallKeep.setup: option "appName" is required');
261+
}
262+
if (typeof options.appName !== 'string') {
263+
reject('RNCallKeep.setup: option "appName" should be of type "string"');
264+
}
248265

249-
resolve(RNCallKeepModule.setup(options));
250-
});
266+
resolve(RNCallKeepModule.setup(options));
267+
});
251268

252269
_setupAndroid = async (options) => {
253270
RNCallKeepModule.setup(options);
@@ -272,27 +289,26 @@ class RNCallKeep {
272289
}
273290
};
274291

275-
_alert = async (options, condition) => new Promise((resolve, reject) => {
276-
if (!condition) {
277-
return resolve(false);
278-
}
292+
_alert = async (options, condition) =>
293+
new Promise((resolve, reject) => {
294+
if (!condition) {
295+
return resolve(false);
296+
}
279297

280-
Alert.alert(
281-
options.alertTitle,
282-
options.alertDescription,
283-
[
284-
{
285-
text: options.cancelButton,
286-
onPress: reject,
287-
style: 'cancel',
288-
},
289-
{ text: options.okButton,
290-
onPress: () => resolve(true)
291-
},
292-
],
293-
{ cancelable: true },
294-
);
295-
});
298+
Alert.alert(
299+
options.alertTitle,
300+
options.alertDescription,
301+
[
302+
{
303+
text: options.cancelButton,
304+
onPress: reject,
305+
style: 'cancel',
306+
},
307+
{ text: options.okButton, onPress: () => resolve(true) },
308+
],
309+
{ cancelable: true }
310+
);
311+
});
296312

297313
backToForeground() {
298314
if (isIOS) {
@@ -301,7 +317,6 @@ class RNCallKeep {
301317

302318
NativeModules.RNCallKeep.backToForeground();
303319
}
304-
305320
}
306321

307322
export default new RNCallKeep();

ios/RNCallKeep/RNCallKeep.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ + (void)initCallKitProvider {
350350
}
351351
}
352352

353+
RCT_EXPORT_METHOD(getCalls:(RCTPromiseResolveBlock)resolve
354+
rejecter:(RCTPromiseRejectBlock)reject)
355+
{
356+
#ifdef DEBUG
357+
NSLog(@"[RNCallKeep][getCalls]");
358+
#endif
359+
resolve([RNCallKeep getCalls]);
360+
}
361+
353362
- (void)requestTransaction:(CXTransaction *)transaction
354363
{
355364
#ifdef DEBUG
@@ -395,6 +404,27 @@ + (BOOL)isCallActive:(NSString *)uuidString
395404
return false;
396405
}
397406

407+
+ (NSMutableArray *) getCalls
408+
{
409+
#ifdef DEBUG
410+
NSLog(@"[RNCallKeep][getCalls]");
411+
#endif
412+
CXCallObserver *callObserver = [[CXCallObserver alloc] init];
413+
NSMutableArray *currentCalls = [NSMutableArray array];
414+
for(CXCall *call in callObserver.calls){
415+
NSString *uuidString = [call.UUID UUIDString];
416+
NSDictionary *requestedCall= @{
417+
@"callUUID": uuidString,
418+
@"outgoing": call.outgoing? @YES : @NO,
419+
@"onHold": call.onHold? @YES : @NO,
420+
@"hasConnected": call.hasConnected ? @YES : @NO,
421+
@"hasEnded": call.hasEnded ? @YES : @NO
422+
};
423+
[currentCalls addObject:requestedCall];
424+
}
425+
return currentCalls;
426+
}
427+
398428
+ (void)endCallWithUUID:(NSString *)uuidString
399429
reason:(int)reason
400430
{

0 commit comments

Comments
 (0)