using System; using Java.Lang; //using Shared.Droid.TBL; using Moe.Feng.Support.Biometricprompt; using Android.OS; using Android.Content; //using Com.Bigkoo.Pickerview; namespace Shared { public class TouchIDUtils { public enum TouchIDSupperType { TouchID, FaceID, None } public enum TouchIDState { NotSupport, Success, Fail, InputPassword, TouchIDLockout } //BiometricPromptCompat mBiometricPrompt; //public TouchIDUtils() //{ // mOnIAuthenticationCallback = new OnIAuthenticationCallback(this); // mBiometricPrompt = new BiometricPromptCompat.Builder(Application.Activity).SetTitle("验证").SetSubtitle("").SetDescription("").Build(); //} public static TouchIDSupperType getTouchIDSupperType() { TouchIDSupperType type = TouchIDSupperType.None; bool bIsHardwareDetected = BiometricPromptCompat.IsHardwareDetected(Application.Activity); bool bHasEnrolledFingerprints = BiometricPromptCompat.HasEnrolledFingerprints(Application.Activity); if (bIsHardwareDetected && bHasEnrolledFingerprints) { type = TouchIDSupperType.TouchID; } else { type = TouchIDSupperType.None; } return type; } public EventHandler OnHDLTouchIDStateBackEvent; //StateBlock mStateBlock; /// /// 弹窗指纹验证 descTitle:输入密码验证按钮标题 mLocalizedReason:Touch显示的描述 /// /// descTitle:输入密码验证按钮标题 mLocalizedReason:Touch显示的描述 public void showTouchIDWithDescribe(string descTitle, string mLocalizedReason) { if (descTitle == null) { descTitle = "输入密码验证"; } if (mLocalizedReason == null) { mLocalizedReason = "通过已有指纹验证"; } //BiometricPromptCompat biometricPrompt = // new BiometricPromptCompat.Builder(Application.Activity).SetTitle("验证").SetSubtitle(mLocalizedReason).SetDescription("").SetNegativeButton(descTitle, new Android.Content.IDialogInterfaceOnClickListenerInvoker().Build(); BiometricPromptCompat biometricPrompt = new BiometricPromptCompat.Builder(Application.Activity).SetTitle("验证").SetSubtitle(mLocalizedReason).SetDescription("").SetNegativeButton(descTitle, new MyIputPasswordClickListener(this)).Build(); CancellationSignal cancellationSignal = new CancellationSignal(); cancellationSignal.CancelEvent += (sender1, e1) => { Shared.HDLUtils.WriteLine("TouchIDUtils:点击了取消按钮"); }; //biometricPrompt.Authenticate(cancellationSignal, null); biometricPrompt.Authenticate(cancellationSignal, new OnIAuthenticationCallback(this)); } class MyIputPasswordClickListener : Java.Lang.Object,Android.Content.IDialogInterfaceOnClickListener { TouchIDUtils _TouchIDUtils; public MyIputPasswordClickListener(TouchIDUtils obj) { _TouchIDUtils = obj; } public void OnClick(IDialogInterface dialog, int which) { _TouchIDUtils.OnHDLTouchIDStateBackEvent?.Invoke(this, TouchIDState.InputPassword); } } //①. OnAuthenticationError(int errorCode, ICharSequence errString) 这个接口会再系统指纹认证出现不可恢复的错误的时候才会调用,并且参数errorCode就给出了错误码,标识了错误的原因。 // 在AndroidP以前,这个方法回调回来之后,指纹识别sensor将会被关闭,也就是说,你再把手指放在指纹硬件上,将不会有反应了。这时候你需要提示用户关闭指纹识别弹窗,或改用密码支付等等 // 什么情况下会回调error错误呢?比如,连续识别错误5次指纹、指纹硬件不可用等等。 //②. OnAuthenticationFailed()这个接口会在系统指纹认证失败的情况的下才会回调。注意这里的认证失败和上面的认证错误是不一样的,虽然结果都是不能认证。 // 认证失败是指所有的信息都采集完整,并且没有任何异常,但是这个指纹和之前注册的指纹是不相符的;但是认证错误是指在采集或者认证的过程中出现了错误, // 比如指纹传感器工作异常等。也就是说认证失败是一个可以预期的正常情况,而认证错误是不可预期的异常情况。 //③.OnAuthenticationHelp(int helpMsgId, ICharSequence helpString)上面的认证失败是认证过程中的一个异常情况,我们说那种情况是因为出现了不可恢复的错误, // 而我们这里的OnAuthenticationHelp方法是出现了可以回复的异常才会调用的。什么是可以恢复的异常呢?一个常见的例子就是:手指移动太快,当我们把手指放到传感器上的时候, // 如果我们很快地将手指移走的话,那么指纹传感器可能只采集了部分的信息,因此认证会失败。但是这个错误是可以恢复的,因此只要提示用户再次按下指纹,并且不要太快移走就可以解决。 //④. OnAuthenticationSucceeded(FingerprintManagerCompati.AuthenticationResult result)这个接口会在认证成功之后回调。我们可以在这个方法中提示用户认证成功。 // 这里需要说明一下,如果我们上面在调用authenticate的时候,我们的CryptoObject不是null的话,那么我们在这个方法中可以通过AuthenticationResult来获得Cypher对象然后调用它的doFinal方法。 // doFinal方法会检查结果是不是会拦截或者篡改过,如果是的话会抛出一个异常。当我们发现这些异常的时候都应该将认证当做是失败来来处理,为了安全建议大家都这么做。 /// /// OnProgressChangeListener 继承响应事件 /// OnIAuthenticationCallback mOnIAuthenticationCallback; //BiometricPromptCompat.IAuthenticationCallback public class OnIAuthenticationCallback : Java.Lang.Object,BiometricPromptCompat.IAuthenticationCallback { TouchIDUtils _TouchIDUtils; public OnIAuthenticationCallback(TouchIDUtils obj) { _TouchIDUtils = obj; } public void OnAuthenticationError(int mErrorId, ICharSequence mErrorStr) { //throw new NotImplementedException(); if (mErrorId == 7) { _TouchIDUtils.OnHDLTouchIDStateBackEvent?.Invoke(this, TouchIDState.NotSupport); //Shared.HDLUtils.WriteLine("KK:Error:" + mErrorStr.ToString()); } else { Shared.HDLUtils.WriteLine("TouchIDUtils:Error" + mErrorStr.ToString() + " CODE:" + mErrorId); } } public void OnAuthenticationFailed() { //throw new NotImplementedException(); Shared.HDLUtils.WriteLine("TouchIDUtils:Failed无法识别,请再试一次"); } public void OnAuthenticationHelp(int mErrorId, ICharSequence mErrorStr) { //throw new NotImplementedException(); //Shared.HDLUtils.WriteLine("KK:Help无法识别,请再试一次"); Shared.HDLUtils.WriteLine("TouchIDUtils:Error" + mErrorStr.ToString() + " CODE:" + mErrorId); } public void OnAuthenticationSucceeded(BiometricPromptCompat.IAuthenticationResult p0) { _TouchIDUtils.OnHDLTouchIDStateBackEvent?.Invoke(this, TouchIDState.Success); } } // 单例 static TouchIDUtils ourInstance = new TouchIDUtils(); public static TouchIDUtils Instance { get { return ourInstance; } } } }