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.
		
		
		
		
		
			
		
			
	
	
		
			81 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			81 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Swift
		
	
| 
											8 years ago
										 | // | ||
| 
											8 years ago
										 | //  Copyright (c) 2018 Open Whisper Systems. All rights reserved. | ||
| 
											8 years ago
										 | // | ||
|  | 
 | ||
|  | import Foundation | ||
| 
											5 years ago
										 | 
 | ||
| 
											8 years ago
										 | 
 | ||
|  | // This entity has responsibility for blocking the device from sleeping if | ||
|  | // certain behaviors (e.g. recording or playing voice messages) are in progress. | ||
|  | //  | ||
| 
											8 years ago
										 | // Sleep blocking is keyed using "block objects" whose lifetime corresponds to | ||
| 
											8 years ago
										 | // the duration of the block.  For example, sleep blocking during audio playback | ||
|  | // can be keyed to the audio player.  This provides a measure of robustness. | ||
|  | // On the one hand, we can use weak references to track block objects and stop | ||
|  | // blocking if the block object is deallocated even if removeBlock() is not | ||
|  | // called.  On the other hand, we will also get correct behavior to addBlock() | ||
|  | // being called twice with the same block object. | ||
| 
											8 years ago
										 | @objc | ||
|  | public class DeviceSleepManager: NSObject { | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public static let sharedInstance = DeviceSleepManager() | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     private class SleepBlock: CustomDebugStringConvertible { | ||
| 
											8 years ago
										 |         weak var blockObject: NSObject? | ||
|  | 
 | ||
| 
											8 years ago
										 |         var debugDescription: String { | ||
|  |             return "SleepBlock(\(String(reflecting: blockObject)))" | ||
|  |         } | ||
|  | 
 | ||
| 
											8 years ago
										 |         init(blockObject: NSObject) { | ||
|  |             self.blockObject = blockObject | ||
|  |         } | ||
|  |     } | ||
|  |     private var blocks: [SleepBlock] = [] | ||
|  | 
 | ||
| 
											8 years ago
										 |     private override init() { | ||
| 
											8 years ago
										 |         super.init() | ||
|  | 
 | ||
|  |         NotificationCenter.default.addObserver(self, | ||
| 
											8 years ago
										 |                                                selector: #selector(didEnterBackground), | ||
|  |                                                name: NSNotification.Name.OWSApplicationDidEnterBackground, | ||
|  |                                                object: nil) | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
|  |     deinit { | ||
|  |         NotificationCenter.default.removeObserver(self) | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     private func didEnterBackground() { | ||
| 
											8 years ago
										 |         ensureSleepBlocking() | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func addBlock(blockObject: NSObject) { | ||
|  |         blocks.append(SleepBlock(blockObject: blockObject)) | ||
|  | 
 | ||
|  |         ensureSleepBlocking() | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func removeBlock(blockObject: NSObject) { | ||
|  |         blocks = blocks.filter { | ||
| 
											8 years ago
										 |             $0.blockObject != nil && $0.blockObject != blockObject | ||
| 
											8 years ago
										 |         } | ||
|  | 
 | ||
|  |         ensureSleepBlocking() | ||
|  |     } | ||
|  | 
 | ||
|  |     private func ensureSleepBlocking() { | ||
|  |         // Cull expired blocks. | ||
|  |         blocks = blocks.filter { | ||
|  |             $0.blockObject != nil | ||
|  |         } | ||
|  |         let shouldBlock = blocks.count > 0 | ||
|  | 
 | ||
| 
											8 years ago
										 |         CurrentAppContext().ensureSleepBlocking(shouldBlock, blockingObjects: blocks) | ||
| 
											8 years ago
										 |     } | ||
|  | } |