1 /* <lambda>null2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.biometrics.fingerprint2.domain.interactor 18 19 import android.hardware.fingerprint.FingerprintManager 20 import android.os.CancellationSignal 21 import android.util.Log 22 import com.android.settings.biometrics.fingerprint2.data.repository.UserRepo 23 import com.android.settings.biometrics.fingerprint2.lib.domain.interactor.AuthenitcateInteractor 24 import com.android.settings.biometrics.fingerprint2.lib.model.FingerprintAuthAttemptModel 25 import kotlin.coroutines.resume 26 import kotlinx.coroutines.CancellableContinuation 27 import kotlinx.coroutines.flow.first 28 import kotlinx.coroutines.suspendCancellableCoroutine 29 30 class AuthenticateInteractorImpl( 31 private val fingerprintManager: FingerprintManager, 32 private val userRepo: UserRepo, 33 ) : AuthenitcateInteractor { 34 35 override suspend fun authenticate(): FingerprintAuthAttemptModel { 36 val userId = userRepo.currentUser.first() 37 return suspendCancellableCoroutine { c: CancellableContinuation<FingerprintAuthAttemptModel> -> 38 val authenticationCallback = 39 object : FingerprintManager.AuthenticationCallback() { 40 41 override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { 42 super.onAuthenticationError(errorCode, errString) 43 if (c.isCompleted) { 44 Log.d(TAG, "framework sent down onAuthError after finish") 45 return 46 } 47 c.resume(FingerprintAuthAttemptModel.Error(errorCode, errString.toString())) 48 } 49 50 override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult) { 51 super.onAuthenticationSucceeded(result) 52 if (c.isCompleted) { 53 Log.d(TAG, "framework sent down onAuthError after finish") 54 return 55 } 56 c.resume(FingerprintAuthAttemptModel.Success(result.fingerprint?.biometricId ?: -1)) 57 } 58 } 59 60 val cancellationSignal = CancellationSignal() 61 c.invokeOnCancellation { cancellationSignal.cancel() } 62 fingerprintManager.authenticate( 63 null, 64 cancellationSignal, 65 authenticationCallback, 66 null, 67 userId, 68 ) 69 } 70 } 71 72 companion object { 73 private const val TAG = "AuthenticateInteractor" 74 } 75 } 76