@telnyx/react-voice-commons-sdk 0.1.2 → 0.1.4

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.
Files changed (55) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +469 -483
  3. package/ios/CallKitBridge.swift +2 -7
  4. package/lib/callkit/callkit-coordinator.d.ts +110 -117
  5. package/lib/callkit/callkit-coordinator.js +664 -727
  6. package/lib/callkit/callkit.d.ts +41 -41
  7. package/lib/callkit/callkit.js +252 -242
  8. package/lib/callkit/index.js +15 -47
  9. package/lib/callkit/use-callkit.d.ts +19 -19
  10. package/lib/callkit/use-callkit.js +270 -310
  11. package/lib/context/TelnyxVoiceContext.d.ts +9 -9
  12. package/lib/context/TelnyxVoiceContext.js +10 -13
  13. package/lib/hooks/use-callkit-coordinator.d.ts +9 -17
  14. package/lib/hooks/use-callkit-coordinator.js +45 -50
  15. package/lib/hooks/useAppReadyNotifier.js +13 -15
  16. package/lib/hooks/useAppStateHandler.d.ts +6 -11
  17. package/lib/hooks/useAppStateHandler.js +95 -110
  18. package/lib/hooks/useNetworkStateHandler.d.ts +0 -0
  19. package/lib/hooks/useNetworkStateHandler.js +0 -0
  20. package/lib/index.d.ts +3 -21
  21. package/lib/index.js +50 -201
  22. package/lib/internal/CallKitHandler.d.ts +6 -6
  23. package/lib/internal/CallKitHandler.js +96 -104
  24. package/lib/internal/callkit-manager.d.ts +57 -57
  25. package/lib/internal/callkit-manager.js +299 -316
  26. package/lib/internal/calls/call-state-controller.d.ts +73 -86
  27. package/lib/internal/calls/call-state-controller.js +263 -307
  28. package/lib/internal/session/session-manager.d.ts +71 -75
  29. package/lib/internal/session/session-manager.js +360 -424
  30. package/lib/internal/user-defaults-helpers.js +49 -39
  31. package/lib/internal/voice-pn-bridge.d.ts +114 -12
  32. package/lib/internal/voice-pn-bridge.js +212 -5
  33. package/lib/models/call-state.d.ts +46 -44
  34. package/lib/models/call-state.js +70 -68
  35. package/lib/models/call.d.ts +161 -133
  36. package/lib/models/call.js +454 -382
  37. package/lib/models/config.d.ts +11 -18
  38. package/lib/models/config.js +37 -35
  39. package/lib/models/connection-state.d.ts +10 -10
  40. package/lib/models/connection-state.js +16 -16
  41. package/lib/telnyx-voice-app.d.ts +28 -28
  42. package/lib/telnyx-voice-app.js +513 -480
  43. package/lib/telnyx-voip-client.d.ts +167 -167
  44. package/lib/telnyx-voip-client.js +385 -390
  45. package/package.json +115 -104
  46. package/src/callkit/callkit-coordinator.ts +830 -846
  47. package/src/hooks/useNetworkStateHandler.ts +0 -0
  48. package/src/internal/calls/call-state-controller.ts +407 -384
  49. package/src/internal/session/session-manager.ts +483 -467
  50. package/src/internal/voice-pn-bridge.ts +266 -18
  51. package/src/models/call-state.ts +105 -98
  52. package/src/models/call.ts +502 -388
  53. package/src/telnyx-voice-app.tsx +788 -690
  54. package/src/telnyx-voip-client.ts +551 -539
  55. package/src/types/telnyx-sdk.d.ts +93 -79
