-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply more dependency injection to the DeviceAuthenticator.
- Loading branch information
Showing
4 changed files
with
127 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
DuckDuckGo/Device Authentication/QuartzIdleStateDetector.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// DeviceIdleStateDetector.swift | ||
// | ||
// Copyright © 2022 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import CoreGraphics | ||
import os.log | ||
|
||
final class QuartzIdleStateDetector: DeviceIdleStateDetector { | ||
|
||
private enum Constants { | ||
static let intervalBetweenIdleChecks: TimeInterval = 1 | ||
} | ||
|
||
private var timer: Timer? | ||
private let idleTimeCallback: (TimeInterval) -> Void | ||
|
||
init(idleTimeCallback: @escaping (TimeInterval) -> Void) { | ||
self.idleTimeCallback = idleTimeCallback | ||
} | ||
|
||
deinit { | ||
cancelIdleCheckTimer() | ||
} | ||
|
||
func beginIdleCheckTimer() { | ||
os_log("Beginning idle check timer", log: .autoLock) | ||
|
||
self.timer?.invalidate() | ||
self.timer = nil | ||
|
||
let timer = Timer(timeInterval: Constants.intervalBetweenIdleChecks, repeats: true) { [weak self] _ in | ||
guard let self = self else { | ||
return | ||
} | ||
|
||
self.idleTimeCallback(self.secondsSinceLastEvent()) | ||
} | ||
|
||
self.timer = timer | ||
RunLoop.current.add(timer, forMode: .common) | ||
} | ||
|
||
func cancelIdleCheckTimer() { | ||
os_log("Cancelling idle check timer", log: .autoLock) | ||
self.timer?.invalidate() | ||
self.timer = nil | ||
} | ||
|
||
private func secondsSinceLastEvent() -> TimeInterval { | ||
let anyInputEventType = CGEventType(rawValue: ~0)! | ||
let seconds = CGEventSource.secondsSinceLastEventType(.hidSystemState, eventType: anyInputEventType) | ||
|
||
os_log("Idle duration since last user input event: %f", log: .autoLock, seconds) | ||
|
||
return seconds | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
Unit Tests/Device Authentication/DeviceAuthenticatorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// DeviceAuthenticatorTests.swift | ||
// | ||
// Copyright © 2022 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import DuckDuckGo_Privacy_Browser | ||
|
||
class DeviceAuthenticatorTests: XCTestCase { | ||
|
||
|
||
|
||
} |