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 
18 package com.android.systemui.keyguard.ui.binder
19 
20 import android.view.View
21 import androidx.lifecycle.Lifecycle
22 import androidx.lifecycle.repeatOnLifecycle
23 import com.android.app.tracing.coroutines.launch
24 import com.android.systemui.common.ui.view.LongPressHandlingView
25 import com.android.systemui.keyguard.ui.viewmodel.KeyguardLongPressViewModel
26 import com.android.systemui.lifecycle.repeatWhenAttached
27 import com.android.systemui.plugins.FalsingManager
28 
29 object KeyguardLongPressViewBinder {
30     /**
31      * Drives UI for the lock screen long-press feature.
32      *
33      * @param view The view that listens for long-presses.
34      * @param viewModel The view-model that models the UI state.
35      * @param onSingleTap A callback to invoke when the system decides that there was a single tap.
36      * @param falsingManager [FalsingManager] for making sure the long-press didn't just happen in
37      *   the user's pocket.
38      */
39     @JvmStatic
40     fun bind(
41         view: LongPressHandlingView,
42         viewModel: KeyguardLongPressViewModel,
43         onSingleTap: () -> Unit,
44         falsingManager: FalsingManager,
45     ) {
46         view.listener =
47             object : LongPressHandlingView.Listener {
48                 override fun onLongPressDetected(view: View, x: Int, y: Int) {
49                     if (falsingManager.isFalseLongTap(FalsingManager.LOW_PENALTY)) {
50                         return
51                     }
52 
53                     viewModel.onLongPress()
54                 }
55 
56                 override fun onSingleTapDetected(view: View) {
57                     if (falsingManager.isFalseTap(FalsingManager.LOW_PENALTY)) {
58                         return
59                     }
60 
61                     onSingleTap()
62                 }
63             }
64 
65         view.repeatWhenAttached {
66             repeatOnLifecycle(Lifecycle.State.STARTED) {
67                 launch("$TAG#viewModel.isLongPressHandlingEnabled") {
68                     viewModel.isLongPressHandlingEnabled.collect { isEnabled ->
69                         view.setLongPressHandlingEnabled(isEnabled)
70                     }
71                 }
72             }
73         }
74     }
75 
76     private const val TAG = "KeyguardLongPressViewBinder"
77 }
78