Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 825a9c8b authored by Chandru S's avatar Chandru S
Browse files

Add FaceSensorRepository to provide the face sensor state when it becomes available.

Following the pattern followed by FingerprintPropertyRepository

Bug: 275788040
Test: atest FaceSensorRepositoryImplTest
Change-Id: I261a3f3ef2d11a7842316e4bb5397012b31df2c7
parent 98be6f49
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package com.android.systemui.biometrics.dagger

import com.android.settingslib.udfps.UdfpsUtils
import com.android.systemui.biometrics.data.repository.FacePropertyRepository
import com.android.systemui.biometrics.data.repository.FacePropertyRepositoryImpl
import com.android.systemui.biometrics.data.repository.FaceSettingsRepository
import com.android.systemui.biometrics.data.repository.FaceSettingsRepositoryImpl
import com.android.systemui.biometrics.data.repository.FingerprintPropertyRepository
@@ -51,6 +53,10 @@ interface BiometricsModule {
    @SysUISingleton
    fun faceSettings(impl: FaceSettingsRepositoryImpl): FaceSettingsRepository

    @Binds
    @SysUISingleton
    fun faceSensors(impl: FacePropertyRepositoryImpl): FacePropertyRepository

    @Binds
    @SysUISingleton
    fun biometricPromptRepository(impl: PromptRepositoryImpl): PromptRepository
+81 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.systemui.biometrics.data.repository

import android.hardware.face.FaceManager
import android.hardware.face.FaceSensorPropertiesInternal
import android.hardware.face.IFaceAuthenticatorsRegisteredCallback
import com.android.systemui.biometrics.shared.model.SensorStrength
import com.android.systemui.biometrics.shared.model.toSensorStrength
import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
import com.android.systemui.common.coroutine.ConflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn

/** A repository for the global state of Face sensor. */
interface FacePropertyRepository {
    /** Face sensor information, null if it is not available. */
    val sensorInfo: Flow<FaceSensorInfo?>
}

/** Describes a biometric sensor */
data class FaceSensorInfo(val id: Int, val strength: SensorStrength)

private const val TAG = "FaceSensorPropertyRepositoryImpl"

@SysUISingleton
class FacePropertyRepositoryImpl
@Inject
constructor(@Application private val applicationScope: CoroutineScope, faceManager: FaceManager?) :
    FacePropertyRepository {

    private val sensorProps: Flow<List<FaceSensorPropertiesInternal>> =
        faceManager?.let {
            ConflatedCallbackFlow.conflatedCallbackFlow {
                    val callback =
                        object : IFaceAuthenticatorsRegisteredCallback.Stub() {
                            override fun onAllAuthenticatorsRegistered(
                                sensors: List<FaceSensorPropertiesInternal>
                            ) {
                                trySendWithFailureLogging(
                                    sensors,
                                    TAG,
                                    "onAllAuthenticatorsRegistered"
                                )
                            }
                        }
                    it.addAuthenticatorsRegisteredCallback(callback)
                    awaitClose {}
                }
                .shareIn(applicationScope, SharingStarted.Eagerly)
        }
            ?: flowOf(emptyList())

    override val sensorInfo: Flow<FaceSensorInfo?> =
        sensorProps
            .map { it.firstOrNull() }
            .map { it?.let { FaceSensorInfo(it.sensorId, it.sensorStrength.toSensorStrength()) } }
}
+2 −10
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.hardware.fingerprint.FingerprintSensorPropertiesInternal
import android.hardware.fingerprint.IFingerprintAuthenticatorsRegisteredCallback
import com.android.systemui.biometrics.shared.model.FingerprintSensorType
import com.android.systemui.biometrics.shared.model.SensorStrength
import com.android.systemui.biometrics.shared.model.toSensorStrength
import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
@@ -106,7 +107,7 @@ constructor(

    private fun setProperties(prop: FingerprintSensorPropertiesInternal) {
        _sensorId.value = prop.sensorId
        _strength.value = sensorStrengthIntToObject(prop.sensorStrength)
        _strength.value = prop.sensorStrength.toSensorStrength()
        _sensorType.value = sensorTypeIntToObject(prop.sensorType)
        _sensorLocations.value =
            prop.allLocations.associateBy { sensorLocationInternal ->
@@ -119,15 +120,6 @@ constructor(
    }
}

private fun sensorStrengthIntToObject(value: Int): SensorStrength {
    return when (value) {
        0 -> SensorStrength.CONVENIENCE
        1 -> SensorStrength.WEAK
        2 -> SensorStrength.STRONG
        else -> throw IllegalArgumentException("Invalid SensorStrength value: $value")
    }
}

private fun sensorTypeIntToObject(value: Int): FingerprintSensorType {
    return when (value) {
        0 -> FingerprintSensorType.UNKNOWN
+10 −1
Original line number Diff line number Diff line
@@ -18,9 +18,18 @@ package com.android.systemui.biometrics.shared.model

import android.hardware.biometrics.SensorProperties

/** Fingerprint sensor security strength. Represents [SensorProperties.Strength]. */
/** Sensor security strength. Represents [SensorProperties.Strength]. */
enum class SensorStrength {
    CONVENIENCE,
    WEAK,
    STRONG,
}

/** Convert [this] to corresponding [SensorStrength] */
fun Int.toSensorStrength(): SensorStrength =
    when (this) {
        0 -> SensorStrength.CONVENIENCE
        1 -> SensorStrength.WEAK
        2 -> SensorStrength.STRONG
        else -> throw IllegalArgumentException("Invalid SensorStrength value: $this")
    }
+91 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.systemui.biometrics.data.repository

import android.hardware.biometrics.SensorProperties
import android.hardware.face.FaceManager
import android.hardware.face.FaceSensorPropertiesInternal
import android.hardware.face.IFaceAuthenticatorsRegisteredCallback
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.biometrics.shared.model.SensorStrength
import com.android.systemui.coroutines.collectLastValue
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.ArgumentCaptor
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(JUnit4::class)
class FacePropertyRepositoryImplTest : SysuiTestCase() {
    @JvmField @Rule val mockitoRule: MockitoRule = MockitoJUnit.rule()

    private lateinit var underTest: FacePropertyRepository
    private lateinit var testScope: TestScope

    @Captor private lateinit var callback: ArgumentCaptor<IFaceAuthenticatorsRegisteredCallback>
    @Mock private lateinit var faceManager: FaceManager
    @Before
    fun setup() {
        testScope = TestScope()
        underTest = createRepository(faceManager)
    }

    private fun createRepository(manager: FaceManager? = faceManager) =
        FacePropertyRepositoryImpl(testScope.backgroundScope, manager)

    @Test
    fun whenFaceManagerIsNotPresentIsNull() =
        testScope.runTest {
            underTest = createRepository(null)
            val sensor = collectLastValue(underTest.sensorInfo)

            assertThat(sensor()).isNull()
        }

    @Test
    fun providesTheValuePassedToTheAuthenticatorsRegisteredCallback() {
        testScope.runTest {
            val sensor by collectLastValue(underTest.sensorInfo)
            runCurrent()
            verify(faceManager).addAuthenticatorsRegisteredCallback(callback.capture())

            callback.value.onAllAuthenticatorsRegistered(
                listOf(createSensorProperties(1, SensorProperties.STRENGTH_STRONG))
            )

            assertThat(sensor).isEqualTo(FaceSensorInfo(1, SensorStrength.STRONG))
        }
    }

    private fun createSensorProperties(id: Int, strength: Int) =
        FaceSensorPropertiesInternal(id, strength, 0, emptyList(), 1, false, false, false)
}
Loading