1 /*
2  * Copyright (C) 2021 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.biometrics
18 
19 import android.content.Context
20 import android.hardware.display.DisplayManager
21 import android.hardware.fingerprint.FingerprintSensorPropertiesInternal
22 import android.os.Handler
23 import android.view.DisplayInfo
24 import com.android.app.tracing.traceSection
25 import com.android.systemui.biometrics.BiometricDisplayListener.SensorType.Generic
26 
27 /**
28  * A listener for keeping overlays for biometric sensors aligned with the physical device
29  * device's screen. The [onChanged] will be dispatched on the [handler]
30  * whenever a relevant change to the device's configuration (orientation, fold, display change,
31  * etc.) may require the UI to change for the given [sensorType].
32  */
33 class BiometricDisplayListener(
34     private val context: Context,
35     private val displayManager: DisplayManager,
36     private val handler: Handler,
37     private val sensorType: SensorType = SensorType.Generic,
38     private val onChanged: () -> Unit
39 ) : DisplayManager.DisplayListener {
40 
41     private var cachedDisplayInfo = DisplayInfo()
42 
onDisplayAddednull43     override fun onDisplayAdded(displayId: Int) {}
onDisplayRemovednull44     override fun onDisplayRemoved(displayId: Int) {}
onDisplayChangednull45     override fun onDisplayChanged(displayId: Int) {
46         traceSection({ "BiometricDisplayListener($sensorType)#onDisplayChanged" }) {
47             val rotationChanged = didRotationChange()
48 
49             when (sensorType) {
50                 is SensorType.SideFingerprint -> onChanged()
51                 else -> {
52                     if (rotationChanged) {
53                         onChanged()
54                     }
55                 }
56             }
57         }
58     }
59 
didRotationChangenull60     private fun didRotationChange(): Boolean {
61         val last = cachedDisplayInfo.rotation
62         context.display?.getDisplayInfo(cachedDisplayInfo)
63         return last != cachedDisplayInfo.rotation
64     }
65 
66     /** Listen for changes. */
enablenull67     fun enable() {
68         context.display?.getDisplayInfo(cachedDisplayInfo)
69         displayManager.registerDisplayListener(
70             this,
71             handler,
72             DisplayManager.EVENT_FLAG_DISPLAY_CHANGED
73         )
74     }
75 
76     /** Stop listening for changes. */
disablenull77     fun disable() {
78         displayManager.unregisterDisplayListener(this)
79     }
80 
81     /**
82      * Type of sensor to determine what kind of display changes require layouts.
83      *
84      * The [Generic] type should be used in cases where the modality can vary, such as
85      * biometric prompt (and this object will likely change as multi-mode auth is added).
86      */
87     sealed class SensorType {
88         data object Generic : SensorType()
89         data object UnderDisplayFingerprint : SensorType()
90         data class SideFingerprint(
91             val properties: FingerprintSensorPropertiesInternal
92         ) : SensorType()
93     }
94 }
95