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.annotation.IdRes
20 import android.content.Context
21 import android.hardware.biometrics.BiometricManager
22 import android.hardware.biometrics.ComponentInfoInternal
23 import android.hardware.biometrics.PromptInfo
24 import android.hardware.biometrics.SensorProperties
25 import android.hardware.face.FaceSensorPropertiesInternal
26 import android.hardware.face.FaceSensorProperties
27 import android.hardware.fingerprint.FingerprintSensorProperties
28 import android.hardware.fingerprint.FingerprintSensorPropertiesInternal
29 import android.os.Bundle
30
31 import android.testing.ViewUtils
32 import android.view.LayoutInflater
33
34 /**
35 * Inflate the given BiometricPrompt layout and initialize it with test parameters.
36 *
37 * This attaches the view so be sure to call [destroyDialog] at the end of the test.
38 */
39 @IdRes
40 internal fun <T : AuthBiometricView> Int.asTestAuthBiometricView(
41 context: Context,
42 callback: AuthBiometricView.Callback,
43 panelController: AuthPanelController,
44 allowDeviceCredential: Boolean = false,
45 savedState: Bundle? = null,
46 hideDelay: Int = 0
47 ): T {
48 val view = LayoutInflater.from(context).inflate(this, null, false) as T
49 view.mAnimationDurationLong = 0
50 view.mAnimationDurationShort = 0
51 view.mAnimationDurationHideDialog = hideDelay
52 view.setPromptInfo(buildPromptInfo(allowDeviceCredential))
53 view.setCallback(callback)
54 view.restoreState(savedState)
55 view.setPanelController(panelController)
56
57 ViewUtils.attachView(view)
58
59 return view
60 }
61
buildPromptInfonull62 private fun buildPromptInfo(allowDeviceCredential: Boolean): PromptInfo {
63 val promptInfo = PromptInfo()
64 promptInfo.title = "Title"
65 var authenticators = BiometricManager.Authenticators.BIOMETRIC_WEAK
66 if (allowDeviceCredential) {
67 authenticators = authenticators or BiometricManager.Authenticators.DEVICE_CREDENTIAL
68 } else {
69 promptInfo.negativeButtonText = "Negative"
70 }
71 promptInfo.authenticators = authenticators
72 return promptInfo
73 }
74
75 /** Detach the view, if needed. */
destroyDialognull76 internal fun AuthBiometricView?.destroyDialog() {
77 if (this != null && isAttachedToWindow) {
78 ViewUtils.detachView(this)
79 }
80 }
81
82 /** Create [FingerprintSensorPropertiesInternal] for a test. */
fingerprintSensorPropertiesInternalnull83 internal fun fingerprintSensorPropertiesInternal(
84 ids: List<Int> = listOf(0)
85 ): List<FingerprintSensorPropertiesInternal> {
86 val componentInfo = listOf(
87 ComponentInfoInternal(
88 "fingerprintSensor" /* componentId */,
89 "vendor/model/revision" /* hardwareVersion */, "1.01" /* firmwareVersion */,
90 "00000001" /* serialNumber */, "" /* softwareVersion */
91 ),
92 ComponentInfoInternal(
93 "matchingAlgorithm" /* componentId */,
94 "" /* hardwareVersion */, "" /* firmwareVersion */, "" /* serialNumber */,
95 "vendor/version/revision" /* softwareVersion */
96 )
97 )
98 return ids.map { id ->
99 FingerprintSensorPropertiesInternal(
100 id,
101 SensorProperties.STRENGTH_STRONG,
102 5 /* maxEnrollmentsPerUser */,
103 componentInfo,
104 FingerprintSensorProperties.TYPE_REAR,
105 false /* resetLockoutRequiresHardwareAuthToken */
106 )
107 }
108 }
109
110 /** Create [FaceSensorPropertiesInternal] for a test. */
faceSensorPropertiesInternalnull111 internal fun faceSensorPropertiesInternal(
112 ids: List<Int> = listOf(1)
113 ): List<FaceSensorPropertiesInternal> {
114 val componentInfo = listOf(
115 ComponentInfoInternal(
116 "faceSensor" /* componentId */,
117 "vendor/model/revision" /* hardwareVersion */, "1.01" /* firmwareVersion */,
118 "00000001" /* serialNumber */, "" /* softwareVersion */
119 ),
120 ComponentInfoInternal(
121 "matchingAlgorithm" /* componentId */,
122 "" /* hardwareVersion */, "" /* firmwareVersion */, "" /* serialNumber */,
123 "vendor/version/revision" /* softwareVersion */
124 )
125 )
126 return ids.map { id ->
127 FaceSensorPropertiesInternal(
128 id,
129 SensorProperties.STRENGTH_STRONG,
130 2 /* maxEnrollmentsPerUser */,
131 componentInfo,
132 FaceSensorProperties.TYPE_RGB,
133 true /* supportsFaceDetection */,
134 true /* supportsSelfIllumination */,
135 false /* resetLockoutRequiresHardwareAuthToken */
136 )
137 }
138 }
139