mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 06:15:59 +00:00
Add wakelock when transferring files
This commit is contained in:
1 parent
0f6fef89fe
commit
f4a21d3f0a
4 files changed
+1580
-1558
No files matched your search
@@ -17,7 +17,7 @@ import SwiftECC
|
|||||||
class InboundNearbyConnection: NearbyConnection {
|
class InboundNearbyConnection: NearbyConnection {
|
||||||
|
|
||||||
private var currentState: State = .initial
|
private var currentState: State = .initial
|
||||||
private var wasRejected = false
|
public var wasRejected = false
|
||||||
public var delegate: InboundNearbyConnectionDelegate?
|
public var delegate: InboundNearbyConnectionDelegate?
|
||||||
private var cipherCommitment: Data?
|
private var cipherCommitment: Data?
|
||||||
|
|
||||||
@@ -38,13 +38,11 @@ class InboundNearbyConnection: NearbyConnection {
|
|||||||
super.disconnect()
|
super.disconnect()
|
||||||
currentState = .disconnected
|
currentState = .disconnected
|
||||||
|
|
||||||
if !self.wasRejected {
|
DispatchQueue.main.async {
|
||||||
DispatchQueue.main.async {
|
self.delegate?.connectionWasTerminated(connection: self, error: self.lastError)
|
||||||
self.delegate?.connectionWasTerminated(connection: self, error: self.lastError)
|
|
||||||
|
SaveFilesManager.shared.movePendingFilesToTarget()
|
||||||
SaveFilesManager.shared.movePendingFilesToTarget()
|
SaveFilesManager.shared.stopAccessingSecurityScopedResource()
|
||||||
SaveFilesManager.shared.stopAccessingSecurityScopedResource()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,20 +12,19 @@ import System
|
|||||||
|
|
||||||
public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearbyConnectionDelegate, OutboundNearbyConnectionDelegate {
|
public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearbyConnectionDelegate, OutboundNearbyConnectionDelegate {
|
||||||
|
|
||||||
|
private let sleepManager = SleepManager.shared
|
||||||
private var tcpListener: NWListener
|
private var tcpListener: NWListener
|
||||||
public let endpointID: [UInt8] = generateEndpointID()
|
|
||||||
private var mdnsServices: [NetService] = []
|
private var mdnsServices: [NetService] = []
|
||||||
private var activeConnections: [String: InboundNearbyConnection] = [:]
|
private var incomingConnections: [String: InboundNearbyConnection] = [:]
|
||||||
private var foundServices: [String: FoundServiceInfo] = [:]
|
private var foundServices: [String: FoundServiceInfo] = [:]
|
||||||
private var shareExtensionDelegates: [ShareExtensionDelegate] = []
|
private var shareExtensionDelegates: [ShareExtensionDelegate] = []
|
||||||
private var outgoingTransfers: [String: OutgoingTransferInfo] = [:]
|
private var outgoingTransfers: [String: OutgoingTransferInfo] = [:]
|
||||||
public var mainAppDelegate: (any MainAppDelegate)?
|
|
||||||
private var discoveryRefCount = 0
|
private var discoveryRefCount = 0
|
||||||
|
private var browsers: [NWBrowser] = []
|
||||||
|
private let serviceTypes = ["_FC9F5ED42C8A._tcp."]
|
||||||
|
|
||||||
let serviceTypes = ["_FC9F5ED42C8A._tcp."]
|
public let endpointID: [UInt8] = generateEndpointID()
|
||||||
|
public var mainAppDelegate: (any MainAppDelegate)?
|
||||||
private var browsers: [NWBrowser] = []
|
|
||||||
|
|
||||||
public static let shared = NearbyConnectionManager()
|
public static let shared = NearbyConnectionManager()
|
||||||
|
|
||||||
|
|
||||||
@@ -49,7 +48,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
|||||||
tcpListener.newConnectionHandler = { (connection: NWConnection) in
|
tcpListener.newConnectionHandler = { (connection: NWConnection) in
|
||||||
let id = UUID().uuidString
|
let id = UUID().uuidString
|
||||||
let conn = InboundNearbyConnection(connection: connection, id: id)
|
let conn = InboundNearbyConnection(connection: connection, id: id)
|
||||||
self.activeConnections[id] = conn
|
self.incomingConnections[id] = conn
|
||||||
conn.delegate = self
|
conn.delegate = self
|
||||||
conn.start()
|
conn.start()
|
||||||
}
|
}
|
||||||
@@ -70,7 +69,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
|||||||
// Generate a new random ID
|
// Generate a new random ID
|
||||||
var id: [UInt8] = []
|
var id: [UInt8] = []
|
||||||
let alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".compactMap { UInt8($0.asciiValue!) }
|
let alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".compactMap { UInt8($0.asciiValue!) }
|
||||||
for _ in 0 ..< 4 { // use 0..<4 instead of 0...3 for clarity
|
for _ in 0 ..< 4 {
|
||||||
id.append(alphabet[Int.random(in: 0 ..< alphabet.count)])
|
id.append(alphabet[Int.random(in: 0 ..< alphabet.count)])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,6 +88,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
|||||||
0xFC, 0x9F, 0x5E, // Service ID hash
|
0xFC, 0x9F, 0x5E, // Service ID hash
|
||||||
0, 0,
|
0, 0,
|
||||||
]
|
]
|
||||||
|
|
||||||
let name = Data(nameBytes).urlSafeBase64EncodedString()
|
let name = Data(nameBytes).urlSafeBase64EncodedString()
|
||||||
let endpointInfo = EndpointInfo(name: Host.current().localizedName!, deviceType: .computer)
|
let endpointInfo = EndpointInfo(name: Host.current().localizedName!, deviceType: .computer)
|
||||||
|
|
||||||
@@ -107,20 +107,21 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
|||||||
|
|
||||||
|
|
||||||
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection _: InboundNearbyConnection) {
|
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection _: InboundNearbyConnection) {
|
||||||
guard let delegate = mainAppDelegate else { return }
|
mainAppDelegate?.obtainUserConsent(for: transfer, from: device)
|
||||||
delegate.obtainUserConsent(for: transfer, from: device)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func connectionWasTerminated(connection: InboundNearbyConnection, error: Error?) {
|
func connectionWasTerminated(connection: InboundNearbyConnection, error: Error?) {
|
||||||
guard let delegate = mainAppDelegate else { return }
|
incomingConnections.removeValue(forKey: connection.id)
|
||||||
delegate.incomingTransfer(id: connection.id, didFinishWith: error)
|
|
||||||
activeConnections.removeValue(forKey: connection.id)
|
if !connection.wasRejected {
|
||||||
|
mainAppDelegate?.incomingTransfer(id: connection.id, didFinishWith: error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public func submitUserConsent(transferID: String, accept: Bool, storeInTemp: Bool = false) {
|
public func submitUserConsent(transferID: String, accept: Bool, storeInTemp: Bool = false) {
|
||||||
guard let conn = activeConnections[transferID] else { return }
|
guard let conn = incomingConnections[transferID] else { return }
|
||||||
|
|
||||||
log("Submitting user consent for transfer, accepted: \(accept), store in temp: \(storeInTemp)")
|
log("Submitting user consent for transfer, accepted: \(accept), store in temp: \(storeInTemp)")
|
||||||
conn.submitUserConsent(accepted: accept, storeInTemp: storeInTemp)
|
conn.submitUserConsent(accepted: accept, storeInTemp: storeInTemp)
|
||||||
@@ -304,7 +305,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
|||||||
|
|
||||||
|
|
||||||
public func getActiveConnectionsCount() -> Int {
|
public func getActiveConnectionsCount() -> Int {
|
||||||
return activeConnections.count
|
return incomingConnections.count + outgoingTransfers.count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,31 +9,51 @@ import Foundation
|
|||||||
import IOKit
|
import IOKit
|
||||||
import IOKit.pwr_mgt
|
import IOKit.pwr_mgt
|
||||||
|
|
||||||
class SleepManager{
|
class SleepManager {
|
||||||
public static let shared = SleepManager()
|
public static let shared = SleepManager()
|
||||||
private var assertionID: IOPMAssertionID = 0
|
private var assertionID: IOPMAssertionID = 0
|
||||||
|
private var timer: Timer?
|
||||||
|
|
||||||
public func disableSleep() {
|
private init() {
|
||||||
if(assertionID != 0) {
|
// Start periodic check
|
||||||
|
timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) { [weak self] _ in
|
||||||
|
self?.updateSleepState()
|
||||||
|
}
|
||||||
|
|
||||||
|
RunLoop.main.add(timer!, forMode: .common)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func disableSleep() {
|
||||||
|
if assertionID != 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log("[SleepManager] Enabling Wakelock")
|
||||||
|
|
||||||
IOPMAssertionCreateWithName(
|
IOPMAssertionCreateWithName(
|
||||||
kIOPMAssertionTypePreventUserIdleSystemSleep as CFString,
|
kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString,
|
||||||
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
||||||
"QuickDrop Data Transfer" as CFString,
|
"QuickDrop Data Transfer" as CFString,
|
||||||
&assertionID
|
&assertionID
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func enableSleep() {
|
private func enableSleep() {
|
||||||
if(assertionID == 0 || NearbyConnectionManager.shared.getActiveConnectionsCount() != 0){
|
if assertionID == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log("[SleepManager] Disabling Wakelock")
|
||||||
IOPMAssertionRelease(assertionID)
|
IOPMAssertionRelease(assertionID)
|
||||||
assertionID = 0
|
assertionID = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
private init() {}
|
private func updateSleepState() {
|
||||||
|
let activeConnections = NearbyConnectionManager.shared.getActiveConnectionsCount()
|
||||||
|
if activeConnections > 0 {
|
||||||
|
disableSleep()
|
||||||
|
} else {
|
||||||
|
enableSleep()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+1530
-1527
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user