session-ios/Session/Meta/AppDelegate.swift

150 lines
7.5 KiB
Swift
Raw Normal View History

import PromiseKit
import WebRTC
2021-10-06 08:00:12 +02:00
import SessionUIKit
import UIKit
extension AppDelegate {
// MARK: Call handling
@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)
}
@objc func setUpCallHandling() {
2021-11-03 05:31:50 +01:00
// Pre offer messages
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
// Ignore pre offer message afte the same call instance has been generated
if let currentCall = callManager.currentCall, currentCall.uuid == message.uuid! { return }
guard callManager.currentCall == nil && SSKPreferences.areCallsEnabled else {
callManager.handleIncomingCallOfferInBusyOrUnenabledState(offerMessage: message, using: transaction)
2021-11-11 01:09:52 +01:00
return
}
// 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
MessageReceiver.handleOfferCallMessage = { message in
DispatchQueue.main.async {
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-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()
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
}
}
}
// MARK: Configuration message
@objc(syncConfigurationIfNeeded)
func syncConfigurationIfNeeded() {
2021-02-26 05:56:41 +01:00
guard Storage.shared.getUser()?.name != nil else { return }
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
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()
}
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(()) }
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
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
}
// MARK: Closed group poller
@objc func startClosedGroupPoller() {
guard OWSIdentityManager.shared().identityKeyPair() != nil else { return }
ClosedGroupPoller.shared.start()
}
@objc func stopClosedGroupPoller() {
ClosedGroupPoller.shared.stop()
}
// MARK: Theme
@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 {
if #available(iOS 13.0, *) {
return UITraitCollection.current.userInterfaceStyle == .dark ? .dark : .light
} else {
return .light
}
}
}
}