• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.systemui.common.ui.view.LongPressHandlingView
24 import com.android.systemui.keyguard.ui.viewmodel.KeyguardLongPressViewModel
25 import com.android.systemui.lifecycle.repeatWhenAttached
26 import com.android.systemui.plugins.FalsingManager
27 import kotlinx.coroutines.launch
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                         x = x,
55                         y = y,
56                     )
57                 }
58 
59                 override fun onSingleTapDetected(view: View) {
60                     if (falsingManager.isFalseTap(FalsingManager.LOW_PENALTY)) {
61                         return
62                     }
63 
64                     onSingleTap()
65                 }
66             }
67 
68         view.repeatWhenAttached {
69             repeatOnLifecycle(Lifecycle.State.STARTED) {
70                 launch {
71                     viewModel.isLongPressHandlingEnabled.collect { isEnabled ->
72                         view.setLongPressHandlingEnabled(isEnabled)
73                     }
74                 }
75 
76                 launch {
77                     var dismissMenu: (() -> Unit)? = null
78 
79                     viewModel.menu.collect { menuOrNull ->
80                         if (menuOrNull != null) {
81                             dismissMenu =
82                                 KeyguardLongPressPopupViewBinder.createAndShow(
83                                     container = view,
84                                     viewModel = menuOrNull,
85                                     onDismissed = menuOrNull.onDismissed,
86                                 )
87                         } else {
88                             dismissMenu?.invoke()
89                         }
90                     }
91                 }
92             }
93         }
94     }
95 }
96