Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JMCallKitEmitter.swift 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright @ 2018-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import AVKit
  17. import CallKit
  18. import Foundation
  19. internal final class JMCallKitEmitter: NSObject, CXProviderDelegate {
  20. private var listeners = Set<JMCallKitEventListenerWrapper>()
  21. internal override init() {}
  22. // MARK: - Add/remove listeners
  23. func addListener(_ listener: JMCallKitListener) {
  24. let wrapper = JMCallKitEventListenerWrapper(listener: listener)
  25. listeners.insert(wrapper)
  26. }
  27. func removeListener(_ listener: JMCallKitListener) {
  28. // XXX Constructing a new JMCallKitEventListenerWrapper instance in
  29. // order to remove the specified listener from listeners is (1) a bit
  30. // funny (though may make a statement about performance) and (2) not
  31. // really an option because the specified listener may already be
  32. // executing its dealloc (like RNCallKit).
  33. listeners.forEach {
  34. // 1. JMCallKitEventListenerWrapper weakly references
  35. // JMCallKitListener so it may be nice to clean
  36. // JMCallKitEventListenerWrapperinstances up if they've lost
  37. // their associated JMCallKitListener instances (e.g. for
  38. // example, because whoever did addListener forgot to
  39. // removeListener). Unfortunately, I don't know how to do it
  40. // because JMCallKitEventListenerWrapper is a struct.
  41. //
  42. // 2. XXX JMCallKitEventListenerWrapper implements the weird
  43. // equality by JMCallKitListener hash which (1) I don't
  44. // understand and (2) I don't know how to invoke without
  45. // duplicating.
  46. if ($0.hashValue == listener.hash) {
  47. listeners.remove($0)
  48. }
  49. }
  50. }
  51. // MARK: - CXProviderDelegate
  52. func providerDidReset(_ provider: CXProvider) {
  53. listeners.forEach { $0.listener?.providerDidReset?() }
  54. }
  55. func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
  56. listeners.forEach {
  57. $0.listener?.performAnswerCall?(UUID: action.callUUID)
  58. }
  59. action.fulfill()
  60. }
  61. func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
  62. listeners.forEach {
  63. $0.listener?.performEndCall?(UUID: action.callUUID)
  64. }
  65. action.fulfill()
  66. }
  67. func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
  68. listeners.forEach {
  69. $0.listener?.performSetMutedCall?(UUID: action.callUUID, isMuted: action.isMuted)
  70. }
  71. action.fulfill()
  72. }
  73. func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
  74. listeners.forEach {
  75. $0.listener?.performStartCall?(UUID: action.callUUID,
  76. isVideo: action.isVideo)
  77. }
  78. action.fulfill()
  79. }
  80. func provider(_ provider: CXProvider,
  81. didActivate audioSession: AVAudioSession) {
  82. listeners.forEach {
  83. $0.listener?.providerDidActivateAudioSession?(session: audioSession)
  84. }
  85. }
  86. func provider(_ provider: CXProvider,
  87. didDeactivate audioSession: AVAudioSession) {
  88. listeners.forEach {
  89. $0.listener?.providerDidDeactivateAudioSession?(
  90. session: audioSession)
  91. }
  92. }
  93. }
  94. fileprivate struct JMCallKitEventListenerWrapper: Hashable {
  95. internal weak var listener: JMCallKitListener?
  96. public init(listener: JMCallKitListener) {
  97. self.listener = listener
  98. }
  99. public static func ==(lhs: JMCallKitEventListenerWrapper,
  100. rhs: JMCallKitEventListenerWrapper) -> Bool {
  101. // XXX We're aware that "[t]wo instances with equal hash values are not
  102. // necessarily equal to each other."
  103. return lhs.hashValue == rhs.hashValue
  104. }
  105. func hash(into hasher: inout Hasher) {
  106. hasher.combine(self.listener?.hash)
  107. }
  108. }