voice-react-native-sdk 1.6.2-fork.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 (227) hide show
  1. package/LICENSE +206 -0
  2. package/README.md +179 -0
  3. package/android/build.gradle +113 -0
  4. package/android/gradle.properties +29 -0
  5. package/android/src/main/AndroidManifest.xml +30 -0
  6. package/android/src/main/java/com/twiliovoicereactnative/AudioSwitchManager.java +141 -0
  7. package/android/src/main/java/com/twiliovoicereactnative/CallListenerProxy.java +186 -0
  8. package/android/src/main/java/com/twiliovoicereactnative/CallMessageListenerProxy.java +103 -0
  9. package/android/src/main/java/com/twiliovoicereactnative/CallRecordDatabase.java +183 -0
  10. package/android/src/main/java/com/twiliovoicereactnative/CommonConstants.java +195 -0
  11. package/android/src/main/java/com/twiliovoicereactnative/ConfigurationProperties.java +41 -0
  12. package/android/src/main/java/com/twiliovoicereactnative/Constants.java +23 -0
  13. package/android/src/main/java/com/twiliovoicereactnative/ExpoActivityLifecycleListener.java +27 -0
  14. package/android/src/main/java/com/twiliovoicereactnative/ExpoApplicationLifecycleListener.java +14 -0
  15. package/android/src/main/java/com/twiliovoicereactnative/ExpoModule.kt +36 -0
  16. package/android/src/main/java/com/twiliovoicereactnative/ExpoPackage.java +20 -0
  17. package/android/src/main/java/com/twiliovoicereactnative/JSEventEmitter.java +98 -0
  18. package/android/src/main/java/com/twiliovoicereactnative/MediaPlayerManager.java +58 -0
  19. package/android/src/main/java/com/twiliovoicereactnative/NotificationUtility.java +363 -0
  20. package/android/src/main/java/com/twiliovoicereactnative/ReactNativeArgumentsSerializer.java +276 -0
  21. package/android/src/main/java/com/twiliovoicereactnative/SDKLog.java +74 -0
  22. package/android/src/main/java/com/twiliovoicereactnative/StatsListenerProxy.java +178 -0
  23. package/android/src/main/java/com/twiliovoicereactnative/TwilioVoiceReactNativeModule.java +778 -0
  24. package/android/src/main/java/com/twiliovoicereactnative/TwilioVoiceReactNativePackage.java +28 -0
  25. package/android/src/main/java/com/twiliovoicereactnative/VoiceActivityProxy.java +101 -0
  26. package/android/src/main/java/com/twiliovoicereactnative/VoiceApplicationProxy.java +133 -0
  27. package/android/src/main/java/com/twiliovoicereactnative/VoiceFirebaseMessagingService.java +88 -0
  28. package/android/src/main/java/com/twiliovoicereactnative/VoiceService.java +403 -0
  29. package/android/src/main/res/drawable/answered_call_small_icon.png +0 -0
  30. package/android/src/main/res/drawable/ic_launcher_round.png +0 -0
  31. package/android/src/main/res/drawable/ic_launcher_sdk.png +0 -0
  32. package/android/src/main/res/drawable/incoming_call_small_icon.png +0 -0
  33. package/android/src/main/res/drawable/outgoing_call_small_icon.png +0 -0
  34. package/android/src/main/res/raw/disconnect.wav +0 -0
  35. package/android/src/main/res/raw/incoming.wav +0 -0
  36. package/android/src/main/res/raw/outgoing.wav +0 -0
  37. package/android/src/main/res/raw/ringtone.wav +0 -0
  38. package/android/src/main/res/raw/silent.wav +0 -0
  39. package/android/src/main/res/values/colors.xml +11 -0
  40. package/android/src/main/res/values/config.xml +5 -0
  41. package/android/src/main/res/values/dimens.xml +7 -0
  42. package/android/src/main/res/values/strings.xml +25 -0
  43. package/android/src/main/res/values/styles.xml +10 -0
  44. package/android/src/main/res/values-night/colors.xml +11 -0
  45. package/expo-config-plugin/android.js +191 -0
  46. package/expo-config-plugin/ios.js +22 -0
  47. package/expo-module.config.json +6 -0
  48. package/ios/TwilioVoicePushRegistry.h +19 -0
  49. package/ios/TwilioVoicePushRegistry.m +72 -0
  50. package/ios/TwilioVoiceReactNative+CallInvite.m +56 -0
  51. package/ios/TwilioVoiceReactNative+CallKit.m +533 -0
  52. package/ios/TwilioVoiceReactNative+CallMessage.m +81 -0
  53. package/ios/TwilioVoiceReactNative.h +74 -0
  54. package/ios/TwilioVoiceReactNative.m +1046 -0
  55. package/ios/TwilioVoiceReactNative.xcodeproj/project.pbxproj +328 -0
  56. package/ios/TwilioVoiceReactNativeConstants.h +200 -0
  57. package/ios/Utilities/TwilioVoiceStatsReport.h +175 -0
  58. package/lib/commonjs/AudioDevice.js +101 -0
  59. package/lib/commonjs/AudioDevice.js.map +1 -0
  60. package/lib/commonjs/Call.js +765 -0
  61. package/lib/commonjs/Call.js.map +1 -0
  62. package/lib/commonjs/CallInvite.js +452 -0
  63. package/lib/commonjs/CallInvite.js.map +1 -0
  64. package/lib/commonjs/CallMessage/CallMessage.js +54 -0
  65. package/lib/commonjs/CallMessage/CallMessage.js.map +1 -0
  66. package/lib/commonjs/CallMessage/IncomingCallMessage.js +120 -0
  67. package/lib/commonjs/CallMessage/IncomingCallMessage.js.map +1 -0
  68. package/lib/commonjs/CallMessage/OutgoingCallMessage.js +148 -0
  69. package/lib/commonjs/CallMessage/OutgoingCallMessage.js.map +1 -0
  70. package/lib/commonjs/ExpoModule.js +69 -0
  71. package/lib/commonjs/ExpoModule.js.map +1 -0
  72. package/lib/commonjs/Voice.js +611 -0
  73. package/lib/commonjs/Voice.js.map +1 -0
  74. package/lib/commonjs/common.js +26 -0
  75. package/lib/commonjs/common.js.map +1 -0
  76. package/lib/commonjs/constants.js +158 -0
  77. package/lib/commonjs/constants.js.map +1 -0
  78. package/lib/commonjs/error/InvalidArgumentError.js +32 -0
  79. package/lib/commonjs/error/InvalidArgumentError.js.map +1 -0
  80. package/lib/commonjs/error/InvalidStateError.js +33 -0
  81. package/lib/commonjs/error/InvalidStateError.js.map +1 -0
  82. package/lib/commonjs/error/TwilioError.js +38 -0
  83. package/lib/commonjs/error/TwilioError.js.map +1 -0
  84. package/lib/commonjs/error/UnsupportedPlatformError.js +33 -0
  85. package/lib/commonjs/error/UnsupportedPlatformError.js.map +1 -0
  86. package/lib/commonjs/error/generated.js +2343 -0
  87. package/lib/commonjs/error/generated.js.map +1 -0
  88. package/lib/commonjs/error/index.js +112 -0
  89. package/lib/commonjs/error/index.js.map +1 -0
  90. package/lib/commonjs/error/utility.js +37 -0
  91. package/lib/commonjs/error/utility.js.map +1 -0
  92. package/lib/commonjs/index.js +95 -0
  93. package/lib/commonjs/index.js.map +1 -0
  94. package/lib/commonjs/type/AudioDevice.js +6 -0
  95. package/lib/commonjs/type/AudioDevice.js.map +1 -0
  96. package/lib/commonjs/type/Call.js +6 -0
  97. package/lib/commonjs/type/Call.js.map +1 -0
  98. package/lib/commonjs/type/CallInvite.js +6 -0
  99. package/lib/commonjs/type/CallInvite.js.map +1 -0
  100. package/lib/commonjs/type/CallKit.js +30 -0
  101. package/lib/commonjs/type/CallKit.js.map +1 -0
  102. package/lib/commonjs/type/CallMessage.js +6 -0
  103. package/lib/commonjs/type/CallMessage.js.map +1 -0
  104. package/lib/commonjs/type/Error.js +6 -0
  105. package/lib/commonjs/type/Error.js.map +1 -0
  106. package/lib/commonjs/type/NativeModule.js +6 -0
  107. package/lib/commonjs/type/NativeModule.js.map +1 -0
  108. package/lib/commonjs/type/RTCStats.js +29 -0
  109. package/lib/commonjs/type/RTCStats.js.map +1 -0
  110. package/lib/commonjs/type/Voice.js +6 -0
  111. package/lib/commonjs/type/Voice.js.map +1 -0
  112. package/lib/commonjs/type/common.js +2 -0
  113. package/lib/commonjs/type/common.js.map +1 -0
  114. package/lib/module/AudioDevice.js +97 -0
  115. package/lib/module/AudioDevice.js.map +1 -0
  116. package/lib/module/Call.js +766 -0
  117. package/lib/module/Call.js.map +1 -0
  118. package/lib/module/CallInvite.js +450 -0
  119. package/lib/module/CallInvite.js.map +1 -0
  120. package/lib/module/CallMessage/CallMessage.js +51 -0
  121. package/lib/module/CallMessage/CallMessage.js.map +1 -0
  122. package/lib/module/CallMessage/IncomingCallMessage.js +116 -0
  123. package/lib/module/CallMessage/IncomingCallMessage.js.map +1 -0
  124. package/lib/module/CallMessage/OutgoingCallMessage.js +154 -0
  125. package/lib/module/CallMessage/OutgoingCallMessage.js.map +1 -0
  126. package/lib/module/ExpoModule.js +58 -0
  127. package/lib/module/ExpoModule.js.map +1 -0
  128. package/lib/module/Voice.js +598 -0
  129. package/lib/module/Voice.js.map +1 -0
  130. package/lib/module/common.js +11 -0
  131. package/lib/module/common.js.map +1 -0
  132. package/lib/module/constants.js +151 -0
  133. package/lib/module/constants.js.map +1 -0
  134. package/lib/module/error/InvalidArgumentError.js +23 -0
  135. package/lib/module/error/InvalidArgumentError.js.map +1 -0
  136. package/lib/module/error/InvalidStateError.js +24 -0
  137. package/lib/module/error/InvalidStateError.js.map +1 -0
  138. package/lib/module/error/TwilioError.js +29 -0
  139. package/lib/module/error/TwilioError.js.map +1 -0
  140. package/lib/module/error/UnsupportedPlatformError.js +24 -0
  141. package/lib/module/error/UnsupportedPlatformError.js.map +1 -0
  142. package/lib/module/error/generated.js +2315 -0
  143. package/lib/module/error/generated.js.map +1 -0
  144. package/lib/module/error/index.js +6 -0
  145. package/lib/module/error/index.js.map +1 -0
  146. package/lib/module/error/utility.js +27 -0
  147. package/lib/module/error/utility.js.map +1 -0
  148. package/lib/module/index.js +23 -0
  149. package/lib/module/index.js.map +1 -0
  150. package/lib/module/type/AudioDevice.js +2 -0
  151. package/lib/module/type/AudioDevice.js.map +1 -0
  152. package/lib/module/type/Call.js +2 -0
  153. package/lib/module/type/Call.js.map +1 -0
  154. package/lib/module/type/CallInvite.js +2 -0
  155. package/lib/module/type/CallInvite.js.map +1 -0
  156. package/lib/module/type/CallKit.js +22 -0
  157. package/lib/module/type/CallKit.js.map +1 -0
  158. package/lib/module/type/CallMessage.js +2 -0
  159. package/lib/module/type/CallMessage.js.map +1 -0
  160. package/lib/module/type/Error.js +2 -0
  161. package/lib/module/type/Error.js.map +1 -0
  162. package/lib/module/type/NativeModule.js +2 -0
  163. package/lib/module/type/NativeModule.js.map +1 -0
  164. package/lib/module/type/RTCStats.js +21 -0
  165. package/lib/module/type/RTCStats.js.map +1 -0
  166. package/lib/module/type/Voice.js +2 -0
  167. package/lib/module/type/Voice.js.map +1 -0
  168. package/lib/module/type/common.js +2 -0
  169. package/lib/module/type/common.js.map +1 -0
  170. package/lib/typescript/AudioDevice.d.ts +74 -0
  171. package/lib/typescript/Call.d.ts +874 -0
  172. package/lib/typescript/CallInvite.d.ts +484 -0
  173. package/lib/typescript/CallMessage/CallMessage.d.ts +53 -0
  174. package/lib/typescript/CallMessage/IncomingCallMessage.d.ts +75 -0
  175. package/lib/typescript/CallMessage/OutgoingCallMessage.d.ts +198 -0
  176. package/lib/typescript/ExpoModule.d.ts +13 -0
  177. package/lib/typescript/Voice.d.ts +620 -0
  178. package/lib/typescript/common.d.ts +11 -0
  179. package/lib/typescript/constants.d.ts +148 -0
  180. package/lib/typescript/error/InvalidArgumentError.d.ts +11 -0
  181. package/lib/typescript/error/InvalidStateError.d.ts +12 -0
  182. package/lib/typescript/error/TwilioError.d.ts +14 -0
  183. package/lib/typescript/error/UnsupportedPlatformError.d.ts +12 -0
  184. package/lib/typescript/error/generated.d.ts +1558 -0
  185. package/lib/typescript/error/index.d.ts +5 -0
  186. package/lib/typescript/error/utility.d.ts +12 -0
  187. package/lib/typescript/index.d.ts +18 -0
  188. package/lib/typescript/type/AudioDevice.d.ts +15 -0
  189. package/lib/typescript/type/Call.d.ts +59 -0
  190. package/lib/typescript/type/CallInvite.d.ts +40 -0
  191. package/lib/typescript/type/CallKit.d.ts +60 -0
  192. package/lib/typescript/type/CallMessage.d.ts +20 -0
  193. package/lib/typescript/type/Error.d.ts +9 -0
  194. package/lib/typescript/type/NativeModule.d.ts +59 -0
  195. package/lib/typescript/type/RTCStats.d.ts +91 -0
  196. package/lib/typescript/type/Voice.d.ts +16 -0
  197. package/lib/typescript/type/common.d.ts +15 -0
  198. package/package.json +167 -0
  199. package/src/AudioDevice.tsx +88 -0
  200. package/src/Call.tsx +1343 -0
  201. package/src/CallInvite.tsx +757 -0
  202. package/src/CallMessage/CallMessage.ts +83 -0
  203. package/src/CallMessage/IncomingCallMessage.ts +104 -0
  204. package/src/CallMessage/OutgoingCallMessage.ts +308 -0
  205. package/src/ExpoModule.ts +59 -0
  206. package/src/Voice.tsx +1010 -0
  207. package/src/common.ts +16 -0
  208. package/src/constants.ts +193 -0
  209. package/src/error/InvalidArgumentError.ts +19 -0
  210. package/src/error/InvalidStateError.ts +19 -0
  211. package/src/error/TwilioError.ts +22 -0
  212. package/src/error/UnsupportedPlatformError.ts +19 -0
  213. package/src/error/generated.ts +2277 -0
  214. package/src/error/index.ts +18 -0
  215. package/src/error/utility.ts +36 -0
  216. package/src/index.tsx +24 -0
  217. package/src/type/AudioDevice.ts +18 -0
  218. package/src/type/Call.ts +102 -0
  219. package/src/type/CallInvite.ts +59 -0
  220. package/src/type/CallKit.ts +61 -0
  221. package/src/type/CallMessage.ts +31 -0
  222. package/src/type/Error.ts +11 -0
  223. package/src/type/NativeModule.ts +93 -0
  224. package/src/type/RTCStats.ts +99 -0
  225. package/src/type/Voice.ts +31 -0
  226. package/src/type/common.ts +16 -0
  227. package/twilio-voice-react-native.podspec +22 -0
