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.shade
18 
19 import android.os.PowerManager
20 import android.view.GestureDetector
21 import android.view.MotionEvent
22 import com.android.systemui.bouncer.domain.interactor.PrimaryBouncerInteractor
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.keyguard.data.repository.KeyguardRepository
25 import com.android.systemui.plugins.FalsingManager
26 import com.android.systemui.plugins.FalsingManager.LOW_PENALTY
27 import com.android.systemui.plugins.statusbar.StatusBarStateController
28 import com.android.systemui.power.domain.interactor.PowerInteractor
29 import com.android.systemui.statusbar.StatusBarState
30 import javax.inject.Inject
31 
32 /**
33  * This gestureListener will wake up by tap when the device is dreaming but not dozing, and the
34  * selected screensaver is hosted in lockscreen. Tap is gated by the falsing manager.
35  *
36  * Touches go through the [NotificationShadeWindowViewController].
37  */
38 @SysUISingleton
39 class LockscreenHostedDreamGestureListener
40 @Inject
41 constructor(
42     private val falsingManager: FalsingManager,
43     private val powerInteractor: PowerInteractor,
44     private val statusBarStateController: StatusBarStateController,
45     private val primaryBouncerInteractor: PrimaryBouncerInteractor,
46     private val keyguardRepository: KeyguardRepository,
47     private val shadeLogger: ShadeLogger,
48 ) : GestureDetector.SimpleOnGestureListener() {
49     private val TAG = this::class.simpleName
50 
onSingleTapUpnull51     override fun onSingleTapUp(e: MotionEvent): Boolean {
52         if (shouldHandleMotionEvent()) {
53             if (!falsingManager.isFalseTap(LOW_PENALTY)) {
54                 shadeLogger.d("$TAG#onSingleTapUp tap handled, requesting wakeUpIfDreaming")
55                 powerInteractor.wakeUpIfDreaming(
56                     "DREAMING_SINGLE_TAP",
57                     PowerManager.WAKE_REASON_TAP
58                 )
59             } else {
60                 shadeLogger.d("$TAG#onSingleTapUp false tap ignored")
61             }
62             return true
63         }
64         return false
65     }
66 
shouldHandleMotionEventnull67     private fun shouldHandleMotionEvent(): Boolean {
68         return keyguardRepository.isActiveDreamLockscreenHosted.value &&
69             statusBarStateController.state == StatusBarState.KEYGUARD &&
70             !primaryBouncerInteractor.isBouncerShowing()
71     }
72 }
73