The NSE should only run on iOS 13.3 or later where the "filtering" entitlement is available since our notifications don't contain any content and will often not trigger any user visible content. We control this by setting the deployment target to iOS 13.3. This does not handle calls as it's currently impossible to wake the main app or launch CallKit from within the NSE. Should we reach a point where we need to use this extension in production the service will need to be able to differentiate between call and non-call messages and deliver them as VOIP or Vanilla pushes as appropriate. Alternatively, Apple may introduce some way for us to signal the main app that a call message has been received. This does not currently address the potential for the NSE and the main app to be running and trying to process messages at the same time. As long as the websocket is connected and the main app is processing messages in a timely fashion the NSE will never be called since the service will not send pushes for these messages, but censorship circumvention users and users where the websocket is disconnected for some reason will legitimately receive pushes and we will want to process those messages. How we will handle these cases requires further thought since just terminating the NSE when the app launches is not sufficient. We could potentially do something like terminate the NSE everytime the main app runs the message fetcher job which should only happen if either the user pulls-to-refresh on the conversation list or the websocket is actively connected and receiving messsages. I plan to address this in a follow-up pull request. Currently this code will not ever be run since the service never sends vanilla push notifications. Eventually we will need to add logic into the main app to: a) detect we're on iOS 13.3 or later and b) update a flag on the service telling it to stop using VOIP pushes for non-call messages. The API for requesting this from the service does not yet exist.
89 lines
3.3 KiB
Swift
89 lines
3.3 KiB
Swift
//
|
|
// Copyright (c) 2020 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import AVFoundation
|
|
import SignalServiceKit
|
|
|
|
public struct AudioSource: Hashable {
|
|
|
|
public let image: UIImage
|
|
public let localizedName: String
|
|
public let portDescription: AVAudioSessionPortDescription?
|
|
|
|
// The built-in loud speaker / aka speakerphone
|
|
public let isBuiltInSpeaker: Bool
|
|
|
|
// The built-in quiet speaker, aka the normal phone handset receiver earpiece
|
|
public let isBuiltInEarPiece: Bool
|
|
|
|
public init(localizedName: String, image: UIImage, isBuiltInSpeaker: Bool, isBuiltInEarPiece: Bool, portDescription: AVAudioSessionPortDescription? = nil) {
|
|
self.localizedName = localizedName
|
|
self.image = image
|
|
self.isBuiltInSpeaker = isBuiltInSpeaker
|
|
self.isBuiltInEarPiece = isBuiltInEarPiece
|
|
self.portDescription = portDescription
|
|
}
|
|
|
|
public init(portDescription: AVAudioSessionPortDescription) {
|
|
|
|
let isBuiltInEarPiece = portDescription.portType == AVAudioSession.Port.builtInMic
|
|
|
|
// portDescription.portName works well for BT linked devices, but if we are using
|
|
// the built in mic, we have "iPhone Microphone" which is a little awkward.
|
|
// In that case, instead we prefer just the model name e.g. "iPhone" or "iPad"
|
|
let localizedName = isBuiltInEarPiece ? UIDevice.current.localizedModel : portDescription.portName
|
|
|
|
self.init(localizedName: localizedName,
|
|
image: #imageLiteral(resourceName: "button_phone_white"), // TODO
|
|
isBuiltInSpeaker: false,
|
|
isBuiltInEarPiece: isBuiltInEarPiece,
|
|
portDescription: portDescription)
|
|
}
|
|
|
|
// Speakerphone is handled separately from the other audio routes as it doesn't appear as an "input"
|
|
public static var builtInSpeaker: AudioSource {
|
|
return self.init(localizedName: NSLocalizedString("AUDIO_ROUTE_BUILT_IN_SPEAKER", comment: "action sheet button title to enable built in speaker during a call"),
|
|
image: #imageLiteral(resourceName: "button_phone_white"), //TODO
|
|
isBuiltInSpeaker: true,
|
|
isBuiltInEarPiece: false)
|
|
}
|
|
|
|
// MARK: Hashable
|
|
|
|
public static func ==(lhs: AudioSource, rhs: AudioSource) -> Bool {
|
|
// Simply comparing the `portDescription` vs the `portDescription.uid`
|
|
// caused multiple instances of the built in mic to turn up in a set.
|
|
if lhs.isBuiltInSpeaker && rhs.isBuiltInSpeaker {
|
|
return true
|
|
}
|
|
|
|
if lhs.isBuiltInSpeaker || rhs.isBuiltInSpeaker {
|
|
return false
|
|
}
|
|
|
|
guard let lhsPortDescription = lhs.portDescription else {
|
|
owsFailDebug("only the built in speaker should lack a port description")
|
|
return false
|
|
}
|
|
|
|
guard let rhsPortDescription = rhs.portDescription else {
|
|
owsFailDebug("only the built in speaker should lack a port description")
|
|
return false
|
|
}
|
|
|
|
return lhsPortDescription.uid == rhsPortDescription.uid
|
|
}
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
guard let portDescription = self.portDescription else {
|
|
assert(self.isBuiltInSpeaker)
|
|
hasher.combine("Built In Speaker")
|
|
return
|
|
}
|
|
|
|
hasher.combine(portDescription.uid)
|
|
}
|
|
}
|