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

Commit c94f83d9 authored by Ilya Matyukhin's avatar Ilya Matyukhin
Browse files

Add HAL-to-framework constant conversion

This CL adds functions to fingerprint's and face's AidlConversionUtils
that map HAL-level constants to the framework constants. This makes the
layers independent from the numerical values of the constants,
which makes it easier to add new constants in the future.

Bug: 181358178
Test: atest CtsBiometricsTestCases
Change-Id: Ia9864eb7dae7933cc02b2019117bc590d3f30184
parent ce09e187
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
@@ -16,10 +16,14 @@

package android.hardware.biometrics;

import android.annotation.IntDef;
import android.app.KeyguardManager;
import android.hardware.biometrics.BiometricManager.Authenticators;
import android.hardware.face.FaceManager;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Interface containing all of the face-specific constants.
 *
@@ -48,6 +52,31 @@ public interface BiometricFaceConstants {
    // Error messages from face authentication hardware during initialization, enrollment,
    // authentication or removal. Must agree with the list in HAL h file
    //

    /**
     * @hide
     */
    @IntDef({FACE_ERROR_HW_UNAVAILABLE,
            FACE_ERROR_UNABLE_TO_PROCESS,
            FACE_ERROR_TIMEOUT,
            FACE_ERROR_NO_SPACE,
            FACE_ERROR_CANCELED,
            FACE_ERROR_UNABLE_TO_REMOVE,
            FACE_ERROR_LOCKOUT,
            FACE_ERROR_VENDOR,
            FACE_ERROR_LOCKOUT_PERMANENT,
            FACE_ERROR_USER_CANCELED,
            FACE_ERROR_NOT_ENROLLED,
            FACE_ERROR_HW_NOT_PRESENT,
            FACE_ERROR_NEGATIVE_BUTTON,
            BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL,
            BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED,
            BIOMETRIC_ERROR_RE_ENROLL,
            FACE_ERROR_UNKNOWN
    })
    @Retention(RetentionPolicy.SOURCE)
    @interface FaceError {}

    /**
     * The hardware is unavailable. Try again later.
     */
@@ -158,6 +187,12 @@ public interface BiometricFaceConstants {
     */
    int BIOMETRIC_ERROR_RE_ENROLL = 16;

    /**
     * Unknown error received from the HAL.
     * @hide
     */
    int FACE_ERROR_UNKNOWN = 17;

    /**
     * @hide
     */
@@ -168,6 +203,36 @@ public interface BiometricFaceConstants {
    // existing constants. These must agree with face@1.0/types.hal.
    //

    /**
     * @hide
     */
    @IntDef({FACE_ACQUIRED_GOOD,
            FACE_ACQUIRED_INSUFFICIENT,
            FACE_ACQUIRED_TOO_BRIGHT,
            FACE_ACQUIRED_TOO_DARK,
            FACE_ACQUIRED_TOO_CLOSE,
            FACE_ACQUIRED_TOO_FAR,
            FACE_ACQUIRED_TOO_HIGH,
            FACE_ACQUIRED_TOO_LOW,
            FACE_ACQUIRED_TOO_RIGHT,
            FACE_ACQUIRED_TOO_LEFT,
            FACE_ACQUIRED_POOR_GAZE,
            FACE_ACQUIRED_NOT_DETECTED,
            FACE_ACQUIRED_TOO_MUCH_MOTION,
            FACE_ACQUIRED_RECALIBRATE,
            FACE_ACQUIRED_TOO_DIFFERENT,
            FACE_ACQUIRED_TOO_SIMILAR,
            FACE_ACQUIRED_PAN_TOO_EXTREME,
            FACE_ACQUIRED_TILT_TOO_EXTREME,
            FACE_ACQUIRED_ROLL_TOO_EXTREME,
            FACE_ACQUIRED_FACE_OBSCURED,
            FACE_ACQUIRED_START,
            FACE_ACQUIRED_SENSOR_DIRTY,
            FACE_ACQUIRED_VENDOR,
            FACE_ACQUIRED_UNKNOWN})
    @Retention(RetentionPolicy.SOURCE)
    @interface FaceAcquired {}

    /**
     * The image acquired was good.
     */
@@ -342,6 +407,12 @@ public interface BiometricFaceConstants {
     */
    int FACE_ACQUIRED_VENDOR = 22;

    /**
     * Unknown acquired code received from the HAL.
     * @hide
     */
    int FACE_ACQUIRED_UNKNOWN = 23;

    /**
     * @hide
     */
+19 −3
Original line number Diff line number Diff line
@@ -57,7 +57,10 @@ public interface BiometricFingerprintConstants {
            FINGERPRINT_ERROR_HW_NOT_PRESENT,
            FINGERPRINT_ERROR_NEGATIVE_BUTTON,
            BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL,
            BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED})
            BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED,
            BIOMETRIC_ERROR_RE_ENROLL,
            BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED,
            FINGERPRINT_ERROR_UNKNOWN})
    @Retention(RetentionPolicy.SOURCE)
    @interface FingerprintError {}

@@ -163,7 +166,7 @@ public interface BiometricFingerprintConstants {
     * sensor's strength can currently only meet {@link Authenticators#BIOMETRIC_WEAK}.
     * @hide
     */
    public static final int BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED = 15;
    int BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED = 15;

    /**
     * Authentication cannot proceed because re-enrollment is required.
@@ -171,6 +174,12 @@ public interface BiometricFingerprintConstants {
     */
    int BIOMETRIC_ERROR_RE_ENROLL = 16;

    /**
     * Unknown error received from the HAL.
     * @hide
     */
    int FINGERPRINT_ERROR_UNKNOWN = 17;

    /**
     * @hide
     */
@@ -191,7 +200,8 @@ public interface BiometricFingerprintConstants {
            FINGERPRINT_ACQUIRED_TOO_SLOW,
            FINGERPRINT_ACQUIRED_TOO_FAST,
            FINGERPRINT_ACQUIRED_VENDOR,
            FINGERPRINT_ACQUIRED_START})
            FINGERPRINT_ACQUIRED_START,
            FINGERPRINT_ACQUIRED_UNKNOWN})
    @Retention(RetentionPolicy.SOURCE)
    @interface FingerprintAcquired {}

