2222public class LocalAuthPlugin implements MethodCallHandler {
2323 private final Registrar registrar ;
2424 private final AtomicBoolean authInProgress = new AtomicBoolean (false );
25+ private AuthenticationHelper authenticationHelper ;
2526
2627 /** Plugin registration. */
2728 public static void registerWith (Registrar registrar ) {
@@ -37,7 +38,7 @@ private LocalAuthPlugin(Registrar registrar) {
3738 @ Override
3839 public void onMethodCall (MethodCall call , final Result result ) {
3940 if (call .method .equals ("authenticateWithBiometrics" )) {
40- if (! authInProgress .compareAndSet ( false , true )) {
41+ if (authInProgress .get ( )) {
4142 // Apps should not invoke another authentication request while one is in progress,
4243 // so we classify this as an error condition. If we ever find a legitimate use case for
4344 // this, we can try to cancel the ongoing auth and start a new one but for now, not worth
@@ -59,7 +60,8 @@ public void onMethodCall(MethodCall call, final Result result) {
5960 null );
6061 return ;
6162 }
62- AuthenticationHelper authenticationHelper =
63+ authInProgress .set (true );
64+ authenticationHelper =
6365 new AuthenticationHelper (
6466 (FragmentActivity ) activity ,
6567 call ,
@@ -112,8 +114,27 @@ public void onError(String code, String error) {
112114 } catch (Exception e ) {
113115 result .error ("no_biometrics_available" , e .getMessage (), null );
114116 }
117+ } else if (call .method .equals (("stopAuthentication" ))) {
118+ stopAuthentication (result );
115119 } else {
116120 result .notImplemented ();
117121 }
118122 }
123+
124+ /*
125+ Stops the authentication if in progress.
126+ */
127+ private void stopAuthentication (Result result ) {
128+ try {
129+ if (authenticationHelper != null && authInProgress .get ()) {
130+ authenticationHelper .stopAuthentication ();
131+ authenticationHelper = null ;
132+ result .success (true );
133+ return ;
134+ }
135+ result .success (false );
136+ } catch (Exception e ) {
137+ result .success (false );
138+ }
139+ }
119140}
0 commit comments