1 /*
<lambda>null2  * 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.ui.binder
18 
19 import android.util.Log
20 import androidx.core.view.isInvisible
21 import androidx.lifecycle.Lifecycle
22 import androidx.lifecycle.repeatOnLifecycle
23 import com.android.systemui.biometrics.domain.interactor.UdfpsOverlayInteractor
24 import com.android.systemui.biometrics.ui.view.UdfpsTouchOverlay
25 import com.android.systemui.biometrics.ui.viewmodel.UdfpsTouchOverlayViewModel
26 import com.android.systemui.deviceentry.shared.DeviceEntryUdfpsRefactor
27 import com.android.systemui.lifecycle.repeatWhenAttached
28 import kotlinx.coroutines.ExperimentalCoroutinesApi
29 import kotlinx.coroutines.launch
30 
31 @ExperimentalCoroutinesApi
32 object UdfpsTouchOverlayBinder {
33 
34     /**
35      * Updates visibility for the UdfpsTouchOverlay which controls whether the view will receive
36      * touches or not. For some devices, this is instead handled by UdfpsOverlayInteractor, so this
37      * viewBinder will send the information to the interactor.
38      */
39     @JvmStatic
40     fun bind(
41         view: UdfpsTouchOverlay,
42         viewModel: UdfpsTouchOverlayViewModel,
43         udfpsOverlayInteractor: UdfpsOverlayInteractor,
44     ) {
45         if (DeviceEntryUdfpsRefactor.isUnexpectedlyInLegacyMode()) return
46         view.repeatWhenAttached {
47             repeatOnLifecycle(Lifecycle.State.CREATED) {
48                 launch {
49                         viewModel.shouldHandleTouches.collect { shouldHandleTouches ->
50                             Log.d(
51                                 "UdfpsTouchOverlayBinder",
52                                 "[$view]: update shouldHandleTouches=$shouldHandleTouches"
53                             )
54                             view.isInvisible = !shouldHandleTouches
55                             udfpsOverlayInteractor.setHandleTouches(shouldHandleTouches)
56                         }
57                     }
58                     .invokeOnCompletion {
59                         Log.d(
60                             "UdfpsTouchOverlayBinder",
61                             "[$view-detached]: update shouldHandleTouches=false"
62                         )
63                         udfpsOverlayInteractor.setHandleTouches(false)
64                     }
65             }
66         }
67     }
68 }
69