• 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 android.view.accessibility.AccessibilityNodeInfo
22 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
23 import androidx.lifecycle.Lifecycle
24 import androidx.lifecycle.repeatOnLifecycle
25 import com.android.app.tracing.coroutines.launchTraced as launch
26 import com.android.systemui.common.ui.view.TouchHandlingView
27 import com.android.systemui.keyguard.ui.viewmodel.KeyguardTouchHandlingViewModel
28 import com.android.systemui.lifecycle.repeatWhenAttached
29 import com.android.systemui.plugins.FalsingManager
30 import com.android.systemui.res.R
31 
32 object KeyguardTouchViewBinder {
33     /**
34      * Drives UI for the lock screen long-press feature.
35      *
36      * @param view The view that listens for long-presses.
37      * @param viewModel The view-model that models the UI state.
38      * @param onSingleTap A callback to invoke when the system decides that there was a single tap.
39      * @param falsingManager [FalsingManager] for making sure the long-press didn't just happen in
40      *   the user's pocket.
41      */
42     @JvmStatic
43     fun bind(
44         view: TouchHandlingView,
45         viewModel: KeyguardTouchHandlingViewModel,
46         onSingleTap: (x: Int, y: Int) -> Unit,
47         falsingManager: FalsingManager,
48     ) {
49         view.accessibilityHintLongPressAction =
50             AccessibilityNodeInfo.AccessibilityAction(
51                 AccessibilityNodeInfoCompat.ACTION_LONG_CLICK,
52                 view.resources.getString(R.string.lock_screen_settings),
53             )
54         view.listener =
55             object : TouchHandlingView.Listener {
56                 override fun onLongPressDetected(
57                     view: View,
58                     x: Int,
59                     y: Int,
60                     isA11yAction: Boolean,
61                 ) {
62                     if (
63                         !isA11yAction && falsingManager.isFalseLongTap(FalsingManager.LOW_PENALTY)
64                     ) {
65                         return
66                     }
67 
68                     viewModel.onLongPress(isA11yAction)
69                 }
70 
71                 override fun onSingleTapDetected(view: View, x: Int, y: Int) {
72                     if (falsingManager.isFalseTap(FalsingManager.LOW_PENALTY)) {
73                         return
74                     }
75 
76                     onSingleTap(x, y)
77                 }
78 
79                 override fun onDoubleTapDetected(view: View) {
80                     if (falsingManager.isFalseDoubleTap()) {
81                         return
82                     }
83                     viewModel.onDoubleClick()
84                 }
85             }
86 
87         view.repeatWhenAttached {
88             repeatOnLifecycle(Lifecycle.State.STARTED) {
89                 launch("$TAG#viewModel.isLongPressHandlingEnabled") {
90                     viewModel.isLongPressHandlingEnabled.collect { isEnabled ->
91                         view.setLongPressHandlingEnabled(isEnabled)
92                         view.contentDescription =
93                             if (isEnabled) {
94                                 view.resources.getString(R.string.accessibility_desc_lock_screen)
95                             } else {
96                                 null
97                             }
98                     }
99                 }
100                 launch("$TAG#viewModel.isDoubleTapHandlingEnabled") {
101                     viewModel.isDoubleTapHandlingEnabled.collect { isEnabled ->
102                         view.setDoublePressHandlingEnabled(isEnabled)
103                         view.contentDescription =
104                             if (isEnabled) {
105                                 view.resources.getString(R.string.accessibility_desc_lock_screen)
106                             } else {
107                                 null
108                             }
109                     }
110                 }
111             }
112         }
113     }
114 
115     private const val TAG = "KeyguardTouchViewBinder"
116 }
117