@@ -254,6 +264,12 @@ public interface BiometricFingerprintConstants {
     */
    int FINGERPRINT_ACQUIRED_START = 7;

    /**
     * Unknown acquired code received from the HAL.
     * @hide
     */
    int FINGERPRINT_ACQUIRED_UNKNOWN = 8;

    /**
     * @hide
     */
+95 −43
Original line number Diff line number Diff line
@@ -18,10 +18,13 @@ package com.android.server.biometrics.sensors.face.aidl;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.hardware.biometrics.BiometricFaceConstants;
import android.hardware.biometrics.face.AcquiredInfo;
import android.hardware.biometrics.face.AuthenticationFrame;
import android.hardware.biometrics.face.BaseFrame;
import android.hardware.biometrics.face.Cell;
import android.hardware.biometrics.face.EnrollmentFrame;
import android.hardware.biometrics.face.Error;
import android.hardware.face.FaceAuthenticationFrame;
import android.hardware.face.FaceDataFrame;
import android.hardware.face.FaceEnrollCell;
@@ -32,38 +35,112 @@ import android.hardware.face.FaceEnrollFrame;
 */
final class AidlConversionUtils {
    // Prevent instantiation.
    private AidlConversionUtils() {}
    private AidlConversionUtils() {
    }

    @NonNull
    public static FaceAuthenticationFrame convert(@NonNull AuthenticationFrame frame) {
        return new FaceAuthenticationFrame(convert(frame.data));
    public static @BiometricFaceConstants.FaceError int toFrameworkError(byte aidlError) {
        if (aidlError == Error.UNKNOWN) {
            // No framework constant available
            return BiometricFaceConstants.FACE_ERROR_UNKNOWN;
        } else if (aidlError == Error.HW_UNAVAILABLE) {
            return BiometricFaceConstants.FACE_ERROR_HW_UNAVAILABLE;
        } else if (aidlError == Error.UNABLE_TO_PROCESS) {
            return BiometricFaceConstants.FACE_ERROR_UNABLE_TO_PROCESS;
        } else if (aidlError == Error.TIMEOUT) {
            return BiometricFaceConstants.FACE_ERROR_TIMEOUT;
        } else if (aidlError == Error.NO_SPACE) {
            return BiometricFaceConstants.FACE_ERROR_NO_SPACE;
        } else if (aidlError == Error.CANCELED) {
            return BiometricFaceConstants.FACE_ERROR_CANCELED;
        } else if (aidlError == Error.UNABLE_TO_REMOVE) {
            return BiometricFaceConstants.FACE_ERROR_UNABLE_TO_REMOVE;
        } else if (aidlError == Error.VENDOR) {
            return BiometricFaceConstants.FACE_ERROR_VENDOR;
        } else if (aidlError == Error.REENROLL_REQUIRED) {
            return BiometricFaceConstants.BIOMETRIC_ERROR_RE_ENROLL;
        } else {
            return BiometricFaceConstants.FACE_ERROR_UNKNOWN;
        }
    }

    @NonNull
    public static AuthenticationFrame convert(@NonNull FaceAuthenticationFrame frame) {
        final AuthenticationFrame convertedFrame = new AuthenticationFrame();
        convertedFrame.data = convert(frame.getData());
        return convertedFrame;
    public static @BiometricFaceConstants.FaceAcquired int toFrameworkAcquiredInfo(
            byte aidlAcquired) {
        if (aidlAcquired == AcquiredInfo.UNKNOWN) {
            return BiometricFaceConstants.FACE_ACQUIRED_UNKNOWN;
        } else if (aidlAcquired == AcquiredInfo.GOOD) {
            return BiometricFaceConstants.FACE_ACQUIRED_GOOD;
        } else if (aidlAcquired == AcquiredInfo.INSUFFICIENT) {
            return BiometricFaceConstants.FACE_ACQUIRED_INSUFFICIENT;
        } else if (aidlAcquired == AcquiredInfo.TOO_BRIGHT) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_BRIGHT;
        } else if (aidlAcquired == AcquiredInfo.TOO_DARK) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_DARK;
        } else if (aidlAcquired == AcquiredInfo.TOO_CLOSE) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_CLOSE;
        } else if (aidlAcquired == AcquiredInfo.TOO_FAR) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_FAR;
        } else if (aidlAcquired == AcquiredInfo.FACE_TOO_HIGH) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_HIGH;
        } else if (aidlAcquired == AcquiredInfo.FACE_TOO_LOW) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_LOW;
        } else if (aidlAcquired == AcquiredInfo.FACE_TOO_RIGHT) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_RIGHT;
        } else if (aidlAcquired == AcquiredInfo.FACE_TOO_LEFT) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_LEFT;
        } else if (aidlAcquired == AcquiredInfo.POOR_GAZE) {
            return BiometricFaceConstants.FACE_ACQUIRED_POOR_GAZE;
        } else if (aidlAcquired == AcquiredInfo.NOT_DETECTED) {
            return BiometricFaceConstants.FACE_ACQUIRED_NOT_DETECTED;
        } else if (aidlAcquired == AcquiredInfo.TOO_MUCH_MOTION) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_MUCH_MOTION;
        } else if (aidlAcquired == AcquiredInfo.RECALIBRATE) {
            return BiometricFaceConstants.FACE_ACQUIRED_RECALIBRATE;
        } else if (aidlAcquired == AcquiredInfo.TOO_DIFFERENT) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_DIFFERENT;
        } else if (aidlAcquired == AcquiredInfo.TOO_SIMILAR) {
            return BiometricFaceConstants.FACE_ACQUIRED_TOO_SIMILAR;
        } else if (aidlAcquired == AcquiredInfo.PAN_TOO_EXTREME) {
            return BiometricFaceConstants.FACE_ACQUIRED_PAN_TOO_EXTREME;
        } else if (aidlAcquired == AcquiredInfo.TILT_TOO_EXTREME) {
            return BiometricFaceConstants.FACE_ACQUIRED_TILT_TOO_EXTREME;
        } else if (aidlAcquired == AcquiredInfo.ROLL_TOO_EXTREME) {
            return BiometricFaceConstants.FACE_ACQUIRED_ROLL_TOO_EXTREME;
        } else if (aidlAcquired == AcquiredInfo.FACE_OBSCURED) {
            return BiometricFaceConstants.FACE_ACQUIRED_FACE_OBSCURED;
        } else if (aidlAcquired == AcquiredInfo.START) {
            return BiometricFaceConstants.FACE_ACQUIRED_START;
        } else if (aidlAcquired == AcquiredInfo.SENSOR_DIRTY) {
            return BiometricFaceConstants.FACE_ACQUIRED_SENSOR_DIRTY;
        } else if (aidlAcquired == AcquiredInfo.VENDOR) {
            return BiometricFaceConstants.FACE_ACQUIRED_VENDOR;
        } else if (aidlAcquired == AcquiredInfo.FIRST_FRAME_RECEIVED) {
            // No framework constant available
            return BiometricFaceConstants.FACE_ACQUIRED_UNKNOWN;
        } else if (aidlAcquired == AcquiredInfo.DARK_GLASSES_DETECTED) {
            // No framework constant available
            return BiometricFaceConstants.FACE_ACQUIRED_UNKNOWN;
        } else if (aidlAcquired == AcquiredInfo.MOUTH_COVERING_DETECTED) {
            // No framework constant available
            return BiometricFaceConstants.FACE_ACQUIRED_UNKNOWN;
        } else {
            return BiometricFaceConstants.FACE_ACQUIRED_UNKNOWN;
        }
    }

    @NonNull
    public static FaceEnrollFrame convert(@NonNull EnrollmentFrame frame) {
        return new FaceEnrollFrame(convert(frame.cell), frame.stage, convert(frame.data));
    public static FaceAuthenticationFrame convert(@NonNull AuthenticationFrame frame) {
        return new FaceAuthenticationFrame(convert(frame.data));
    }

    @NonNull
    public static EnrollmentFrame convert(@NonNull FaceEnrollFrame frame) {
        final EnrollmentFrame convertedFrame = new EnrollmentFrame();
        convertedFrame.cell = convert(frame.getCell());
        convertedFrame.stage = (byte) frame.getStage();
        convertedFrame.data = convert(frame.getData());
        return convertedFrame;
    public static FaceEnrollFrame convert(@NonNull EnrollmentFrame frame) {
        return new FaceEnrollFrame(convert(frame.cell), frame.stage, convert(frame.data));
    }

    @NonNull
    public static FaceDataFrame convert(@NonNull BaseFrame frame) {
        return new FaceDataFrame(
                frame.acquiredInfo,
                toFrameworkAcquiredInfo(frame.acquiredInfo),
                frame.vendorCode,
                frame.pan,
                frame.tilt,
@@ -71,33 +148,8 @@ final class AidlConversionUtils {
                frame.isCancellable);
    }

    @NonNull
    public static BaseFrame convert(@NonNull FaceDataFrame frame) {
        final BaseFrame convertedFrame = new BaseFrame();
        convertedFrame.acquiredInfo = (byte) frame.getAcquiredInfo();
        convertedFrame.vendorCode = frame.getVendorCode();
        convertedFrame.pan = frame.getPan();
        convertedFrame.tilt = frame.getTilt();
        convertedFrame.distance = frame.getDistance();
        convertedFrame.isCancellable = frame.isCancellable();
        return convertedFrame;
    }

    @Nullable
    public static FaceEnrollCell convert(@Nullable Cell cell) {
        return cell == null ? null : new FaceEnrollCell(cell.x, cell.y, cell.z);
    }

    @Nullable
    public static Cell convert(@Nullable FaceEnrollCell cell) {
        if (cell == null) {
            return null;
        }

        final Cell convertedCell = new Cell();
        convertedCell.x = cell.getX();
        convertedCell.y = cell.getY();
        convertedCell.z = cell.getZ();
        return convertedCell;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -223,7 +223,7 @@ public class Sensor {
                }

                final ErrorConsumer errorConsumer = (ErrorConsumer) client;
                errorConsumer.onError(error, vendorCode);
                errorConsumer.onError(AidlConversionUtils.toFrameworkError(error), vendorCode);

                if (error == Error.HW_UNAVAILABLE) {
                    mCallback.onHardwareUnavailable();
+90 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.server.biometrics.sensors.fingerprint.aidl;

import android.hardware.biometrics.BiometricFingerprintConstants;
import android.hardware.biometrics.fingerprint.AcquiredInfo;
import android.hardware.biometrics.fingerprint.Error;

/**
 * Utilities for converting between hardware and framework-defined AIDL models.
 */
final class AidlConversionUtils {
    // Prevent instantiation.
    private AidlConversionUtils() {
    }

    public static @BiometricFingerprintConstants.FingerprintError int toFrameworkError(
            byte aidlError) {
        if (aidlError == Error.UNKNOWN) {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_UNKNOWN;
        } else if (aidlError == Error.HW_UNAVAILABLE) {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_HW_UNAVAILABLE;
        } else if (aidlError == Error.UNABLE_TO_PROCESS) {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_UNABLE_TO_PROCESS;
        } else if (aidlError == Error.TIMEOUT) {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_TIMEOUT;
        } else if (aidlError == Error.NO_SPACE) {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_NO_SPACE;
        } else if (aidlError == Error.CANCELED) {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_CANCELED;
        } else if (aidlError == Error.UNABLE_TO_REMOVE) {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_UNABLE_TO_REMOVE;
        } else if (aidlError == Error.VENDOR) {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_VENDOR;
        } else {
            return BiometricFingerprintConstants.FINGERPRINT_ERROR_UNKNOWN;
        }
    }

    public static @BiometricFingerprintConstants.FingerprintAcquired int toFrameworkAcquiredInfo(
            byte aidlAcquiredInfo) {
        if (aidlAcquiredInfo == AcquiredInfo.UNKNOWN) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_UNKNOWN;
        } else if (aidlAcquiredInfo == AcquiredInfo.GOOD) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_GOOD;
        } else if (aidlAcquiredInfo == AcquiredInfo.PARTIAL) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_PARTIAL;
        } else if (aidlAcquiredInfo == AcquiredInfo.INSUFFICIENT) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_INSUFFICIENT;
        } else if (aidlAcquiredInfo == AcquiredInfo.SENSOR_DIRTY) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_IMAGER_DIRTY;
        } else if (aidlAcquiredInfo == AcquiredInfo.TOO_SLOW) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_TOO_SLOW;
        } else if (aidlAcquiredInfo == AcquiredInfo.TOO_FAST) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_TOO_FAST;
        } else if (aidlAcquiredInfo == AcquiredInfo.VENDOR) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_VENDOR;
        } else if (aidlAcquiredInfo == AcquiredInfo.START) {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_START;
        } else if (aidlAcquiredInfo == AcquiredInfo.TOO_DARK) {
            // No framework constant available
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_UNKNOWN;
        } else if (aidlAcquiredInfo == AcquiredInfo.TOO_BRIGHT) {
            // No framework constant available
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_UNKNOWN;
        } else if (aidlAcquiredInfo == AcquiredInfo.IMMOBILE) {
            // No framework constant available
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_UNKNOWN;
        } else if (aidlAcquiredInfo == AcquiredInfo.RETRYING_CAPTURE) {
            // No framework constant available
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_UNKNOWN;
        } else {
            return BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_UNKNOWN;
        }
    }
}
Loading