@@ -0,0 +1,757 @@
1
+ /**
2
+ * Copyright © 2022 Twilio, Inc. All rights reserved. Licensed under the Twilio
3
+ * license.
4
+ *
5
+ * See LICENSE in the project root for license information.
6
+ */
7
+
8
+ import { EventEmitter } from 'eventemitter3';
9
+ import { Call } from './Call';
10
+ import { NativeEventEmitter, NativeModule, Platform } from './common';
11
+ import { InvalidStateError } from './error/InvalidStateError';
12
+ import { TwilioError } from './error/TwilioError';
13
+ import { UnsupportedPlatformError } from './error/UnsupportedPlatformError';
14
+ import { constructTwilioError } from './error/utility';
15
+ import type {
16
+ NativeCallInviteInfo,
17
+ NativeCallInviteEvent,
18
+ NativeCallInviteAcceptedEvent,
19
+ NativeCallInviteCancelledEvent,
20
+ NativeCallInviteMessageReceivedEvent,
21
+ } from './type/CallInvite';
22
+ import type { CustomParameters, Uuid } from './type/common';
23
+ import { CallMessage, validateCallMessage } from './CallMessage/CallMessage';
24
+ import { IncomingCallMessage } from './CallMessage/IncomingCallMessage';
25
+ import { OutgoingCallMessage } from './CallMessage/OutgoingCallMessage';
26
+ import { Constants } from './constants';
27
+
28
+ /**
29
+ * Defines strict typings for all events emitted by {@link (CallInvite:class)
30
+ * | CallInvite objects}.
31
+ *
32
+ * @remarks
33
+ * Note that the `on` function is an alias for the `addListener` function.
34
+ * They share identical functionality and either may be used interchangeably.
35
+ *
36
+ * - See also the {@link (CallInvite:class) | CallInvite class}.
37
+ * - See also the {@link (CallInvite:namespace) | CallInvite namespace}.
38
+ *
39
+ * @public
40
+ */
41
+ export declare interface CallInvite {
42
+ /**
43
+ * ------------
44
+ * Emit Typings
45
+ * ------------
46
+ */
47
+
48
+ /** @internal */
49
+ emit(acceptedEvent: CallInvite.Event.Accepted, call: Call): boolean;
50
+
51
+ /** @internal */
52
+ emit(rejectedEvent: CallInvite.Event.Rejected): boolean;
53
+
54
+ /** @internal */
55
+ emit(
56
+ cancelledEvent: CallInvite.Event.Cancelled,
57
+ error?: TwilioError
58
+ ): boolean;
59
+
60
+ /** @internal */
61
+ emit(notificationTappedEvent: CallInvite.Event.NotificationTapped): boolean;
62
+
63
+ /** @internal */
64
+ emit(
65
+ messageReceivedEvent: CallInvite.Event.MessageReceived,
66
+ incomingCallMessage: IncomingCallMessage
67
+ ): boolean;
68
+
69
+ /**
70
+ * ----------------
71
+ * Listener Typings
72
+ * ----------------
73
+ */
74
+
75
+ /**
76
+ * Accepted event. Raised when the call invite has been accepted.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * voice.on(Voice.Event.CallInvite, (callInvite) => {
81
+ * callInvite.on(CallInvite.Event.Accepted, (call) => {
82
+ * // the call invite was accepted through either the native layer
83
+ * // or the js layer
84
+ * });
85
+ * });
86
+ * ```
87
+ *
88
+ * @remarks
89
+ *
90
+ * @param acceptedEvent - The raised event string.
91
+ * @param listener - A listener function that will be invoked when the event
92
+ * is raised.
93
+ * @returns - The call invite object.
94
+ */
95
+ addListener(
96
+ acceptedEvent: CallInvite.Event.Accepted,
97
+ listener: CallInvite.Listener.Accepted
98
+ ): this;
99
+ /** {@inheritDoc (CallInvite:interface).(addListener:1)} */
100
+ on(
101
+ acceptedEvent: CallInvite.Event.Accepted,
102
+ listener: CallInvite.Listener.Accepted
103
+ ): this;
104
+
105
+ /**
106
+ * Rejected event. Raised when the call invite has been rejected.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * voice.on(Voice.Event.CallInvite, (callInvite) => {
111
+ * callInvite.on(CallInvite.Event.Rejected, () => {
112
+ * // the call invite was rejected through either the native layer
113
+ * // or the js layer
114
+ * });
115
+ * });
116
+ * ```
117
+ *
118
+ * @remarks
119
+ *
120
+ * @param rejectedEvent - The raised event string.
121
+ * @param listener - A listener function that will be invoked when the event
122
+ * is raised.
123
+ * @returns - The call invite object.
124
+ */
125
+ addListener(
126
+ rejectedEvent: CallInvite.Event.Rejected,
127
+ listener: CallInvite.Listener.Rejected
128
+ ): this;
129
+ /** {@inheritDoc (CallInvite:interface).(addListener:2)} */
130
+ on(
131
+ rejectedEvent: CallInvite.Event.Rejected,
132
+ listener: CallInvite.Listener.Rejected
133
+ ): this;
134
+
135
+ /**
136
+ * Cancelled event. Raised when the call invite has been cancelled.
137
+ *
138
+ * @example
139
+ * ```ts
140
+ * voice.on(Voice.Event.CallInvite, (callInvite) => {
141
+ * callInvite.on(CallInvite.Event.Cancelled, (error) => {
142
+ * // the call invite was cancelled
143
+ * });
144
+ * });
145
+ * ```
146
+ *
147
+ * @remarks
148
+ *
149
+ * @param cancelledEvent - The raised event string.
150
+ * @param listener - A listener function that will be invoked when the event
151
+ * is raised.
152
+ * @returns - The call invite object.
153
+ */
154
+ addListener(
155
+ cancelledEvent: CallInvite.Event.Cancelled,
156
+ listener: CallInvite.Listener.Cancelled
157
+ ): this;
158
+ /** {@inheritDoc (CallInvite:interface).(addListener:3)} */
159
+ on(
160
+ cancelledEvent: CallInvite.Event.Cancelled,
161
+ listener: CallInvite.Listener.Cancelled
162
+ ): this;
163
+
164
+ /**
165
+ * Notification tapped event. Raised when the call invite notification has
166
+ * been tapped.
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * voice.on(Voice.Event.CallInvite, (callInvite) => {
171
+ * callInvite.on(CallInvite.Event.NotificationTapped, () => {
172
+ * // the call invite notification was tapped
173
+ * });
174
+ * });
175
+ * ```
176
+ *
177
+ * @remarks
178
+ * This API is Android specific.
179
+ *
180
+ * @param notificationTappedEvent - The raised event string.
181
+ * @param listener - A listener function that will be invoked when the event
182
+ * is raised.
183
+ * @returns - The call invite object.
184
+ */
185
+ addListener(
186
+ notificationTappedEvent: CallInvite.Event.NotificationTapped,
187
+ listener: CallInvite.Listener.NotificationTapped
188
+ ): this;
189
+ /** {@inheritDoc (CallInvite:interface).(addListener:4)} */
190
+ on(
191
+ notificationTappedEvent: CallInvite.Event.NotificationTapped,
192
+ listener: CallInvite.Listener.NotificationTapped
193
+ ): this;
194
+
195
+ /**
196
+ * MessageReceived event. Raised when a {@link IncomingCallMessage} is
197
+ * received.
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * voice.on(Voice.Event.CallInvite, (callInvite) => {
202
+ * callInvite.addListener(CallInvite.Event.MessageReceived, (message) => {
203
+ * // callMessage received
204
+ * });
205
+ * });
206
+ * ```
207
+ *
208
+ * @param messageReceivedEvent - The raised event string.
209
+ * @param listener - A listener function that will be invoked when the event
210
+ * is raised.
211
+ * @returns - The callMessage object
212
+ */
213
+ addListener(
214
+ messageReceivedEvent: CallInvite.Event.MessageReceived,
215
+ listener: CallInvite.Listener.MessageReceived
216
+ ): this;
217
+ /** {@inheritDoc (CallInvite:interface).(addListener:5)} */
218
+ on(
219
+ messageReceivedEvent: CallInvite.Event.MessageReceived,
220
+ listener: CallInvite.Listener.MessageReceived
221
+ ): this;
222
+ }
223
+
224
+ /**
225
+ * Provides access to information about a call invite, including the call
226
+ * parameters, and exposes functionality to accept or decline a call.
227
+ *
228
+ * @remarks
229
+ *
230
+ * Note that when a `CallInvite` is acted upon (i.e. when
231
+ * {@link (CallInvite:class).accept} or {@link (CallInvite:class).reject} is
232
+ * invoked), then the `CallInvite` is "settled".
233
+ *
234
+ * The state of the `CallInvite` is changed from
235
+ * {@link (CallInvite:namespace).State.Pending} to
236
+ * {@link (CallInvite:namespace).State.Accepted} or
237
+ * {@link (CallInvite:namespace).State.Rejected} and the `CallInvite` can no
238
+ * longer be acted upon further.
239
+ *
240
+ * Further action after "settling" a `CallInvite` will throw an error.
241
+ *
242
+ * - See the {@link (CallInvite:namespace) | CallInvite namespace} for
243
+ * enumerations and types used by this class.
244
+ *
245
+ * @public
246
+ */
247
+ export class CallInvite extends EventEmitter {
248
+ /**
249
+ * The current state of the call invite.
250
+ *
251
+ * @remarks
252
+ * See {@link (CallInvite:namespace).State}.
253
+ */
254
+ private _state: CallInvite.State;
255
+ /**
256
+ * The `Uuid` of this call invite. Used to identify calls between the JS and
257
+ * native layer so we can associate events and native functionality between
258
+ * the layers.
259
+ */
260
+ private _uuid: Uuid;
261
+ /**
262
+ * A string representing the SID of this call.
263
+ */
264
+ private _callSid: string;
265
+ /**
266
+ * Call custom parameters.
267
+ */
268
+ private _customParameters: CustomParameters;
269
+ /**
270
+ * Call `from` parameter.
271
+ */
272
+ private _from: string;
273
+ /**
274
+ * Call `to` parameter.
275
+ */
276
+ private _to: string;
277
+
278
+ /**
279
+ * These objects should not be instantiated by consumers of the SDK. All
280
+ * instances of the `CallInvite` class should be emitted by the SDK.
281
+ *
282
+ * @param nativeCallInviteInfo - A dataobject containing the native
283
+ * information of a call invite.
284
+ * @param state - Mocking options for testing.
285
+ *
286
+ * @internal
287
+ */
288
+ constructor(
289
+ { uuid, callSid, customParameters, from, to }: NativeCallInviteInfo,
290
+ state: CallInvite.State
291
+ ) {
292
+ super();
293
+
294
+ this._uuid = uuid;
295
+ this._callSid = callSid;
296
+ this._customParameters = { ...customParameters };
297
+ this._from = from;
298
+ this._to = to;
299
+
300
+ this._state = state;
301
+
302
+ NativeEventEmitter.addListener(
303
+ Constants.ScopeCallInvite,
304
+ this._handleNativeCallInviteEvent
305
+ );
306
+ }
307
+
308
+ /**
309
+ * This helper function serves as both a runtime-check error log and a
310
+ * compile-time type-guard. If the switch-case statement below is non-
311
+ * exhaustive, then the type passed to this function will _not_ have type
312
+ * `never`.
313
+ */
314
+ private _handleUnexpectedCallInviteEventType(event: never) {
315
+ throw new TwilioError(
316
+ `Unknown event type "${(event as any)?.type}" reached call invite.`
317
+ );
318
+ }
319
+
320
+ /**
321
+ * This intermediate native call invite event handler acts as a "gate", only
322
+ * executing the actual call invite event handler (such as `Accepted`) if
323
+ * this call invite object matches the `Uuid` of the call invite that had an
324
+ * event raised.
325
+ * @param nativeCallInviteEvent - A call invite event directly from the native
326
+ * layer.
327
+ */
328
+ private _handleNativeCallInviteEvent = (
329
+ nativeCallInviteEvent: NativeCallInviteEvent
330
+ ) => {
331
+ if (typeof nativeCallInviteEvent !== 'object') {
332
+ throw new TwilioError(
333
+ `Received a "${typeof nativeCallInviteEvent}" native call invite event.`
334
+ );
335
+ }
336
+
337
+ if (nativeCallInviteEvent === null) {
338
+ throw new TwilioError('Received a null native call invite event.');
339
+ }
340
+
341
+ if (typeof nativeCallInviteEvent.callSid !== 'string') {
342
+ throw new TwilioError(
343
+ 'Received a native call invite event without a call SID.'
344
+ );
345
+ }
346
+
347
+ if (nativeCallInviteEvent.callSid !== this._callSid) {
348
+ return;
349
+ }
350
+
351
+ switch (nativeCallInviteEvent.type) {
352
+ case Constants.CallInviteEventTypeValueAccepted:
353
+ return this._handleCallInviteAccepted(nativeCallInviteEvent);
354
+ case Constants.CallInviteEventTypeValueRejected:
355
+ return this._handleCallInviteRejected();
356
+ case Constants.CallInviteEventTypeValueCancelled:
357
+ return this._handleCallInviteCancelled(nativeCallInviteEvent);
358
+ case Constants.CallInviteEventTypeValueNotificationTapped:
359
+ return this._handleCallInviteNotificationTapped();
360
+ case Constants.CallEventMessageReceived:
361
+ return this._handleMessageReceivedEvent(nativeCallInviteEvent);
362
+ default:
363
+ return this._handleUnexpectedCallInviteEventType(nativeCallInviteEvent);
364
+ }
365
+ };
366
+
367
+ /**
368
+ * Handler for the {@link (CallInvite:namespace).Event.MessageReceived} event.
369
+ * @param nativeCallEvent - The native call event.
370
+ */
371
+ private _handleMessageReceivedEvent = (
372
+ nativeCallInviteEvent: NativeCallInviteMessageReceivedEvent
373
+ ) => {
374
+ const { callMessage: callMessageInfo } = nativeCallInviteEvent;
375
+
376
+ const callMessage = new IncomingCallMessage(callMessageInfo);
377
+
378
+ this.emit(CallInvite.Event.MessageReceived, callMessage);
379
+ };
380
+
381
+ /**
382
+ * Handle when this call invite is accepted.
383
+ */
384
+ private _handleCallInviteAccepted = ({
385
+ callInvite,
386
+ }: NativeCallInviteAcceptedEvent) => {
387
+ this._state = CallInvite.State.Accepted;
388
+
389
+ const callInfo = {
390
+ uuid: callInvite.uuid,
391
+ customParameters: callInvite.customParameters,
392
+ sid: callInvite.callSid,
393
+ from: callInvite.from,
394
+ to: callInvite.to,
395
+ };
396
+
397
+ const call = new Call(callInfo);
398
+
399
+ this.emit(CallInvite.Event.Accepted, call);
400
+ };
401
+
402
+ /**
403
+ * Handle when this call invite is rejected.
404
+ */
405
+ private _handleCallInviteRejected = () => {
406
+ this._state = CallInvite.State.Rejected;
407
+ this.emit(CallInvite.Event.Rejected);
408
+ };
409
+
410
+ /**
411
+ * Handle when a call invite is cancelled.
412
+ */
413
+ private _handleCallInviteCancelled = (
414
+ nativeCallInviteCancelledEvent: NativeCallInviteCancelledEvent
415
+ ) => {
416
+ this._state = CallInvite.State.Cancelled;
417
+
418
+ const error = nativeCallInviteCancelledEvent.error
419
+ ? constructTwilioError(
420
+ nativeCallInviteCancelledEvent.error.message,
421
+ nativeCallInviteCancelledEvent.error.code
422
+ )
423
+ : undefined;
424
+
425
+ this.emit(CallInvite.Event.Cancelled, error);
426
+ };
427
+
428
+ /**
429
+ * Handle when a call invite notification is tapped.
430
+ */
431
+ private _handleCallInviteNotificationTapped = () => {
432
+ this.emit(CallInvite.Event.NotificationTapped);
433
+ };
434
+
435
+ /**
436
+ * Accept a call invite. Sets the state of this call invite to
437
+ * {@link (CallInvite:namespace).State.Accepted}.
438
+ * @param options - Options to pass to the native layer when accepting the
439
+ * call.
440
+ * @returns
441
+ * - Resolves when a {@link (Call:class) | Call object} associated with this
442
+ * {@link (CallInvite:class)} has been created.
443
+ */
444
+ async accept(options: CallInvite.AcceptOptions = {}): Promise<Call> {
445
+ if (this._state !== CallInvite.State.Pending) {
446
+ throw new InvalidStateError(
447
+ `Call in state "${this._state}", ` +
448
+ `expected state "${CallInvite.State.Pending}".`
449
+ );
450
+ }
451
+
452
+ const acceptResult = await NativeModule.callInvite_accept(
453
+ this._uuid,
454
+ options
455
+ )
456
+ .then((callInfo) => {
457
+ return { type: 'ok', callInfo } as const;
458
+ })
459
+ .catch((error) => {
460
+ const code = error.userInfo.code;
461
+ const message = error.userInfo.message;
462
+ return { type: 'err', message, code } as const;
463
+ });
464
+
465
+ if (acceptResult.type === 'err') {
466
+ throw constructTwilioError(acceptResult.message, acceptResult.code);
467
+ }
468
+
469
+ return new Call(acceptResult.callInfo);
470
+ }
471
+
472
+ /**
473
+ * Reject a call invite. Sets the state of this call invite to
474
+ * {@link (CallInvite:namespace).State.Rejected}.
475
+ * @returns
476
+ * - Resolves when the {@link (CallInvite:class)} has been rejected.
477
+ */
478
+ async reject(): Promise<void> {
479
+ if (this._state !== CallInvite.State.Pending) {
480
+ throw new InvalidStateError(
481
+ `Call in state "${this._state}", ` +
482
+ `expected state "${CallInvite.State.Pending}".`
483
+ );
484
+ }
485
+
486
+ await NativeModule.callInvite_reject(this._uuid);
487
+ }
488
+
489
+ /**
490
+ * Check if a `CallInvite` is valid.
491
+ *
492
+ * @returns
493
+ * - TODO
494
+ *
495
+ * @alpha
496
+ */
497
+ isValid(): Promise<boolean> {
498
+ return NativeModule.callInvite_isValid(this._uuid);
499
+ }
500
+
501
+ /**
502
+ * Get the call SID associated with this `CallInvite` class.
503
+ * @returns - A string representing the call SID.
504
+ */
505
+ getCallSid(): string {
506
+ return this._callSid;
507
+ }
508
+
509
+ /**
510
+ * Get the custom parameters of the call associated with this `CallInvite`
511
+ * class.
512
+ * @returns - A `Record` of custom parameters.
513
+ */
514
+ getCustomParameters(): CustomParameters {
515
+ return this._customParameters;
516
+ }
517
+
518
+ /**
519
+ * Get the `from` parameter of the call associated with this `CallInvite`
520
+ * class.
521
+ * @returns - A `string` representing the `from` parameter.
522
+ */
523
+ getFrom(): string {
524
+ return this._from;
525
+ }
526
+
527
+ /**
528
+ * Get the `state` of the `CallInvite`.
529
+ * @returns - The `state` of this `CallInvite`.
530
+ */
531
+ getState(): CallInvite.State {
532
+ return this._state;
533
+ }
534
+
535
+ /**
536
+ * Get the `to` parameter of the call associated with this `CallInvite`
537
+ * class.
538
+ * @returns - A `string` representing the `to` parameter.
539
+ */
540
+ getTo(): string {
541
+ return this._to;
542
+ }
543
+
544
+ /**
545
+ * Send a CallMessage.
546
+ *
547
+ * @example
548
+ * To send a user-defined-message
549
+ * ```typescript
550
+ * const outgoingCallMessage: OutgoingCallMessage = await callInvite.sendMessage({
551
+ * content: { key1: 'This is a messsage from the parent call invite' },
552
+ * contentType: 'application/json',
553
+ * messageType: 'user-defined-message'
554
+ * });
555
+ *
556
+ * outgoingCallMessage.addListener(OutgoingCallMessage.Event.Failure, (error) => {
557
+ * // outgoingCallMessage failed, handle error
558
+ * });
559
+ *
560
+ * outgoingCallMessage.addListener(OutgoingCallMessage.Event.Sent, () => {
561
+ * // outgoingCallMessage sent
562
+ * });
563
+ * ```
564
+ *
565
+ * @param message - The call message to send.
566
+ *
567
+ * @returns
568
+ * A `Promise` that
569
+ * - Resolves with the OutgoingCallMessage object.
570
+ * - Rejects when the message is unable to be sent.
571
+ */
572
+ async sendMessage(message: CallMessage): Promise<OutgoingCallMessage> {
573
+ const { content, contentType, messageType } = validateCallMessage(message);
574
+
575
+ const voiceEventSid = await NativeModule.call_sendMessage(
576
+ this._uuid,
577
+ content,
578
+ contentType,
579
+ messageType
580
+ );
581
+
582
+ const outgoingCallMessage = new OutgoingCallMessage({
583
+ content,
584
+ contentType,
585
+ messageType,
586
+ voiceEventSid,
587
+ });
588
+
589
+ return outgoingCallMessage;
590
+ }
591
+
592
+ /**
593
+ * Update the caller name displayed in the iOS system incoming call screen.
594
+ *
595
+ * @param newHandle - The new value of the caller's name.
596
+ *
597
+ * @remarks
598
+ * Unsupported platforms:
599
+ * - Android
600
+ *
601
+ * This API is specific to iOS and unavailable in Android. Invoke this method
602
+ * after the incoming call has been reported to CallKit and before the call
603
+ * has been accepted. For example, perform an async request to your app server
604
+ * to fetch the full name of the caller and use this method to replace the
605
+ * default caller name in `from`.
606
+ *
607
+ * @returns
608
+ * - Resolves when the caller name has been updated.
609
+ */
610
+ async updateCallerHandle(newHandle: string): Promise<void> {
611
+ switch (Platform.OS) {
612
+ case 'ios':
613
+ return NativeModule.callInvite_updateCallerHandle(
614
+ this._uuid,
615
+ newHandle
616
+ );
617
+ default:
618
+ throw new UnsupportedPlatformError(
619
+ `Unsupported platform "${Platform.OS}". This method is only supported on iOS.`
620
+ );
621
+ }
622
+ }
623
+ }
624
+
625
+ /**
626
+ * Provides enumerations and types used by a {@link (CallInvite:class)
627
+ * | CallInvite object}.
628
+ *
629
+ * @remarks
630
+ * - See also the {@link (CallInvite:class) | CallInvite class}.
631
+ *
632
+ * @public
633
+ */
634
+ export namespace CallInvite {
635
+ /**
636
+ * Options to pass to the native layer when accepting the call.
637
+ */
638
+ export interface AcceptOptions {}
639
+
640
+ /**
641
+ * An enumeration of {@link (CallInvite:class)} states.
642
+ */
643
+ export enum State {
644
+ /**
645
+ * State of a call invite when it has not been acted upon.
646
+ */
647
+ Pending = 'pending',
648
+
649
+ /**
650
+ * State of a call invite when it has been accepted.
651
+ */
652
+ Accepted = 'accepted',
653
+
654
+ /**
655
+ * State of a call invite when it has been rejected.
656
+ */
657
+ Rejected = 'rejected',
658
+
659
+ /**
660
+ * State of a call invite when it has been cancelled.
661
+ */
662
+ Cancelled = 'cancelled',
663
+ }
664
+
665
+ /**
666
+ * Enumeration of all event strings emitted by {@link (CallInvite:class)}
667
+ * objects.
668
+ */
669
+ export enum Event {
670
+ /**
671
+ * Event string for the `Accepted` event.
672
+ * See {@link (CallInvite:interface).(addListener:1)}.
673
+ */
674
+ Accepted = 'accepted',
675
+
676
+ /**
677
+ * Event string for the `Rejected` event.
678
+ * See {@link (CallInvite:interface).(addListener:2)}.
679
+ */
680
+ Rejected = 'rejected',
681
+
682
+ /**
683
+ * Event string for the `Cancelled` event.
684
+ * See {@link (CallInvite:interface).(addListener:3)}.
685
+ */
686
+ Cancelled = 'cancelled',
687
+
688
+ /**
689
+ * Event string for the `NotificationTapped` event.
690
+ * See {@link (CallInvite:interface).(addListener:4)}.
691
+ */
692
+ NotificationTapped = 'notificationTapped',
693
+
694
+ /**
695
+ * Event string for the `MessageReceived` event.
696
+ * See {@link (CallInvite:interface).(addListener:5)}
697
+ */
698
+ MessageReceived = 'messageReceived',
699
+ }
700
+
701
+ /**
702
+ * Listener types for all events emitted by a
703
+ * {@link (CallInvite:class) | Call invite object.}
704
+ */
705
+ export namespace Listener {
706
+ /**
707
+ * Accepted event listener. This should be the function signature of any
708
+ * event listener bound to the {@link (CallInvite:namespace).Event.Accepted}
709
+ * event.
710
+ *
711
+ * @remarks
712
+ * See {@link (CallInvite:interface).(addListener:1)}.
713
+ */
714
+ export type Accepted = (call: Call) => void;
715
+
716
+ /**
717
+ * Rejected event listener. This should be the function signature of any
718
+ * event listener bound to the {@link (CallInvite:namespace).Event.Rejected}
719
+ * event.
720
+ *
721
+ * @remarks
722
+ * See {@link (CallInvite:interface).(addListener:2)}.
723
+ */
724
+ export type Rejected = () => void;
725
+
726
+ /**
727
+ * Cancelled event listener. This should be the function signature of any
728
+ * event listener bound to the
729
+ * {@link (CallInvite:namespace).Event.Cancelled} event.
730
+ *
731
+ * @remarks
732
+ * See {@link (CallInvite:interface).(addListener:3)}.
733
+ */
734
+ export type Cancelled = (error?: TwilioError) => void;
735
+
736
+ /**
737
+ * Rejected event listener. This should be the function signature of any
738
+ * event listener bound to the
739
+ * {@link (CallInvite:namespace).Event.NotificationTapped} event.
740
+ *
741
+ * @remarks
742
+ * See {@link (CallInvite:interface).(addListener:4)}.
743
+ */
744
+ export type NotificationTapped = () => void;
745
+
746
+ /**
747
+ * CallInviteMessage received event listener. This should be the function signature of
748
+ * any event listener bound to the {@link (CallInvite:namespace).Event.MessageReceived} event.
749
+ *
750
+ * @remarks
751
+ * See {@link (CallInvite:interface).(addListener:5)}.
752
+ */
753
+ export type MessageReceived = (
754
+ incomingCallMessage: IncomingCallMessage
755
+ ) => void;
756
+ }
757
+ }