Android11.0源码关于指纹&人脸的录入及解锁源码分析
1. 指纹录入
指纹录入文件路径:
frameworks/base/core/java/android/hardware/fingerprint/FingerprintManager.java packages/apps/Settings/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollSidecar.java
指纹录入回调:
private FingerprintManager.EnrollmentCallback mEnrollmentCallback = new FingerprintManager.EnrollmentCallback() { /** * 指纹录入过程,每个指纹模板成功都走 */ @Override public void onEnrollmentProgress(int remaining) { //指纹录入过程,remaining值从24(和对应指纹厂商有关)到0每次都会触发 // if (remaining == 0) {代表指纹录入结果成功} FingerprintEnrollSidecar.super.onEnrollmentProgress(remaining); } /** * 指纹模板录入异常回调 */ @Override public void onEnrollmentHelp(int helpMsgId, CharSequence helpString) { //指纹模板录入异常提示,helpMsgId:异常码,helpString:异常提示 FingerprintEnrollSidecar.super.onEnrollmentHelp(helpMsgId, helpString); } /** * 指纹录入结果失败 */ @Override public void onEnrollmentError(int errMsgId, CharSequence errString) { //指纹录入结果失败,errMsgId:失败错误码,errString:失败错误原因 FingerprintEnrollSidecar.super.onEnrollmentError(errMsgId, errString); } };
**
2. 人脸录入
**
人脸录入文件路径:
frameworks/base/core/java/android/hardware/face/FaceManager.java packages/apps/Settings/src/com/android/settings/biometrics/face/FaceEnrollSidecar.java
人脸录入回调:
private FaceManager.EnrollmentCallback mEnrollmentCallback = new FaceManager.EnrollmentCallback() { /** * 人脸录入过程,在每个注册步骤进行时调用 * @param remaining 剩余值 */ @Override public void onEnrollmentProgress(int remaining) { //人脸录入过程,remaining值从100(和对应人脸厂商有关)到0每次都会触发 //if (remaining == 0) {代表人脸录入结果成功} FaceEnrollSidecar.super.onEnrollmentProgress(remaining); } @Override public void onEnrollmentHelp(int helpMsgId, CharSequence helpString) { //人脸录入异常提示,helpMsgId:异常码,helpString:异常提示 FaceEnrollSidecar.super.onEnrollmentHelp(helpMsgId, helpString); } @Override public void onEnrollmentError(int errMsgId, CharSequence errString) { //人脸录入结果失败,errMsgId:失败错误码,errString:失败错误原因 FaceEnrollSidecar.super.onEnrollmentError(errMsgId, errString); } };
3. 指纹解锁
指纹解锁注入回调文件路径:
frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
private FingerprintManager.AuthenticationCallback mFingerprintAuthenticationCallback = new AuthenticationCallback() { /** * 指纹解锁结果失败回调 */ @Override public void onAuthenticationFailed() { handleFingerprintAuthFailed(); } /** * 指纹解锁结果成功回调 */ @Override public void onAuthenticationSucceeded(AuthenticationResult result) { Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded"); handleFingerprintAuthenticated(result.getUserId(), result.isStrongBiometric()); Trace.endSection(); } /** * 指纹解锁异常回调,在身份验证期间遇到可恢复错误时调用 * @param helpMsgId 异常码 * @param helpString 异常原因 */ @Override public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { //if(helpMsgId == 1008){指纹解锁误触} handleFingerprintHelp(helpMsgId, helpString.toString()); } /** * 指纹解锁结果失败原因回调(脱靶/图像异常/SPI通信/软件流程), * 遇到不可恢复的错误且操作完成时调用。 * @param errMsgId 错误码 * @param errString 显示“失败错误原因” */ @Override public void onAuthenticationError(int errMsgId, CharSequence errString) { handleFingerprintError(errMsgId, errString.toString()); } /** * 指纹解锁在采集指纹图像但尚未处理时调用 */ @Override public void onAuthenticationAcquired(int acquireInfo) { handleFingerprintAcquired(acquireInfo); } };
4. 人脸解锁
人脸解锁注入回调文件路经:
frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
FaceManager.AuthenticationCallback mFaceAuthenticationCallback = new FaceManager.AuthenticationCallback() { /** * 人脸解锁结果失败,当检测到人脸但无法识别时调用。 */ @Override public void onAuthenticationFailed() { handleFaceAuthFailed(); } /** * 人脸解锁结果成功,识别人脸时调用 */ @Override public void onAuthenticationSucceeded(FaceManager.AuthenticationResult result) { Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded"); handleFaceAuthenticated(result.getUserId(), result.isStrongBiometric()); Trace.endSection(); } /** * 人脸解锁异常回调,在身份验证期间遇到可恢复错误时调用。 * 帮助字符串用于为用户提供出错的指导,例如“传感器脏了,请清洁它。” * @param helpMsgId 异常码 * @param helpString 出错的指导提示 */ @Override public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { handleFaceHelp(helpMsgId, helpString.toString()); } /** * 人脸解锁结果失败原因回调,遇到不可恢复的错误且操作完成时调用。 * 不会对此对象进行进一步回调。 * @param errMsgId 错误码 * @param errString 显示“失败错误原因” */ @Override public void onAuthenticationError(int errMsgId, CharSequence errString) { if (errMsgId > 1000) { //触发获取人脸识别结果失败 //触发获取人脸识别结果失败原因 } handleFaceError(errMsgId, errString.toString()); } /** * 人脸解锁在采集人脸图像但尚未处理时调用 */ @Override public void onAuthenticationAcquired(int acquireInfo) { handleFaceAcquired(acquireInfo); } };
5.指纹管理
文件路径:
frameworks/base/core/java/android/hardware/fingerprint/FingerprintManager.java
910 private void sendAcquiredResult(long deviceId, int acquireInfo, int vendorCode) { 911 if (mAuthenticationCallback != null) { 912 mAuthenticationCallback.onAuthenticationAcquired(acquireInfo); 913 } //指纹录入或解锁,msg为过程中异常提示信息 914 final String msg = getAcquiredString(mContext, acquireInfo, vendorCode); 915 if (msg == null) { 916 return; 917 } 918 // emulate HAL 2.1 behavior and send real acquiredInfo //指纹录入或解锁,clientInfo为过程中异常提示对应的异常码 919 final int clientInfo = acquireInfo == FINGERPRINT_ACQUIRED_VENDOR 920 ? (vendorCode + FINGERPRINT_ACQUIRED_VENDOR_BASE) : acquireInfo; 921 if (mEnrollmentCallback != null) { 922 mEnrollmentCallback.onEnrollmentHelp(clientInfo, msg); 923 } else if (mAuthenticationCallback != null) { 924 if (acquireInfo != BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_START) { 925 mAuthenticationCallback.onAuthenticationHelp(clientInfo, msg); 926 } 927 } 928 }
930 private void sendErrorResult(long deviceId, int errMsgId, int vendorCode) { 931 // emulate HAL 2.1 behavior and send real errMsgId //指纹录入或解锁,clientErrMsgId为结果失败信息错误码 932 final int clientErrMsgId = errMsgId == FINGERPRINT_ERROR_VENDOR 933 ? (vendorCode + FINGERPRINT_ERROR_VENDOR_BASE) : errMsgId; 934 if (mEnrollmentCallback != null) { 935 mEnrollmentCallback.onEnrollmentError(clientErrMsgId, 936 getErrorString(mContext, errMsgId, vendorCode)); 937 } else if (mAuthenticationCallback != null) { 938 mAuthenticationCallback.onAuthenticationError(clientErrMsgId, 939 getErrorString(mContext, errMsgId, vendorCode)); 940 } else if (mRemovalCallback != null) { 941 mRemovalCallback.onRemovalError(mRemovalFingerprint, clientErrMsgId, 942 getErrorString(mContext, errMsgId, vendorCode)); 943 } else if (mEnumerateCallback != null) { 944 mEnumerateCallback.onEnumerateError(clientErrMsgId, 945 getErrorString(mContext, errMsgId, vendorCode)); 946 } 947 }
6.人脸管理
文件路径:
frameworks/base/core/java/android/hardware/face/FaceManager.java
1077 private void sendAcquiredResult(long deviceId, int acquireInfo, int vendorCode) { 1078 if (mAuthenticationCallback != null) { 1079 mAuthenticationCallback.onAuthenticationAcquired(acquireInfo); 1080 } //人脸录入或解锁,msg为过程中异常提示信息 1081 final String msg = getAcquiredString(mContext, acquireInfo, vendorCode); //人脸录入或解锁,clientInfo为过程中异常提示对应的异常码 1082 final int clientInfo = acquireInfo == FACE_ACQUIRED_VENDOR 1083 ? (vendorCode + FACE_ACQUIRED_VENDOR_BASE) : acquireInfo; 1084 if (mEnrollmentCallback != null) { 1085 mEnrollmentCallback.onEnrollmentHelp(clientInfo, msg); 1086 } else if (mAuthenticationCallback != null && msg != null) { 1087 mAuthenticationCallback.onAuthenticationHelp(clientInfo, msg); 1088 } 1089 }
1041 private void sendErrorResult(long deviceId, int errMsgId, int vendorCode) { 1042 // emulate HAL 2.1 behavior and send real errMsgId //人脸录入或解锁,clientErrMsgId为结果失败信息错误码 1043 final int clientErrMsgId = errMsgId == FACE_ERROR_VENDOR 1044 ? (vendorCode + FACE_ERROR_VENDOR_BASE) : errMsgId; 1045 if (mEnrollmentCallback != null) { 1046 mEnrollmentCallback.onEnrollmentError(clientErrMsgId, 1047 getErrorString(mContext, errMsgId, vendorCode)); 1048 } else if (mAuthenticationCallback != null) { 1049 mAuthenticationCallback.onAuthenticationError(clientErrMsgId, 1050 getErrorString(mContext, errMsgId, vendorCode)); 1051 } else if (mRemovalCallback != null) { 1052 mRemovalCallback.onRemovalError(mRemovalFace, clientErrMsgId, 1053 getErrorString(mContext, errMsgId, vendorCode)); 1054 } 1055 }