react-native-call-keeper
Version:
A React Native module for handling VoIP calls with CallKit (iOS) and ConnectionService (Android) support. Compatible with the New Architecture and Expo.
176 lines (156 loc) • 4.55 kB
JavaScript
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import NativeCallKeeper from './NativeCallKeeper';
class CallKeeperModule {
listeners = new Map();
constructor() {
// Event emitters require the native module instance, not the TurboModule spec
// Always use NativeModules for event emitter initialization
const CallKeeperNative = NativeModules.CallKeeper;
if (!CallKeeperNative) {
console.error('[CallKeeper] Native module not found. Make sure the module is properly linked.');
}
this.eventEmitter = new NativeEventEmitter(CallKeeperNative);
}
/**
* Initialize the CallKeeper module with configuration options
*/
async setup(options) {
try {
await NativeCallKeeper.setup(options);
} catch (error) {
console.error('CallKeeper setup error:', error);
throw error;
}
}
/**
* Display an incoming call notification
*/
async displayIncomingCall(callUUID, handle, localizedCallerName, handleType = 'generic', hasVideo = false) {
return NativeCallKeeper.displayIncomingCall(callUUID, handle, localizedCallerName, handleType, hasVideo);
}
/**
* Start an outgoing call
*/
async startCall(callUUID, handle, contactIdentifier, handleType = 'generic', hasVideo = false) {
return NativeCallKeeper.startCall(callUUID, handle, contactIdentifier, handleType, hasVideo);
}
/**
* End a specific call
*/
async endCall(callUUID) {
return NativeCallKeeper.endCall(callUUID);
}
/**
* End all active calls
*/
async endAllCalls() {
return NativeCallKeeper.endAllCalls();
}
/**
* Answer an incoming call
*/
async answerIncomingCall(callUUID) {
return NativeCallKeeper.answerIncomingCall(callUUID);
}
/**
* Reject an incoming call
*/
async rejectCall(callUUID) {
return NativeCallKeeper.rejectCall(callUUID);
}
/**
* Set mute status for a call
*/
async setMutedCall(callUUID, muted) {
return NativeCallKeeper.setMutedCall(callUUID, muted);
}
/**
* Put a call on hold
*/
async setOnHold(callUUID, onHold) {
return NativeCallKeeper.setOnHold(callUUID, onHold);
}
/**
* Report that an outgoing call has connected
*/
async reportConnectedOutgoingCall(callUUID) {
return NativeCallKeeper.reportConnectedOutgoingCall(callUUID);
}
/**
* Report that a call has ended
* @param callUUID - The UUID of the call
* @param reason - End call reason (1: failed, 2: remote ended, 3: local ended, 4: answered elsewhere, 5: declined elsewhere, 6: missed)
*/
async reportEndCallWithUUID(callUUID, reason) {
return NativeCallKeeper.reportEndCallWithUUID(callUUID, reason);
}
/**
* Update the display information for a call
*/
async updateDisplay(callUUID, displayName, handle) {
return NativeCallKeeper.updateDisplay(callUUID, displayName, handle);
}
/**
* Check if the app has necessary permissions
*/
async checkPermissions() {
return NativeCallKeeper.checkPermissions();
}
/**
* Check if there's an active managed call
*/
async checkIsInManagedCall() {
return NativeCallKeeper.checkIsInManagedCall();
}
/**
* Set availability for receiving calls (Android only)
*/
async setAvailable(available) {
if (Platform.OS === 'android') {
return NativeCallKeeper.setAvailable(available);
}
}
/**
* Set the current call as active
*/
async setCurrentCallActive(callUUID) {
return NativeCallKeeper.setCurrentCallActive(callUUID);
}
/**
* Bring the app to foreground
*/
async backToForeground() {
return NativeCallKeeper.backToForeground();
}
/**
* Add event listener
*/
addEventListener(eventType, listener) {
const subscription = this.eventEmitter.addListener(eventType, listener);
if (!this.listeners.has(eventType)) {
this.listeners.set(eventType, []);
}
this.listeners.get(eventType)?.push(subscription);
}
/**
* Remove event listener
*/
removeEventListener(eventType) {
const subscriptions = this.listeners.get(eventType);
if (subscriptions) {
subscriptions.forEach(subscription => subscription.remove());
this.listeners.delete(eventType);
}
}
/**
* Remove all event listeners
*/
removeAllListeners() {
this.listeners.forEach(subscriptions => {
subscriptions.forEach(subscription => subscription.remove());
});
this.listeners.clear();
}
}
export default new CallKeeperModule();
//# sourceMappingURL=index.js.map