Lucas Dupin | c9c2c8f | 2019-06-26 16:32:56 -0700 | [diff] [blame] | 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() { |
| 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 | } |