2021-01-13 04:20:12 +01:00
|
|
|
import PromiseKit
|
2021-08-17 07:41:13 +02:00
|
|
|
import WebRTC
|
2021-10-06 08:00:12 +02:00
|
|
|
import SessionUIKit
|
|
|
|
import UIKit
|
2021-01-13 04:08:41 +01:00
|
|
|
|
|
|
|
extension AppDelegate {
|
|
|
|
|
2021-10-12 07:43:30 +02:00
|
|
|
// MARK: Call handling
|
2021-11-10 01:13:37 +01:00
|
|
|
@objc func handleAppActivatedWithOngoingCallIfNeeded() {
|
|
|
|
guard let call = AppEnvironment.shared.callManager.currentCall else { return }
|
|
|
|
if let callVC = CurrentAppContext().frontmostViewController() as? CallVC, callVC.call == call { return }
|
|
|
|
guard let presentingVC = CurrentAppContext().frontmostViewController() else { preconditionFailure() } // TODO: Handle more gracefully
|
|
|
|
let callVC = CallVC(for: call)
|
|
|
|
if let conversationVC = presentingVC as? ConversationVC, let contactThread = conversationVC.thread as? TSContactThread, contactThread.contactSessionID() == call.sessionID {
|
|
|
|
callVC.conversationVC = conversationVC
|
|
|
|
conversationVC.inputAccessoryView?.isHidden = true
|
|
|
|
conversationVC.inputAccessoryView?.alpha = 0
|
|
|
|
}
|
|
|
|
presentingVC.present(callVC, animated: true, completion: nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-12 07:43:30 +02:00
|
|
|
@objc func setUpCallHandling() {
|
2021-11-03 05:31:50 +01:00
|
|
|
// Pre offer messages
|
2021-11-12 06:24:47 +01:00
|
|
|
MessageReceiver.handleNewCallOfferMessageIfNeeded = { (message, transaction) in
|
2021-11-03 05:31:50 +01:00
|
|
|
guard CurrentAppContext().isMainApp else { return }
|
2021-11-11 01:09:52 +01:00
|
|
|
let callManager = AppEnvironment.shared.callManager
|
2021-11-12 06:24:47 +01:00
|
|
|
// Ignore pre offer message afte the same call instance has been generated
|
|
|
|
if let currentCall = callManager.currentCall, currentCall.uuid == message.uuid! { return }
|
2021-11-11 06:09:39 +01:00
|
|
|
guard callManager.currentCall == nil && SSKPreferences.areCallsEnabled else {
|
2021-11-11 02:12:12 +01:00
|
|
|
callManager.handleIncomingCallOfferInBusyOrUnenabledState(offerMessage: message, using: transaction)
|
2021-11-11 01:09:52 +01:00
|
|
|
return
|
|
|
|
}
|
2021-11-12 06:24:47 +01:00
|
|
|
// Create incoming call message
|
|
|
|
let thread = TSContactThread.getOrCreateThread(withContactSessionID: message.sender!, transaction: transaction)
|
|
|
|
let tsMessage = TSIncomingMessage.from(message, associatedWith: thread)
|
|
|
|
tsMessage.save(with: transaction)
|
|
|
|
// Handle UI
|
|
|
|
if let caller = message.sender, let uuid = message.uuid {
|
|
|
|
let call = SessionCall(for: caller, uuid: uuid, mode: .answer)
|
|
|
|
call.callMessageTimestamp = message.sentTimestamp
|
|
|
|
DispatchQueue.main.async {
|
2021-11-09 06:05:23 +01:00
|
|
|
if CurrentAppContext().isMainAppAndActive {
|
|
|
|
guard let presentingVC = CurrentAppContext().frontmostViewController() else { preconditionFailure() } // TODO: Handle more gracefully
|
|
|
|
if let conversationVC = presentingVC as? ConversationVC, let contactThread = conversationVC.thread as? TSContactThread, contactThread.contactSessionID() == caller {
|
|
|
|
let callVC = CallVC(for: call)
|
|
|
|
callVC.conversationVC = conversationVC
|
|
|
|
conversationVC.inputAccessoryView?.isHidden = true
|
|
|
|
conversationVC.inputAccessoryView?.alpha = 0
|
|
|
|
presentingVC.present(callVC, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
call.reportIncomingCallIfNeeded{ error in
|
|
|
|
if let error = error {
|
|
|
|
SNLog("[Calls] Failed to report incoming call to CallKit due to error: \(error)")
|
|
|
|
let incomingCallBanner = IncomingCallBanner(for: call)
|
|
|
|
incomingCallBanner.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 05:31:50 +01:00
|
|
|
}
|
2021-08-18 02:33:33 +02:00
|
|
|
// Offer messages
|
2021-08-17 07:41:13 +02:00
|
|
|
MessageReceiver.handleOfferCallMessage = { message in
|
|
|
|
DispatchQueue.main.async {
|
2021-11-11 23:17:06 +01:00
|
|
|
guard let call = AppEnvironment.shared.callManager.currentCall, message.uuid! == call.uuid else { return }
|
2021-08-17 08:02:20 +02:00
|
|
|
let sdp = RTCSessionDescription(type: .offer, sdp: message.sdps![0])
|
2021-11-03 05:31:50 +01:00
|
|
|
call.didReceiveRemoteSDP(sdp: sdp)
|
2021-08-17 07:41:13 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-18 05:07:15 +02:00
|
|
|
// Answer messages
|
|
|
|
MessageReceiver.handleAnswerCallMessage = { message in
|
|
|
|
DispatchQueue.main.async {
|
2021-11-12 03:33:19 +01:00
|
|
|
guard let call = AppEnvironment.shared.callManager.currentCall, message.uuid! == call.uuid else { return }
|
2021-11-11 06:51:54 +01:00
|
|
|
AppEnvironment.shared.callManager.invalidateTimeoutTimer()
|
2021-11-11 06:09:39 +01:00
|
|
|
call.hasStartedConnecting = true
|
|
|
|
let sdp = RTCSessionDescription(type: .answer, sdp: message.sdps![0])
|
|
|
|
call.didReceiveRemoteSDP(sdp: sdp)
|
2021-10-07 07:53:38 +02:00
|
|
|
guard let callVC = CurrentAppContext().frontmostViewController() as? CallVC else { return }
|
|
|
|
callVC.handleAnswerMessage(message)
|
2021-08-18 05:07:15 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-18 02:33:33 +02:00
|
|
|
// End call messages
|
|
|
|
MessageReceiver.handleEndCallMessage = { message in
|
|
|
|
DispatchQueue.main.async {
|
2021-10-06 08:00:12 +02:00
|
|
|
if let currentBanner = IncomingCallBanner.current { currentBanner.dismiss() }
|
2021-11-10 04:31:02 +01:00
|
|
|
if let callVC = CurrentAppContext().frontmostViewController() as? CallVC { callVC.handleEndCallMessage() }
|
2021-10-11 04:14:39 +02:00
|
|
|
if let miniCallView = MiniCallView.current { miniCallView.dismiss() }
|
2021-11-07 23:12:18 +01:00
|
|
|
AppEnvironment.shared.callManager.reportCurrentCallEnded(reason: .remoteEnded)
|
2021-08-18 02:33:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-17 07:41:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-12 07:43:30 +02:00
|
|
|
// MARK: Configuration message
|
2021-01-13 04:20:12 +01:00
|
|
|
@objc(syncConfigurationIfNeeded)
|
|
|
|
func syncConfigurationIfNeeded() {
|
2021-02-26 05:56:41 +01:00
|
|
|
guard Storage.shared.getUser()?.name != nil else { return }
|
2021-01-13 04:08:41 +01:00
|
|
|
let userDefaults = UserDefaults.standard
|
|
|
|
let lastSync = userDefaults[.lastConfigurationSync] ?? .distantPast
|
2021-05-03 03:42:07 +02:00
|
|
|
guard Date().timeIntervalSince(lastSync) > 7 * 24 * 60 * 60,
|
2021-03-04 03:50:13 +01:00
|
|
|
let configurationMessage = ConfigurationMessage.getCurrent() else { return } // Sync every 2 days
|
2021-01-13 04:08:41 +01:00
|
|
|
let destination = Message.Destination.contact(publicKey: getUserHexEncodedPublicKey())
|
|
|
|
Storage.shared.write { transaction in
|
|
|
|
let job = MessageSendJob(message: configurationMessage, destination: destination)
|
|
|
|
JobQueue.shared.add(job, using: transaction)
|
|
|
|
}
|
2021-01-13 06:10:06 +01:00
|
|
|
userDefaults[.lastConfigurationSync] = Date()
|
2021-01-13 04:08:41 +01:00
|
|
|
}
|
2021-01-13 04:20:12 +01:00
|
|
|
|
|
|
|
func forceSyncConfigurationNowIfNeeded() -> Promise<Void> {
|
2021-03-04 03:50:13 +01:00
|
|
|
guard Storage.shared.getUser()?.name != nil,
|
|
|
|
let configurationMessage = ConfigurationMessage.getCurrent() else { return Promise.value(()) }
|
2021-01-13 04:20:12 +01:00
|
|
|
let destination = Message.Destination.contact(publicKey: getUserHexEncodedPublicKey())
|
|
|
|
let (promise, seal) = Promise<Void>.pending()
|
2021-01-13 06:10:06 +01:00
|
|
|
Storage.writeSync { transaction in
|
2021-01-13 04:20:12 +01:00
|
|
|
MessageSender.send(configurationMessage, to: destination, using: transaction).done {
|
|
|
|
seal.fulfill(())
|
|
|
|
}.catch { _ in
|
|
|
|
seal.fulfill(()) // Fulfill even if this failed; the configuration in the swarm should be at most 2 days old
|
|
|
|
}.retainUntilComplete()
|
|
|
|
}
|
|
|
|
return promise
|
|
|
|
}
|
2021-05-12 02:33:29 +02:00
|
|
|
|
2021-10-12 07:43:30 +02:00
|
|
|
// MARK: Closed group poller
|
2021-05-12 02:33:29 +02:00
|
|
|
@objc func startClosedGroupPoller() {
|
|
|
|
guard OWSIdentityManager.shared().identityKeyPair() != nil else { return }
|
|
|
|
ClosedGroupPoller.shared.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func stopClosedGroupPoller() {
|
|
|
|
ClosedGroupPoller.shared.stop()
|
|
|
|
}
|
2021-08-07 10:56:59 +02:00
|
|
|
|
2021-10-12 07:43:30 +02:00
|
|
|
// MARK: Theme
|
2021-08-07 10:56:59 +02:00
|
|
|
@objc func getAppModeOrSystemDefault() -> AppMode {
|
|
|
|
let userDefaults = UserDefaults.standard
|
2021-08-18 01:56:28 +02:00
|
|
|
if userDefaults.dictionaryRepresentation().keys.contains("appMode") {
|
|
|
|
let mode = userDefaults.integer(forKey: "appMode")
|
|
|
|
return AppMode(rawValue: mode) ?? .light
|
|
|
|
} else {
|
2021-08-07 10:56:59 +02:00
|
|
|
if #available(iOS 13.0, *) {
|
|
|
|
return UITraitCollection.current.userInterfaceStyle == .dark ? .dark : .light
|
|
|
|
} else {
|
|
|
|
return .light
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 04:08:41 +01:00
|
|
|
}
|