-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathAppLockState.swift
62 lines (53 loc) · 2.02 KB
/
AppLockState.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import Foundation
import MonotonicTime
public struct MonotonicTimestamp: Codable, Equatable {
public var bootTimestamp: Int32
public var uptime: Int32
public init(bootTimestamp: Int32, uptime: Int32) {
self.bootTimestamp = bootTimestamp
self.uptime = uptime
}
}
public struct UnlockAttempts: Codable, Equatable {
public var count: Int32
public var timestamp: MonotonicTimestamp
public init(count: Int32, timestamp: MonotonicTimestamp) {
self.count = count
self.timestamp = timestamp
}
}
public struct LockState: Codable, Equatable {
public var isManuallyLocked: Bool
public var autolockTimeout: Int32?
public var unlockAttempts: UnlockAttempts?
public var applicationActivityTimestamp: MonotonicTimestamp?
public init(isManuallyLocked: Bool = false, autolockTimeout: Int32? = nil, unlockAttemts: UnlockAttempts? = nil, applicationActivityTimestamp: MonotonicTimestamp? = nil) {
self.isManuallyLocked = isManuallyLocked
self.autolockTimeout = autolockTimeout
self.unlockAttempts = unlockAttemts
self.applicationActivityTimestamp = applicationActivityTimestamp
}
}
public func appLockStatePath(rootPath: String) -> String {
return rootPath + "/lockState.json"
}
public func isAppLocked(state: LockState) -> Bool {
if state.isManuallyLocked {
return true
} else if let autolockTimeout = state.autolockTimeout {
var bootTimestamp: Int32 = 0
let uptime = getDeviceUptimeSeconds(&bootTimestamp)
let timestamp = MonotonicTimestamp(bootTimestamp: bootTimestamp, uptime: uptime)
if let applicationActivityTimestamp = state.applicationActivityTimestamp {
if timestamp.bootTimestamp != applicationActivityTimestamp.bootTimestamp {
return true
}
if timestamp.uptime >= applicationActivityTimestamp.uptime + autolockTimeout {
return true
}
} else {
return true
}
}
return false
}