Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CallKit.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import {
  2. NativeModules,
  3. NativeEventEmitter,
  4. Platform
  5. } from 'react-native';
  6. const RNCallKit = NativeModules.RNCallKit;
  7. /**
  8. * Thin wrapper around Apple's CallKit functionality.
  9. *
  10. * In CallKit requests are performed via actions (either user or system started)
  11. * and async events are reported via dedicated methods. This class exposes that
  12. * functionality in the form of methods and events. One important thing to note
  13. * is that even if an action is started by the system (because the user pressed
  14. * the "end call" button in the CallKit view, for example) the event will be
  15. * emitted in the same way as it would if the action originated from calling
  16. * the "endCall" method in this class, for example.
  17. *
  18. * Emitted events:
  19. * - performAnswerCallAction: The user pressed the answer button.
  20. * - performEndCallAction: The call should be ended.
  21. * - performSetMutedCallAction: The call muted state should change. The
  22. * ancillary `data` object contains a `muted` attribute.
  23. * - providerDidReset: The system has reset, all calls should be terminated.
  24. * This event gets no associated data.
  25. *
  26. * All events get a `data` object with a `callUUID` property, unless stated
  27. * otherwise.
  28. */
  29. class CallKit extends NativeEventEmitter {
  30. /**
  31. * Initializes a new {@code CallKit} instance.
  32. */
  33. constructor() {
  34. super(RNCallKit);
  35. this._setup = false;
  36. }
  37. /**
  38. * Returns True if the current platform is supported, false otherwise. The
  39. * supported platforms are: iOS >= 10.
  40. *
  41. * @private
  42. * @returns {boolean}
  43. */
  44. static isSupported() {
  45. return Platform.OS === 'ios' && parseInt(Platform.Version, 10) >= 10;
  46. }
  47. /**
  48. * Checks if CallKit was setup, and throws an exception in that case.
  49. *
  50. * @private
  51. * @returns {void}
  52. */
  53. _checkSetup() {
  54. if (!this._setup) {
  55. throw new Error('CallKit not initialized, call setup() first.');
  56. }
  57. }
  58. /**
  59. * Adds a listener for the given event.
  60. *
  61. * @param {string} event - Name of the event we are interested in.
  62. * @param {Function} listener - Function which will be called when the
  63. * desired event is emitted.
  64. * @returns {void}
  65. */
  66. addEventListener(event, listener) {
  67. this._checkSetup();
  68. if (!CallKit.isSupported()) {
  69. return;
  70. }
  71. this.addListener(event, listener);
  72. }
  73. /**
  74. * Notifies CallKit about an incoming call. This will display the system
  75. * incoming call view.
  76. *
  77. * @param {string} uuid - Unique identifier for the call.
  78. * @param {string} handle - Call handle in CallKit's terms. The room URL.
  79. * @param {boolean} hasVideo - True if it's a video call, false otherwise.
  80. * @returns {Promise}
  81. */
  82. displayIncomingCall(uuid, handle, hasVideo = true) {
  83. this._checkSetup();
  84. if (!CallKit.isSupported()) {
  85. return Promise.resolve();
  86. }
  87. return RNCallKit.displayIncomingCall(uuid, handle, hasVideo);
  88. }
  89. /**
  90. * Request CallKit to end the call.
  91. *
  92. * @param {string} uuid - Unique identifier for the call.
  93. * @returns {Promise}
  94. */
  95. endCall(uuid) {
  96. this._checkSetup();
  97. if (!CallKit.isSupported()) {
  98. return Promise.resolve();
  99. }
  100. return RNCallKit.endCall(uuid);
  101. }
  102. /**
  103. * Removes a listener for the given event.
  104. *
  105. * @param {string} event - Name of the event we are no longer interested in.
  106. * @param {Function} listener - Function which used to be called when the
  107. * desired event was emitted.
  108. * @returns {void}
  109. */
  110. removeEventListener(event, listener) {
  111. this._checkSetup();
  112. if (!CallKit.isSupported()) {
  113. return;
  114. }
  115. this.removeListener(event, listener);
  116. }
  117. /**
  118. * Indicate CallKit that the outgoing call with the given UUID is now
  119. * connected.
  120. *
  121. * @param {string} uuid - Unique identifier for the call.
  122. * @returns {Promise}
  123. */
  124. reportConnectedOutgoingCall(uuid) {
  125. this._checkSetup();
  126. if (!CallKit.isSupported()) {
  127. return Promise.resolve();
  128. }
  129. return RNCallKit.reportConnectedOutgoingCall(uuid);
  130. }
  131. /**
  132. * Indicate CallKit that the call with the given UUID has failed.
  133. *
  134. * @param {string} uuid - Unique identifier for the call.
  135. * @returns {Promise}
  136. */
  137. reportCallFailed(uuid) {
  138. this._checkSetup();
  139. if (!CallKit.isSupported()) {
  140. return Promise.resolve();
  141. }
  142. return RNCallKit.reportCallFailed(uuid);
  143. }
  144. /**
  145. * Tell CallKit about the audio muted state.
  146. *
  147. * @param {string} uuid - Unique identifier for the call.
  148. * @param {boolean} muted - True if audio is muted, false otherwise.
  149. * @returns {Promise}
  150. */
  151. setMuted(uuid, muted) {
  152. this._checkSetup();
  153. if (!CallKit.isSupported()) {
  154. return Promise.resolve();
  155. }
  156. return RNCallKit.setMuted(uuid, muted);
  157. }
  158. /**
  159. * Prepare / initialize CallKit. This method must be called before any
  160. * other.
  161. *
  162. * @param {Object} options - Initialization options.
  163. * @param {string} options.imageName - Image to be used in CallKit's
  164. * application button..
  165. * @param {string} options.ringtoneSound - Ringtone to be used for incoming
  166. * calls.
  167. * @returns {void}
  168. */
  169. setup(options = {}) {
  170. if (CallKit.isSupported()) {
  171. options.appName = NativeModules.AppInfo.name;
  172. RNCallKit.setup(options);
  173. }
  174. this._setup = true;
  175. }
  176. /**
  177. * Indicate CallKit about a new outgoing call.
  178. *
  179. * @param {string} uuid - Unique identifier for the call.
  180. * @param {string} handle - Call handle in CallKit's terms. The room URL in
  181. * our case.
  182. * @param {boolean} hasVideo - True if it's a video call, false otherwise.
  183. * @returns {Promise}
  184. */
  185. startCall(uuid, handle, hasVideo = true) {
  186. this._checkSetup();
  187. if (!CallKit.isSupported()) {
  188. return Promise.resolve();
  189. }
  190. return RNCallKit.startCall(uuid, handle, hasVideo);
  191. }
  192. /**
  193. * Updates an ongoing call's parameters.
  194. *
  195. * @param {string} uuid - Unique identifier for the call.
  196. * @param {Object} options - Object with properties which should be updated.
  197. * @param {string} options.displayName - Display name for the caller.
  198. * @param {boolean} options.hasVideo - True if the call has video, false
  199. * otherwise.
  200. * @returns {Promise}
  201. */
  202. updateCall(uuid, options) {
  203. this._checkSetup();
  204. if (!CallKit.isSupported()) {
  205. return Promise.resolve();
  206. }
  207. return RNCallKit.updateCall(uuid, options);
  208. }
  209. }
  210. export default new CallKit();