1 /*
2  * Copyright (C) 2019 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.statusbar.phone
18 
19 import android.hardware.Sensor
20 import android.hardware.TriggerEvent
21 import android.hardware.TriggerEventListener
22 import com.android.keyguard.KeyguardUpdateMonitor
23 import com.android.keyguard.KeyguardUpdateMonitorCallback
24 import com.android.systemui.Dumpable
25 import com.android.systemui.dump.DumpManager
26 import com.android.systemui.plugins.statusbar.StatusBarStateController
27 import com.android.systemui.util.Assert
28 import com.android.systemui.util.sensors.AsyncSensorManager
29 import java.io.FileDescriptor
30 import java.io.PrintWriter
31 
32 class KeyguardLiftController constructor(
33     private val statusBarStateController: StatusBarStateController,
34     private val asyncSensorManager: AsyncSensorManager,
35     private val keyguardUpdateMonitor: KeyguardUpdateMonitor,
36     dumpManager: DumpManager
37 ) : StatusBarStateController.StateListener, Dumpable, KeyguardUpdateMonitorCallback() {
38 
39     private val pickupSensor = asyncSensorManager.getDefaultSensor(Sensor.TYPE_PICK_UP_GESTURE)
40     private var isListening = false
41     private var bouncerVisible = false
42 
43     init {
44         dumpManager.registerDumpable(javaClass.name, this)
45         statusBarStateController.addCallback(this)
46         keyguardUpdateMonitor.registerCallback(this)
47         updateListeningState()
48     }
49 
50     private val listener: TriggerEventListener = object : TriggerEventListener() {
onTriggernull51         override fun onTrigger(event: TriggerEvent?) {
52             Assert.isMainThread()
53             // Not listening anymore since trigger events unregister themselves
54             isListening = false
55             updateListeningState()
56             keyguardUpdateMonitor.requestFaceAuth()
57         }
58     }
59 
onDozingChangednull60     override fun onDozingChanged(isDozing: Boolean) {
61         updateListeningState()
62     }
63 
onKeyguardBouncerChangednull64     override fun onKeyguardBouncerChanged(bouncer: Boolean) {
65         bouncerVisible = bouncer
66         updateListeningState()
67     }
68 
onKeyguardVisibilityChangednull69     override fun onKeyguardVisibilityChanged(showing: Boolean) {
70         updateListeningState()
71     }
72 
dumpnull73     override fun dump(fd: FileDescriptor, pw: PrintWriter, args: Array<out String>) {
74         pw.println("KeyguardLiftController:")
75         pw.println("  pickupSensor: $pickupSensor")
76         pw.println("  isListening: $isListening")
77         pw.println("  bouncerVisible: $bouncerVisible")
78     }
79 
updateListeningStatenull80     private fun updateListeningState() {
81         if (pickupSensor == null) {
82             return
83         }
84         val onKeyguard = keyguardUpdateMonitor.isKeyguardVisible &&
85                 !statusBarStateController.isDozing
86 
87         val shouldListen = onKeyguard || bouncerVisible
88         if (shouldListen != isListening) {
89             isListening = shouldListen
90 
91             if (shouldListen) {
92                 asyncSensorManager.requestTriggerSensor(listener, pickupSensor)
93             } else {
94                 asyncSensorManager.cancelTriggerSensor(listener, pickupSensor)
95             }
96         }
97     }
98 }