blob: f4635d1270a8a9560c77a3f85d7c8ecb174df5d5 [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
19import android.content.Context
20import android.hardware.Sensor
21import android.hardware.TriggerEvent
22import android.hardware.TriggerEventListener
23import com.android.keyguard.KeyguardUpdateMonitor
24import com.android.keyguard.KeyguardUpdateMonitorCallback
25import com.android.systemui.plugins.statusbar.StatusBarStateController
26import com.android.systemui.util.Assert
27import com.android.systemui.util.AsyncSensorManager
28
29class KeyguardLiftController constructor(
30 context: Context,
31 private val statusBarStateController: StatusBarStateController,
32 private val asyncSensorManager: AsyncSensorManager
33) : StatusBarStateController.StateListener, KeyguardUpdateMonitorCallback() {
34
35 private val keyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(context)
36 private val pickupSensor = asyncSensorManager.getDefaultSensor(Sensor.TYPE_PICK_UP_GESTURE)
37 private var isListening = false
38 private var bouncerVisible = false
39
40 init {
41 statusBarStateController.addCallback(this)
42 keyguardUpdateMonitor.registerCallback(this)
43 updateListeningState()
44 }
45
46 private val listener: TriggerEventListener = object : TriggerEventListener() {
47 override fun onTrigger(event: TriggerEvent?) {
48 Assert.isMainThread()
49 // Not listening anymore since trigger events unregister themselves
50 isListening = false
51 updateListeningState()
52 keyguardUpdateMonitor.requestFaceAuth()
53 }
54 }
55
56 override fun onDozingChanged(isDozing: Boolean) {
57 updateListeningState()
58 }
59
60 override fun onKeyguardBouncerChanged(bouncer: Boolean) {
61 bouncerVisible = bouncer
62 updateListeningState()
63 }
64
65 override fun onKeyguardVisibilityChanged(showing: Boolean) {
66 updateListeningState()
67 }
68
69 private fun updateListeningState() {
70 val onKeyguard = keyguardUpdateMonitor.isKeyguardVisible &&
71 !statusBarStateController.isDozing
72
73 val shouldListen = onKeyguard || bouncerVisible
74 if (shouldListen != isListening) {
75 isListening = shouldListen
76
77 if (shouldListen) {
78 asyncSensorManager.requestTriggerSensor(listener, pickupSensor)
79 } else {
80 asyncSensorManager.cancelTriggerSensor(listener, pickupSensor)
81 }
82 }
83 }
84}