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

Commit 517c2f53 authored by Kevin Chyn's avatar Kevin Chyn Committed by Android (Google) Code Review
Browse files

Merge "Clean up ServiceListener / ClientMonitor"

parents f894a536 84f162af
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,5 +29,5 @@ oneway interface IBiometricSensorReceiver {
    // on the cookie.
    void onError(int sensorId, int cookie, int error, int vendorCode);
    // Notifies that a biometric has been acquired.
    void onAcquired(int sensorId, int acquiredInfo, String message);
    void onAcquired(int sensorId, int acquiredInfo, int vendorCode);
}
+27 −7
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static android.hardware.biometrics.BiometricAuthenticator.TYPE_NONE;

import android.annotation.IntDef;
import android.annotation.Nullable;
import android.content.Context;
import android.hardware.biometrics.BiometricAuthenticator;
import android.hardware.biometrics.BiometricConstants;
import android.hardware.biometrics.BiometricManager;
@@ -30,6 +31,8 @@ import android.hardware.biometrics.IBiometricSensorReceiver;
import android.hardware.biometrics.IBiometricServiceReceiver;
import android.hardware.biometrics.IBiometricSysuiReceiver;
import android.hardware.biometrics.PromptInfo;
import android.hardware.face.FaceManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.IBinder;
import android.os.RemoteException;
import android.security.KeyStore;
@@ -121,6 +124,7 @@ final class AuthSession implements IBinder.DeathRecipient {
        void onClientDied();
    }

    private final Context mContext;
    private final IStatusBarService mStatusBarService;
    private final IBiometricSysuiReceiver mSysuiReceiver;
    private final KeyStore mKeyStore;
