session-ios/Signal/src/call/Speakerbox/CallKitCallUIAdaptee.swift

370 lines
13 KiB
Swift

//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
import Foundation
import UIKit
import CallKit
import AVFoundation
import SignalServiceKit
/**
* Connects user interface to the CallService using CallKit.
*
* User interface is routed to the CallManager which requests CXCallActions, and if the CXProvider accepts them,
* their corresponding consequences are implmented in the CXProviderDelegate methods, e.g. using the CallService
*/
@available(iOS 10.0, *)
final class CallKitCallUIAdaptee: NSObject, CallUIAdaptee, CXProviderDelegate {
let TAG = "[CallKitCallUIAdaptee]"
private let callManager: CallKitCallManager
internal let callService: CallService
internal let notificationsAdapter: CallNotificationsAdapter
internal let contactsManager: OWSContactsManager
private let provider: CXProvider
// CallKit handles incoming ringer stop/start for us. Yay!
let hasManualRinger = false
// The app's provider configuration, representing its CallKit capabilities
static var providerConfiguration: CXProviderConfiguration {
let localizedName = NSLocalizedString("APPLICATION_NAME", comment: "Name of application")
let providerConfiguration = CXProviderConfiguration(localizedName: localizedName)
providerConfiguration.supportsVideo = true
providerConfiguration.maximumCallGroups = 1
providerConfiguration.maximumCallsPerCallGroup = 1
providerConfiguration.supportedHandleTypes = [.phoneNumber, .generic]
let iconMaskImage = #imageLiteral(resourceName: "logoSignal")
providerConfiguration.iconTemplateImageData = UIImagePNGRepresentation(iconMaskImage)
providerConfiguration.ringtoneSound = "r.caf"
return providerConfiguration
}
init(callService: CallService, contactsManager: OWSContactsManager, notificationsAdapter: CallNotificationsAdapter) {
AssertIsOnMainThread()
Logger.debug("\(self.TAG) \(#function)")
self.callManager = CallKitCallManager()
self.callService = callService
self.contactsManager = contactsManager
self.notificationsAdapter = notificationsAdapter
self.provider = CXProvider(configuration: type(of: self).providerConfiguration)
super.init()
self.provider.setDelegate(self, queue: nil)
}
// MARK: CallUIAdaptee
func startOutgoingCall(handle: String) -> SignalCall {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
let call = SignalCall.outgoingCall(localId: UUID(), remotePhoneNumber: handle)
// Add the new outgoing call to the app's list of calls.
// So we can find it in the provider delegate callbacks.
callManager.addCall(call)
callManager.startCall(call)
return call
}
// Called from CallService after call has ended to clean up any remaining CallKit call state.
func failCall(_ call: SignalCall, error: CallError) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
switch error {
case .timeout(description: _):
provider.reportCall(with: call.localId, endedAt: Date(), reason: CXCallEndedReason.unanswered)
default:
provider.reportCall(with: call.localId, endedAt: Date(), reason: CXCallEndedReason.failed)
}
self.callManager.removeCall(call)
}
func reportIncomingCall(_ call: SignalCall, callerName: String) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
// Construct a CXCallUpdate describing the incoming call, including the caller.
let update = CXCallUpdate()
if Environment.getCurrent().preferences.isCallKitPrivacyEnabled() {
let callKitId = CallKitCallManager.kAnonymousCallHandlePrefix + call.localId.uuidString
update.remoteHandle = CXHandle(type: .generic, value: callKitId)
TSStorageManager.shared().setPhoneNumber(call.remotePhoneNumber, forCallKitId:callKitId)
update.localizedCallerName = NSLocalizedString("CALLKIT_ANONYMOUS_CONTACT_NAME", comment: "The generic name used for calls if CallKit privacy is enabled")
} else {
update.localizedCallerName = self.contactsManager.stringForConversationTitle(withPhoneIdentifier: call.remotePhoneNumber)
update.remoteHandle = CXHandle(type: .phoneNumber, value: call.remotePhoneNumber)
}
update.hasVideo = call.hasLocalVideo
disableUnsupportedFeatures(callUpdate: update)
// Report the incoming call to the system
provider.reportNewIncomingCall(with: call.localId, update: update) { error in
/*
Only add incoming call to the app's list of calls if the call was allowed (i.e. there was no error)
since calls may be "denied" for various legitimate reasons. See CXErrorCodeIncomingCallError.
*/
guard error == nil else {
Logger.error("\(self.TAG) failed to report new incoming call")
return
}
self.callManager.addCall(call)
}
}
func answerCall(localId: UUID) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
owsFail("\(self.TAG) \(#function) CallKit should answer calls via system call screen, not via notifications.")
}
func answerCall(_ call: SignalCall) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
callManager.answer(call: call)
}
func declineCall(localId: UUID) {
AssertIsOnMainThread()
owsFail("\(self.TAG) \(#function) CallKit should decline calls via system call screen, not via notifications.")
}
func declineCall(_ call: SignalCall) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
callManager.localHangup(call: call)
}
func recipientAcceptedCall(_ call: SignalCall) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
self.provider.reportOutgoingCall(with: call.localId, connectedAt: nil)
let update = CXCallUpdate()
disableUnsupportedFeatures(callUpdate: update)
provider.reportCall(with: call.localId, updated: update)
}
func localHangupCall(_ call: SignalCall) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
callManager.localHangup(call: call)
}
func remoteDidHangupCall(_ call: SignalCall) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
provider.reportCall(with: call.localId, endedAt: nil, reason: CXCallEndedReason.remoteEnded)
}
func remoteBusy(_ call: SignalCall) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
provider.reportCall(with: call.localId, endedAt: nil, reason: CXCallEndedReason.unanswered)
}
func setIsMuted(call: SignalCall, isMuted: Bool) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
callManager.setIsMuted(call: call, isMuted: isMuted)
}
func setHasLocalVideo(call: SignalCall, hasLocalVideo: Bool) {
AssertIsOnMainThread()
Logger.debug("\(self.TAG) \(#function)")
let update = CXCallUpdate()
update.hasVideo = hasLocalVideo
// Update the CallKit UI.
provider.reportCall(with: call.localId, updated: update)
self.callService.setHasLocalVideo(hasLocalVideo: hasLocalVideo)
}
// MARK: CXProviderDelegate
func providerDidReset(_ provider: CXProvider) {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
// End any ongoing calls if the provider resets, and remove them from the app's list of calls,
// since they are no longer valid.
callService.handleFailedCurrentCall(error: .providerReset)
// Remove all calls from the app's list of calls.
callManager.removeAllCalls()
}
func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
AssertIsOnMainThread()
Logger.info("\(TAG) in \(#function) CXStartCallAction")
guard let call = callManager.callWithLocalId(action.callUUID) else {
Logger.error("\(TAG) unable to find call in \(#function)")
return
}
// We can't wait for long before fulfilling the CXAction, else CallKit will show a "Failed Call". We don't
// actually need to wait for the outcome of the handleOutgoingCall promise, because it handles any errors by
// manually failing the call.
let callPromise = self.callService.handleOutgoingCall(call)
callPromise.retainUntilComplete()
action.fulfill()
self.provider.reportOutgoingCall(with: call.localId, startedConnectingAt: nil)
if Environment.getCurrent().preferences.isCallKitPrivacyEnabled() {
// Update the name used in the CallKit UI for outgoing calls.
let update = CXCallUpdate()
update.localizedCallerName = NSLocalizedString("CALLKIT_ANONYMOUS_CONTACT_NAME",
comment: "The generic name used for calls if CallKit privacy is enabled")
provider.reportCall(with: call.localId, updated: update)
}
}
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
AssertIsOnMainThread()
Logger.info("\(TAG) Received \(#function) CXAnswerCallAction")
// Retrieve the instance corresponding to the action's call UUID
guard let call = callManager.callWithLocalId(action.callUUID) else {
action.fail()
return
}
self.callService.handleAnswerCall(call)
self.showCall(call)
action.fulfill()
}
public func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
AssertIsOnMainThread()
Logger.info("\(TAG) Received \(#function) CXEndCallAction")
guard let call = callManager.callWithLocalId(action.callUUID) else {
Logger.error("\(self.TAG) in \(#function) trying to end unknown call with localId: \(action.callUUID)")
action.fail()
return
}
self.callService.handleLocalHungupCall(call)
// Signal to the system that the action has been successfully performed.
action.fulfill()
// Remove the ended call from the app's list of calls.
self.callManager.removeCall(call)
}
public func provider(_ provider: CXProvider, perform action: CXSetHeldCallAction) {
AssertIsOnMainThread()
Logger.info("\(TAG) Received \(#function) CXSetHeldCallAction")
guard let call = callManager.callWithLocalId(action.callUUID) else {
action.fail()
return
}
// Update the SignalCall's underlying hold state.
self.callService.setIsOnHold(call: call, isOnHold: action.isOnHold)
// Signal to the system that the action has been successfully performed.
action.fulfill()
}
public func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
AssertIsOnMainThread()
Logger.info("\(TAG) Received \(#function) CXSetMutedCallAction")
guard let call = callManager.callWithLocalId(action.callUUID) else {
Logger.error("\(TAG) Failing CXSetMutedCallAction for unknown call: \(action.callUUID)")
action.fail()
return
}
self.callService.setIsMuted(call: call, isMuted: action.isMuted)
action.fulfill()
}
public func provider(_ provider: CXProvider, perform action: CXSetGroupCallAction) {
AssertIsOnMainThread()
Logger.warn("\(TAG) unimplemented \(#function) for CXSetGroupCallAction")
}
public func provider(_ provider: CXProvider, perform action: CXPlayDTMFCallAction) {
AssertIsOnMainThread()
Logger.warn("\(TAG) unimplemented \(#function) for CXPlayDTMFCallAction")
}
func provider(_ provider: CXProvider, timedOutPerforming action: CXAction) {
AssertIsOnMainThread()
owsFail("\(TAG) Timed out \(#function) while performing \(action)")
// React to the action timeout if necessary, such as showing an error UI.
}
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
AssertIsOnMainThread()
Logger.debug("\(TAG) Received \(#function)")
CallAudioSession.shared.isRTCAudioEnabled = true
}
func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
AssertIsOnMainThread()
Logger.debug("\(TAG) Received \(#function)")
CallAudioSession.shared.isRTCAudioEnabled = false
}
// MARK: - Util
private func disableUnsupportedFeatures(callUpdate: CXCallUpdate) {
// Call Holding is failing to restart audio when "swapping" calls on the CallKit screen
// until user returns to in-app call screen.
callUpdate.supportsHolding = false
// Not yet supported
callUpdate.supportsGrouping = false
callUpdate.supportsUngrouping = false
// Is there any reason to support this?
callUpdate.supportsDTMF = false
}
}