From d6f50cb246deaf1f32d0dfcdb25f4455b05d5000 Mon Sep 17 00:00:00 2001 From: Ayoub Laaziz Date: Wed, 28 Nov 2018 15:11:21 +0100 Subject: [PATCH 1/6] check if device in call --- index.js | 54 ++++++++++++++++++++++++++++++++++++--- ios/RNCallKit/RNCallKit.m | 38 ++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index c2b14b8..a024523 100644 --- a/index.js +++ b/index.js @@ -2,20 +2,59 @@ import { NativeModules, + NativeEventEmitter, Platform, } from 'react-native'; -import { listeners } from './actions' - const _RNCallKit = NativeModules.RNCallKit; +const _RNCallKitEmitter = new NativeEventEmitter(_RNCallKit); const _callkitEventHandlers = new Map(); -export default class RNCallKit { +const RNCallKitDidReceiveStartCallAction = 'RNCallKitDidReceiveStartCallAction'; +const RNCallKitPerformAnswerCallAction = 'RNCallKitPerformAnswerCallAction'; +const RNCallKitPerformEndCallAction = 'RNCallKitPerformEndCallAction'; +const RNCallKitDidActivateAudioSession = 'RNCallKitDidActivateAudioSession'; +const RNCallKitDidDisplayIncomingCall = 'RNCallKitDidDisplayIncomingCall'; +const RNCallKitDidPerformSetMutedCallAction = 'RNCallKitDidPerformSetMutedCallAction'; +export default class RNCallKit { static addEventListener(type, handler) { if (Platform.OS !== 'ios') return; - const listener = listeners[type](handler) + var listener; + if (type === 'didReceiveStartCallAction') { + listener = _RNCallKitEmitter.addListener( + RNCallKitDidReceiveStartCallAction, + (data) => { handler(data);} + ); + _RNCallKit._startCallActionEventListenerAdded(); + } else if (type === 'answerCall') { + listener = _RNCallKitEmitter.addListener( + RNCallKitPerformAnswerCallAction, + (data) => { handler(data);} + ); + } else if (type === 'endCall') { + listener = _RNCallKitEmitter.addListener( + RNCallKitPerformEndCallAction, + (data) => { handler(data); } + ); + } else if (type === 'didActivateAudioSession') { + listener = _RNCallKitEmitter.addListener( + RNCallKitDidActivateAudioSession, + () => { handler(); } + ); + } else if (type === 'didDisplayIncomingCall') { + listener = _RNCallKitEmitter.addListener( + RNCallKitDidDisplayIncomingCall, + (data) => { handler(data.error); } + ); + } else if (type === 'didPerformSetMutedCallAction') { + listener = _RNCallKitEmitter.addListener( + RNCallKitDidPerformSetMutedCallAction, + (data) => { handler(data.muted); } + ); + } + _callkitEventHandlers.set(handler, listener); } @@ -65,6 +104,13 @@ export default class RNCallKit { _RNCallKit.endAllCalls(); } + static checkIfInCall() { + return Platform.OS === 'ios' ? + _RNCallKit.checkIfInCall() : + Promise.reject('RNCallKit.checkIfInCall was called from unsupported OS'); + + } + static setMutedCAll(uuid, muted) { if (Platform.OS !== 'ios') return; _RNCallKit.setMutedCall(uuid, muted); diff --git a/ios/RNCallKit/RNCallKit.m b/ios/RNCallKit/RNCallKit.m index fc3439f..8ac83f0 100644 --- a/ios/RNCallKit/RNCallKit.m +++ b/ios/RNCallKit/RNCallKit.m @@ -67,7 +67,7 @@ - (void)dealloc RNCallKitPerformEndCallAction, RNCallKitDidActivateAudioSession, RNCallKitDidDisplayIncomingCall, - RNCallKitDidPerformSetMutedCallAction + RNCallKitDidPerformSetMutedCallAction, ]; } @@ -184,6 +184,42 @@ - (void)dealloc } } + +// + + + +RCT_REMAP_METHOD(checkIfInCall, + checkIfInCallWithResolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) +{ + for (CXCall *call in self.callKitCallController.callObserver.calls) { + if (call.hasEnded == false) { + NSLog(@"CXCallState: inCall"); + resolve(@"true"); + break; + } else { + NSLog(@"CXCallState: not inCall"); + } + } + + +} + +RCT_EXPORT_METHOD(deviceStatus) +{ + for (CXCall *call1 in self.callKitCallController.callObserver.calls) { + if (call1.hasEnded == false) { + NSLog(@"CXCallState: inCall"); + } else { + NSLog(@"CXCallState: not inCall"); + } + } + } + +// + + RCT_EXPORT_METHOD(setHeldCall:(NSString *)uuidString onHold:(BOOL)onHold) { #ifdef DEBUG From 9f693284b49b1e64049c3a60739db2acb8f2c345 Mon Sep 17 00:00:00 2001 From: Ayoub Laaziz Date: Wed, 28 Nov 2018 16:00:49 +0100 Subject: [PATCH 2/6] reformat code --- ios/RNCallKit/RNCallKit.m | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/ios/RNCallKit/RNCallKit.m b/ios/RNCallKit/RNCallKit.m index 8ac83f0..f2d9e7e 100644 --- a/ios/RNCallKit/RNCallKit.m +++ b/ios/RNCallKit/RNCallKit.m @@ -185,37 +185,29 @@ - (void)dealloc } -// - - +// check If In Call RCT_REMAP_METHOD(checkIfInCall, checkIfInCallWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - for (CXCall *call in self.callKitCallController.callObserver.calls) { - if (call.hasEnded == false) { - NSLog(@"CXCallState: inCall"); - resolve(@"true"); - break; - } else { - NSLog(@"CXCallState: not inCall"); + int flag = 0; + @try { + for (CXCall *call in self.callKitCallController.callObserver.calls) { + if (call.hasEnded == false) { + flag = 1; + resolve(@"true"); + break; + } } - } - - -} - -RCT_EXPORT_METHOD(deviceStatus) -{ - for (CXCall *call1 in self.callKitCallController.callObserver.calls) { - if (call1.hasEnded == false) { - NSLog(@"CXCallState: inCall"); - } else { - NSLog(@"CXCallState: not inCall"); + if(flag == 0) { + resolve(@"false"); } } - } + @catch (NSException *exception) { + NSLog(@"%@", exception.reason); + } +} // From d3c4c14818a5dd5249b0a56a8af9712d331cf56f Mon Sep 17 00:00:00 2001 From: Ayoub Laaziz Date: Thu, 13 Dec 2018 11:03:16 +0100 Subject: [PATCH 3/6] If applied, this commit will add a function check if device in call --- index.js | 4 ++-- ios/RNCallKit/RNCallKit.m | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index a024523..ba3d9aa 100644 --- a/index.js +++ b/index.js @@ -104,9 +104,9 @@ export default class RNCallKit { _RNCallKit.endAllCalls(); } - static checkIfInCall() { + static checkIfInCall(uuid) { return Platform.OS === 'ios' ? - _RNCallKit.checkIfInCall() : + _RNCallKit.checkIfInCall(uuid) : Promise.reject('RNCallKit.checkIfInCall was called from unsupported OS'); } diff --git a/ios/RNCallKit/RNCallKit.m b/ios/RNCallKit/RNCallKit.m index f2d9e7e..f9cdc94 100644 --- a/ios/RNCallKit/RNCallKit.m +++ b/ios/RNCallKit/RNCallKit.m @@ -24,6 +24,7 @@ static NSString *const RNCallKitDidActivateAudioSession = @"RNCallKitDidActivateAudioSession"; static NSString *const RNCallKitDidDisplayIncomingCall = @"RNCallKitDidDisplayIncomingCall"; static NSString *const RNCallKitDidPerformSetMutedCallAction = @"RNCallKitDidPerformSetMutedCallAction"; +static NSString *const RNCallKitCheckIfInCallAction = @"RNCallKitCheckIfInCallAction"; @implementation RNCallKit { @@ -68,6 +69,7 @@ - (void)dealloc RNCallKitDidActivateAudioSession, RNCallKitDidDisplayIncomingCall, RNCallKitDidPerformSetMutedCallAction, + RNCallKitCheckIfInCallAction ]; } @@ -187,25 +189,28 @@ - (void)dealloc // check If In Call -RCT_REMAP_METHOD(checkIfInCall, +RCT_EXPORT_METHOD(checkIfInCall:(NSString *)uuidString checkIfInCallWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { int flag = 0; + NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString]; + NSLog(@"[RNCallKit][checkIfInCall] uuid = %@", uuid); @try { for (CXCall *call in self.callKitCallController.callObserver.calls) { - if (call.hasEnded == false) { + NSLog(@"[RNCallKit][checkIfInCall] call UUID = %@", call.UUID); + if (call.hasEnded == false && uuid != call.UUID) { flag = 1; - resolve(@"true"); + resolve(@{@"inCall": @YES}); break; } } if(flag == 0) { - resolve(@"false"); + resolve(@{@"inCall": @NO}); } } @catch (NSException *exception) { - NSLog(@"%@", exception.reason); + NSLog(@"[RNCallKit][checkIfInCall] exception %@", exception.reason); } } From abcf350db805807ed8fd7f04bfd3a7bcf83297af Mon Sep 17 00:00:00 2001 From: Ayoub Laaziz Date: Thu, 13 Dec 2018 11:07:23 +0100 Subject: [PATCH 4/6] add reject --- ios/RNCallKit/RNCallKit.m | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/RNCallKit/RNCallKit.m b/ios/RNCallKit/RNCallKit.m index f9cdc94..79fb868 100644 --- a/ios/RNCallKit/RNCallKit.m +++ b/ios/RNCallKit/RNCallKit.m @@ -211,6 +211,7 @@ - (void)dealloc } @catch (NSException *exception) { NSLog(@"[RNCallKit][checkIfInCall] exception %@", exception.reason); + reject(@{@"error": exception.reason}); } } From c5767b29df5a25c0949e166a5b6ec74075d79a9a Mon Sep 17 00:00:00 2001 From: Ayoub Laaziz Date: Thu, 13 Dec 2018 11:11:36 +0100 Subject: [PATCH 5/6] remove reject --- ios/RNCallKit/RNCallKit.m | 1 - 1 file changed, 1 deletion(-) diff --git a/ios/RNCallKit/RNCallKit.m b/ios/RNCallKit/RNCallKit.m index 79fb868..f9cdc94 100644 --- a/ios/RNCallKit/RNCallKit.m +++ b/ios/RNCallKit/RNCallKit.m @@ -211,7 +211,6 @@ - (void)dealloc } @catch (NSException *exception) { NSLog(@"[RNCallKit][checkIfInCall] exception %@", exception.reason); - reject(@{@"error": exception.reason}); } } From ef91aa1e047c157c41f67723fc235be025de0217 Mon Sep 17 00:00:00 2001 From: Ayoub Laaziz Date: Thu, 13 Dec 2018 15:41:09 +0100 Subject: [PATCH 6/6] reformat code --- ios/RNCallKit/RNCallKit.m | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ios/RNCallKit/RNCallKit.m b/ios/RNCallKit/RNCallKit.m index f9cdc94..010042c 100644 --- a/ios/RNCallKit/RNCallKit.m +++ b/ios/RNCallKit/RNCallKit.m @@ -188,24 +188,25 @@ - (void)dealloc // check If In Call - RCT_EXPORT_METHOD(checkIfInCall:(NSString *)uuidString checkIfInCallWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { int flag = 0; - NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString]; - NSLog(@"[RNCallKit][checkIfInCall] uuid = %@", uuid); @try { for (CXCall *call in self.callKitCallController.callObserver.calls) { + NSString *callUid = [call.UUID UUIDString]; NSLog(@"[RNCallKit][checkIfInCall] call UUID = %@", call.UUID); - if (call.hasEnded == false && uuid != call.UUID) { + NSLog(@"[RNCallKit][checkIfInCall] call Ended = %d", call.hasEnded); + NSLog(@"[RNCallKit][checkIfInCall] My Call %d", [uuidString isEqualToString:callUid]); + if (call.hasEnded == false && ![uuidString isEqualToString:callUid]) { flag = 1; - resolve(@{@"inCall": @YES}); break; } } - if(flag == 0) { + if(flag == 1) { + resolve(@{@"inCall": @YES}); + } else { resolve(@{@"inCall": @NO}); } } @@ -213,7 +214,6 @@ - (void)dealloc NSLog(@"[RNCallKit][checkIfInCall] exception %@", exception.reason); } } - //