2018-10-24 22:38:05 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public protocol OWSProximityMonitoringManager: class {
|
|
|
|
func add(lifetime: AnyObject)
|
|
|
|
func remove(lifetime: AnyObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public class OWSProximityMonitoringManagerImpl: NSObject, OWSProximityMonitoringManager {
|
|
|
|
var lifetimes: [Weak<AnyObject>] = []
|
|
|
|
let serialQueue = DispatchQueue(label: "ProximityMonitoringManagerImpl")
|
|
|
|
|
|
|
|
// MARK:
|
|
|
|
|
|
|
|
var device: UIDevice {
|
|
|
|
return UIDevice.current
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK:
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public func add(lifetime: AnyObject) {
|
|
|
|
serialQueue.sync {
|
|
|
|
if !lifetimes.contains { $0.value === lifetime } {
|
|
|
|
lifetimes.append(Weak(value: lifetime))
|
|
|
|
}
|
|
|
|
reconcile()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public func remove(lifetime: AnyObject) {
|
|
|
|
serialQueue.sync {
|
|
|
|
lifetimes = lifetimes.filter { $0.value !== lifetime }
|
|
|
|
reconcile()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func reconcile() {
|
|
|
|
if _isDebugAssertConfiguration() {
|
|
|
|
assertOnQueue(serialQueue)
|
|
|
|
}
|
|
|
|
lifetimes = lifetimes.filter { $0.value != nil }
|
|
|
|
if lifetimes.isEmpty {
|
|
|
|
Logger.debug("disabling proximity monitoring")
|
2018-10-25 17:12:20 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.device.isProximityMonitoringEnabled = false
|
|
|
|
}
|
2018-10-24 22:38:05 +02:00
|
|
|
} else {
|
|
|
|
Logger.debug("enabling proximity monitoring for lifetimes: \(lifetimes)")
|
2018-10-25 17:12:20 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.device.isProximityMonitoringEnabled = true
|
|
|
|
}
|
2018-10-24 22:38:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|