From 06e71f796d352ea4e08622f5196158f346e3d4a9 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Mon, 19 Oct 2026 12:49:52 +0200 Subject: [PATCH] Remove the redundant biometric refresh calls from AppLockService The biometric state hash now comes from LAContext.domainState, which unlike the deprecated evaluatedPolicyDomainState doesn't need canEvaluatePolicy to be called before it can be read. Drop updateBiometrics() and make LAContextMock return its values directly instead of waiting for a policy evaluation. --- .../Services/AppLock/AppLockService.swift | 18 +----------------- .../Services/AppLock/LAContextMock.swift | 19 +++---------------- 2 files changed, 4 insertions(+), 33 deletions(-) diff --git a/ElementX/Sources/Services/AppLock/AppLockService.swift b/ElementX/Sources/Services/AppLock/AppLockService.swift index cce2d022e..5d62f9104 100644 --- a/ElementX/Sources/Services/AppLock/AppLockService.swift +++ b/ElementX/Sources/Services/AppLock/AppLockService.swift @@ -44,7 +44,6 @@ class AppLockService: AppLockServiceProtocol { } var biometryType: LABiometryType { - updateBiometrics() guard context.biometricStateHash != nil else { return .none } return context.biometryType } @@ -55,7 +54,6 @@ class AppLockService: AppLockServiceProtocol { var biometricUnlockTrusted: Bool { guard let state = keychainController.pinCodeBiometricState() else { return false } - updateBiometrics() return state == context.biometricStateHash } @@ -68,8 +66,6 @@ class AppLockService: AppLockServiceProtocol { self.appSettings = appSettings self.context = context timer = AppLockTimer(gracePeriod: appSettings.appLockGracePeriod) - - updateBiometrics() } func setupPINCode(_ pinCode: String) -> Result { @@ -133,7 +129,6 @@ class AppLockService: AppLockServiceProtocol { if biometricUnlockEnabled, !biometricUnlockTrusted { MXLog.info("Fixing trust for biometric unlock.") - updateBiometrics() _ = enableBiometricUnlock() } @@ -195,16 +190,6 @@ class AppLockService: AppLockServiceProtocol { // MARK: - Private - /// Queries the context for supported biometrics and enrolment state. - private func updateBiometrics() { - var error: NSError? - context.canEvaluatePolicy(unlockPolicy, error: &error) - - if let error { - MXLog.error("Biometrics error: \(error)") - } - } - /// Creates a context specifically for unlocking the app. The titles are customised, /// and the fresh context ensures that the user is promoted to unlock based on /// `timer.gracePeriod` rather than any system to defined grace period. @@ -232,8 +217,7 @@ class AppLockService: AppLockServiceProtocol { nonisolated extension LAContext { /// A hash of the enrolled biometric data that changes whenever biometrics are added or removed. - /// Unlike `evaluatedPolicyDomainState`, this doesn't need `canEvaluatePolicy` to be called first, - /// so the `updateBiometrics()` calls made before reading it are now redundant. + /// Unlike the deprecated `evaluatedPolicyDomainState`, this doesn't need `canEvaluatePolicy` to be called first. @objc var biometricStateHash: Data? { domainState.biometry.stateHash } diff --git a/ElementX/Sources/Services/AppLock/LAContextMock.swift b/ElementX/Sources/Services/AppLock/LAContextMock.swift index 85d549044..b4f382ae9 100644 --- a/ElementX/Sources/Services/AppLock/LAContextMock.swift +++ b/ElementX/Sources/Services/AppLock/LAContextMock.swift @@ -9,27 +9,20 @@ import LocalAuthentication /// A customised context that allows injecting a few mock values but otherwise behaves as expected. -/// It works as the actual context does and won't update the return values of `biometryType` and -/// `biometricStateHash` until either `canEvaluatePolicy` or -/// `evaluatePolicy` have been called. nonisolated class LAContextMock: LAContext { var biometryTypeValue: LABiometryType! - private var internalBiometryTypeValue: LABiometryType! override var biometryType: LABiometryType { - internalBiometryTypeValue + biometryTypeValue } var biometricStateHashValue: Data? - private var internalBiometricStateHashValue: Data? override var biometricStateHash: Data? { - internalBiometricStateHashValue + biometricStateHashValue } var canEvaluatePolicyReturnValue: Bool? override func canEvaluatePolicy(_ policy: LAPolicy, error: NSErrorPointer) -> Bool { - let result = canEvaluatePolicyReturnValue ?? super.canEvaluatePolicy(policy, error: error) - updateInternalValues() - return result + canEvaluatePolicyReturnValue ?? super.canEvaluatePolicy(policy, error: error) } var evaluatePolicyReturnValue: Bool! @@ -41,15 +34,9 @@ nonisolated class LAContextMock: LAContext { override func evaluatePolicy(_ policy: LAPolicy, localizedReason: String) async throws -> Bool { evaluatePolicyCallsCount += 1 - updateInternalValues() if let evaluatePolicyThrowableError { throw evaluatePolicyThrowableError } return evaluatePolicyReturnValue } - - private func updateInternalValues() { - internalBiometryTypeValue = biometryTypeValue - internalBiometricStateHashValue = biometricStateHashValue - } }