|
@@ -0,0 +1,334 @@
|
|
1
|
+//
|
|
2
|
+// Based on RNCallKit
|
|
3
|
+//
|
|
4
|
+// Original license:
|
|
5
|
+//
|
|
6
|
+// Copyright (c) 2016, Ian Yu-Hsun Lin
|
|
7
|
+//
|
|
8
|
+// Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
+// purpose with or without fee is hereby granted, provided that the above
|
|
10
|
+// copyright notice and this permission notice appear in all copies.
|
|
11
|
+//
|
|
12
|
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
13
|
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
14
|
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
15
|
+// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
16
|
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
17
|
+// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
18
|
+// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
19
|
+//
|
|
20
|
+
|
|
21
|
+#import <AVFoundation/AVFoundation.h>
|
|
22
|
+#import <Foundation/Foundation.h>
|
|
23
|
+#import <UIKit/UIKit.h>
|
|
24
|
+
|
|
25
|
+#import <React/RCTBridge.h>
|
|
26
|
+#import <React/RCTConvert.h>
|
|
27
|
+#import <React/RCTEventEmitter.h>
|
|
28
|
+#import <React/RCTEventDispatcher.h>
|
|
29
|
+#import <React/RCTUtils.h>
|
|
30
|
+
|
|
31
|
+// Weakly load CallKit, because it's not available on iOS 9.
|
|
32
|
+@import CallKit;
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+// Events we will emit.
|
|
36
|
+static NSString *const RNCallKitPerformAnswerCallAction = @"performAnswerCallAction";
|
|
37
|
+static NSString *const RNCallKitPerformEndCallAction = @"performEndCallAction";
|
|
38
|
+static NSString *const RNCallKitPerformSetMutedCallAction = @"performSetMutedCallAction";
|
|
39
|
+static NSString *const RNCallKitProviderDidReset = @"providerDidReset";
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+@interface RNCallKit : RCTEventEmitter <CXProviderDelegate>
|
|
43
|
+@end
|
|
44
|
+
|
|
45
|
+@implementation RNCallKit
|
|
46
|
+{
|
|
47
|
+ CXCallController *callKitCallController;
|
|
48
|
+ CXProvider *callKitProvider;
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+RCT_EXPORT_MODULE()
|
|
52
|
+
|
|
53
|
+- (NSArray<NSString *> *)supportedEvents
|
|
54
|
+{
|
|
55
|
+ return @[
|
|
56
|
+ RNCallKitPerformAnswerCallAction,
|
|
57
|
+ RNCallKitPerformEndCallAction,
|
|
58
|
+ RNCallKitPerformSetMutedCallAction,
|
|
59
|
+ RNCallKitProviderDidReset
|
|
60
|
+ ];
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+// Configure CallKit
|
|
64
|
+RCT_EXPORT_METHOD(setup:(NSDictionary *)options)
|
|
65
|
+{
|
|
66
|
+#ifdef DEBUG
|
|
67
|
+ NSLog(@"[RNCallKit][setup] options = %@", options);
|
|
68
|
+#endif
|
|
69
|
+ callKitCallController = [[CXCallController alloc] init];
|
|
70
|
+ if (callKitProvider) {
|
|
71
|
+ [callKitProvider invalidate];
|
|
72
|
+ }
|
|
73
|
+ callKitProvider = [[CXProvider alloc] initWithConfiguration:[self getProviderConfiguration: options]];
|
|
74
|
+ [callKitProvider setDelegate:self queue:nil];
|
|
75
|
+}
|
|
76
|
+
|
|
77
|
+#pragma mark - CXCallController call actions
|
|
78
|
+
|
|
79
|
+// Display the incoming call to the user
|
|
80
|
+RCT_EXPORT_METHOD(displayIncomingCall:(NSString *)uuidString
|
|
81
|
+ handle:(NSString *)handle
|
|
82
|
+ hasVideo:(BOOL)hasVideo
|
|
83
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
84
|
+ reject:(RCTPromiseRejectBlock)reject)
|
|
85
|
+{
|
|
86
|
+#ifdef DEBUG
|
|
87
|
+ NSLog(@"[RNCallKit][displayIncomingCall] uuidString = %@", uuidString);
|
|
88
|
+#endif
|
|
89
|
+ NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
|
|
90
|
+ CXCallUpdate *callUpdate = [[CXCallUpdate alloc] init];
|
|
91
|
+ callUpdate.remoteHandle
|
|
92
|
+ = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:handle];
|
|
93
|
+ callUpdate.supportsDTMF = NO;
|
|
94
|
+ callUpdate.supportsHolding = NO;
|
|
95
|
+ callUpdate.supportsGrouping = NO;
|
|
96
|
+ callUpdate.supportsUngrouping = NO;
|
|
97
|
+ callUpdate.hasVideo = hasVideo;
|
|
98
|
+
|
|
99
|
+ [callKitProvider reportNewIncomingCallWithUUID:uuid
|
|
100
|
+ update:callUpdate
|
|
101
|
+ completion:^(NSError * _Nullable error) {
|
|
102
|
+ if (error == nil) {
|
|
103
|
+ resolve(nil);
|
|
104
|
+ } else {
|
|
105
|
+ reject(nil, @"Error reporting new incoming call", error);
|
|
106
|
+ }
|
|
107
|
+ }];
|
|
108
|
+}
|
|
109
|
+
|
|
110
|
+// End call
|
|
111
|
+RCT_EXPORT_METHOD(endCall:(NSString *)uuidString
|
|
112
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
113
|
+ reject:(RCTPromiseRejectBlock)reject)
|
|
114
|
+{
|
|
115
|
+#ifdef DEBUG
|
|
116
|
+ NSLog(@"[RNCallKit][endCall] uuidString = %@", uuidString);
|
|
117
|
+#endif
|
|
118
|
+ NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
|
|
119
|
+ CXEndCallAction *action = [[CXEndCallAction alloc] initWithCallUUID:uuid];
|
|
120
|
+ CXTransaction *transaction = [[CXTransaction alloc] initWithAction:action];
|
|
121
|
+ [self requestTransaction:transaction resolve:resolve reject:reject];
|
|
122
|
+}
|
|
123
|
+
|
|
124
|
+// Mute / unmute (audio)
|
|
125
|
+RCT_EXPORT_METHOD(setMuted:(NSString *)uuidString
|
|
126
|
+ muted:(BOOL) muted
|
|
127
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
128
|
+ reject:(RCTPromiseRejectBlock)reject)
|
|
129
|
+{
|
|
130
|
+#ifdef DEBUG
|
|
131
|
+ NSLog(@"[RNCallKit][setMuted] uuidString = %@", uuidString);
|
|
132
|
+#endif
|
|
133
|
+ NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
|
|
134
|
+ CXSetMutedCallAction *action
|
|
135
|
+ = [[CXSetMutedCallAction alloc] initWithCallUUID:uuid muted:muted];
|
|
136
|
+ CXTransaction *transaction = [[CXTransaction alloc] initWithAction:action];
|
|
137
|
+ [self requestTransaction:transaction resolve:resolve reject:reject];
|
|
138
|
+}
|
|
139
|
+
|
|
140
|
+// Start outgoing call
|
|
141
|
+RCT_EXPORT_METHOD(startCall:(NSString *)uuidString
|
|
142
|
+ handle:(NSString *)handle
|
|
143
|
+ video:(BOOL)video
|
|
144
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
145
|
+ reject:(RCTPromiseRejectBlock)reject)
|
|
146
|
+{
|
|
147
|
+#ifdef DEBUG
|
|
148
|
+ NSLog(@"[RNCallKit][startCall] uuidString = %@", uuidString);
|
|
149
|
+#endif
|
|
150
|
+ NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
|
|
151
|
+ CXHandle *callHandle
|
|
152
|
+ = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:handle];
|
|
153
|
+ CXStartCallAction *action
|
|
154
|
+ = [[CXStartCallAction alloc] initWithCallUUID:uuid handle:callHandle];
|
|
155
|
+ action.video = video;
|
|
156
|
+ CXTransaction *transaction = [[CXTransaction alloc] initWithAction:action];
|
|
157
|
+ [self requestTransaction:transaction resolve:resolve reject:reject];
|
|
158
|
+}
|
|
159
|
+
|
|
160
|
+// Indicate call failed
|
|
161
|
+RCT_EXPORT_METHOD(reportCallFailed:(NSString *)uuidString
|
|
162
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
163
|
+ reject:(RCTPromiseRejectBlock)reject)
|
|
164
|
+{
|
|
165
|
+ NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
|
|
166
|
+ [callKitProvider reportCallWithUUID:uuid
|
|
167
|
+ endedAtDate:[NSDate date]
|
|
168
|
+ reason:CXCallEndedReasonFailed];
|
|
169
|
+ resolve(nil);
|
|
170
|
+}
|
|
171
|
+
|
|
172
|
+// Indicate outgoing call connected
|
|
173
|
+RCT_EXPORT_METHOD(reportConnectedOutgoingCall:(NSString *)uuidString
|
|
174
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
175
|
+ reject:(RCTPromiseRejectBlock)reject)
|
|
176
|
+{
|
|
177
|
+ NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
|
|
178
|
+ [callKitProvider reportOutgoingCallWithUUID:uuid
|
|
179
|
+ connectedAtDate:[NSDate date]];
|
|
180
|
+ resolve(nil);
|
|
181
|
+}
|
|
182
|
+
|
|
183
|
+// Update call in case we have a display name or video capability changes
|
|
184
|
+RCT_EXPORT_METHOD(updateCall:(NSString *)uuidString
|
|
185
|
+ options:(NSDictionary *)options
|
|
186
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
187
|
+ reject:(RCTPromiseRejectBlock)reject)
|
|
188
|
+{
|
|
189
|
+#ifdef DEBUG
|
|
190
|
+ NSLog(@"[RNCallKit][updateCall] uuidString = %@ options = %@", uuidString, options);
|
|
191
|
+#endif
|
|
192
|
+ NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
|
|
193
|
+ CXCallUpdate *update = [[CXCallUpdate alloc] init];
|
|
194
|
+ if (options[@"displayName"]) {
|
|
195
|
+ update.localizedCallerName = options[@"displayName"];
|
|
196
|
+ }
|
|
197
|
+ if (options[@"hasVideo"]) {
|
|
198
|
+ update.hasVideo = [(NSNumber*)options[@"hasVideo"] boolValue];
|
|
199
|
+ }
|
|
200
|
+ [callKitProvider reportCallWithUUID:uuid updated:update];
|
|
201
|
+ resolve(nil);
|
|
202
|
+}
|
|
203
|
+
|
|
204
|
+#pragma mark - Helper methods
|
|
205
|
+
|
|
206
|
+- (CXProviderConfiguration *)getProviderConfiguration:(NSDictionary* )settings
|
|
207
|
+{
|
|
208
|
+#ifdef DEBUG
|
|
209
|
+ NSLog(@"[RNCallKit][getProviderConfiguration]");
|
|
210
|
+#endif
|
|
211
|
+ CXProviderConfiguration *providerConfiguration
|
|
212
|
+ = [[CXProviderConfiguration alloc] initWithLocalizedName:settings[@"appName"]];
|
|
213
|
+ providerConfiguration.supportsVideo = YES;
|
|
214
|
+ providerConfiguration.maximumCallGroups = 1;
|
|
215
|
+ providerConfiguration.maximumCallsPerCallGroup = 1;
|
|
216
|
+ providerConfiguration.supportedHandleTypes
|
|
217
|
+ = [NSSet setWithObjects:[NSNumber numberWithInteger:CXHandleTypeGeneric], nil];
|
|
218
|
+ if (settings[@"imageName"]) {
|
|
219
|
+ providerConfiguration.iconTemplateImageData
|
|
220
|
+ = UIImagePNGRepresentation([UIImage imageNamed:settings[@"imageName"]]);
|
|
221
|
+ }
|
|
222
|
+ if (settings[@"ringtoneSound"]) {
|
|
223
|
+ providerConfiguration.ringtoneSound = settings[@"ringtoneSound"];
|
|
224
|
+ }
|
|
225
|
+ return providerConfiguration;
|
|
226
|
+}
|
|
227
|
+
|
|
228
|
+- (void)requestTransaction:(CXTransaction *)transaction
|
|
229
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
230
|
+ reject:(RCTPromiseRejectBlock)reject
|
|
231
|
+{
|
|
232
|
+#ifdef DEBUG
|
|
233
|
+ NSLog(@"[RNCallKit][requestTransaction] transaction = %@", transaction);
|
|
234
|
+#endif
|
|
235
|
+ [callKitCallController requestTransaction:transaction completion:^(NSError * _Nullable error) {
|
|
236
|
+ if (error == nil) {
|
|
237
|
+ resolve(nil);
|
|
238
|
+ } else {
|
|
239
|
+ NSLog(@"[RNCallKit][requestTransaction] Error requesting transaction (%@): (%@)", transaction.actions, error);
|
|
240
|
+ reject(nil, @"Error processing CallKit transaction", error);
|
|
241
|
+ }
|
|
242
|
+ }];
|
|
243
|
+}
|
|
244
|
+
|
|
245
|
+#pragma mark - CXProviderDelegate
|
|
246
|
+
|
|
247
|
+// Called when the provider has been reset. We should terminate all calls.
|
|
248
|
+- (void)providerDidReset:(CXProvider *)provider {
|
|
249
|
+#ifdef DEBUG
|
|
250
|
+ NSLog(@"[RNCallKit][CXProviderDelegate][provider:providerDidReset]");
|
|
251
|
+#endif
|
|
252
|
+ [self sendEventWithName:RNCallKitProviderDidReset body:nil];
|
|
253
|
+}
|
|
254
|
+
|
|
255
|
+// Answering incoming call
|
|
256
|
+- (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAction *)action
|
|
257
|
+{
|
|
258
|
+#ifdef DEBUG
|
|
259
|
+ NSLog(@"[RNCallKit][CXProviderDelegate][provider:performAnswerCallAction]");
|
|
260
|
+#endif
|
|
261
|
+ [self sendEventWithName:RNCallKitPerformAnswerCallAction
|
|
262
|
+ body:@{ @"callUUID": action.callUUID.UUIDString }];
|
|
263
|
+ [action fulfill];
|
|
264
|
+}
|
|
265
|
+
|
|
266
|
+// Call ended, user request
|
|
267
|
+- (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)action
|
|
268
|
+{
|
|
269
|
+#ifdef DEBUG
|
|
270
|
+ NSLog(@"[RNCallKit][CXProviderDelegate][provider:performEndCallAction]");
|
|
271
|
+#endif
|
|
272
|
+ [self sendEventWithName:RNCallKitPerformEndCallAction
|
|
273
|
+ body:@{ @"callUUID": action.callUUID.UUIDString }];
|
|
274
|
+ [action fulfill];
|
|
275
|
+}
|
|
276
|
+
|
|
277
|
+// Handle audio mute from CallKit view
|
|
278
|
+- (void)provider:(CXProvider *)provider performSetMutedCallAction:(CXSetMutedCallAction *)action {
|
|
279
|
+#ifdef DEBUG
|
|
280
|
+ NSLog(@"[RNCallKit][CXProviderDelegate][provider:performSetMutedCallAction]");
|
|
281
|
+#endif
|
|
282
|
+ [self sendEventWithName:RNCallKitPerformSetMutedCallAction
|
|
283
|
+ body:@{ @"callUUID": action.callUUID.UUIDString,
|
|
284
|
+ @"muted": [NSNumber numberWithBool:action.muted]}];
|
|
285
|
+ [action fulfill];
|
|
286
|
+}
|
|
287
|
+
|
|
288
|
+// Starting outgoing call
|
|
289
|
+- (void)provider:(CXProvider *)provider performStartCallAction:(CXStartCallAction *)action
|
|
290
|
+{
|
|
291
|
+#ifdef DEBUG
|
|
292
|
+ NSLog(@"[RNCallKit][CXProviderDelegate][provider:performStartCallAction]");
|
|
293
|
+#endif
|
|
294
|
+ [action fulfill];
|
|
295
|
+
|
|
296
|
+ // Update call info
|
|
297
|
+ CXCallUpdate *callUpdate = [[CXCallUpdate alloc] init];
|
|
298
|
+ callUpdate.remoteHandle = action.handle;
|
|
299
|
+ callUpdate.supportsDTMF = NO;
|
|
300
|
+ callUpdate.supportsHolding = NO;
|
|
301
|
+ callUpdate.supportsGrouping = NO;
|
|
302
|
+ callUpdate.supportsUngrouping = NO;
|
|
303
|
+ callUpdate.hasVideo = action.isVideo;
|
|
304
|
+ [callKitProvider reportCallWithUUID:action.callUUID updated:callUpdate];
|
|
305
|
+
|
|
306
|
+ // Notify the system about the outgoing call
|
|
307
|
+ [callKitProvider reportOutgoingCallWithUUID:action.callUUID
|
|
308
|
+ startedConnectingAtDate:[NSDate date]];
|
|
309
|
+}
|
|
310
|
+
|
|
311
|
+// These just help with debugging
|
|
312
|
+
|
|
313
|
+- (void)provider:(CXProvider *)provider didActivateAudioSession:(AVAudioSession *)audioSession
|
|
314
|
+{
|
|
315
|
+#ifdef DEBUG
|
|
316
|
+ NSLog(@"[RNCallKit][CXProviderDelegate][provider:didActivateAudioSession]");
|
|
317
|
+#endif
|
|
318
|
+}
|
|
319
|
+
|
|
320
|
+- (void)provider:(CXProvider *)provider didDeactivateAudioSession:(AVAudioSession *)audioSession
|
|
321
|
+{
|
|
322
|
+#ifdef DEBUG
|
|
323
|
+ NSLog(@"[RNCallKit][CXProviderDelegate][provider:didDeactivateAudioSession]");
|
|
324
|
+#endif
|
|
325
|
+}
|
|
326
|
+
|
|
327
|
+- (void)provider:(CXProvider *)provider timedOutPerformingAction:(CXAction *)action
|
|
328
|
+{
|
|
329
|
+#ifdef DEBUG
|
|
330
|
+ NSLog(@"[RNCallKit][CXProviderDelegate][provider:timedOutPerformingAction]");
|
|
331
|
+#endif
|
|
332
|
+}
|
|
333
|
+
|
|
334
|
+@end
|