You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CallKit.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { NativeModules, NativeEventEmitter } from 'react-native';
  2. import { getName } from '../../app/functions';
  3. /**
  4. * Thin wrapper around Apple's CallKit functionality.
  5. *
  6. * In CallKit requests are performed via actions (either user or system started)
  7. * and async events are reported via dedicated methods. This class exposes that
  8. * functionality in the form of methods and events. One important thing to note
  9. * is that even if an action is started by the system (because the user pressed
  10. * the "end call" button in the CallKit view, for example) the event will be
  11. * emitted in the same way as it would if the action originated from calling
  12. * the "endCall" method in this class, for example.
  13. *
  14. * Emitted events:
  15. * - performAnswerCallAction: The user pressed the answer button.
  16. * - performEndCallAction: The call should be ended.
  17. * - performSetMutedCallAction: The call muted state should change. The
  18. * ancillary `data` object contains a `muted` attribute.
  19. * - providerDidReset: The system has reset, all calls should be terminated.
  20. * This event gets no associated data.
  21. *
  22. * All events get a `data` object with a `callUUID` property, unless stated
  23. * otherwise.
  24. */
  25. let CallKit = NativeModules.RNCallKit;
  26. // XXX Rather than wrapping RNCallKit in a new class and forwarding the many
  27. // methods of the latter to the former, add the one additional method that we
  28. // need to RNCallKit.
  29. if (CallKit) {
  30. const eventEmitter = new NativeEventEmitter(CallKit);
  31. CallKit = {
  32. ...CallKit,
  33. addListener: eventEmitter.addListener.bind(eventEmitter),
  34. registerSubscriptions(context, delegate) {
  35. CallKit.setProviderConfiguration({
  36. iconTemplateImageName: 'CallKitIcon',
  37. localizedName: getName()
  38. });
  39. return [
  40. CallKit.addListener(
  41. 'performEndCallAction',
  42. delegate._onPerformEndCallAction,
  43. context),
  44. CallKit.addListener(
  45. 'performSetMutedCallAction',
  46. delegate._onPerformSetMutedCallAction,
  47. context),
  48. // According to CallKit's documentation, when the system resets
  49. // we should terminate all calls. Hence, providerDidReset is
  50. // the same to us as performEndCallAction.
  51. CallKit.addListener(
  52. 'providerDidReset',
  53. delegate._onPerformEndCallAction,
  54. context)
  55. ];
  56. }
  57. };
  58. }
  59. export default CallKit;