2017-02-01 23:49:32 +01:00
|
|
|
//
|
2018-02-21 01:52:49 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-02-01 23:49:32 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2017-11-28 00:17:46 +01:00
|
|
|
import SignalServiceKit
|
2017-12-01 16:48:18 +01:00
|
|
|
import SignalMessaging
|
2017-02-01 23:49:32 +01:00
|
|
|
|
|
|
|
/**
|
2017-03-23 14:55:39 +01:00
|
|
|
* Creates an outbound call via WebRTC.
|
2017-02-01 23:49:32 +01:00
|
|
|
*/
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc public class OutboundCallInitiator: NSObject {
|
2017-02-01 23:49:32 +01:00
|
|
|
|
|
|
|
let contactsManager: OWSContactsManager
|
|
|
|
let contactsUpdater: ContactsUpdater
|
|
|
|
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc public init(contactsManager: OWSContactsManager, contactsUpdater: ContactsUpdater) {
|
2017-02-01 23:49:32 +01:00
|
|
|
self.contactsManager = contactsManager
|
|
|
|
self.contactsUpdater = contactsUpdater
|
2017-02-07 21:47:33 +01:00
|
|
|
|
|
|
|
super.init()
|
2017-12-07 16:33:27 +01:00
|
|
|
|
|
|
|
SwiftSingletons.register(self)
|
2017-02-01 23:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* |handle| is a user formatted phone number, e.g. from a system contacts entry
|
|
|
|
*/
|
2018-05-15 17:56:21 +02:00
|
|
|
@discardableResult @objc public func initiateCall(handle: String) -> Bool {
|
2018-08-15 16:24:29 +02:00
|
|
|
Logger.info("\(logTag) in \(#function) with handle: \(handle)")
|
2017-03-14 23:29:16 +01:00
|
|
|
|
2017-07-13 18:59:35 +02:00
|
|
|
guard let recipientId = PhoneNumber(fromE164: handle)?.toE164() else {
|
2018-08-15 16:24:29 +02:00
|
|
|
Logger.warn("\(logTag) unable to parse signalId from phone number: \(handle)")
|
2017-03-22 21:54:14 +01:00
|
|
|
return false
|
2017-02-01 23:49:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-02 16:08:47 +02:00
|
|
|
return initiateCall(recipientId: recipientId, isVideo: false)
|
2017-02-01 23:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* |recipientId| is a e164 formatted phone number.
|
|
|
|
*/
|
2018-05-15 17:56:21 +02:00
|
|
|
@discardableResult @objc public func initiateCall(recipientId: String,
|
2018-05-02 16:08:47 +02:00
|
|
|
isVideo: Bool) -> Bool {
|
2017-03-22 21:54:14 +01:00
|
|
|
// Rather than an init-assigned dependency property, we access `callUIAdapter` via Environment
|
2017-02-02 18:21:48 +01:00
|
|
|
// because it can change after app launch due to user settings
|
2018-06-11 21:31:54 +02:00
|
|
|
let callUIAdapter = SignalApp.shared().callUIAdapter
|
2017-11-07 16:32:28 +01:00
|
|
|
guard let frontmostViewController = UIApplication.shared.frontmostViewController else {
|
2018-08-15 16:24:29 +02:00
|
|
|
owsFail("\(logTag) could not identify frontmostViewController in \(#function)")
|
2017-11-07 16:32:28 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-02-02 18:21:48 +01:00
|
|
|
|
2017-06-01 03:12:18 +02:00
|
|
|
let showedAlert = SafetyNumberConfirmationAlert.presentAlertIfNecessary(recipientId: recipientId,
|
|
|
|
confirmationText: CallStrings.confirmAndCallButtonTitle,
|
2018-02-21 01:52:49 +01:00
|
|
|
contactsManager: self.contactsManager,
|
|
|
|
completion: { didConfirmIdentity in
|
2017-06-01 03:12:18 +02:00
|
|
|
if didConfirmIdentity {
|
2018-05-02 16:08:47 +02:00
|
|
|
_ = self.initiateCall(recipientId: recipientId, isVideo: isVideo)
|
2017-06-01 03:12:18 +02:00
|
|
|
}
|
2018-02-21 01:52:49 +01:00
|
|
|
})
|
2017-06-01 03:12:18 +02:00
|
|
|
guard !showedAlert else {
|
|
|
|
return false
|
|
|
|
}
|
2017-05-31 17:49:30 +02:00
|
|
|
|
2017-03-26 14:50:51 +02:00
|
|
|
// Check for microphone permissions
|
|
|
|
// Alternative way without prompting for permissions:
|
|
|
|
// if AVAudioSession.sharedInstance().recordPermission() == .denied {
|
2017-11-07 16:32:28 +01:00
|
|
|
frontmostViewController.ows_ask(forMicrophonePermissions: { [weak self] granted in
|
|
|
|
// Success callback; camera permissions are granted.
|
|
|
|
|
|
|
|
guard let strongSelf = self else {
|
|
|
|
return
|
2017-03-26 14:50:51 +02:00
|
|
|
}
|
2017-11-07 16:32:28 +01:00
|
|
|
|
|
|
|
// Here the permissions are either granted or denied
|
|
|
|
guard granted == true else {
|
2018-08-15 16:24:29 +02:00
|
|
|
Logger.warn("\(strongSelf.logTag) aborting due to missing microphone permissions.")
|
2017-11-07 16:32:28 +01:00
|
|
|
OWSAlerts.showNoMicrophonePermissionAlert()
|
|
|
|
return
|
|
|
|
}
|
2018-05-02 16:08:47 +02:00
|
|
|
callUIAdapter.startAndShowOutgoingCall(recipientId: recipientId, hasLocalVideo: isVideo)
|
2017-11-07 16:32:28 +01:00
|
|
|
})
|
|
|
|
|
2017-03-22 21:54:14 +01:00
|
|
|
return true
|
2017-02-07 21:47:33 +01:00
|
|
|
}
|
2017-02-01 23:49:32 +01:00
|
|
|
}
|