mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.7 KiB
Swift
92 lines
2.7 KiB
Swift
7 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
7 years ago
|
//
|
||
|
|
||
|
@objc
|
||
|
public protocol OWSProximityMonitoringManager: class {
|
||
|
func add(lifetime: AnyObject)
|
||
|
func remove(lifetime: AnyObject)
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public class OWSProximityMonitoringManagerImpl: NSObject, OWSProximityMonitoringManager {
|
||
|
var lifetimes: [Weak<AnyObject>] = []
|
||
|
|
||
7 years ago
|
public override init() {
|
||
|
super.init()
|
||
|
|
||
7 years ago
|
AppReadiness.runNowOrWhenAppWillBecomeReady {
|
||
7 years ago
|
self.setup()
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// MARK:
|
||
|
|
||
|
var device: UIDevice {
|
||
|
return UIDevice.current
|
||
|
}
|
||
|
|
||
|
// MARK:
|
||
|
|
||
|
@objc
|
||
|
public func add(lifetime: AnyObject) {
|
||
7 years ago
|
objc_sync_enter(self)
|
||
|
|
||
5 years ago
|
if !lifetimes.contains(where: { $0.value === lifetime }) {
|
||
7 years ago
|
lifetimes.append(Weak(value: lifetime))
|
||
7 years ago
|
}
|
||
7 years ago
|
reconcile()
|
||
|
|
||
|
objc_sync_exit(self)
|
||
7 years ago
|
}
|
||
|
|
||
|
@objc
|
||
|
public func remove(lifetime: AnyObject) {
|
||
7 years ago
|
objc_sync_enter(self)
|
||
|
|
||
|
lifetimes = lifetimes.filter { $0.value !== lifetime }
|
||
|
reconcile()
|
||
|
|
||
|
objc_sync_exit(self)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public func setup() {
|
||
6 years ago
|
NotificationCenter.default.addObserver(self, selector: #selector(proximitySensorStateDidChange(notification:)), name: UIDevice.proximityStateDidChangeNotification, object: nil)
|
||
7 years ago
|
}
|
||
|
|
||
|
@objc
|
||
|
func proximitySensorStateDidChange(notification: Notification) {
|
||
|
Logger.debug("")
|
||
|
// This is crazy, but if we disable `device.isProximityMonitoringEnabled` while
|
||
|
// `device.proximityState` is true (while the device is held to the ear)
|
||
|
// then `device.proximityState` remains true, even after we bring the phone
|
||
|
// away from the ear and re-enable monitoring.
|
||
|
//
|
||
|
// To resolve this, we wait to disable proximity monitoring until `proximityState`
|
||
|
// is false.
|
||
|
if self.device.proximityState {
|
||
|
self.add(lifetime: self)
|
||
|
} else {
|
||
|
self.remove(lifetime: self)
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
func reconcile() {
|
||
|
lifetimes = lifetimes.filter { $0.value != nil }
|
||
|
if lifetimes.isEmpty {
|
||
7 years ago
|
DispatchQueue.main.async {
|
||
7 years ago
|
Logger.debug("disabling proximity monitoring")
|
||
7 years ago
|
self.device.isProximityMonitoringEnabled = false
|
||
|
}
|
||
7 years ago
|
} else {
|
||
7 years ago
|
let lifetimes = self.lifetimes
|
||
7 years ago
|
DispatchQueue.main.async {
|
||
7 years ago
|
Logger.debug("willEnable proximity monitoring for lifetimes: \(lifetimes), proximityState: \(self.device.proximityState)")
|
||
7 years ago
|
self.device.isProximityMonitoringEnabled = true
|
||
7 years ago
|
Logger.debug("didEnable proximity monitoring for lifetimes: \(lifetimes), proximityState: \(self.device.proximityState)")
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
}
|
||
|
}
|