mirror of https://github.com/oxen-io/session-ios
				
				
				
			Use reference counting to disable proximity monitoring after audio message
Multiple overlapping activities require proximity monitoring (namely, CallViewController and listening to audio messages). These activities can overlap arbitrarily, so we use a reference counting strategy to keep proximity monitoring on as long as one of these activities is active.pull/1/head
							parent
							
								
									7f37400f1d
								
							
						
					
					
						commit
						5632bd2d83
					
				@ -0,0 +1,55 @@
 | 
			
		||||
//
 | 
			
		||||
//  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")
 | 
			
		||||
            device.isProximityMonitoringEnabled = false
 | 
			
		||||
        } else {
 | 
			
		||||
            Logger.debug("enabling proximity monitoring for lifetimes: \(lifetimes)")
 | 
			
		||||
            device.isProximityMonitoringEnabled = true
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue