@@ -50,6 +50,7 @@ public class FingerprintAuth extends CordovaPlugin {
5050 public static KeyStore mKeyStore ;
5151 public static KeyGenerator mKeyGenerator ;
5252 public static Cipher mCipher ;
53+ private FingerprintManager mFingerPrintManager ;
5354
5455 public static CallbackContext mCallbackContext ;
5556 public static PluginResult mPluginResult ;
@@ -82,6 +83,9 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
8283 mKeyguardManager = cordova .getActivity ().getSystemService (KeyguardManager .class );
8384 mPluginResult = new PluginResult (PluginResult .Status .NO_RESULT );
8485
86+ mFingerPrintManager = cordova .getActivity ().getApplicationContext ()
87+ .getSystemService (FingerprintManager .class );
88+
8589 try {
8690 mKeyGenerator = KeyGenerator .getInstance (
8791 KeyProperties .KEY_ALGORITHM_AES , ANDROID_KEY_STORE );
@@ -118,45 +122,66 @@ public boolean execute(final String action,
118122 JSONArray args ,
119123 CallbackContext callbackContext ) throws JSONException {
120124 mCallbackContext = callbackContext ;
121- Log .v (TAG , "FingerprintAuth action:" + action );
125+ Log .v (TAG , "FingerprintAuth action: " + action );
122126
123127 JSONObject arg_object = args .getJSONObject (0 );
124- mClientId = arg_object .getString ("clientId" );
125- mClientSecret = arg_object .getString ("clientSecret" );
126128
127129 if (action .equals ("authenticate" )) {
128- createKey ();
129- cordova .getActivity ().runOnUiThread (new Runnable () {
130- public void run () {
131- // Set up the crypto object for later. The object will be authenticated by use
132- // of the fingerprint.
133- if (initCipher ()) {
130+ mClientId = arg_object .getString ("clientId" );
131+ mClientSecret = arg_object .getString ("clientSecret" );
132+ if (isFingerprintAuthAvailable ()) {
133+ createKey ();
134+ cordova .getActivity ().runOnUiThread (new Runnable () {
135+ public void run () {
136+ // Set up the crypto object for later. The object will be authenticated by use
137+ // of the fingerprint.
138+ if (initCipher ()) {
134139
135- mFragment = new FingerprintAuthenticationDialogFragment ();
136- mFragment .setCancelable (false );
137- // Show the fingerprint dialog. The user has the option to use the fingerprint with
138- // crypto, or you can fall back to using a server-side verified password.
139- mFragment .setCryptoObject (new FingerprintManager .CryptoObject (mCipher ));
140- mFragment .show (cordova .getActivity ().getFragmentManager (), DIALOG_FRAGMENT_TAG );
141- } else {
142- // This happens if the lock screen has been disabled or or a fingerprint got
143- // enrolled. Thus show the dialog to authenticate with their password first
144- // and ask the user if they want to authenticate with fingerprints in the
145- // future
146- mFragment .setCryptoObject (new FingerprintManager .CryptoObject (mCipher ));
147- mFragment .setStage (
148- FingerprintAuthenticationDialogFragment .Stage .NEW_FINGERPRINT_ENROLLED );
149- mFragment .show (cordova .getActivity ().getFragmentManager (), DIALOG_FRAGMENT_TAG );
140+ mFragment = new FingerprintAuthenticationDialogFragment ();
141+ mFragment .setCancelable (false );
142+ // Show the fingerprint dialog. The user has the option to use the fingerprint with
143+ // crypto, or you can fall back to using a server-side verified password.
144+ mFragment .setCryptoObject (new FingerprintManager .CryptoObject (mCipher ));
145+ mFragment .show (cordova .getActivity ().getFragmentManager (), DIALOG_FRAGMENT_TAG );
146+ } else {
147+ // This happens if the lock screen has been disabled or or a fingerprint got
148+ // enrolled. Thus show the dialog to authenticate with their password first
149+ // and ask the user if they want to authenticate with fingerprints in the
150+ // future
151+ mFragment .setCryptoObject (new FingerprintManager .CryptoObject (mCipher ));
152+ mFragment .setStage (
153+ FingerprintAuthenticationDialogFragment .Stage .NEW_FINGERPRINT_ENROLLED );
154+ mFragment .show (cordova .getActivity ().getFragmentManager (), DIALOG_FRAGMENT_TAG );
155+ }
150156 }
151- }
152- });
153- mPluginResult .setKeepCallback (true );
157+ });
158+ mPluginResult .setKeepCallback (true );
159+ mCallbackContext .sendPluginResult (mPluginResult );
160+
161+ } else {
162+ mPluginResult = new PluginResult (PluginResult .Status .ERROR );
163+ mCallbackContext .error ("Fingerprint authentication not available" );
164+ mCallbackContext .sendPluginResult (mPluginResult );
165+ }
166+ return true ;
167+ } else if (action .equals ("availability" )) {
168+ JSONObject resultJson = new JSONObject ();
169+ resultJson .put ("isAvailable" , isFingerprintAuthAvailable ());
170+ resultJson .put ("isHardwareDetected" , mFingerPrintManager .isHardwareDetected ());
171+ resultJson .put ("hasEnrolledFingerprints" , mFingerPrintManager .hasEnrolledFingerprints ())
172+ mPluginResult = new PluginResult (PluginResult .Status .OK );
173+ mCallbackContext .success (resultJson );
154174 mCallbackContext .sendPluginResult (mPluginResult );
155175 return true ;
156176 }
157177 return false ;
158178 }
159179
180+ private boolean isFingerprintAuthAvailable () {
181+ return mFingerPrintManager .isHardwareDetected ()
182+ && mFingerPrintManager .hasEnrolledFingerprints ();
183+ }
184+
160185 /**
161186 * Initialize the {@link Cipher} instance with the created key in the {@link #createKey()}
162187 * method.
0 commit comments