1 /* 2 * Copyright (C) 2022 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.server.biometrics.log; 18 19 import android.hardware.biometrics.BiometricsProtoEnums; 20 import android.hardware.biometrics.common.OperationContext; 21 import android.hardware.biometrics.common.OperationReason; 22 import android.util.Slog; 23 24 import com.android.internal.util.FrameworkStatsLog; 25 26 /** 27 * Wrapper for {@link FrameworkStatsLog} to isolate the testable parts. 28 */ 29 public class BiometricFrameworkStatsLogger { 30 31 private static final String TAG = "BiometricFrameworkStatsLogger"; 32 33 private static final BiometricFrameworkStatsLogger sInstance = 34 new BiometricFrameworkStatsLogger(); 35 BiometricFrameworkStatsLogger()36 private BiometricFrameworkStatsLogger() {} 37 38 /** Shared instance. */ getInstance()39 public static BiometricFrameworkStatsLogger getInstance() { 40 return sInstance; 41 } 42 43 /** {@see FrameworkStatsLog.BIOMETRIC_ACQUIRED}. */ acquired(OperationContext operationContext, int statsModality, int statsAction, int statsClient, boolean isDebug, int acquiredInfo, int vendorCode, int targetUserId)44 public void acquired(OperationContext operationContext, 45 int statsModality, int statsAction, int statsClient, boolean isDebug, 46 int acquiredInfo, int vendorCode, int targetUserId) { 47 FrameworkStatsLog.write(FrameworkStatsLog.BIOMETRIC_ACQUIRED, 48 statsModality, 49 targetUserId, 50 operationContext.isCrypto, 51 statsAction, 52 statsClient, 53 acquiredInfo, 54 vendorCode, 55 isDebug, 56 -1 /* sensorId */, 57 operationContext.id, 58 sessionType(operationContext.reason), 59 operationContext.isAod); 60 } 61 62 /** {@see FrameworkStatsLog.BIOMETRIC_AUTHENTICATED}. */ authenticate(OperationContext operationContext, int statsModality, int statsAction, int statsClient, boolean isDebug, long latency, int authState, boolean requireConfirmation, int targetUserId, float ambientLightLux)63 public void authenticate(OperationContext operationContext, 64 int statsModality, int statsAction, int statsClient, boolean isDebug, long latency, 65 int authState, boolean requireConfirmation, int targetUserId, float ambientLightLux) { 66 FrameworkStatsLog.write(FrameworkStatsLog.BIOMETRIC_AUTHENTICATED, 67 statsModality, 68 targetUserId, 69 operationContext.isCrypto, 70 statsClient, 71 requireConfirmation, 72 authState, 73 sanitizeLatency(latency), 74 isDebug, 75 -1 /* sensorId */, 76 ambientLightLux, 77 operationContext.id, 78 sessionType(operationContext.reason), 79 operationContext.isAod); 80 } 81 82 /** {@see FrameworkStatsLog.BIOMETRIC_AUTHENTICATED}. */ authenticate(OperationContext operationContext, int statsModality, int statsAction, int statsClient, boolean isDebug, long latency, int authState, boolean requireConfirmation, int targetUserId, ALSProbe alsProbe)83 public void authenticate(OperationContext operationContext, 84 int statsModality, int statsAction, int statsClient, boolean isDebug, long latency, 85 int authState, boolean requireConfirmation, int targetUserId, ALSProbe alsProbe) { 86 alsProbe.awaitNextLux((ambientLightLux) -> { 87 authenticate(operationContext, statsModality, statsAction, statsClient, isDebug, 88 latency, authState, requireConfirmation, targetUserId, ambientLightLux); 89 }, null /* handler */); 90 } 91 92 /** {@see FrameworkStatsLog.BIOMETRIC_ENROLLED}. */ enroll(int statsModality, int statsAction, int statsClient, int targetUserId, long latency, boolean enrollSuccessful, float ambientLightLux)93 public void enroll(int statsModality, int statsAction, int statsClient, 94 int targetUserId, long latency, boolean enrollSuccessful, float ambientLightLux) { 95 FrameworkStatsLog.write(FrameworkStatsLog.BIOMETRIC_ENROLLED, 96 statsModality, 97 targetUserId, 98 sanitizeLatency(latency), 99 enrollSuccessful, 100 -1, /* sensorId */ 101 ambientLightLux); 102 } 103 104 /** {@see FrameworkStatsLog.BIOMETRIC_ERROR_OCCURRED}. */ error(OperationContext operationContext, int statsModality, int statsAction, int statsClient, boolean isDebug, long latency, int error, int vendorCode, int targetUserId)105 public void error(OperationContext operationContext, 106 int statsModality, int statsAction, int statsClient, boolean isDebug, long latency, 107 int error, int vendorCode, int targetUserId) { 108 FrameworkStatsLog.write(FrameworkStatsLog.BIOMETRIC_ERROR_OCCURRED, 109 statsModality, 110 targetUserId, 111 operationContext.isCrypto, 112 statsAction, 113 statsClient, 114 error, 115 vendorCode, 116 isDebug, 117 sanitizeLatency(latency), 118 -1 /* sensorId */, 119 operationContext.id, 120 sessionType(operationContext.reason), 121 operationContext.isAod); 122 } 123 124 /** {@see FrameworkStatsLog.BIOMETRIC_SYSTEM_HEALTH_ISSUE_DETECTED}. */ reportUnknownTemplateEnrolledHal(int statsModality)125 public void reportUnknownTemplateEnrolledHal(int statsModality) { 126 FrameworkStatsLog.write(FrameworkStatsLog.BIOMETRIC_SYSTEM_HEALTH_ISSUE_DETECTED, 127 statsModality, 128 BiometricsProtoEnums.ISSUE_UNKNOWN_TEMPLATE_ENROLLED_HAL, 129 -1 /* sensorId */); 130 } 131 132 /** {@see FrameworkStatsLog.BIOMETRIC_SYSTEM_HEALTH_ISSUE_DETECTED}. */ reportUnknownTemplateEnrolledFramework(int statsModality)133 public void reportUnknownTemplateEnrolledFramework(int statsModality) { 134 FrameworkStatsLog.write(FrameworkStatsLog.BIOMETRIC_SYSTEM_HEALTH_ISSUE_DETECTED, 135 statsModality, 136 BiometricsProtoEnums.ISSUE_UNKNOWN_TEMPLATE_ENROLLED_FRAMEWORK, 137 -1 /* sensorId */); 138 } 139 sanitizeLatency(long latency)140 private long sanitizeLatency(long latency) { 141 if (latency < 0) { 142 Slog.w(TAG, "found a negative latency : " + latency); 143 return -1; 144 } 145 return latency; 146 } 147 sessionType(@perationReason byte reason)148 private static int sessionType(@OperationReason byte reason) { 149 if (reason == OperationReason.BIOMETRIC_PROMPT) { 150 return BiometricsProtoEnums.SESSION_TYPE_BIOMETRIC_PROMPT; 151 } 152 if (reason == OperationReason.KEYGUARD) { 153 return BiometricsProtoEnums.SESSION_TYPE_KEYGUARD_ENTRY; 154 } 155 return BiometricsProtoEnums.SESSION_TYPE_UNKNOWN; 156 } 157 } 158