import 'react-native-get-random-values';
import RNCallKeep, { CONSTANTS } from 'react-native-callkeep';
const RING_MS = 45_000;
type Ringing = {
onAnswer: (callUUID: string) => void;
onDecline: () => void;
timer: ReturnType<typeof setTimeout>;
};
const ringing = new Map<string, Ringing>();
const active = new Map<string, { onHangUp: () => void }>();
export function uuid(): string {
const b = new Uint8Array(16);
(globalThis as any).crypto.getRandomValues(b);
b[6] = (b[6] & 0x0f) | 0x40;
b[8] = (b[8] & 0x3f) | 0x80;
const h = Array.from(b, x => x.toString(16).padStart(2, '0')).join('');
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`.toUpperCase();
}
let ready: Promise<boolean> | null = null;
export function setupCallKit(): Promise<boolean> {
if (ready) return ready;
ready = RNCallKeep.setup({
ios: {
appName: 'Quple',
supportsVideo: true,
maximumCallGroups: '1',
maximumCallsPerCallGroup: '1',
includesCallsInRecents: true,
},
android: {
alertTitle: 'Permissions required',
alertDescription: 'Quple needs to access your phone accounts',
cancelButton: 'Cancel',
okButton: 'OK',
additionalPermissions: [],
},
});
RNCallKeep.addEventListener('answerCall', ({ callUUID }) => {
const id = callUUID.toUpperCase();
const r = takeRinging(id);
if (!r) return;
active.set(id, { onHangUp: () => {} });
r.onAnswer(id);
});
RNCallKeep.addEventListener('endCall', ({ callUUID }) => {
const id = callUUID.toUpperCase();
const r = takeRinging(id);
if (r) {
r.onDecline();
return;
}
const a = active.get(id);
if (a) {
active.delete(id);
a.onHangUp();
}
});
return ready;
}
function takeRinging(id: string): Ringing | undefined {
const r = ringing.get(id);
if (r) {
clearTimeout(r.timer);
ringing.delete(id);
}
return r;
}
export function ring(from: string, onAnswer: (callUUID: string) => void, onDecline: () => void): string {
const id = uuid();
const timer = setTimeout(() => {
if (takeRinging(id)) {
RNCallKeep.reportEndCallWithUUID(id, CONSTANTS.END_CALL_REASONS.UNANSWERED);
onDecline();
}
}, RING_MS);
ringing.set(id, { onAnswer, onDecline, timer });
RNCallKeep.displayIncomingCall(id, from, from, 'generic', true);
return id;
}
* Stops ringing. The caller gave up: as when it rings out (onDecline).
* `elsewhere`: another of your devices answered, so no onDecline (which
* would hang up the call that's going on there).
*/
export function stopRinging(id: string, elsewhere = false) {
const r = takeRinging(id);
if (!r) return;
const { END_CALL_REASONS } = CONSTANTS;
RNCallKeep.reportEndCallWithUUID(id, elsewhere ? END_CALL_REASONS.ANSWERED_ELSEWHERE : END_CALL_REASONS.REMOTE_ENDED);
if (!elsewhere) r.onDecline();
}
export function startOutgoing(to: string): string {
const id = uuid();
active.set(id, { onHangUp: () => {} });
RNCallKeep.startCall(id, to, to, 'generic', true);
return id;
}
export function connected(id: string) {
RNCallKeep.reportConnectedOutgoingCallWithUUID(id);
}
export function onHangUp(id: string, fn: () => void) {
const a = active.get(id);
if (a) a.onHangUp = fn;
}
export function finish(id: string) {
if (!active.delete(id)) return;
RNCallKeep.endCall(id);
}