CallProviderDelegate.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // CallProviderDelegate.swift
  3. // FloatingButtonApp
  4. //
  5. // Created by Yayan Dwi on 10/08/21.
  6. //
  7. import Foundation
  8. import UIKit
  9. import CallKit
  10. import AVFoundation
  11. import SwiftUI
  12. import nuSDKService
  13. class CallProviderDelegate: NSObject {
  14. static let providerConfiguration: CXProviderConfiguration = {
  15. let providerConfiguration = CXProviderConfiguration()
  16. providerConfiguration.maximumCallsPerCallGroup = 10
  17. providerConfiguration.maximumCallGroups = 10
  18. providerConfiguration.supportsVideo = true
  19. providerConfiguration.supportedHandleTypes = [.phoneNumber]
  20. // providerConfiguration.ringtoneSound = "call_in.mp3"
  21. return providerConfiguration
  22. }()
  23. let callManager: CallManager
  24. private let provider: CXProvider
  25. init(callManager: CallManager) {
  26. self.callManager = callManager
  27. provider = CXProvider(configuration: type(of: self).providerConfiguration)
  28. super.init()
  29. provider.setDelegate(self, queue: nil)
  30. }
  31. func reportIncomingCall(uuid: UUID, handle: String, hasVideo: Bool = false, completion: ((Error?) -> Void)? = nil) {
  32. let update = CXCallUpdate()
  33. if let user = User.getData(pin: handle) {
  34. update.remoteHandle = CXHandle(type: .phoneNumber, value: user.fullName)
  35. }
  36. update.hasVideo = hasVideo
  37. update.supportsGrouping = true
  38. update.supportsUngrouping = true
  39. update.supportsHolding = true
  40. provider.reportNewIncomingCall(with: uuid, update: update) { error in
  41. if error == nil {
  42. let call = Call(uuid: uuid)
  43. call.handle = handle
  44. call.hasVideo = hasVideo
  45. self.callManager.addCall(call)
  46. }
  47. completion?(error)
  48. }
  49. }
  50. }
  51. extension CallProviderDelegate: CXProviderDelegate {
  52. func providerDidReset(_ provider: CXProvider) {
  53. //print(("providerDidReset...")
  54. for call in callManager.calls {
  55. call.endCall()
  56. }
  57. callManager.removeAllCalls()
  58. }
  59. func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
  60. //print(("CXStartCallAction...\(action.callUUID)")
  61. let call = Call(uuid: action.callUUID, isOutgoing: true)
  62. call.handle = action.handle.value
  63. call.hasStartedConnectingDidChange = { [weak self] in
  64. self?.provider.reportOutgoingCall(with: call.uuid, startedConnectingAt: call.connectingDate)
  65. }
  66. call.hasConnectedDidChange = { [weak self] in
  67. self?.provider.reportOutgoingCall(with: call.uuid, connectedAt: call.connectDate)
  68. }
  69. if self.callManager.calls.count > 0 {
  70. self.callManager.setOnHoldStatus(for: self.callManager.calls.last!, to: true)
  71. }
  72. action.fulfill()
  73. //print(("JUMLAH START CALL \(self.callManager.calls.count)")
  74. self.callManager.addCall(call)
  75. if self.callManager.calls.count > 1 {
  76. DigiX.shared.callManager.startGroupCall(uuid1: call.uuid)
  77. API.initiateCCall(sParty: call.handle)
  78. }
  79. }
  80. func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
  81. //print(("CXAnswerCallAction...\(action.callUUID)")
  82. guard let call = callManager.callWithUUID(uuid: action.callUUID) else {
  83. action.fail()
  84. return
  85. }
  86. call.answerCall()
  87. action.fulfill()
  88. if call.hasVideo {
  89. } else {
  90. let controller = QmeraAudioViewController()
  91. controller.user = User.getData(pin: call.handle)
  92. controller.isOnGoing = true
  93. controller.isOutgoing = false
  94. controller.modalPresentationStyle = .overCurrentContext
  95. if UIApplication.shared.visibleViewController is UIAlertController {
  96. let vc = UIApplication.shared.visibleViewController as! UIAlertController
  97. vc.dismiss(animated: true, completion: {
  98. if UIApplication.shared.visibleViewController?.navigationController != nil {
  99. UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
  100. } else {
  101. UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
  102. }
  103. })
  104. } else {
  105. if UIApplication.shared.visibleViewController?.navigationController != nil {
  106. UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
  107. } else {
  108. UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
  109. }
  110. }
  111. }
  112. }
  113. func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
  114. //print(("CXEndCallAction...\(action.callUUID)")
  115. guard let call = callManager.callWithUUID(uuid: action.callUUID) else {
  116. action.fail()
  117. return
  118. }
  119. call.endCall()
  120. action.fulfill()
  121. self.callManager.removeCall(call)
  122. //print(("JUMLAH CALL \(self.callManager.calls.count)")
  123. if self.callManager.calls.count == 0, !call.isReceiveEnd {
  124. //print(("MASUK TERMINATE CALL DELEGATE")
  125. API.terminateCall(sParty: nil)
  126. DispatchQueue.global().async {
  127. if let pin = call.handle {
  128. _ = DigiX.write(message: CoreMessage_TMessageBank.endCall(pin: pin))
  129. }
  130. }
  131. }
  132. }
  133. func provider(_ provider: CXProvider, perform action: CXSetHeldCallAction) {
  134. //print(("CXSetHeldCallAction...\(action.callUUID)")
  135. guard let call = callManager.callWithUUID(uuid: action.callUUID) else {
  136. action.fail()
  137. return
  138. }
  139. call.isOnHold = action.isOnHold
  140. // if call.isOnHold {
  141. // // pause??
  142. // } else {
  143. // // resume??
  144. // }
  145. action.fulfill()
  146. }
  147. func provider(_ provider: CXProvider, timedOutPerforming action: CXAction) {
  148. //print(("Timed out", #function)
  149. }
  150. func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
  151. //print(("Received", #function)
  152. /*
  153. Start call audio media, now that the audio session is activated,
  154. after having its priority elevated.
  155. */
  156. if let call = self.callManager.calls.last {
  157. if call.isOutgoing {
  158. API.initiateCCall(sParty: call.handle)
  159. } else {
  160. API.receiveCCall(sParty: call.handle)
  161. }
  162. }
  163. }
  164. func provider(_ provider: CXProvider, perform action: CXSetGroupCallAction) {
  165. //print(("CXGroupCallAction...\(action.callUUID)")
  166. // perform merge call here where you merge ports of two call audio i/o
  167. action.fulfill()
  168. }
  169. func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
  170. //print(("Received", #function)
  171. /*
  172. Restart any non-call related audio now that the app's audio session is deactivated,
  173. after having its priority restored to normal.
  174. */
  175. }
  176. }