1 /*
<lambda>null2  * 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.graphics.Bitmap
20 import android.hardware.biometrics.BiometricManager.Authenticators
21 import android.hardware.biometrics.ComponentInfoInternal
22 import android.hardware.biometrics.PromptContentView
23 import android.hardware.biometrics.PromptInfo
24 import android.hardware.biometrics.SensorProperties
25 import android.hardware.biometrics.SensorPropertiesInternal
26 import android.hardware.face.FaceSensorProperties
27 import android.hardware.face.FaceSensorPropertiesInternal
28 import android.hardware.fingerprint.FingerprintSensorProperties
29 import android.hardware.fingerprint.FingerprintSensorPropertiesInternal
30 
31 /** Create [FingerprintSensorPropertiesInternal] for a test. */
32 internal fun fingerprintSensorPropertiesInternal(
33     ids: List<Int> = listOf(0),
34     strong: Boolean = true,
35     sensorType: Int = FingerprintSensorProperties.TYPE_REAR
36 ): List<FingerprintSensorPropertiesInternal> {
37     val componentInfo =
38         listOf(
39             ComponentInfoInternal(
40                 "fingerprintSensor" /* componentId */,
41                 "vendor/model/revision" /* hardwareVersion */,
42                 "1.01" /* firmwareVersion */,
43                 "00000001" /* serialNumber */,
44                 "" /* softwareVersion */
45             ),
46             ComponentInfoInternal(
47                 "matchingAlgorithm" /* componentId */,
48                 "" /* hardwareVersion */,
49                 "" /* firmwareVersion */,
50                 "" /* serialNumber */,
51                 "vendor/version/revision" /* softwareVersion */
52             )
53         )
54     return ids.map { id ->
55         FingerprintSensorPropertiesInternal(
56             id,
57             if (strong) SensorProperties.STRENGTH_STRONG else SensorProperties.STRENGTH_WEAK,
58             5 /* maxEnrollmentsPerUser */,
59             componentInfo,
60             sensorType,
61             false /* resetLockoutRequiresHardwareAuthToken */
62         )
63     }
64 }
65 
66 /** Create [FaceSensorPropertiesInternal] for a test. */
faceSensorPropertiesInternalnull67 internal fun faceSensorPropertiesInternal(
68     ids: List<Int> = listOf(1),
69     strong: Boolean = true,
70 ): List<FaceSensorPropertiesInternal> {
71     val componentInfo =
72         listOf(
73             ComponentInfoInternal(
74                 "faceSensor" /* componentId */,
75                 "vendor/model/revision" /* hardwareVersion */,
76                 "1.01" /* firmwareVersion */,
77                 "00000001" /* serialNumber */,
78                 "" /* softwareVersion */
79             ),
80             ComponentInfoInternal(
81                 "matchingAlgorithm" /* componentId */,
82                 "" /* hardwareVersion */,
83                 "" /* firmwareVersion */,
84                 "" /* serialNumber */,
85                 "vendor/version/revision" /* softwareVersion */
86             )
87         )
88     return ids.map { id ->
89         FaceSensorPropertiesInternal(
90             id,
91             if (strong) SensorProperties.STRENGTH_STRONG else SensorProperties.STRENGTH_WEAK,
92             2 /* maxEnrollmentsPerUser */,
93             componentInfo,
94             FaceSensorProperties.TYPE_RGB,
95             true /* supportsFaceDetection */,
96             true /* supportsSelfIllumination */,
97             false /* resetLockoutRequiresHardwareAuthToken */
98         )
99     }
100 }
101 
102 @Authenticators.Types
extractAuthenticatorTypesnull103 internal fun Collection<SensorPropertiesInternal?>.extractAuthenticatorTypes(): Int {
104     var authenticators = Authenticators.EMPTY_SET
105     mapNotNull { it?.sensorStrength }
106         .forEach { strength ->
107             authenticators =
108                 authenticators or
109                     when (strength) {
110                         SensorProperties.STRENGTH_CONVENIENCE ->
111                             Authenticators.BIOMETRIC_CONVENIENCE
112                         SensorProperties.STRENGTH_WEAK -> Authenticators.BIOMETRIC_WEAK
113                         SensorProperties.STRENGTH_STRONG -> Authenticators.BIOMETRIC_STRONG
114                         else -> Authenticators.EMPTY_SET
115                     }
116         }
117     return authenticators
118 }
119 
promptInfonull120 internal fun promptInfo(
121     logoRes: Int = -1,
122     logoBitmap: Bitmap? = null,
123     logoDescription: String? = null,
124     title: String = "title",
125     subtitle: String = "sub",
126     description: String = "desc",
127     contentView: PromptContentView? = null,
128     credentialTitle: String? = "cred title",
129     credentialSubtitle: String? = "cred sub",
130     credentialDescription: String? = "cred desc",
131     negativeButton: String = "neg",
132 ): PromptInfo {
133     val info = PromptInfo()
134     if (logoBitmap != null) {
135         info.setLogo(logoRes, logoBitmap)
136     }
137     info.logoDescription = logoDescription
138     info.title = title
139     info.subtitle = subtitle
140     info.description = description
141     info.contentView = contentView
142     credentialTitle?.let { info.deviceCredentialTitle = it }
143     credentialSubtitle?.let { info.deviceCredentialSubtitle = it }
144     credentialDescription?.let { info.deviceCredentialDescription = it }
145     info.negativeButtonText = negativeButton
146     return info
147 }
148