1 /*
2  * Copyright (C) 2023 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.systemui.biometrics.domain.interactor
18 
19 import android.view.MotionEvent
20 import com.android.keyguard.KeyguardUpdateMonitor
21 import com.android.settingslib.udfps.UdfpsOverlayParams
22 import com.android.systemui.biometrics.AuthController
23 import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
24 import com.android.systemui.common.coroutine.ConflatedCallbackFlow
25 import com.android.systemui.dagger.SysUISingleton
26 import com.android.systemui.dagger.qualifiers.Application
27 import javax.inject.Inject
28 import kotlinx.coroutines.CoroutineScope
29 import kotlinx.coroutines.channels.awaitClose
30 import kotlinx.coroutines.flow.SharingStarted
31 import kotlinx.coroutines.flow.StateFlow
32 import kotlinx.coroutines.flow.stateIn
33 
34 /** Encapsulates business logic for interacting with the UDFPS overlay. */
35 @SysUISingleton
36 class UdfpsOverlayInteractor
37 @Inject
38 constructor(private val authController: AuthController, @Application scope: CoroutineScope) {
39 
40     /** Whether a touch is within the under-display fingerprint sensor area */
41     fun isTouchWithinUdfpsArea(ev: MotionEvent): Boolean {
42         val isUdfpsEnrolled = authController.isUdfpsEnrolled(KeyguardUpdateMonitor.getCurrentUser())
43         val isWithinOverlayBounds =
44             udfpsOverlayParams.value.overlayBounds.contains(ev.rawX.toInt(), ev.rawY.toInt())
45         return isUdfpsEnrolled && isWithinOverlayBounds
46     }
47 
48     /** Returns the current udfpsOverlayParams */
49     val udfpsOverlayParams: StateFlow<UdfpsOverlayParams> =
50         ConflatedCallbackFlow.conflatedCallbackFlow {
51                 val callback =
52                     object : AuthController.Callback {
53                         override fun onUdfpsLocationChanged(
54                             udfpsOverlayParams: UdfpsOverlayParams
55                         ) {
56                             trySendWithFailureLogging(
57                                 udfpsOverlayParams,
58                                 TAG,
59                                 "update udfpsOverlayParams"
60                             )
61                         }
62                     }
63                 authController.addCallback(callback)
64                 awaitClose { authController.removeCallback(callback) }
65             }
66             .stateIn(scope, started = SharingStarted.Eagerly, initialValue = UdfpsOverlayParams())
67 
68     companion object {
69         private const val TAG = "UdfpsOverlayInteractor"
70     }
71 }
72