diff --git a/index.js b/index.js index c2b14b8..ba3d9aa 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(uuid) { + return Platform.OS === 'ios' ? + _RNCallKit.checkIfInCall(uuid) : + 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..010042c 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 { @@ -67,7 +68,8 @@ - (void)dealloc RNCallKitPerformEndCallAction, RNCallKitDidActivateAudioSession, RNCallKitDidDisplayIncomingCall, - RNCallKitDidPerformSetMutedCallAction + RNCallKitDidPerformSetMutedCallAction, + RNCallKitCheckIfInCallAction ]; } @@ -184,6 +186,37 @@ - (void)dealloc } } + +// check If In Call +RCT_EXPORT_METHOD(checkIfInCall:(NSString *)uuidString + checkIfInCallWithResolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) +{ + int flag = 0; + @try { + for (CXCall *call in self.callKitCallController.callObserver.calls) { + NSString *callUid = [call.UUID UUIDString]; + NSLog(@"[RNCallKit][checkIfInCall] call 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; + break; + } + } + if(flag == 1) { + resolve(@{@"inCall": @YES}); + } else { + resolve(@{@"inCall": @NO}); + } + } + @catch (NSException *exception) { + NSLog(@"[RNCallKit][checkIfInCall] exception %@", exception.reason); + } +} +// + + RCT_EXPORT_METHOD(setHeldCall:(NSString *)uuidString onHold:(BOOL)onHold) { #ifdef DEBUG