@@ -1,388 +1,502 @@
1
- import { BehaviorSubject, Observable } from 'rxjs';
2
- import { distinctUntilChanged, map } from 'rxjs/operators';
3
- import { TelnyxCallState, CallStateHelpers } from './call-state';
4
- import { Call as TelnyxCall } from '@telnyx/react-native-voice-sdk';
5
- import { Platform } from 'react-native';
6
-
7
- /**
8
- * Represents a call with reactive state streams.
9
- *
10
- * This class wraps the underlying Telnyx Call object and provides
11
- * reactive streams for all call state changes, making it easy to
12
- * integrate with any state management solution.
13
- */
14
- export class Call {
15
- private readonly _callState = new BehaviorSubject<TelnyxCallState>(TelnyxCallState.RINGING);
16
- private readonly _isMuted = new BehaviorSubject<boolean>(false);
17
- private readonly _isHeld = new BehaviorSubject<boolean>(false);
18
- private readonly _duration = new BehaviorSubject<number>(0);
19
-
20
- private _durationTimer?: NodeJS.Timeout;
21
- private _startTime?: Date;
22
-
23
- constructor(
24
- private readonly _telnyxCall: TelnyxCall,
25
- private readonly _callId: string,
26
- private readonly _destination: string,
27
- private readonly _isIncoming: boolean
28
- ) {
29
- this._setupCallListeners();
30
- }
31
-
32
- /**
33
- * Unique identifier for this call
34
- */
35
- get callId(): string {
36
- return this._callId;
37
- }
38
-
39
- /**
40
- * The destination number or SIP URI
41
- */
42
- get destination(): string {
43
- return this._destination;
44
- }
45
-
46
- /**
47
- * Whether this is an incoming call
48
- */
49
- get isIncoming(): boolean {
50
- return this._isIncoming;
51
- }
52
-
53
- /**
54
- * Whether this is an outgoing call
55
- */
56
- get isOutgoing(): boolean {
57
- return !this._isIncoming;
58
- }
59
-
60
- /**
61
- * Current call state (synchronous access)
62
- */
63
- get currentState(): TelnyxCallState {
64
- return this._callState.value;
65
- }
66
-
67
- /**
68
- * Current mute state (synchronous access)
69
- */
70
- get currentIsMuted(): boolean {
71
- return this._isMuted.value;
72
- }
73
-
74
- /**
75
- * Current hold state (synchronous access)
76
- */
77
- get currentIsHeld(): boolean {
78
- return this._isHeld.value;
79
- }
80
-
81
- /**
82
- * Current call duration in seconds (synchronous access)
83
- */
84
- get currentDuration(): number {
85
- return this._duration.value;
86
- }
87
-
88
- /**
89
- * Get the underlying Telnyx Call object (for internal use)
90
- * @internal
91
- */
92
- get telnyxCall(): TelnyxCall {
93
- return this._telnyxCall;
94
- }
95
-
96
- /**
97
- * Observable stream of call state changes
98
- */
99
- get callState$(): Observable<TelnyxCallState> {
100
- return this._callState.asObservable().pipe(distinctUntilChanged());
101
- }
102
-
103
- /**
104
- * Observable stream of mute state changes
105
- */
106
- get isMuted$(): Observable<boolean> {
107
- return this._isMuted.asObservable().pipe(distinctUntilChanged());
108
- }
109
-
110
- /**
111
- * Observable stream of hold state changes
112
- */
113
- get isHeld$(): Observable<boolean> {
114
- return this._isHeld.asObservable().pipe(distinctUntilChanged());
115
- }
116
-
117
- /**
118
- * Observable stream of call duration changes (in seconds)
119
- */
120
- get duration$(): Observable<number> {
121
- return this._duration.asObservable().pipe(distinctUntilChanged());
122
- }
123
-
124
- /**
125
- * Observable that emits true when the call can be answered
126
- */
127
- get canAnswer$(): Observable<boolean> {
128
- return this.callState$.pipe(
129
- map((state) => CallStateHelpers.canAnswer(state)),
130
- distinctUntilChanged()
131
- );
132
- }
133
-
134
- /**
135
- * Observable that emits true when the call can be hung up
136
- */
137
- get canHangup$(): Observable<boolean> {
138
- return this.callState$.pipe(
139
- map((state) => CallStateHelpers.canHangup(state)),
140
- distinctUntilChanged()
141
- );
142
- }
143
-
144
- /**
145
- * Observable that emits true when the call can be put on hold
146
- */
147
- get canHold$(): Observable<boolean> {
148
- return this.callState$.pipe(
149
- map((state) => CallStateHelpers.canHold(state)),
150
- distinctUntilChanged()
151
- );
152
- }
153
-
154
- /**
155
- * Observable that emits true when the call can be resumed from hold
156
- */
157
- get canResume$(): Observable<boolean> {
158
- return this.callState$.pipe(
159
- map((state) => CallStateHelpers.canResume(state)),
160
- distinctUntilChanged()
161
- );
162
- }
163
-
164
- /**
165
- * Answer the incoming call
166
- */
167
- async answer(): Promise<void> {
168
- if (!CallStateHelpers.canAnswer(this.currentState)) {
169
- throw new Error(`Cannot answer call in state: ${this.currentState}`);
170
- }
171
-
172
- try {
173
- // On iOS, use CallKit coordinator for proper audio session handling
174
- if (Platform.OS === 'ios') {
175
- const { callKitCoordinator } = await import('../callkit/callkit-coordinator');
176
- if (callKitCoordinator.isAvailable()) {
177
- console.log('Call: Using CallKit coordinator to answer call (iOS)');
178
- await callKitCoordinator.answerCallFromUI(this._telnyxCall);
179
- return;
180
- }
181
- }
182
-
183
- // Fallback for Android or when CallKit is not available
184
- console.log('Call: Setting state to CONNECTING before answering');
185
- this._callState.next(TelnyxCallState.CONNECTING);
186
-
187
- await this._telnyxCall.answer();
188
- } catch (error) {
189
- console.error('Failed to answer call:', error);
190
- throw error;
191
- }
192
- }
193
-
194
- /**
195
- * Hang up the call
196
- */
197
- async hangup(): Promise<void> {
198
- if (!CallStateHelpers.canHangup(this.currentState)) {
199
- throw new Error(`Cannot hang up call in state: ${this.currentState}`);
200
- }
201
-
202
- try {
203
- // On iOS, use CallKit coordinator for proper CallKit cleanup
204
- if (Platform.OS === 'ios') {
205
- const { callKitCoordinator } = await import('../callkit/callkit-coordinator');
206
- if (callKitCoordinator.isAvailable()) {
207
- console.log('Call: Using CallKit coordinator to end call (iOS)');
208
- await callKitCoordinator.endCallFromUI(this._telnyxCall);
209
- return;
210
- }
211
- }
212
-
213
- // Fallback for Android or when CallKit is not available
214
- await this._telnyxCall.hangup();
215
- } catch (error) {
216
- console.error('Failed to hang up call:', error);
217
- throw error;
218
- }
219
- }
220
-
221
- /**
222
- * Put the call on hold
223
- */
224
- async hold(): Promise<void> {
225
- if (!CallStateHelpers.canHold(this.currentState)) {
226
- throw new Error(`Cannot hold call in state: ${this.currentState}`);
227
- }
228
-
229
- try {
230
- await this._telnyxCall.hold();
231
- } catch (error) {
232
- console.error('Failed to hold call:', error);
233
- throw error;
234
- }
235
- }
236
-
237
- /**
238
- * Resume the call from hold
239
- */
240
- async resume(): Promise<void> {
241
- if (!CallStateHelpers.canResume(this.currentState)) {
242
- throw new Error(`Cannot resume call in state: ${this.currentState}`);
243
- }
244
-
245
- try {
246
- await this._telnyxCall.unhold();
247
- } catch (error) {
248
- console.error('Failed to resume call:', error);
249
- throw error;
250
- }
251
- }
252
-
253
- /**
254
- * Mute the call
255
- */
256
- async mute(): Promise<void> {
257
- if (!CallStateHelpers.canToggleMute(this.currentState)) {
258
- throw new Error(`Cannot mute call in state: ${this.currentState}`);
259
- }
260
-
261
- try {
262
- this._telnyxCall.mute();
263
- this._isMuted.next(true);
264
- } catch (error) {
265
- console.error('Failed to mute call:', error);
266
- throw error;
267
- }
268
- }
269
-
270
- /**
271
- * Unmute the call
272
- */
273
- async unmute(): Promise<void> {
274
- if (!CallStateHelpers.canToggleMute(this.currentState)) {
275
- throw new Error(`Cannot unmute call in state: ${this.currentState}`);
276
- }
277
-
278
- try {
279
- this._telnyxCall.unmute();
280
- this._isMuted.next(false);
281
- } catch (error) {
282
- console.error('Failed to unmute call:', error);
283
- throw error;
284
- }
285
- }
286
-
287
- /**
288
- * Toggle mute state
289
- */
290
- async toggleMute(): Promise<void> {
291
- if (this.currentIsMuted) {
292
- await this.unmute();
293
- } else {
294
- await this.mute();
295
- }
296
- }
297
-
298
- /**
299
- * Set the call to connecting state (used for push notification calls when answered via CallKit)
300
- * @internal
301
- */
302
- setConnecting(): void {
303
- console.log('Call: Setting state to CONNECTING for push notification answer');
304
- this._callState.next(TelnyxCallState.CONNECTING);
305
- }
306
-
307
- /**
308
- * Clean up resources when the call is disposed
309
- */
310
- dispose(): void {
311
- this._stopDurationTimer();
312
- this._callState.complete();
313
- this._isMuted.complete();
314
- this._isHeld.complete();
315
- this._duration.complete();
316
- }
317
-
318
- /**
319
- * Set up listeners for the underlying Telnyx call
320
- */
321
- private _setupCallListeners(): void {
322
- // Map Telnyx call states to our simplified states
323
- this._telnyxCall.on('telnyx.call.state', (call: any, state: any) => {
324
- const telnyxState = this._mapToTelnyxCallState(state);
325
- this._callState.next(telnyxState);
326
-
327
- // Start duration timer when call becomes active
328
- if (telnyxState === TelnyxCallState.ACTIVE && !this._startTime) {
329
- this._startDurationTimer();
330
- }
331
-
332
- // Stop duration timer when call ends
333
- if (CallStateHelpers.isTerminated(telnyxState)) {
334
- this._stopDurationTimer();
335
- }
336
- });
337
- }
338
-
339
- /**
340
- * Map Telnyx SDK call states to our simplified call states
341
- */
342
- private _mapToTelnyxCallState(telnyxState: any): TelnyxCallState {
343
- // This mapping will depend on the actual Telnyx SDK call states
344
- // For now, using a basic mapping - this should be updated based on actual SDK
345
- switch (telnyxState) {
346
- case 'ringing':
347
- case 'new':
348
- return TelnyxCallState.RINGING;
349
- case 'active':
350
- case 'answered':
351
- return TelnyxCallState.ACTIVE;
352
- case 'held':
353
- return TelnyxCallState.HELD;
354
- case 'ended':
355
- case 'hangup':
356
- return TelnyxCallState.ENDED;
357
- case 'failed':
358
- case 'rejected':
359
- return TelnyxCallState.FAILED;
360
- default:
361
- console.warn(`Unknown call state: ${telnyxState}`);
362
- return TelnyxCallState.RINGING;
363
- }
364
- }
365
-
366
- /**
367
- * Start the duration timer
368
- */
369
- private _startDurationTimer(): void {
370
- this._startTime = new Date();
371
- this._durationTimer = setInterval(() => {
372
- if (this._startTime) {
373
- const duration = Math.floor((Date.now() - this._startTime.getTime()) / 1000);
374
- this._duration.next(duration);
375
- }
376
- }, 1000);
377
- }
378
-
379
- /**
380
- * Stop the duration timer
381
- */
382
- private _stopDurationTimer(): void {
383
- if (this._durationTimer) {
384
- clearInterval(this._durationTimer);
385
- this._durationTimer = undefined;
386
- }
387
- }
388
- }
1
+ import { BehaviorSubject, Observable } from 'rxjs';
2
+ import { distinctUntilChanged, map } from 'rxjs/operators';
3
+ import { TelnyxCallState, CallStateHelpers } from './call-state';
4
+ import { Call as TelnyxCall } from '@telnyx/react-native-voice-sdk';
5
+ import { Platform } from 'react-native';
6
+
7
+ /**
8
+ * Represents a call with reactive state streams.
9
+ *
10
+ * This class wraps the underlying Telnyx Call object and provides
11
+ * reactive streams for all call state changes, making it easy to
12
+ * integrate with any state management solution.
13
+ */
14
+ export class Call {
15
+ private readonly _callState = new BehaviorSubject<TelnyxCallState>(TelnyxCallState.RINGING);
16
+ private readonly _isMuted = new BehaviorSubject<boolean>(false);
17
+ private readonly _isHeld = new BehaviorSubject<boolean>(false);
18
+ private readonly _duration = new BehaviorSubject<number>(0);
19
+
20
+ private _durationTimer?: NodeJS.Timeout;
21
+ private _startTime?: Date;
22
+
23
+ constructor(
24
+ private readonly _telnyxCall: TelnyxCall,
25
+ private readonly _callId: string,
26
+ private readonly _destination: string,
27
+ private readonly _isIncoming: boolean,
28
+ isReattached: boolean = false,
29
+ private readonly _originalCallerName?: string,
30
+ private readonly _originalCallerNumber?: string
31
+ ) {
32
+ // Set initial state based on whether this is a reattached call
33
+ if (isReattached) {
34
+ console.log('Call: Setting initial state to ACTIVE for reattached call');
35
+ this._callState.next(TelnyxCallState.ACTIVE);
36
+ }
37
+
38
+ this._setupCallListeners();
39
+ }
40
+
41
+ /**
42
+ * Unique identifier for this call
43
+ */
44
+ get callId(): string {
45
+ return this._callId;
46
+ }
47
+
48
+ /**
49
+ * The destination number or SIP URI
50
+ */
51
+ get destination(): string {
52
+ return this._destination;
53
+ }
54
+
55
+ /**
56
+ * Whether this is an incoming call
57
+ */
58
+ get isIncoming(): boolean {
59
+ return this._isIncoming;
60
+ }
61
+
62
+ /**
63
+ * Whether this is an outgoing call
64
+ */
65
+ get isOutgoing(): boolean {
66
+ return !this._isIncoming;
67
+ }
68
+
69
+ /**
70
+ * Current call state (synchronous access)
71
+ */
72
+ get currentState(): TelnyxCallState {
73
+ return this._callState.value;
74
+ }
75
+
76
+ /**
77
+ * Current mute state (synchronous access)
78
+ */
79
+ get currentIsMuted(): boolean {
80
+ return this._isMuted.value;
81
+ }
82
+
83
+ /**
84
+ * Current hold state (synchronous access)
85
+ */
86
+ get currentIsHeld(): boolean {
87
+ return this._isHeld.value;
88
+ }
89
+
90
+ /**
91
+ * Current call duration in seconds (synchronous access)
92
+ */
93
+ get currentDuration(): number {
94
+ return this._duration.value;
95
+ }
96
+
97
+ /**
98
+ * Custom headers received from the WebRTC INVITE message.
99
+ * These headers are passed during call initiation and can contain application-specific information.
100
+ * Format should be [{"name": "X-Header-Name", "value": "Value"}] where header names must start with "X-".
101
+ */
102
+ get inviteCustomHeaders(): { name: string; value: string }[] | null {
103
+ return this._telnyxCall.inviteCustomHeaders;
104
+ }
105
+
106
+ /**
107
+ * Custom headers received from the WebRTC ANSWER message.
108
+ * These headers are passed during call acceptance and can contain application-specific information.
109
+ * Format should be [{"name": "X-Header-Name", "value": "Value"}] where header names must start with "X-".
110
+ */
111
+ get answerCustomHeaders(): { name: string; value: string }[] | null {
112
+ return this._telnyxCall.answerCustomHeaders;
113
+ }
114
+
115
+ /**
116
+ * Get the underlying Telnyx Call object (for internal use)
117
+ * @internal
118
+ */
119
+ get telnyxCall(): TelnyxCall {
120
+ return this._telnyxCall;
121
+ }
122
+
123
+ /**
124
+ * Observable stream of call state changes
125
+ */
126
+ get callState$(): Observable<TelnyxCallState> {
127
+ return this._callState.asObservable().pipe(distinctUntilChanged());
128
+ }
129
+
130
+ /**
131
+ * Observable stream of mute state changes
132
+ */
133
+ get isMuted$(): Observable<boolean> {
134
+ return this._isMuted.asObservable().pipe(distinctUntilChanged());
135
+ }
136
+
137
+ /**
138
+ * Observable stream of hold state changes
139
+ */
140
+ get isHeld$(): Observable<boolean> {
141
+ return this._isHeld.asObservable().pipe(distinctUntilChanged());
142
+ }
143
+
144
+ /**
145
+ * Observable stream of call duration changes (in seconds)
146
+ */
147
+ get duration$(): Observable<number> {
148
+ return this._duration.asObservable().pipe(distinctUntilChanged());
149
+ }
150
+
151
+ /**
152
+ * Observable that emits true when the call can be answered
153
+ */
154
+ get canAnswer$(): Observable<boolean> {
155
+ return this.callState$.pipe(
156
+ map((state) => CallStateHelpers.canAnswer(state)),
157
+ distinctUntilChanged()
158
+ );
159
+ }
160
+
161
+ /**
162
+ * Observable that emits true when the call can be hung up
163
+ */
164
+ get canHangup$(): Observable<boolean> {
165
+ return this.callState$.pipe(
166
+ map((state) => CallStateHelpers.canHangup(state)),
167
+ distinctUntilChanged()
168
+ );
169
+ }
170
+
171
+ /**
172
+ * Observable that emits true when the call can be put on hold
173
+ */
174
+ get canHold$(): Observable<boolean> {
175
+ return this.callState$.pipe(
176
+ map((state) => CallStateHelpers.canHold(state)),
177
+ distinctUntilChanged()
178
+ );
179
+ }
180
+
181
+ /**
182
+ * Observable that emits true when the call can be resumed from hold
183
+ */
184
+ get canResume$(): Observable<boolean> {
185
+ return this.callState$.pipe(
186
+ map((state) => CallStateHelpers.canResume(state)),
187
+ distinctUntilChanged()
188
+ );
189
+ }
190
+
191
+ /**
192
+ * Answer the incoming call
193
+ * @param customHeaders Optional custom headers to include with the answer
194
+ */
195
+ async answer(customHeaders?: { name: string; value: string }[]): Promise<void> {
196
+ if (!CallStateHelpers.canAnswer(this.currentState)) {
197
+ throw new Error(`Cannot answer call in state: ${this.currentState}`);
198
+ }
199
+
200
+ try {
201
+ // On iOS, use CallKit coordinator for proper audio session handling
202
+ if (Platform.OS === 'ios') {
203
+ const { callKitCoordinator } = await import('../callkit/callkit-coordinator');
204
+ if (callKitCoordinator.isAvailable()) {
205
+ console.log('Call: Using CallKit coordinator to answer call (iOS)');
206
+ await callKitCoordinator.answerCallFromUI(this._telnyxCall);
207
+ return;
208
+ }
209
+ }
210
+
211
+ // Fallback for Android or when CallKit is not available
212
+ console.log('Call: Setting state to CONNECTING before answering');
213
+ this._callState.next(TelnyxCallState.CONNECTING);
214
+
215
+ // Pass custom headers to the underlying Telnyx call
216
+ await this._telnyxCall.answer(customHeaders);
217
+ } catch (error) {
218
+ console.error('Failed to answer call:', error);
219
+ throw error;
220
+ }
221
+ }
222
+
223
+ /**
224
+ * Hang up the call
225
+ * @param customHeaders Optional custom headers to include with the hangup request
226
+ */
227
+ async hangup(customHeaders?: { name: string; value: string }[]): Promise<void> {
228
+ if (!CallStateHelpers.canHangup(this.currentState)) {
229
+ throw new Error(`Cannot hang up call in state: ${this.currentState}`);
230
+ }
231
+
232
+ try {
233
+ // On iOS, use CallKit coordinator for proper CallKit cleanup
234
+ if (Platform.OS === 'ios') {
235
+ const { callKitCoordinator } = await import('../callkit/callkit-coordinator');
236
+ if (callKitCoordinator.isAvailable()) {
237
+ console.log('Call: Using CallKit coordinator to end call (iOS)');
238
+ await callKitCoordinator.endCallFromUI(this._telnyxCall);
239
+ return;
240
+ }
241
+ }
242
+
243
+ // Fallback for Android or when CallKit is not available
244
+ await this._telnyxCall.hangup(customHeaders);
245
+
246
+ // On Android, also notify the native side to hide ongoing notification
247
+ if (Platform.OS === 'android') {
248
+ try {
249
+ const { VoicePnBridge } = await import('../internal/voice-pn-bridge');
250
+ await VoicePnBridge.endCall(this._callId);
251
+ console.log('Call: Notified Android to hide ongoing notification');
252
+ } catch (error) {
253
+ console.error('Call: Failed to notify Android about call end:', error);
254
+ // Don't fail the hangup if notification hiding fails
255
+ }
256
+ }
257
+ } catch (error) {
258
+ console.error('Failed to hang up call:', error);
259
+ throw error;
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Put the call on hold
265
+ */
266
+ async hold(): Promise<void> {
267
+ if (!CallStateHelpers.canHold(this.currentState)) {
268
+ throw new Error(`Cannot hold call in state: ${this.currentState}`);
269
+ }
270
+
271
+ try {
272
+ await this._telnyxCall.hold();
273
+ } catch (error) {
274
+ console.error('Failed to hold call:', error);
275
+ throw error;
276
+ }
277
+ }
278
+
279
+ /**
280
+ * Resume the call from hold
281
+ */
282
+ async resume(): Promise<void> {
283
+ if (!CallStateHelpers.canResume(this.currentState)) {
284
+ throw new Error(`Cannot resume call in state: ${this.currentState}`);
285
+ }
286
+
287
+ try {
288
+ await this._telnyxCall.unhold();
289
+ } catch (error) {
290
+ console.error('Failed to resume call:', error);
291
+ throw error;
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Mute the call
297
+ */
298
+ async mute(): Promise<void> {
299
+ if (!CallStateHelpers.canToggleMute(this.currentState)) {
300
+ throw new Error(`Cannot mute call in state: ${this.currentState}`);
301
+ }
302
+
303
+ try {
304
+ this._telnyxCall.mute();
305
+ this._isMuted.next(true);
306
+ } catch (error) {
307
+ console.error('Failed to mute call:', error);
308
+ throw error;
309
+ }
310
+ }
311
+
312
+ /**
313
+ * Unmute the call
314
+ */
315
+ async unmute(): Promise<void> {
316
+ if (!CallStateHelpers.canToggleMute(this.currentState)) {
317
+ throw new Error(`Cannot unmute call in state: ${this.currentState}`);
318
+ }
319
+
320
+ try {
321
+ this._telnyxCall.unmute();
322
+ this._isMuted.next(false);
323
+ } catch (error) {
324
+ console.error('Failed to unmute call:', error);
325
+ throw error;
326
+ }
327
+ }
328
+
329
+ /**
330
+ * Toggle mute state
331
+ */
332
+ async toggleMute(): Promise<void> {
333
+ if (this.currentIsMuted) {
334
+ await this.unmute();
335
+ } else {
336
+ await this.mute();
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Set the call to connecting state (used for push notification calls when answered via CallKit)
342
+ * @internal
343
+ */
344
+ setConnecting(): void {
345
+ console.log('Call: Setting state to CONNECTING for push notification answer');
346
+ this._callState.next(TelnyxCallState.CONNECTING);
347
+ }
348
+
349
+ /**
350
+ * Clean up resources when the call is disposed
351
+ */
352
+ dispose(): void {
353
+ this._stopDurationTimer();
354
+ this._callState.complete();
355
+ this._isMuted.complete();
356
+ this._isHeld.complete();
357
+ this._duration.complete();
358
+ }
359
+
360
+ /**
361
+ * Set up listeners for the underlying Telnyx call
362
+ */
363
+ private _setupCallListeners(): void {
364
+ // Map Telnyx call states to our simplified states
365
+ this._telnyxCall.on('telnyx.call.state', (call: any, state: any) => {
366
+ const telnyxState = this._mapToTelnyxCallState(state);
367
+ this._callState.next(telnyxState);
368
+
369
+ // Start duration timer when call becomes active
370
+ if (telnyxState === TelnyxCallState.ACTIVE && !this._startTime) {
371
+ this._startDurationTimer();
372
+
373
+ // Show ongoing call notification on Android when call becomes active
374
+ // This covers both locally answered calls and calls that become active from remote side
375
+ if (Platform.OS === 'android') {
376
+ (async () => {
377
+ try {
378
+ const { VoicePnBridge } = await import('../internal/voice-pn-bridge');
379
+
380
+ // Extract caller information based on call direction
381
+ let callerNumber: string | undefined;
382
+ let callerName: string | undefined;
383
+
384
+ if (this._isIncoming) {
385
+ // For incoming calls, use the remote caller ID (who's calling us)
386
+ callerNumber = this._telnyxCall.remoteCallerIdNumber;
387
+ callerName = this._telnyxCall.remoteCallerIdName;
388
+ } else {
389
+ // For outgoing calls, use our own caller ID (what we're showing to them)
390
+ // These are the values we set when making the call
391
+ callerNumber = this._telnyxCall.localCallerIdNumber || this._originalCallerNumber;
392
+ callerName = this._telnyxCall.localCallerIdName || this._originalCallerName;
393
+ }
394
+
395
+ // Fallback logic for better notification display - avoid "Unknown" when possible
396
+ let displayName: string;
397
+ let displayNumber: string;
398
+
399
+ if (this._isIncoming) {
400
+ // For incoming calls: use caller name or fall back to caller number, then destination
401
+ displayName = callerName || callerNumber || this._destination;
402
+ displayNumber = callerNumber || this._destination;
403
+ } else {
404
+ // For outgoing calls: use our caller ID or descriptive text
405
+ displayName = callerName || `${this._destination}`;
406
+ displayNumber = callerNumber || this._destination;
407
+ }
408
+
409
+ await VoicePnBridge.showOngoingCallNotification(
410
+ displayName,
411
+ displayNumber,
412
+ this._callId
413
+ );
414
+ console.log('Call: Showed ongoing call notification on Android (call active)', {
415
+ isIncoming: this._isIncoming,
416
+ callerName: displayName,
417
+ callerNumber: displayNumber,
418
+ });
419
+ } catch (error) {
420
+ console.error('Call: Failed to show ongoing call notification on active:', error);
421
+ }
422
+ })();
423
+ }
424
+ }
425
+
426
+ // Stop duration timer when call ends
427
+ if (CallStateHelpers.isTerminated(telnyxState)) {
428
+ this._stopDurationTimer();
429
+
430
+ // Clean up ongoing call notification on Android when call ends
431
+ if (Platform.OS === 'android') {
432
+ (async () => {
433
+ try {
434
+ const { VoicePnBridge } = await import('../internal/voice-pn-bridge');
435
+ await VoicePnBridge.endCall(this._callId);
436
+ console.log('Call: Cleaned up ongoing call notification (call terminated)');
437
+ } catch (error) {
438
+ console.error(
439
+ 'Call: Failed to clean up ongoing call notification on termination:',
440
+ error
441
+ );
442
+ }
443
+ })();
444
+ }
445
+ }
446
+ });
447
+ }
448
+
449
+ /**
450
+ * Map Telnyx SDK call states to our simplified call states
451
+ */
452
+ private _mapToTelnyxCallState(telnyxState: any): TelnyxCallState {
453
+ // This mapping will depend on the actual Telnyx SDK call states
454
+ // For now, using a basic mapping - this should be updated based on actual SDK
455
+ switch (telnyxState) {
456
+ case 'ringing':
457
+ case 'new':
458
+ return TelnyxCallState.RINGING;
459
+ case 'connecting':
460
+ return TelnyxCallState.CONNECTING;
461
+ case 'active':
462
+ case 'answered':
463
+ return TelnyxCallState.ACTIVE;
464
+ case 'held':
465
+ return TelnyxCallState.HELD;
466
+ case 'ended':
467
+ case 'hangup':
468
+ return TelnyxCallState.ENDED;
469
+ case 'failed':
470
+ case 'rejected':
471
+ return TelnyxCallState.FAILED;
472
+ case 'dropped':
473
+ return TelnyxCallState.DROPPED;
474
+ default:
475
+ console.warn(`Unknown call state: ${telnyxState}`);
476
+ return TelnyxCallState.RINGING;
477
+ }
478
+ }
479
+
480
+ /**
481
+ * Start the duration timer
482
+ */
483
+ private _startDurationTimer(): void {
484
+ this._startTime = new Date();
485
+ this._durationTimer = setInterval(() => {
486
+ if (this._startTime) {
487
+ const duration = Math.floor((Date.now() - this._startTime.getTime()) / 1000);
488
+ this._duration.next(duration);
489
+ }
490
+ }, 1000);
491
+ }
492
+
493
+ /**
494
+ * Stop the duration timer
495
+ */
496
+ private _stopDurationTimer(): void {
497
+ if (this._durationTimer) {
498
+ clearInterval(this._durationTimer);
499
+ this._durationTimer = undefined;
500
+ }
501
+ }
502
+ }