@robono/calling-react-native 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/LICENSE +130 -0
- package/README.md +153 -2
- package/RobonoCallingRuntime.podspec +15 -0
- package/SBOM.spdx.json +983 -0
- package/android/build.gradle +8 -0
- package/android/src/main/AndroidManifest.xml +8 -0
- package/android/src/main/java/com/robono/calling/CallingPackage.java +15 -0
- package/android/src/main/java/com/robono/calling/CallingRuntime.java +75 -0
- package/android/src/main/java/com/robono/calling/CallingService.java +99 -0
- package/app.plugin.cjs +17 -0
- package/assets/bell.wav +0 -0
- package/assets/click.wav +0 -0
- package/assets/connecting.wav +0 -0
- package/assets/ended.wav +0 -0
- package/assets/marimba.wav +0 -0
- package/assets/pulse.wav +0 -0
- package/assets/ringing.wav +0 -0
- package/dist/appearance.d.ts +64 -0
- package/dist/appearance.js +126 -0
- package/dist/audio.d.mts +6 -0
- package/dist/audio.mjs +89 -0
- package/dist/controller.d.mts +7 -0
- package/dist/controller.mjs +211 -0
- package/dist/expo-notifications.d.ts +12 -0
- package/dist/expo-notifications.js +44 -0
- package/dist/history.d.ts +16 -0
- package/dist/history.js +12 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +23 -0
- package/dist/native.d.ts +18 -0
- package/dist/native.js +174 -0
- package/dist/notification-router.d.ts +18 -0
- package/dist/notification-router.js +59 -0
- package/dist/notifications.d.ts +37 -0
- package/dist/notifications.js +84 -0
- package/dist/runtime.d.ts +21 -0
- package/dist/runtime.js +227 -0
- package/dist/screens.d.ts +60 -0
- package/dist/screens.js +59 -0
- package/dist/types.d.ts +67 -0
- package/dist/types.js +1 -0
- package/examples/CallingHost.tsx +204 -0
- package/examples/ExpoCallingHost.tsx +42 -0
- package/ios/RobonoCallingRuntime.m +84 -0
- package/package.json +104 -4
- package/react-native.config.js +4 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// No UI, push registration, account storage or network-specific identity here.
|
|
2
|
+
/**
|
|
3
|
+
* @param {import('./types.js').CallingOptions & {createClient: (options: any) => import('@robono/calling').RobonoCallingClient}} options
|
|
4
|
+
* @returns {import('./types.js').CallingController}
|
|
5
|
+
*/
|
|
6
|
+
export function createController({ createClient, request, media, audio, resolveContact = () => null, onError = () => { }, forceRelay = false }) {
|
|
7
|
+
const listeners = new Set();
|
|
8
|
+
let disposed = false, foreground = true, action = null, startingContact = null;
|
|
9
|
+
let opening = null, openingTimer, connectedId = null, audioKey = '', epoch = 0;
|
|
10
|
+
let resuming = null;
|
|
11
|
+
let snapshot = { call: null, voiceAvailable: false, muted: false, error: null };
|
|
12
|
+
let state = { phase: 'idle', call: null, contact: null, speaker: false, muted: false, error: null, canMute: false, canSpeaker: false, connectedAt: null };
|
|
13
|
+
const publish = patch => { state = { ...state, ...patch }; for (const listener of listeners)
|
|
14
|
+
listener(); };
|
|
15
|
+
const report = error => { if (!disposed) {
|
|
16
|
+
publish({ error: error.message || 'call_failed' });
|
|
17
|
+
onError(error);
|
|
18
|
+
} };
|
|
19
|
+
const sound = (phase, tone = false, cue = undefined) => {
|
|
20
|
+
const key = `${phase}:${foreground}:${state.speaker}:${!!state.localHeld}`;
|
|
21
|
+
if (key === audioKey && !tone)
|
|
22
|
+
return;
|
|
23
|
+
audioKey = key;
|
|
24
|
+
Promise.resolve(audio.update({ phase, speaker: state.speaker, foreground, endTone: tone, cue, localHeld: state.localHeld })).catch(report);
|
|
25
|
+
};
|
|
26
|
+
const update = next => {
|
|
27
|
+
if (disposed)
|
|
28
|
+
return;
|
|
29
|
+
snapshot = next;
|
|
30
|
+
const call = next.call;
|
|
31
|
+
const active = call && call.state !== 'ended' && call.controlled;
|
|
32
|
+
if (opening && (!active || call.id !== opening))
|
|
33
|
+
return;
|
|
34
|
+
if (opening) {
|
|
35
|
+
opening = null;
|
|
36
|
+
clearTimeout(openingTimer);
|
|
37
|
+
}
|
|
38
|
+
const terminalError = next.error && client.closed;
|
|
39
|
+
const held = active && (next.localHeld || call.localHeld || call.remoteHeld);
|
|
40
|
+
const phase = terminalError ? 'error' : held ? 'held' : active ? (call.state === 'ringing' ? (call.role === 'callee' && action !== 'answer' ? 'incoming' : call.role === 'caller' ? 'outgoing' : 'connecting') : call.state)
|
|
41
|
+
: action === 'call' && next.preparing ? 'outgoing' : 'idle';
|
|
42
|
+
const ended = connectedId !== null && (phase === 'idle' || phase === 'error' || call?.id !== connectedId);
|
|
43
|
+
const endTone = ended && !state.localHeld;
|
|
44
|
+
if (ended)
|
|
45
|
+
connectedId = null;
|
|
46
|
+
if (phase === 'connected')
|
|
47
|
+
connectedId = call.id;
|
|
48
|
+
const contact = active ? resolveContact(call.connectionId) || startingContact : startingContact;
|
|
49
|
+
const newIncoming = active && call.role === 'callee' && state.call?.id !== call.id;
|
|
50
|
+
const previousPhase = state.phase;
|
|
51
|
+
const cue = phase === 'connected' && previousPhase === 'held' ? 'resumed' : phase === 'idle' && call?.state === 'ended' && state.call?.id === call.id ? (call.reason === 'declined' ? 'declined' : ['no_answer', 'cancelled'].includes(call.reason) ? 'unanswered' : undefined) : undefined;
|
|
52
|
+
publish({ phase, call: active && !terminalError ? call : null, contact, muted: next.muted,
|
|
53
|
+
...(newIncoming ? { speaker: false } : {}),
|
|
54
|
+
error: next.error, localHeld: !!next.localHeld,
|
|
55
|
+
...(phase !== 'held' ? { resumeError: null, resumePending: false } : {}),
|
|
56
|
+
connectedAt: ['connected', 'held'].includes(phase) ? call.connectedAt ?? null : null,
|
|
57
|
+
canMute: phase === 'connected' && !next.preparing,
|
|
58
|
+
canSpeaker: ['outgoing', 'connecting', 'connected'].includes(phase) });
|
|
59
|
+
sound(phase, endTone, cue);
|
|
60
|
+
if (phase === 'idle' && !action)
|
|
61
|
+
startingContact = null;
|
|
62
|
+
};
|
|
63
|
+
const client = createClient({ request, ...media, forceRelay, onChange: update });
|
|
64
|
+
const run = async (kind, work) => {
|
|
65
|
+
if (disposed || action || ['loading', 'error'].includes(state.phase))
|
|
66
|
+
throw Error('client_not_ready');
|
|
67
|
+
action = kind;
|
|
68
|
+
const generation = ++epoch;
|
|
69
|
+
try {
|
|
70
|
+
await work();
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (!disposed && generation === epoch && error.message !== 'call_cancelled')
|
|
74
|
+
report(error);
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
if (generation === epoch) {
|
|
79
|
+
action = null;
|
|
80
|
+
update(snapshot);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
client,
|
|
86
|
+
getSnapshot: () => state,
|
|
87
|
+
subscribe(listener) { listeners.add(listener); return () => listeners.delete(listener); },
|
|
88
|
+
async listen() { if (disposed)
|
|
89
|
+
throw Error('client_not_ready'); await client.listen(); },
|
|
90
|
+
async call(connectionId, contact) {
|
|
91
|
+
if (state.phase !== 'idle')
|
|
92
|
+
throw Error('busy');
|
|
93
|
+
startingContact = contact || resolveContact(connectionId);
|
|
94
|
+
return run('call', async () => {
|
|
95
|
+
publish({ phase: 'outgoing', contact: startingContact, speaker: false, canSpeaker: true, error: null });
|
|
96
|
+
sound('outgoing');
|
|
97
|
+
await client.call(connectionId);
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
async answer() {
|
|
101
|
+
if (state.phase !== 'incoming')
|
|
102
|
+
throw Error('no_incoming_call');
|
|
103
|
+
return run('answer', async () => {
|
|
104
|
+
// Keep this screen mounted throughout permission and SDP negotiation.
|
|
105
|
+
publish({ phase: 'connecting', canSpeaker: true });
|
|
106
|
+
sound('connecting');
|
|
107
|
+
await client.answer();
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
async hangUp({ playEndTone = true } = {}) {
|
|
111
|
+
++epoch;
|
|
112
|
+
action = null;
|
|
113
|
+
opening = null;
|
|
114
|
+
clearTimeout(openingTimer);
|
|
115
|
+
const cue = state.phase === 'incoming' ? 'declined' : undefined;
|
|
116
|
+
const tone = connectedId !== null && !state.localHeld;
|
|
117
|
+
connectedId = null;
|
|
118
|
+
publish({ phase: 'idle', call: null, canSpeaker: false, canMute: false, connectedAt: null });
|
|
119
|
+
sound('idle', tone && playEndTone, playEndTone ? cue : undefined);
|
|
120
|
+
await client.hangUp();
|
|
121
|
+
},
|
|
122
|
+
async interrupt() {
|
|
123
|
+
// Pre-answer/setup calls do not have an established conversation to hold.
|
|
124
|
+
if (!['connected', 'held'].includes(state.phase))
|
|
125
|
+
return this.hangUp({ playEndTone: false });
|
|
126
|
+
try {
|
|
127
|
+
await client.hold();
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
report(error);
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
resume() {
|
|
135
|
+
if (resuming)
|
|
136
|
+
return resuming;
|
|
137
|
+
if (state.phase !== 'held' || !state.localHeld)
|
|
138
|
+
return Promise.resolve();
|
|
139
|
+
const callId = state.call?.id;
|
|
140
|
+
publish({ resumePending: true, resumeError: null });
|
|
141
|
+
const work = Promise.resolve().then(() => client.resume(() => audio.resume?.() || Promise.resolve()))
|
|
142
|
+
.catch(error => {
|
|
143
|
+
if (!disposed && state.call?.id === callId && state.phase === 'held') {
|
|
144
|
+
publish({ resumeError: error.message || 'call_state_unavailable' });
|
|
145
|
+
report(error);
|
|
146
|
+
}
|
|
147
|
+
throw error;
|
|
148
|
+
}).finally(() => {
|
|
149
|
+
if (resuming === work)
|
|
150
|
+
resuming = null;
|
|
151
|
+
if (!disposed && state.call?.id === callId)
|
|
152
|
+
publish({ resumePending: false });
|
|
153
|
+
});
|
|
154
|
+
resuming = work;
|
|
155
|
+
return work;
|
|
156
|
+
},
|
|
157
|
+
mute(value) { if (state.canMute)
|
|
158
|
+
client.mute(value); },
|
|
159
|
+
setSpeaker(value) {
|
|
160
|
+
if (!state.canSpeaker)
|
|
161
|
+
return;
|
|
162
|
+
publish({ speaker: value });
|
|
163
|
+
sound(state.phase);
|
|
164
|
+
},
|
|
165
|
+
setForeground(value) { foreground = value; sound(state.phase); },
|
|
166
|
+
async refresh() { return client.refresh(); },
|
|
167
|
+
async openIncoming(callId) {
|
|
168
|
+
if (disposed || typeof callId !== 'string' || !callId || callId.length > 128)
|
|
169
|
+
return false;
|
|
170
|
+
if (state.call?.id === callId)
|
|
171
|
+
return true;
|
|
172
|
+
if (state.phase !== 'idle' && state.phase !== 'error')
|
|
173
|
+
return false;
|
|
174
|
+
opening = callId;
|
|
175
|
+
publish({ phase: 'loading', call: null, contact: null, canSpeaker: false, canMute: false, error: null });
|
|
176
|
+
clearTimeout(openingTimer);
|
|
177
|
+
openingTimer = setTimeout(() => {
|
|
178
|
+
if (opening === callId) {
|
|
179
|
+
opening = null;
|
|
180
|
+
publish({ phase: 'error', error: 'call_unavailable' });
|
|
181
|
+
}
|
|
182
|
+
}, 8000);
|
|
183
|
+
const fresh = await client.refresh();
|
|
184
|
+
if (opening !== callId)
|
|
185
|
+
return state.call?.id === callId;
|
|
186
|
+
// A current response authoritatively rejects a stale/foreign push.
|
|
187
|
+
if (fresh) {
|
|
188
|
+
opening = null;
|
|
189
|
+
clearTimeout(openingTimer);
|
|
190
|
+
publish({ phase: 'error', error: 'call_unavailable' });
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
},
|
|
194
|
+
dismissError() { if (state.phase === 'error')
|
|
195
|
+
publish({ phase: 'idle', error: null }); },
|
|
196
|
+
async dispose() {
|
|
197
|
+
if (disposed)
|
|
198
|
+
return;
|
|
199
|
+
disposed = true;
|
|
200
|
+
++epoch;
|
|
201
|
+
opening = null;
|
|
202
|
+
clearTimeout(openingTimer);
|
|
203
|
+
listeners.clear();
|
|
204
|
+
// No end cue on logout/account switch; native resources release immediately.
|
|
205
|
+
const results = await Promise.allSettled([client.dispose(), Promise.resolve().then(() => audio.dispose())]);
|
|
206
|
+
const failed = results.find(result => result.status === 'rejected');
|
|
207
|
+
if (failed)
|
|
208
|
+
throw failed.reason;
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as Notifications from 'expo-notifications';
|
|
2
|
+
import { type CallingNotificationRouterOptions } from './notification-router.js';
|
|
3
|
+
export { callingNotificationPresentation } from './notification-router.js';
|
|
4
|
+
/** Compose explicitly with your existing handler; preserve ordinary message alerts. */
|
|
5
|
+
export declare function withCallingNotificationPresentation(handler: Notifications.NotificationHandler, canPresentCall: () => boolean): Notifications.NotificationHandler;
|
|
6
|
+
/** Registers listeners, never replaces the app's global presentation handler. */
|
|
7
|
+
export declare function connectExpoCallingNotifications(options: Omit<CallingNotificationRouterOptions, 'dismiss'> & {
|
|
8
|
+
onError(error: Error): void;
|
|
9
|
+
}): {
|
|
10
|
+
ready: Promise<void>;
|
|
11
|
+
dispose(): void;
|
|
12
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as Notifications from 'expo-notifications';
|
|
2
|
+
import { AppState } from 'react-native';
|
|
3
|
+
import { createCallingNotificationRouter, callingNotificationPresentation } from './notification-router.js';
|
|
4
|
+
export { callingNotificationPresentation } from './notification-router.js';
|
|
5
|
+
/** Compose explicitly with your existing handler; preserve ordinary message alerts. */
|
|
6
|
+
export function withCallingNotificationPresentation(handler, canPresentCall) {
|
|
7
|
+
return { ...handler, handleNotification: async (notification) => {
|
|
8
|
+
const decision = callingNotificationPresentation(notification.request.content.data, AppState.currentState === 'active');
|
|
9
|
+
if (decision === 'dismiss' || decision === 'screen' && canPresentCall())
|
|
10
|
+
return { shouldPlaySound: false, shouldSetBadge: false, shouldShowBanner: false, shouldShowList: false };
|
|
11
|
+
return handler.handleNotification(notification);
|
|
12
|
+
} };
|
|
13
|
+
}
|
|
14
|
+
/** Registers listeners, never replaces the app's global presentation handler. */
|
|
15
|
+
export function connectExpoCallingNotifications(options) {
|
|
16
|
+
let disposed = false;
|
|
17
|
+
const router = createCallingNotificationRouter({ ...options, dismiss: async (callId) => {
|
|
18
|
+
const shown = await Notifications.getPresentedNotificationsAsync();
|
|
19
|
+
await Promise.all(shown.filter(item => item.request.content.data?.call_id === callId &&
|
|
20
|
+
options.recipientAccount(item.request.content.data) === options.accountKey)
|
|
21
|
+
.map(item => Notifications.dismissNotificationAsync(item.request.identifier)));
|
|
22
|
+
} });
|
|
23
|
+
const received = Notifications.addNotificationReceivedListener(notification => {
|
|
24
|
+
void router.handle(notification.request.content.data).catch(options.onError);
|
|
25
|
+
});
|
|
26
|
+
const response = async (value) => {
|
|
27
|
+
if (disposed || !value || value.actionIdentifier !== Notifications.DEFAULT_ACTION_IDENTIFIER)
|
|
28
|
+
return;
|
|
29
|
+
const data = value.notification.request.content.data;
|
|
30
|
+
if (!callingNotificationPresentation(data, false))
|
|
31
|
+
return;
|
|
32
|
+
if (await router.handle(data, true)) {
|
|
33
|
+
// Don't clear a different notification that arrived while storage was read.
|
|
34
|
+
const last = await Notifications.getLastNotificationResponseAsync();
|
|
35
|
+
if (!disposed && last?.notification.request.identifier === value.notification.request.identifier)
|
|
36
|
+
await Notifications.clearLastNotificationResponseAsync();
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const opened = Notifications.addNotificationResponseReceivedListener(value => { void response(value).catch(options.onError); });
|
|
40
|
+
const ready = Notifications.getLastNotificationResponseAsync().then(response);
|
|
41
|
+
// Report a startup failure without leaving an unhandled rejection.
|
|
42
|
+
void ready.catch(options.onError);
|
|
43
|
+
return { ready, dispose() { disposed = true; received.remove(); opened.remove(); router.dispose(); } };
|
|
44
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { CallHistoryEntry } from '@robono/calling';
|
|
3
|
+
/** Render in the existing conversation; uses the caller's side and bubble color. */
|
|
4
|
+
export declare function CallHistoryItem({ entry, incomingColor, outgoingColor, textColor, onCallBack, label }: {
|
|
5
|
+
entry: CallHistoryEntry;
|
|
6
|
+
incomingColor: string;
|
|
7
|
+
outgoingColor: string;
|
|
8
|
+
textColor?: string;
|
|
9
|
+
/** Caller must recheck live availability before placing the callback. */
|
|
10
|
+
onCallBack?: () => void;
|
|
11
|
+
label?: (entry: CallHistoryEntry) => {
|
|
12
|
+
label: string;
|
|
13
|
+
detail: string;
|
|
14
|
+
missed: boolean;
|
|
15
|
+
};
|
|
16
|
+
}): React.JSX.Element;
|
package/dist/history.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Pressable, StyleSheet, Text, View } from 'react-native';
|
|
3
|
+
import Svg, { Path } from 'react-native-svg';
|
|
4
|
+
import { describeCall } from '@robono/calling/history';
|
|
5
|
+
/** Render in the existing conversation; uses the caller's side and bubble color. */
|
|
6
|
+
export function CallHistoryItem({ entry, incomingColor, outgoingColor, textColor = '#ffffff', onCallBack, label }) {
|
|
7
|
+
const description = label ? label(entry) : describeCall(entry);
|
|
8
|
+
const incoming = entry.role === 'callee';
|
|
9
|
+
const body = _jsxs(View, { style: [styles.bubble, { backgroundColor: incoming ? incomingColor : outgoingColor }], children: [_jsx(Svg, { width: 24, height: 24, viewBox: "0 0 24 24", accessible: false, children: _jsx(Path, { d: description.missed ? 'M4 5v6h6M4 11l6-6 7 7 4-4' : incoming ? 'M18 5 6 17M6 10v7h7' : 'M6 18 18 6M11 6h7v7', stroke: textColor, strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", fill: "none" }) }), _jsxs(View, { style: styles.text, children: [_jsx(Text, { style: [styles.title, { color: textColor }], children: description.label }), !!description.detail && _jsx(Text, { style: { color: textColor }, children: description.detail })] })] });
|
|
10
|
+
return _jsx(View, { style: { alignItems: incoming ? 'flex-start' : 'flex-end', marginVertical: 4 }, children: onCallBack ? _jsx(Pressable, { onPress: onCallBack, accessibilityRole: "button", accessibilityLabel: `${description.label}. ${description.detail}`, accessibilityHint: "Call this contact", style: styles.width, children: body }) : body });
|
|
11
|
+
}
|
|
12
|
+
const styles = StyleSheet.create({ bubble: { borderRadius: 16, padding: 14, flexDirection: 'row', alignItems: 'center', gap: 12, maxWidth: '85%' }, width: { maxWidth: '100%' }, text: { flexShrink: 1, gap: 4 }, title: { fontSize: 16, fontWeight: '500' } });
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CallingController, CallingOptions } from './types.js';
|
|
2
|
+
export declare function createCallingController(options: CallingOptions): CallingController;
|
|
3
|
+
export { CallingScreen, CallingOverlay, useCalling } from './screens.js';
|
|
4
|
+
export { CallHistoryItem } from './history.js';
|
|
5
|
+
export type { CallingScreenProps, CallingOverlayProps, CallingLabels, CallingTheme } from './screens.js';
|
|
6
|
+
export type { CallAudio, CallingState, CallingController, CallingOptions, Contact, MediaAdapter, Phase } from './types.js';
|
|
7
|
+
export { callingInk, defaultCallingAppearance, validateCallingAppearance, soundCatalog } from './appearance.js';
|
|
8
|
+
export type { CallingAppearance, SoundChoice, SoundEvent } from './appearance.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RobonoCallingClient } from '@robono/calling';
|
|
2
|
+
import { createController } from './controller.mjs';
|
|
3
|
+
import { Platform } from 'react-native';
|
|
4
|
+
export function createCallingController(options) {
|
|
5
|
+
const controller = createController({ ...options, createClient: (settings) => new RobonoCallingClient(settings) });
|
|
6
|
+
const components = new Set(['controller', ...(options.audio.robonoAdapter ? ['native-audio'] : []), ...(options.media.robonoAdapter ? ['native-media'] : [])]);
|
|
7
|
+
let sent = '';
|
|
8
|
+
controller.reportComponents = (extra) => {
|
|
9
|
+
extra.forEach(x => components.add(x));
|
|
10
|
+
const key = [...components].sort().join(',');
|
|
11
|
+
if (sent === key)
|
|
12
|
+
return;
|
|
13
|
+
sent = key;
|
|
14
|
+
// Inventory is explicitly client-declared; it cannot set charges or access.
|
|
15
|
+
void Promise.resolve().then(() => options.request('client-info', { platform: Platform.OS, engineVersion: '0.1.1', mobileVersion: '0.2.0', components: [...components].sort(), appearanceRevision: options.appearanceRevision })).catch(() => { sent = ''; });
|
|
16
|
+
};
|
|
17
|
+
const listen = controller.listen;
|
|
18
|
+
controller.listen = async () => { await listen(); controller.reportComponents?.([]); };
|
|
19
|
+
return controller;
|
|
20
|
+
}
|
|
21
|
+
export { CallingScreen, CallingOverlay, useCalling } from './screens.js';
|
|
22
|
+
export { CallHistoryItem } from './history.js';
|
|
23
|
+
export { callingInk, defaultCallingAppearance, validateCallingAppearance, soundCatalog } from './appearance.js';
|
package/dist/native.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CallAudio, MediaAdapter } from './types.js';
|
|
2
|
+
import { type CallingAppearance } from './appearance.js';
|
|
3
|
+
import type { CallRuntime } from './runtime.js';
|
|
4
|
+
export interface NativeAdapterOptions {
|
|
5
|
+
appearance?: CallingAppearance;
|
|
6
|
+
/** Restore your app's ordinary playback/recording policy after each call. */
|
|
7
|
+
restoreAudio(): Promise<void>;
|
|
8
|
+
onError(error: Error): void;
|
|
9
|
+
/** Default uses the person's system ringtone. Set 'robono' for the supplied chime. */
|
|
10
|
+
ringtone?: 'system' | 'robono';
|
|
11
|
+
/** Supplied native runtime, or your existing application's lifetime adapter. */
|
|
12
|
+
runtime?: CallRuntime;
|
|
13
|
+
}
|
|
14
|
+
/** Import this entry point only when choosing the supplied native audio adapter. */
|
|
15
|
+
export declare function createNativeCallAdapters(options: NativeAdapterOptions): {
|
|
16
|
+
media: MediaAdapter;
|
|
17
|
+
audio: CallAudio;
|
|
18
|
+
};
|
package/dist/native.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { PermissionsAndroid, Platform } from 'react-native';
|
|
2
|
+
import { mediaDevices, MediaStream, RTCPeerConnection } from 'react-native-webrtc';
|
|
3
|
+
import InCallManager from 'react-native-incall-manager';
|
|
4
|
+
import { createAudioPlayer, setAudioModeAsync } from 'expo-audio';
|
|
5
|
+
import { randomUUID } from 'expo-crypto';
|
|
6
|
+
import { createCallAudio } from './audio.mjs';
|
|
7
|
+
import { callingSounds } from './appearance.js';
|
|
8
|
+
let audioOwner = null;
|
|
9
|
+
/** Import this entry point only when choosing the supplied native audio adapter. */
|
|
10
|
+
export function createNativeCallAdapters(options) {
|
|
11
|
+
if (Platform.OS !== 'ios' && Platform.OS !== 'android')
|
|
12
|
+
throw Error('native_platform_required');
|
|
13
|
+
let players;
|
|
14
|
+
const sounds = { ...callingSounds, ...options.appearance?.sounds };
|
|
15
|
+
const owner = Symbol('calling-audio');
|
|
16
|
+
let current = '', session = '', speaker = false, disposed = false;
|
|
17
|
+
let resumedFocus = false;
|
|
18
|
+
// These cues share AVAudioSession with WebRTC. Expo's default pause/finish
|
|
19
|
+
// deactivates that session when no Expo player is playing, without accounting
|
|
20
|
+
// for the live WebRTC microphone. Call cleanup owns session release instead.
|
|
21
|
+
const cueOptions = { keepAudioSessionActive: true };
|
|
22
|
+
const load = () => players ||= {
|
|
23
|
+
ringing: createAudioPlayer(require('../assets/ringing.wav'), cueOptions),
|
|
24
|
+
connecting: createAudioPlayer(require('../assets/connecting.wav'), cueOptions),
|
|
25
|
+
ended: createAudioPlayer(require('../assets/ended.wav'), cueOptions),
|
|
26
|
+
bell: createAudioPlayer(require('../assets/bell.wav'), cueOptions),
|
|
27
|
+
marimba: createAudioPlayer(require('../assets/marimba.wav'), cueOptions),
|
|
28
|
+
pulse: createAudioPlayer(require('../assets/pulse.wav'), cueOptions),
|
|
29
|
+
click: createAudioPlayer(require('../assets/click.wav'), cueOptions),
|
|
30
|
+
};
|
|
31
|
+
const pause = () => {
|
|
32
|
+
if (audioOwner === owner)
|
|
33
|
+
InCallManager.stopRingtone();
|
|
34
|
+
if (players)
|
|
35
|
+
Object.values(players).forEach(player => player.pause());
|
|
36
|
+
current = '';
|
|
37
|
+
};
|
|
38
|
+
const route = () => {
|
|
39
|
+
// null restores automatic headset/Bluetooth routing; false forces earpiece.
|
|
40
|
+
InCallManager.setForceSpeakerphoneOn((speaker ? true : null));
|
|
41
|
+
};
|
|
42
|
+
const audio = createCallAudio({
|
|
43
|
+
schedule: options.runtime?.schedule,
|
|
44
|
+
sound: (event) => sounds[event],
|
|
45
|
+
duration: (event) => ['bell', 'marimba'].includes(sounds[event]) ? 1850 : 700,
|
|
46
|
+
onError: options.onError,
|
|
47
|
+
pause,
|
|
48
|
+
playing: () => !!current,
|
|
49
|
+
async resume() {
|
|
50
|
+
if (audioOwner !== owner || disposed)
|
|
51
|
+
throw Error('call_cancelled');
|
|
52
|
+
if (Platform.OS === 'ios') {
|
|
53
|
+
if (!options.runtime?.activateAudio)
|
|
54
|
+
throw Error('calling_runtime_native_build_required');
|
|
55
|
+
await options.runtime.activateAudio();
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
const result = await InCallManager.requestAudioFocus();
|
|
59
|
+
if (result !== 'AUDIOFOCUS_REQUEST_GRANTED')
|
|
60
|
+
throw Error('audio_still_interrupted');
|
|
61
|
+
resumedFocus = true;
|
|
62
|
+
}
|
|
63
|
+
route();
|
|
64
|
+
},
|
|
65
|
+
async prepare(phase, selected) {
|
|
66
|
+
if (audioOwner && audioOwner !== owner)
|
|
67
|
+
throw Error('audio_busy');
|
|
68
|
+
audioOwner = owner;
|
|
69
|
+
speaker = selected;
|
|
70
|
+
const target = phase === 'incoming' ? 'ring' : 'call';
|
|
71
|
+
if (session !== target) {
|
|
72
|
+
if (session === 'call')
|
|
73
|
+
InCallManager.stop();
|
|
74
|
+
await setAudioModeAsync({ allowsRecording: target === 'call' && Platform.OS === 'ios', playsInSilentMode: target === 'call',
|
|
75
|
+
interruptionMode: 'mixWithOthers', shouldPlayInBackground: target === 'call', shouldRouteThroughEarpiece: target === 'call' });
|
|
76
|
+
if (target === 'call')
|
|
77
|
+
InCallManager.start({ media: 'audio' });
|
|
78
|
+
session = target;
|
|
79
|
+
}
|
|
80
|
+
if (target === 'call')
|
|
81
|
+
route();
|
|
82
|
+
},
|
|
83
|
+
async play(kind, playback = {}) {
|
|
84
|
+
pause();
|
|
85
|
+
current = kind;
|
|
86
|
+
if (playback.event === 'incoming' && !options.appearance && kind === 'ringing' && options.ringtone !== 'robono') {
|
|
87
|
+
InCallManager.startRingtone('_DEFAULT_', [0, 350, 900], 'ambient', 0);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const player = load()[kind];
|
|
91
|
+
player.loop = !!playback.loop;
|
|
92
|
+
// Fresh local assets start at zero. Seeking before native decoding has
|
|
93
|
+
// loaded can reject; play itself queues safely while the asset loads.
|
|
94
|
+
// Android's isLoaded becomes false at STATE_ENDED, even though the file
|
|
95
|
+
// remains prepared. Rewind completed cues as well as currently ready ones.
|
|
96
|
+
if (player.isLoaded || player.currentTime > 0)
|
|
97
|
+
await player.seekTo(0);
|
|
98
|
+
if (current === kind && !disposed)
|
|
99
|
+
player.play();
|
|
100
|
+
},
|
|
101
|
+
async release() {
|
|
102
|
+
pause();
|
|
103
|
+
if (audioOwner !== owner)
|
|
104
|
+
return;
|
|
105
|
+
try {
|
|
106
|
+
InCallManager.stop();
|
|
107
|
+
session = '';
|
|
108
|
+
try {
|
|
109
|
+
if (resumedFocus) {
|
|
110
|
+
resumedFocus = false;
|
|
111
|
+
await InCallManager.abandonAudioFocus();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
try {
|
|
116
|
+
await options.runtime?.end();
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
await options.restoreAudio();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
if (audioOwner === owner)
|
|
125
|
+
audioOwner = null;
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
dispose() { disposed = true; pause(); if (players)
|
|
129
|
+
Object.values(players).forEach(player => player.remove()); players = undefined; },
|
|
130
|
+
});
|
|
131
|
+
audio.robonoAdapter = true;
|
|
132
|
+
const media = {
|
|
133
|
+
robonoAdapter: true,
|
|
134
|
+
createId: randomUUID,
|
|
135
|
+
createPeerConnection: config => new RTCPeerConnection(config),
|
|
136
|
+
createRemoteStream: track => new MediaStream([track]),
|
|
137
|
+
async getUserMedia(context) {
|
|
138
|
+
const current = () => !disposed && (!context || context.isCurrent());
|
|
139
|
+
if (!current())
|
|
140
|
+
throw Error('call_cancelled');
|
|
141
|
+
if (Platform.OS === 'android') {
|
|
142
|
+
const result = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO);
|
|
143
|
+
if (!current())
|
|
144
|
+
throw Error('call_cancelled');
|
|
145
|
+
if (result !== PermissionsAndroid.RESULTS.GRANTED)
|
|
146
|
+
throw Error('microphone_permission_denied');
|
|
147
|
+
}
|
|
148
|
+
await audio.ready();
|
|
149
|
+
if (!current())
|
|
150
|
+
throw Error('call_cancelled');
|
|
151
|
+
await options.runtime?.begin();
|
|
152
|
+
if (!current()) {
|
|
153
|
+
await options.runtime?.end();
|
|
154
|
+
throw Error('call_cancelled');
|
|
155
|
+
}
|
|
156
|
+
let stream;
|
|
157
|
+
try {
|
|
158
|
+
stream = await mediaDevices.getUserMedia({ audio: true, video: false });
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
await options.runtime?.end();
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
if (!current()) {
|
|
165
|
+
stream.getTracks().forEach(track => track.stop());
|
|
166
|
+
await options.runtime?.end();
|
|
167
|
+
throw Error('call_cancelled');
|
|
168
|
+
}
|
|
169
|
+
route();
|
|
170
|
+
return stream;
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
return { media, audio };
|
|
174
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type NotificationStore } from './notifications.js';
|
|
2
|
+
export interface CallingNotificationRouterOptions {
|
|
3
|
+
accountKey: string;
|
|
4
|
+
/** Resolve your backend's recipient field; never infer identity from a call ID. */
|
|
5
|
+
recipientAccount(data: unknown): string | null;
|
|
6
|
+
store: NotificationStore;
|
|
7
|
+
/** Set the root call layer immediately; authentication may still be loading. */
|
|
8
|
+
open(callId: string): void;
|
|
9
|
+
dismiss(callId: string): Promise<void>;
|
|
10
|
+
refresh(): Promise<unknown>;
|
|
11
|
+
}
|
|
12
|
+
/** Mount for one restored account. Dispose on logout before changing accounts. */
|
|
13
|
+
export declare function createCallingNotificationRouter(options: CallingNotificationRouterOptions): {
|
|
14
|
+
handle(data: unknown, tapped?: boolean): Promise<boolean>;
|
|
15
|
+
dispose(): void;
|
|
16
|
+
};
|
|
17
|
+
/** Provider-neutral presentation decision; leave unrelated notifications alone. */
|
|
18
|
+
export declare function callingNotificationPresentation(data: unknown, foreground: boolean): 'screen' | 'notification' | 'dismiss' | null;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { parseCallingNotification } from './notifications.js';
|
|
2
|
+
/** Mount for one restored account. Dispose on logout before changing accounts. */
|
|
3
|
+
export function createCallingNotificationRouter(options) {
|
|
4
|
+
let disposed = false, queue = Promise.resolve();
|
|
5
|
+
const opened = new Set();
|
|
6
|
+
const process = async (data, tapped) => {
|
|
7
|
+
if (disposed || options.recipientAccount(data) !== options.accountKey)
|
|
8
|
+
return false;
|
|
9
|
+
const event = parseCallingNotification(data);
|
|
10
|
+
if (!event)
|
|
11
|
+
return false;
|
|
12
|
+
const ledger = await options.store.load(options.accountKey);
|
|
13
|
+
if (disposed)
|
|
14
|
+
return false;
|
|
15
|
+
const now = Date.now();
|
|
16
|
+
for (const [id, entry] of Object.entries(ledger))
|
|
17
|
+
if (entry.retainUntil <= now)
|
|
18
|
+
delete ledger[id];
|
|
19
|
+
const previous = ledger[event.call_id]?.sequence || 0;
|
|
20
|
+
const expires = Date.parse(event.ring_expires_at);
|
|
21
|
+
if (event.sequence > previous) {
|
|
22
|
+
ledger[event.call_id] = { sequence: event.sequence, retainUntil: Math.max(now, expires) + 86400000 };
|
|
23
|
+
await options.store.save(options.accountKey, ledger);
|
|
24
|
+
}
|
|
25
|
+
if (disposed)
|
|
26
|
+
return false;
|
|
27
|
+
if (expires <= now || previous >= 2 || event.sequence === 2) {
|
|
28
|
+
await options.dismiss(event.call_id);
|
|
29
|
+
if (!disposed)
|
|
30
|
+
await options.refresh();
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
if (tapped && !opened.has(event.call_id)) {
|
|
34
|
+
opened.add(event.call_id);
|
|
35
|
+
options.open(event.call_id);
|
|
36
|
+
await options.dismiss(event.call_id);
|
|
37
|
+
}
|
|
38
|
+
else if (!tapped && previous < event.sequence)
|
|
39
|
+
await options.refresh();
|
|
40
|
+
return true;
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
handle(data, tapped = false) {
|
|
44
|
+
const next = queue.then(() => process(data, tapped));
|
|
45
|
+
queue = next.then(() => { }, () => { });
|
|
46
|
+
return next;
|
|
47
|
+
},
|
|
48
|
+
dispose() { disposed = true; opened.clear(); },
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Provider-neutral presentation decision; leave unrelated notifications alone. */
|
|
52
|
+
export function callingNotificationPresentation(data, foreground) {
|
|
53
|
+
const event = parseCallingNotification(data);
|
|
54
|
+
if (!event)
|
|
55
|
+
return null;
|
|
56
|
+
if (event.event === 'calling.dismissed' || Date.parse(event.ring_expires_at) <= Date.now())
|
|
57
|
+
return 'dismiss';
|
|
58
|
+
return foreground ? 'screen' : 'notification';
|
|
59
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CallingController } from './types.js';
|
|
2
|
+
export interface CallingNotification {
|
|
3
|
+
event: 'calling.incoming' | 'calling.dismissed';
|
|
4
|
+
event_id: string;
|
|
5
|
+
call_id: string;
|
|
6
|
+
connection_id: string;
|
|
7
|
+
ring_expires_at: string;
|
|
8
|
+
sequence: 1 | 2;
|
|
9
|
+
}
|
|
10
|
+
export interface NotificationLedger {
|
|
11
|
+
[callId: string]: {
|
|
12
|
+
sequence: number;
|
|
13
|
+
retainUntil: number;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface NotificationStore {
|
|
17
|
+
load(accountKey: string): Promise<NotificationLedger>;
|
|
18
|
+
save(accountKey: string, ledger: NotificationLedger): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/** Works with AsyncStorage or another durable, account-safe key/value store. */
|
|
21
|
+
export declare function createCallingNotificationStore(storage: {
|
|
22
|
+
getItem(key: string): Promise<string | null>;
|
|
23
|
+
setItem(key: string, value: string): Promise<void>;
|
|
24
|
+
}, prefix?: string): NotificationStore;
|
|
25
|
+
export declare function parseCallingNotification(data: unknown): CallingNotification | null;
|
|
26
|
+
/** Host retains its push provider, credentials and notification handler. */
|
|
27
|
+
export declare function createCallingNotificationHandler(options: {
|
|
28
|
+
controller: CallingController;
|
|
29
|
+
accountKey: string;
|
|
30
|
+
store: NotificationStore;
|
|
31
|
+
dismiss(callId: string): Promise<void>;
|
|
32
|
+
}): {
|
|
33
|
+
handle(data: unknown, options?: {
|
|
34
|
+
opened?: boolean;
|
|
35
|
+
}): Promise<boolean>;
|
|
36
|
+
dispose(): void;
|
|
37
|
+
};
|