Search in sources :

Example 1 with FingerPrintHelper

use of com.keepassdroid.fingerprint.FingerPrintHelper in project KeePassDX by Kunzisoft.

the class PasswordActivity method initForFingerprint.

// fingerprint related code here
@RequiresApi(api = Build.VERSION_CODES.M)
private void initForFingerprint() {
    fingerPrintMode = NOT_CONFIGURED_MODE;
    fingerPrintHelper = new FingerPrintHelper(this, this);
    // when text entered we can enable the logon/purchase button and if required update encryption/decryption mode
    passwordView.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
        }

        @Override
        public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
        }

        @Override
        public void afterTextChanged(final Editable s) {
            if (!fingerprintMustBeConfigured) {
                final boolean validInput = s.length() > 0;
                // encrypt or decrypt mode based on how much input or not
                setFingerPrintView(validInput ? R.string.store_with_fingerprint : R.string.scanning_fingerprint);
                if (validInput)
                    toggleFingerprintMode(STORE_MODE);
                else
                    toggleFingerprintMode(OPEN_MODE);
            }
        }
    });
    // callback for fingerprint findings
    fingerPrintHelper.setAuthenticationCallback(new FingerprintManagerCompat.AuthenticationCallback() {

        @Override
        public void onAuthenticationError(final int errorCode, final CharSequence errString) {
            switch(errorCode) {
                case 5:
                    Log.i(TAG, "Fingerprint authentication error. Code : " + errorCode + " Error : " + errString);
                    break;
                default:
                    Log.e(TAG, "Fingerprint authentication error. Code : " + errorCode + " Error : " + errString);
                    setFingerPrintView(errString.toString(), true);
            }
        }

        @Override
        public void onAuthenticationHelp(final int helpCode, final CharSequence helpString) {
            Log.w(TAG, "Fingerprint authentication help. Code : " + helpCode + " Help : " + helpString);
            showError(helpString);
            setFingerPrintView(helpString.toString(), true);
            fingerprintTextView.setText(helpString);
        }

        @Override
        public void onAuthenticationFailed() {
            Log.e(TAG, "Fingerprint authentication failed, fingerprint not recognized");
            showError(R.string.fingerprint_not_recognized);
        }

        @Override
        public void onAuthenticationSucceeded(final FingerprintManagerCompat.AuthenticationResult result) {
            switch(fingerPrintMode) {
                case STORE_MODE:
                    // newly store the entered password in encrypted way
                    final String password = passwordView.getText().toString();
                    fingerPrintHelper.encryptData(password);
                    break;
                case OPEN_MODE:
                    // retrieve the encrypted value from preferences
                    final String encryptedValue = prefsNoBackup.getString(getPreferenceKeyValue(), null);
                    if (encryptedValue != null) {
                        fingerPrintHelper.decryptData(encryptedValue);
                    }
                    break;
            }
        }
    });
}
Also used : FingerprintManagerCompat(android.support.v4.hardware.fingerprint.FingerprintManagerCompat) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) FingerPrintHelper(com.keepassdroid.fingerprint.FingerPrintHelper) RequiresApi(android.support.annotation.RequiresApi)

Aggregations

RequiresApi (android.support.annotation.RequiresApi)1 FingerprintManagerCompat (android.support.v4.hardware.fingerprint.FingerprintManagerCompat)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 FingerPrintHelper (com.keepassdroid.fingerprint.FingerPrintHelper)1