wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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<TouchIDState> OnHDLTouchIDStateBackEvent;
 
 
        //StateBlock mStateBlock;
 
        /// <summary>
        /// 弹窗指纹验证 descTitle:输入密码验证按钮标题   mLocalizedReason:Touch显示的描述
        /// </summary>
        /// <value>descTitle:输入密码验证按钮标题   mLocalizedReason:Touch显示的描述</value>
        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方法会检查结果是不是会拦截或者篡改过,如果是的话会抛出一个异常。当我们发现这些异常的时候都应该将认证当做是失败来来处理,为了安全建议大家都这么做。
 
 
 
 
 
        /// <summary>
        /// OnProgressChangeListener 继承响应事件
        /// </summary>
        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;
            }
        }
 
      
    }
}