From dc37ab74c16c99f2d3f173dc88d3ac07427bfcd1 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Wed, 7 Aug 2019 10:56:04 +0200 Subject: [PATCH] Fix fingerprint --- .../keepass/activities/PasswordActivity.kt | 76 ++++++------ .../keepass/fingerprint/FingerPrintManager.kt | 109 ++++++++++-------- app/src/main/res/layout/activity_password.xml | 3 +- 3 files changed, 100 insertions(+), 88 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt index 86d1ebf4a..1c0dd97aa 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt @@ -117,6 +117,7 @@ class PasswordActivity : StylishActivity(), checkboxPasswordView = findViewById(R.id.password_checkbox) checkboxKeyFileView = findViewById(R.id.keyfile_checkox) checkboxDefaultDatabaseView = findViewById(R.id.default_database) + fingerPrintInfoView = findViewById(R.id.fingerprint_info) readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrPreference(this, savedInstanceState) @@ -152,27 +153,6 @@ class PasswordActivity : StylishActivity(), confirmButtonView?.isEnabled = isChecked } } - - // Init FingerPrint elements - if (PreferencesUtil.isFingerprintEnable(this) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - fingerPrintInfoView = findViewById(R.id.fingerprint_info) - fingerPrintViewsManager = FingerPrintViewsManager(this, - mDatabaseFileUri, - fingerPrintInfoView, - checkboxPasswordView, - enableButtonOnCheckedChangeListener, - passwordView) { password -> - // Load the database if password is registered or retrieve - password?.let { - // Retrieve from fingerprint - verifyKeyFileCheckboxAndLoadDatabase(password) - } ?: run { - // Register with fingerprint - verifyCheckboxesAndLoadDatabase() - } - } - fingerPrintViewsManager?.initFingerprint() - } } private val onEditorActionListener = object : TextView.OnEditorActionListener { @@ -204,16 +184,6 @@ class PasswordActivity : StylishActivity(), confirmButtonView?.isEnabled = true } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (PreferencesUtil.isFingerprintEnable(this)) { - fingerPrintViewsManager?.initFingerprint() - } else { - fingerPrintViewsManager?.destroy() - } - } else { - checkboxPasswordView?.setOnCheckedChangeListener(enableButtonOnCheckedChangeListener) - } - UriIntentInitTask(WeakReference(this), this, mRememberKeyFile) .execute(intent) } @@ -276,11 +246,6 @@ class PasswordActivity : StylishActivity(), checkboxDefaultDatabaseView?.isChecked = true } - // checks if fingerprint is available, will also start listening for fingerprints when available - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - fingerPrintViewsManager?.checkFingerprintAvailability() - } - // If Activity is launch with a password and want to open directly val intent = intent val password = intent.getStringExtra(KEY_PASSWORD) @@ -290,6 +255,39 @@ class PasswordActivity : StylishActivity(), } if (launchImmediately) { verifyCheckboxesAndLoadDatabase(password, keyFileUri) + } else { + // Init FingerPrint elements + var fingerPrintInit = false + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (PreferencesUtil.isFingerprintEnable(this)) { + if (fingerPrintViewsManager == null) { + fingerPrintViewsManager = FingerPrintViewsManager(this, + mDatabaseFileUri, + fingerPrintInfoView, + checkboxPasswordView, + enableButtonOnCheckedChangeListener, + passwordView) { passwordRetrieve -> + // Load the database if password is registered or retrieve + passwordRetrieve?.let { + // Retrieve from fingerprint + verifyKeyFileCheckboxAndLoadDatabase(it) + } ?: run { + // Register with fingerprint + verifyCheckboxesAndLoadDatabase() + } + } + } + fingerPrintViewsManager?.initFingerprint() + // checks if fingerprint is available, will also start listening for fingerprints when available + fingerPrintViewsManager?.checkFingerprintAvailability() + fingerPrintInit = true + } else { + fingerPrintViewsManager?.destroy() + } + } + if (!fingerPrintInit) { + checkboxPasswordView?.setOnCheckedChangeListener(enableButtonOnCheckedChangeListener) + } } } @@ -395,8 +393,10 @@ class PasswordActivity : StylishActivity(), runOnUiThread { // Recheck fingerprint if error if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - // Stay with the same mode - fingerPrintViewsManager?.reInitWithFingerprintMode() + if (PreferencesUtil.isFingerprintEnable(this@PasswordActivity)) { + // Stay with the same mode + fingerPrintViewsManager?.reInitWithFingerprintMode() + } } if (result.isSuccess) { diff --git a/app/src/main/java/com/kunzisoft/keepass/fingerprint/FingerPrintManager.kt b/app/src/main/java/com/kunzisoft/keepass/fingerprint/FingerPrintManager.kt index 7951c8b34..da861b983 100644 --- a/app/src/main/java/com/kunzisoft/keepass/fingerprint/FingerPrintManager.kt +++ b/app/src/main/java/com/kunzisoft/keepass/fingerprint/FingerPrintManager.kt @@ -53,9 +53,11 @@ class FingerPrintViewsManager(var context: AppCompatActivity, fingerPrintMode = FingerPrintHelper.Mode.NOT_CONFIGURED_MODE + showFingerPrintViews(true) // Start the animation fingerPrintInfoView?.startFingerPrintAnimation() + // Add a check listener to change fingerprint mode checkboxPasswordView?.setOnCheckedChangeListener { compoundButton, checked -> if (!fingerprintMustBeConfigured) { // encrypt or decrypt mode based on how much input or not @@ -77,50 +79,52 @@ class FingerPrintViewsManager(var context: AppCompatActivity, fingerPrintHelper = FingerPrintHelper(context, this) // callback for fingerprint findings - fingerPrintHelper?.setAuthenticationCallback(object : FingerprintManager.AuthenticationCallback() { - override fun onAuthenticationError( - errorCode: Int, - errString: CharSequence) { - when (errorCode) { - 5 -> Log.i(TAG, "Fingerprint authentication error. Code : $errorCode Error : $errString") - else -> { - Log.e(TAG, "Fingerprint authentication error. Code : $errorCode Error : $errString") - setFingerPrintView(errString.toString(), true) - } + fingerPrintHelper?.setAuthenticationCallback(authenticationCallback) + } + } + + private val authenticationCallback = object : FingerprintManager.AuthenticationCallback() { + override fun onAuthenticationError( + errorCode: Int, + errString: CharSequence) { + when (errorCode) { + 5 -> Log.i(TAG, "Fingerprint authentication error. Code : $errorCode Error : $errString") + else -> { + Log.e(TAG, "Fingerprint authentication error. Code : $errorCode Error : $errString") + setFingerPrintView(errString.toString(), true) + } + } + } + + override fun onAuthenticationHelp( + helpCode: Int, + helpString: CharSequence) { + Log.w(TAG, "Fingerprint authentication help. Code : $helpCode Help : $helpString") + showError(helpString) + setFingerPrintView(helpString.toString(), true) + fingerPrintInfoView?.text = helpString.toString() + } + + override fun onAuthenticationFailed() { + Log.e(TAG, "Fingerprint authentication failed, fingerprint not recognized") + showError(R.string.fingerprint_not_recognized) + } + + override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult) { + when (fingerPrintMode) { + FingerPrintHelper.Mode.STORE_MODE -> { + // newly store the entered password in encrypted way + fingerPrintHelper?.encryptData(passwordView?.text.toString()) + } + FingerPrintHelper.Mode.OPEN_MODE -> { + // retrieve the encrypted value from preferences + prefsNoBackup?.getString(preferenceKeyValue, null)?.let { + fingerPrintHelper?.decryptData(it) } } - - override fun onAuthenticationHelp( - helpCode: Int, - helpString: CharSequence) { - Log.w(TAG, "Fingerprint authentication help. Code : $helpCode Help : $helpString") - showError(helpString) - setFingerPrintView(helpString.toString(), true) - fingerPrintInfoView?.text = helpString.toString() - } - - override fun onAuthenticationFailed() { - Log.e(TAG, "Fingerprint authentication failed, fingerprint not recognized") - showError(R.string.fingerprint_not_recognized) - } - - override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult) { - when (fingerPrintMode) { - FingerPrintHelper.Mode.STORE_MODE -> { - // newly store the entered password in encrypted way - fingerPrintHelper?.encryptData(passwordView?.text.toString()) - } - FingerPrintHelper.Mode.OPEN_MODE -> { - // retrieve the encrypted value from preferences - prefsNoBackup?.getString(preferenceKeyValue, null)?.let { - fingerPrintHelper?.decryptData(it) - } - } - FingerPrintHelper.Mode.NOT_CONFIGURED_MODE -> {} - FingerPrintHelper.Mode.WAITING_PASSWORD_MODE -> {} - } - } - }) + FingerPrintHelper.Mode.NOT_CONFIGURED_MODE -> {} + FingerPrintHelper.Mode.WAITING_PASSWORD_MODE -> {} + } } } @@ -179,8 +183,13 @@ class FingerPrintViewsManager(var context: AppCompatActivity, } fun destroy() { + // Restore the checked listener + checkboxPasswordView?.setOnCheckedChangeListener(onCheckedPasswordChangeListener) + stopListening() - // TODO destroy + fingerPrintHelper = null + + showFingerPrintViews(false) } fun inflateOptionsMenu(menuInflater: MenuInflater, menu: Menu) { @@ -188,8 +197,8 @@ class FingerPrintViewsManager(var context: AppCompatActivity, menuInflater.inflate(R.menu.fingerprint, menu) } - private fun hideFingerPrintViews(hide: Boolean) { - context.runOnUiThread { fingerPrintInfoView?.hide = hide } + private fun showFingerPrintViews(show: Boolean) { + context.runOnUiThread { fingerPrintInfoView?.hide = !show } } private fun setFingerPrintView(textId: Int, lock: Boolean = false) { @@ -210,14 +219,16 @@ class FingerPrintViewsManager(var context: AppCompatActivity, // or manually disable if (!PreferencesUtil.isFingerprintEnable(context) || !FingerPrintHelper.isFingerprintSupported(context.getSystemService(FingerprintManager::class.java))) { - hideFingerPrintViews(true) + showFingerPrintViews(false) } else { + // all is set here so we can confirm to user and start listening for fingerprints // show explanations fingerPrintInfoView?.setOnClickListener { _ -> FingerPrintExplanationDialog().show(context.supportFragmentManager, "fingerprintDialog") } - hideFingerPrintViews(false) + showFingerPrintViews(true) + // fingerprint is available but not configured, show icon but in disabled state with some information if (fingerPrintHelper?.hasEnrolledFingerprints() != true) { // This happens when no fingerprints are registered. Listening won't start setFingerPrintView(R.string.configure_fingerprint, true) @@ -236,9 +247,9 @@ class FingerPrintViewsManager(var context: AppCompatActivity, } else { // listen for decryption initDecryptData() - }// all is set here so we can confirm to user and start listening for fingerprints + } }// finally fingerprint available and configured so we can use it - }// fingerprint is available but not configured show icon but in disabled state with some information + } // Show fingerprint key deletion context.invalidateOptionsMenu() diff --git a/app/src/main/res/layout/activity_password.xml b/app/src/main/res/layout/activity_password.xml index ad8c3296b..1b95ca8fa 100644 --- a/app/src/main/res/layout/activity_password.xml +++ b/app/src/main/res/layout/activity_password.xml @@ -66,7 +66,8 @@ + android:layout_height="wrap_content" + android:visibility="gone"/>