@@ -158,12 +162,13 @@ final class AuthSession implements IBinder.DeathRecipient {
    // Timestamp when hardware authentication occurred
    private long mAuthenticatedTimeMs;

    AuthSession(IStatusBarService statusBarService, IBiometricSysuiReceiver sysuiReceiver,
            KeyStore keystore, Random random, ClientDeathReceiver clientDeathReceiver,
            PreAuthInfo preAuthInfo, IBinder token, long operationId, int userId,
            IBiometricSensorReceiver sensorReceiver, IBiometricServiceReceiver clientReceiver,
            String opPackageName, PromptInfo promptInfo,
    AuthSession(Context context, IStatusBarService statusBarService,
            IBiometricSysuiReceiver sysuiReceiver, KeyStore keystore, Random random,
            ClientDeathReceiver clientDeathReceiver, PreAuthInfo preAuthInfo, IBinder token,
            long operationId, int userId, IBiometricSensorReceiver sensorReceiver,
            IBiometricServiceReceiver clientReceiver, String opPackageName, PromptInfo promptInfo,
            int callingUid, int callingPid, int callingUserId, boolean debugEnabled) {
        mContext = context;
        mStatusBarService = statusBarService;
        mSysuiReceiver = sysuiReceiver;
        mKeyStore = keystore;
@@ -421,11 +426,14 @@ final class AuthSession implements IBinder.DeathRecipient {
        return false;
    }

    void onAcquired(int sensorId, int acquiredInfo, String message) {
    void onAcquired(int sensorId, int acquiredInfo, int vendorCode) {
        final String message = getAcquiredMessageForSensor(sensorId, acquiredInfo, vendorCode);
        Slog.d(TAG, "sensorId: " + sensorId + " acquiredInfo: " + acquiredInfo
                + " message: " + message);
        if (message == null) {
            Slog.w(TAG, "Ignoring null message: " + acquiredInfo);
            return;
        }

        try {
            mStatusBarService.onBiometricHelp(message);
        } catch (RemoteException e) {
@@ -756,6 +764,18 @@ final class AuthSession implements IBinder.DeathRecipient {
        return TYPE_NONE;
    }

    private String getAcquiredMessageForSensor(int sensorId, int acquiredInfo, int vendorCode) {
        final @BiometricAuthenticator.Modality int modality = sensorIdToModality(sensorId);
        switch (modality) {
            case BiometricAuthenticator.TYPE_FINGERPRINT:
                return FingerprintManager.getAcquiredString(mContext, acquiredInfo, vendorCode);
            case BiometricAuthenticator.TYPE_FACE:
                return FaceManager.getAcquiredString(mContext, acquiredInfo, vendorCode);
            default:
                return null;
        }
    }

    @Override
    public String toString() {
        return "State: " + mState
+14 −20
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ public abstract class AuthenticationClient extends ClientMonitor {
    public static final int LOCKOUT_TIMED = 1;
    public static final int LOCKOUT_PERMANENT = 2;

    private final boolean mIsStrongBiometric;
    private final boolean mRequireConfirmation;
    private final Surface mSurface;

@@ -68,21 +69,15 @@ public abstract class AuthenticationClient extends ClientMonitor {

    public abstract boolean wasUserDetected();

    public abstract boolean isStrongBiometric();

    /**
     * @return The sensor's unique ID as configured by the framework. See {@link AuthSession}
     */
    public abstract int getSensorId();

    public AuthenticationClient(Context context, Constants constants,
            BiometricServiceBase.DaemonWrapper daemon, IBinder token,
            BiometricServiceBase.ServiceListener listener, int targetUserId, int groupId, long opId,
            ClientMonitorCallbackConverter listener, int targetUserId, int groupId, long opId,
            boolean restricted, String owner, int cookie, boolean requireConfirmation,
            Surface surface) {
            Surface surface, int sensorId, boolean isStrongBiometric) {
        super(context, constants, daemon, token, listener, targetUserId, groupId,
                restricted, owner, cookie);
                restricted, owner, cookie, sensorId);
        mOpId = opId;
        mIsStrongBiometric = isStrongBiometric;
        mRequireConfirmation = requireConfirmation;
        mSurface = surface;
    }
@@ -139,7 +134,7 @@ public abstract class AuthenticationClient extends ClientMonitor {
        super.logOnAuthenticated(getContext(), authenticated, mRequireConfirmation,
                getTargetUserId(), isBiometricPrompt());

        final BiometricServiceBase.ServiceListener listener = getListener();
        final ClientMonitorCallbackConverter listener = getListener();

        mMetricsLogger.action(mConstants.actionBiometricAuth(), authenticated);
        boolean result = false;
@@ -171,9 +166,10 @@ public abstract class AuthenticationClient extends ClientMonitor {
                }
                if (isBiometricPrompt() && listener != null) {
                    // BiometricService will add the token to keystore
                    listener.onAuthenticationSucceededInternal(getSensorId(), byteToken);
                    listener.onAuthenticationSucceeded(getSensorId(), identifier, byteToken,
                            getTargetUserId(), mIsStrongBiometric);
                } else if (!isBiometricPrompt() && listener != null) {
                    if (isStrongBiometric()) {
                    if (mIsStrongBiometric) {
                        KeyStore.getInstance().addAuthToken(byteToken);
                    } else {
                        Slog.d(getLogTag(), "Skipping addAuthToken");
@@ -183,9 +179,11 @@ public abstract class AuthenticationClient extends ClientMonitor {
                        // Explicitly have if/else here to make it super obvious in case the code is
                        // touched in the future.
                        if (!getIsRestricted()) {
                            listener.onAuthenticationSucceeded(identifier, getTargetUserId());
                            listener.onAuthenticationSucceeded(getSensorId(), identifier, byteToken,
                                    getTargetUserId(), mIsStrongBiometric);
                        } else {
                            listener.onAuthenticationSucceeded(null, getTargetUserId());
                            listener.onAuthenticationSucceeded(getSensorId(), null /* identifier */,
                                    byteToken, getTargetUserId(), mIsStrongBiometric);
                        }
                    } catch (RemoteException e) {
                        Slog.e(getLogTag(), "Remote exception", e);
@@ -215,11 +213,7 @@ public abstract class AuthenticationClient extends ClientMonitor {
                    // janky UI on Keyguard/BiometricPrompt since "authentication failed"
                    // will show briefly and be replaced by "device locked out" message.
                    if (listener != null) {
                        if (isBiometricPrompt()) {
                            listener.onAuthenticationFailedInternal(getSensorId());
                        } else {
                            listener.onAuthenticationFailed();
                        }
                        listener.onAuthenticationFailed(getSensorId());
                    }
                }
                result = lockoutMode != LOCKOUT_NONE; // in a lockout mode
+7 −7
Original line number Diff line number Diff line
@@ -151,7 +151,7 @@ public class BiometricService extends SystemService {
                    handleOnAcquired(
                            args.argi1 /* sensorId */,
                            args.argi2 /* acquiredInfo */,
                            (String) args.arg1 /* message */);
                            args.argi3 /* vendorCode */);
                    args.recycle();
                    break;
                }
@@ -418,11 +418,11 @@ public class BiometricService extends SystemService {
        }

        @Override
        public void onAcquired(int sensorId, int acquiredInfo, String message) {
        public void onAcquired(int sensorId, int acquiredInfo, int vendorCode) {
            SomeArgs args = SomeArgs.obtain();
            args.argi1 = sensorId;
            args.argi2 = acquiredInfo;
            args.arg1 = message;
            args.argi3 = vendorCode;
            mHandler.obtainMessage(MSG_ON_ACQUIRED, args).sendToTarget();
        }
    };
@@ -915,7 +915,7 @@ public class BiometricService extends SystemService {
        }
    }

    private void handleOnAcquired(int sensorId, int acquiredInfo, String message) {
    private void handleOnAcquired(int sensorId, int acquiredInfo, int vendorCode) {
        // Should never happen, log this to catch bad HAL behavior (e.g. auth succeeded
        // after user dismissed/canceled dialog).
        if (mCurrentAuthSession == null) {
@@ -923,7 +923,7 @@ public class BiometricService extends SystemService {
            return;
        }

        mCurrentAuthSession.onAcquired(sensorId, acquiredInfo, message);
        mCurrentAuthSession.onAcquired(sensorId, acquiredInfo, vendorCode);
    }

    private void handleOnDismissed(@BiometricPrompt.DismissedReason int reason,
@@ -1063,8 +1063,8 @@ public class BiometricService extends SystemService {
        }

        final boolean debugEnabled = mInjector.isDebugEnabled(getContext(), userId);
        mCurrentAuthSession = new AuthSession(mStatusBarService, mSysuiReceiver, mKeyStore, mRandom,
                mClientDeathReceiver, preAuthInfo, token, operationId, userId,
        mCurrentAuthSession = new AuthSession(getContext(), mStatusBarService, mSysuiReceiver,
                mKeyStore, mRandom, mClientDeathReceiver, preAuthInfo, token, operationId, userId,
                mBiometricSensorReceiver, receiver, opPackageName, promptInfo, callingUid,
                callingPid, callingUserId, debugEnabled);
        try {
+21 −87
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import android.hardware.biometrics.BiometricAuthenticator;
import android.hardware.biometrics.BiometricConstants;
import android.hardware.biometrics.BiometricManager.Authenticators;
import android.hardware.biometrics.BiometricsProtoEnums;
import android.hardware.biometrics.IBiometricSensorReceiver;
import android.hardware.biometrics.IBiometricService;
import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
import android.hardware.fingerprint.Fingerprint;
@@ -234,11 +233,13 @@ public abstract class BiometricServiceBase extends SystemService
        }

        public AuthenticationClientImpl(Context context, DaemonWrapper daemon,
                IBinder token, ServiceListener listener, int targetUserId, int groupId, long opId,
                boolean restricted, String owner, int cookie, boolean requireConfirmation,
                Surface surface) {
                IBinder token, ClientMonitorCallbackConverter listener, int targetUserId,
                int groupId, long opId, boolean restricted, String owner, int cookie,
                boolean requireConfirmation, Surface surface, int sensorId,
                boolean isStrongBiometric) {
            super(context, getConstants(), daemon, token, listener, targetUserId,
                    groupId, opId, restricted, owner, cookie, requireConfirmation, surface);
                    groupId, opId, restricted, owner, cookie, requireConfirmation, surface,
                    sensorId, isStrongBiometric);
        }

        @Override
@@ -297,12 +298,12 @@ public abstract class BiometricServiceBase extends SystemService
    protected abstract class EnrollClientImpl extends EnrollClient {

        public EnrollClientImpl(Context context, DaemonWrapper daemon,
                IBinder token, ServiceListener listener, int userId, int groupId,
                IBinder token, ClientMonitorCallbackConverter listener, int userId, int groupId,
                byte[] cryptoToken, boolean restricted, String owner,
                final int[] disabledFeatures, int timeoutSec, Surface surface) {
                final int[] disabledFeatures, int timeoutSec, Surface surface, int sensorId) {
            super(context, getConstants(), daemon, token, listener,
                    userId, groupId, cryptoToken, restricted, owner, getBiometricUtils(),
                    disabledFeatures, timeoutSec, surface);
                    disabledFeatures, timeoutSec, surface, sensorId);
        }

        @Override
@@ -317,10 +318,10 @@ public abstract class BiometricServiceBase extends SystemService
    private final class InternalRemovalClient extends RemovalClient {
        InternalRemovalClient(Context context,
                DaemonWrapper daemon, IBinder token,
                ServiceListener listener, int templateId, int groupId, int userId,
                boolean restricted, String owner) {
                ClientMonitorCallbackConverter listener, int templateId, int groupId, int userId,
                boolean restricted, String owner, int sensorId) {
            super(context, getConstants(), daemon, token, listener, templateId, groupId,
                    userId, restricted, owner, getBiometricUtils());
                    userId, restricted, owner, getBiometricUtils(), sensorId);
        }

        @Override
@@ -343,11 +344,12 @@ public abstract class BiometricServiceBase extends SystemService

        InternalEnumerateClient(Context context,
                DaemonWrapper daemon, IBinder token,
                ServiceListener listener, int groupId, int userId, boolean restricted,
                String owner, List<? extends BiometricAuthenticator.Identifier> enrolledList,
                BiometricUtils utils) {
                ClientMonitorCallbackConverter listener, int groupId, int userId,
                boolean restricted, String owner,
                List<? extends BiometricAuthenticator.Identifier> enrolledList,
                BiometricUtils utils, int sensorId) {
            super(context, getConstants(), daemon, token, listener, groupId, userId,
                    restricted, owner);
                    restricted, owner, sensorId);
            mEnrolledList = enrolledList;
            mUtils = utils;
        }
@@ -414,74 +416,6 @@ public abstract class BiometricServiceBase extends SystemService
        }
    }

    /**
     * Wraps the callback interface from Service -> Manager
     */
    protected interface ServiceListener {
        default void onEnrollResult(BiometricAuthenticator.Identifier identifier,
                int remaining) throws RemoteException {};

        void onAcquired(int sensorId, int acquiredInfo, int vendorCode) throws RemoteException;

        default void onAuthenticationSucceeded(BiometricAuthenticator.Identifier biometric,
                int userId) throws RemoteException {
            throw new UnsupportedOperationException("Stub!");
        }

        default void onAuthenticationSucceededInternal(int sensorId, byte[] token)
                throws RemoteException {
            throw new UnsupportedOperationException("Stub!");
        }

        default void onAuthenticationFailed() throws RemoteException {
            throw new UnsupportedOperationException("Stub!");
        }

        default void onAuthenticationFailedInternal(int sensorId)
                throws RemoteException {
            throw new UnsupportedOperationException("Stub!");
        }

        void onError(int error, int vendorCode, int cookie) throws RemoteException;

        default void onRemoved(BiometricAuthenticator.Identifier identifier,
                int remaining) throws RemoteException {};

        default void onEnumerated(BiometricAuthenticator.Identifier identifier,
                int remaining) throws RemoteException {};
    }

    /**
     * Wraps the callback interface from Service -> BiometricPrompt
     */
    protected abstract class BiometricServiceListener implements ServiceListener {
        private IBiometricSensorReceiver mWrapperReceiver;

        public BiometricServiceListener(IBiometricSensorReceiver wrapperReceiver) {
            mWrapperReceiver = wrapperReceiver;
        }

        public IBiometricSensorReceiver getWrapperReceiver() {
            return mWrapperReceiver;
        }

        @Override
        public void onAuthenticationSucceededInternal(int sensorId, byte[] token)
                throws RemoteException {
            if (getWrapperReceiver() != null) {
                getWrapperReceiver().onAuthenticationSucceeded(sensorId, token);
            }
        }

        @Override
        public void onAuthenticationFailedInternal(int sensorId)
                throws RemoteException {
            if (getWrapperReceiver() != null) {
                getWrapperReceiver().onAuthenticationFailed(sensorId);
            }
        }
    }

    /**
     * Wraps a portion of the interface from Service -> Daemon that is used by the ClientMonitor
     * subclasses.
@@ -715,9 +649,9 @@ public abstract class BiometricServiceBase extends SystemService
     * Callback handlers from the daemon. The caller must put this on a handler.
     */

    protected void handleAcquired(int sensorId, int acquiredInfo, int vendorCode) {
    protected void handleAcquired(int acquiredInfo, int vendorCode) {
        ClientMonitor client = mCurrentClient;
        if (client != null && client.onAcquired(sensorId, acquiredInfo, vendorCode)) {
        if (client != null && client.onAcquired(acquiredInfo, vendorCode)) {
            removeClient(client);
        }
        if (mPerformanceStats != null && getLockoutMode() == AuthenticationClient.LOCKOUT_NONE
@@ -1284,7 +1218,7 @@ public abstract class BiometricServiceBase extends SystemService
            InternalRemovalClient client = new InternalRemovalClient(getContext(),
                    getDaemonWrapper(), mToken, null /* listener */,
                    template.mIdentifier.getBiometricId(), 0 /* groupId */, template.mUserId,
                    restricted, getContext().getPackageName());
                    restricted, getContext().getPackageName(), getSensorId());
            removeInternal(client);
            FrameworkStatsLog.write(FrameworkStatsLog.BIOMETRIC_SYSTEM_HEALTH_ISSUE_DETECTED,
                    statsModality(),
@@ -1309,7 +1243,7 @@ public abstract class BiometricServiceBase extends SystemService
        InternalEnumerateClient client = new InternalEnumerateClient(getContext(),
                getDaemonWrapper(), mToken, null /* serviceListener */, userId,
                userId, restricted, getContext().getOpPackageName(), enrolledList,
                getBiometricUtils());
                getBiometricUtils(), getSensorId());
        enumerateInternal(client);
    }

Loading