blob: 74d9f5402e366796073680c63231916dfe94ff99 [file] [log] [blame]
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -07001/*
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
17package com.android.systemui.statusbar.phone
18
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070019import android.hardware.Sensor
20import android.hardware.TriggerEvent
21import android.hardware.TriggerEventListener
22import com.android.keyguard.KeyguardUpdateMonitor
23import com.android.keyguard.KeyguardUpdateMonitorCallback
Lucas Dupin64171fe2019-10-30 14:28:29 -070024import com.android.systemui.Dumpable
Ned Burnsaaeb44b2020-02-12 23:48:26 -050025import com.android.systemui.dump.DumpManager
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070026import com.android.systemui.plugins.statusbar.StatusBarStateController
27import com.android.systemui.util.Assert
Dave Mankoff63a12822019-09-16 14:38:06 -040028import com.android.systemui.util.sensors.AsyncSensorManager
Lucas Dupin64171fe2019-10-30 14:28:29 -070029import java.io.FileDescriptor
30import java.io.PrintWriter
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070031
32class KeyguardLiftController constructor(
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070033 private val statusBarStateController: StatusBarStateController,
Dave Mankoff80c612b2019-10-23 17:47:24 -040034 private val asyncSensorManager: AsyncSensorManager,
Lucas Dupin64171fe2019-10-30 14:28:29 -070035 private val keyguardUpdateMonitor: KeyguardUpdateMonitor,
Ned Burnsaaeb44b2020-02-12 23:48:26 -050036 dumpManager: DumpManager
Lucas Dupin64171fe2019-10-30 14:28:29 -070037) : StatusBarStateController.StateListener, Dumpable, KeyguardUpdateMonitorCallback() {
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070038
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070039 private val pickupSensor = asyncSensorManager.getDefaultSensor(Sensor.TYPE_PICK_UP_GESTURE)
40 private var isListening = false
41 private var bouncerVisible = false
42
43 init {
Ned Burnsaaeb44b2020-02-12 23:48:26 -050044 dumpManager.registerDumpable(javaClass.name, this)
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070045 statusBarStateController.addCallback(this)
46 keyguardUpdateMonitor.registerCallback(this)
47 updateListeningState()
48 }
49
50 private val listener: TriggerEventListener = object : TriggerEventListener() {
51 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
60 override fun onDozingChanged(isDozing: Boolean) {
61 updateListeningState()
62 }
63
64 override fun onKeyguardBouncerChanged(bouncer: Boolean) {
65 bouncerVisible = bouncer
66 updateListeningState()
67 }
68
69 override fun onKeyguardVisibilityChanged(showing: Boolean) {
70 updateListeningState()
71 }
72
Lucas Dupin64171fe2019-10-30 14:28:29 -070073 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
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070080 private fun updateListeningState() {
Lucas Dupin3b745dd2019-09-23 11:54:56 -070081 if (pickupSensor == null) {
82 return
83 }
Lucas Dupinc9c2c8f2019-06-26 16:32:56 -070084 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}