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.content.Context
20 import android.hardware.Sensor
21 import android.hardware.TriggerEvent
22 import android.hardware.TriggerEventListener
23 import com.android.keyguard.KeyguardUpdateMonitor
24 import com.android.keyguard.KeyguardUpdateMonitorCallback
25 import com.android.systemui.plugins.statusbar.StatusBarStateController
26 import com.android.systemui.util.Assert
27 import com.android.systemui.util.AsyncSensorManager
28 
29 class 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() {
onTriggernull47         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 
onDozingChangednull56     override fun onDozingChanged(isDozing: Boolean) {
57         updateListeningState()
58     }
59 
onKeyguardBouncerChangednull60     override fun onKeyguardBouncerChanged(bouncer: Boolean) {
61         bouncerVisible = bouncer
62         updateListeningState()
63     }
64 
onKeyguardVisibilityChangednull65     override fun onKeyguardVisibilityChanged(showing: Boolean) {
66         updateListeningState()
67     }
68 
updateListeningStatenull69     private fun updateListeningState() {
70         if (pickupSensor == null) {
71             return
72         }
73         val onKeyguard = keyguardUpdateMonitor.isKeyguardVisible &&
74                 !statusBarStateController.isDozing
75 
76         val userId = KeyguardUpdateMonitor.getCurrentUser()
77         val isFaceEnabled = keyguardUpdateMonitor.isFaceAuthEnabledForUser(userId)
78         val shouldListen = (onKeyguard || bouncerVisible) && isFaceEnabled
79         if (shouldListen != isListening) {
80             isListening = shouldListen
81 
82             if (shouldListen) {
83                 asyncSensorManager.requestTriggerSensor(listener, pickupSensor)
84             } else {
85                 asyncSensorManager.cancelTriggerSensor(listener, pickupSensor)
86             }
87         }
88     }
89 }
90