diff --git a/app/build.gradle b/app/build.gradle index 410211553..76b0864ed 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,7 +5,7 @@ android { buildToolsVersion = "26.0.2" defaultConfig { applicationId = "com.android.keepass" - minSdkVersion 14 + minSdkVersion 11 targetSdkVersion 25 versionCode = 1 versionName = "2.5.0.0" diff --git a/app/src/main/java/com/keepassdroid/PasswordActivity.java b/app/src/main/java/com/keepassdroid/PasswordActivity.java index bb7e475f0..969820abb 100644 --- a/app/src/main/java/com/keepassdroid/PasswordActivity.java +++ b/app/src/main/java/com/keepassdroid/PasswordActivity.java @@ -32,10 +32,12 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; +import android.support.annotation.RequiresApi; import android.support.v4.hardware.fingerprint.FingerprintManagerCompat; import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -58,9 +60,7 @@ import com.keepassdroid.database.edit.OnFinish; import com.keepassdroid.dialog.PasswordEncodingDialogHelper; import com.keepassdroid.fileselect.BrowserDialog; import com.keepassdroid.fingerprint.FingerPrintHelper; -import com.keepassdroid.fingerprint.FingerPrintUtils; import com.keepassdroid.intents.Intents; -import com.keepassdroid.settings.SettingsActivity; import com.keepassdroid.utils.EmptyUtils; import com.keepassdroid.utils.Interaction; import com.keepassdroid.utils.MenuUtil; @@ -92,6 +92,8 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp SharedPreferences prefsNoBackup; private FingerPrintHelper fingerPrintHelper; + private boolean fingerprintMustBeConfigured = true; + private int mode; private static final String PREF_KEY_VALUE_PREFIX = "valueFor_"; // key is a combination of db file name and this prefix private static final String PREF_KEY_IV_PREFIX = "ivFor_"; // key is a combination of db file name and this prefix @@ -140,7 +142,6 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { - case KeePass.EXIT_NORMAL: setEditText(R.id.password, ""); App.getDB().clear(); @@ -193,10 +194,6 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp prefs = PreferenceManager.getDefaultSharedPreferences(this); prefsNoBackup = getSharedPreferences("nobackup", Context.MODE_PRIVATE); - prefsNoBackup.edit() - .putString("test", "test") - .commit(); - mRememberKeyfile = prefs.getBoolean(getString(R.string.keyfile_key), getResources().getBoolean(R.bool.keyfile_default)); setContentView(R.layout.password); @@ -214,8 +211,6 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp passwordView = (EditText) findViewById(R.id.password); new InitTask().execute(i); - - initForFingerprint(); } @Override @@ -233,7 +228,10 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp App.clearShutdown(); // checks if fingerprint is available, will also start listening for fingerprints when available - checkAvailability(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + initForFingerprint(); + checkAvailability(); + } } private void retrieveSettings() { @@ -246,7 +244,6 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp private Uri getKeyFile(Uri dbUri) { if (mRememberKeyfile) { - return App.getFileHistory().getFileByName(dbUri); } else { return null; @@ -261,102 +258,90 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp setEditText(R.id.pass_keyfile, key); } - /* - private void errorMessage(CharSequence text) - { - Toast.makeText(this, text, Toast.LENGTH_LONG).show(); - } - */ - private void errorMessage(int resId) { Toast.makeText(this, resId, Toast.LENGTH_LONG).show(); } // fingerprint related code here - + @RequiresApi(api = Build.VERSION_CODES.M) private void initForFingerprint() { - if (FingerPrintUtils.isFingerprintSupported()) { - fingerPrintHelper = new FingerPrintHelper(this, this); + 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) { + // 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 onTextChanged( - final CharSequence s, - final int start, - final int before, - final int count) { - - } - - @Override - public void afterTextChanged(final Editable s) { + @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 confirmationView.setText(validInput ? R.string.store_with_fingerprint : R.string.scanning_fingerprint); mode = validInput ? toggleMode(Cipher.ENCRYPT_MODE) : toggleMode(Cipher.DECRYPT_MODE); - } - }); + } + }); - // callback for fingerprint findings - fingerPrintHelper.setAuthenticationCallback(new FingerprintManagerCompat.AuthenticationCallback() { - @Override - public void onAuthenticationError( - final int errorCode, - final CharSequence errString) { + // callback for fingerprint findings + fingerPrintHelper.setAuthenticationCallback(new FingerprintManagerCompat.AuthenticationCallback() { + @Override + public void onAuthenticationError( + final int errorCode, + final CharSequence errString) { - // this is triggered on stop/start listening done by helper to switch between modes so don't restart here - // errorCode = 5 - // errString = "Fingerprint operation canceled." - //onException(); - //confirmationView.setText(errString); - // true false fingerprint readings are handled otherwise with the toast messages, see below in code - } + // this is triggered on stop/start listening done by helper to switch between modes so don't restart here + // errorCode = 5 + // errString = "Fingerprint operation canceled." + //onFingerprintException(); + //confirmationView.setText(errString); + // true false fingerprint readings are handled otherwise with the toast messages, see below in code + } - @Override - public void onAuthenticationHelp( - final int helpCode, - final CharSequence helpString) { + @Override + public void onAuthenticationHelp( + final int helpCode, + final CharSequence helpString) { - onException(); - confirmationView.setText(helpString); - } + onFingerprintException(new Exception("onAuthenticationHelp")); + confirmationView.setText(helpString); + } - @Override - public void onAuthenticationSucceeded(final FingerprintManagerCompat.AuthenticationResult result) { + @Override + public void onAuthenticationSucceeded(final FingerprintManagerCompat.AuthenticationResult result) { - if (mode == Cipher.ENCRYPT_MODE) { + if (mode == Cipher.ENCRYPT_MODE) { - // newly store the entered password in encrypted way - final String password = passwordView.getText().toString(); - fingerPrintHelper.encryptData(password); + // newly store the entered password in encrypted way + final String password = passwordView.getText().toString(); + fingerPrintHelper.encryptData(password); - } else if (mode == Cipher.DECRYPT_MODE) { + } else if (mode == Cipher.DECRYPT_MODE) { - // retrieve the encrypted value from preferences - final String encryptedValue = prefsNoBackup.getString(getPreferenceKeyValue(), null); - if (encryptedValue != null) { - fingerPrintHelper.decryptData(encryptedValue); - } + // retrieve the encrypted value from preferences + final String encryptedValue = prefsNoBackup.getString(getPreferenceKeyValue(), null); + if (encryptedValue != null) { + fingerPrintHelper.decryptData(encryptedValue); } } + } - @Override - public void onAuthenticationFailed() { - onException(); - } - }); - } + @Override + public void onAuthenticationFailed() { + onFingerprintException(new Exception("onAuthenticationFailed")); + } + }); } private String getPreferenceKeyValue() { @@ -368,23 +353,19 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp return PREF_KEY_IV_PREFIX + (mDbUri != null ? mDbUri.getPath() : ""); } + @RequiresApi(api = Build.VERSION_CODES.M) private int toggleMode(final int newMode) { - // check if mode is different so we can update fingerprint helper - if (mode != newMode) { - mode = newMode; - switch (mode) { - case Cipher.ENCRYPT_MODE: - fingerPrintHelper.initEncryptData(); - break; - case Cipher.DECRYPT_MODE: - final String ivSpecValue = prefsNoBackup.getString(getPreferenceKeyIvSpec(), null); - fingerPrintHelper.initDecryptData(ivSpecValue); - break; - } - return newMode; + mode = newMode; + switch (mode) { + case Cipher.ENCRYPT_MODE: + fingerPrintHelper.initEncryptData(); + break; + case Cipher.DECRYPT_MODE: + final String ivSpecValue = prefsNoBackup.getString(getPreferenceKeyIvSpec(), null); + fingerPrintHelper.initDecryptData(ivSpecValue); + break; } - // remains in current mode - return mode; + return newMode; } @Override @@ -401,9 +382,10 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp confirmationView.setVisibility(vis); } + @RequiresApi(api = Build.VERSION_CODES.M) private void checkAvailability() { // fingerprint not supported (by API level or hardware) so keep option hidden - if (!FingerPrintUtils.isFingerprintSupported()) { + if (!fingerPrintHelper.isFingerprintSupported(FingerprintManagerCompat.from(this))) { setFingerPrintVisibility(View.GONE); } // fingerprint is available but not configured show icon but in disabled state with some information @@ -418,19 +400,17 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp } // finally fingerprint available and configured so we can use it else { - + fingerprintMustBeConfigured = false; setFingerPrintVisibility(View.VISIBLE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { fingerprintView.setAlpha(1f); } // fingerprint available but no stored password found yet for this DB so show info don't listen if (prefsNoBackup.getString(getPreferenceKeyValue(), null) == null) { - confirmationView.setText(R.string.no_password_stored); } // all is set here so we can confirm to user and start listening for fingerprints else { - confirmationView.setText(R.string.scanning_fingerprint); // listen for decryption by default toggleMode(Cipher.DECRYPT_MODE); @@ -459,24 +439,20 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp confirmButton.performClick(); } + @RequiresApi(api = Build.VERSION_CODES.M) @Override public void onInvalidKeyException() { Toast.makeText(this, R.string.fingerprint_invalid_key, Toast.LENGTH_SHORT).show(); checkAvailability(); // restarts listening } + @RequiresApi(api = Build.VERSION_CODES.M) @Override - public void onException() { - onException(true); - } - - @Override - public void onException(boolean showMessage) { - if (showMessage) { - Toast.makeText(this, R.string.fingerprint_error, Toast.LENGTH_SHORT).show(); - } - checkAvailability(); // restarts listening - + public void onFingerprintException(Exception e) { + Toast.makeText(this, R.string.fingerprint_error, Toast.LENGTH_SHORT).show(); + checkAvailability(); + e.printStackTrace(); + Log.e(this.getClass().getName(), e.getMessage()); } private class DefaultCheckChange implements CompoundButton.OnCheckedChangeListener { @@ -549,8 +525,6 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp int resId, String str) { TextView te = (TextView) findViewById(resId); - assert (te == null); - if (te != null) { te.setText(str); } @@ -741,8 +715,8 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp } private void showBrowserDialog() { - BrowserDialog diag = new BrowserDialog(PasswordActivity.this); - diag.show(); + BrowserDialog browserDialog = new BrowserDialog(PasswordActivity.this); + browserDialog.show(); } }); diff --git a/app/src/main/java/com/keepassdroid/fingerprint/FingerPrintHelper.java b/app/src/main/java/com/keepassdroid/fingerprint/FingerPrintHelper.java index d1da0e1bc..040a056ee 100644 --- a/app/src/main/java/com/keepassdroid/fingerprint/FingerPrintHelper.java +++ b/app/src/main/java/com/keepassdroid/fingerprint/FingerPrintHelper.java @@ -23,9 +23,11 @@ import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.KeyguardManager; import android.content.Context; +import android.os.Build; import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.KeyPermanentlyInvalidatedException; import android.security.keystore.KeyProperties; +import android.support.annotation.RequiresApi; import android.support.v4.hardware.fingerprint.FingerprintManagerCompat; import android.support.v4.os.CancellationSignal; import android.util.Base64; @@ -60,13 +62,7 @@ public class FingerPrintHelper { } public void startListening() { - // no need to start listening when not initialised - if (!isFingerprintInitialized()) { - if (fingerPrintCallback != null) { - fingerPrintCallback.onException(); - } - return; - } + // starts listening for fingerprints with the initialised crypto object cancellationSignal = new CancellationSignal(); fingerprintManager.authenticate( @@ -88,17 +84,10 @@ public class FingerPrintHelper { } public interface FingerPrintCallback { - void handleEncryptedResult(String value, String ivSpec); - void handleDecryptedResult(String value); - void onInvalidKeyException(); - - void onException(); - - void onException(boolean showWarningMessage); - + void onFingerprintException(Exception e); } @TargetApi(BuildCompat.VERSION_CODE_M) @@ -106,12 +95,12 @@ public class FingerPrintHelper { final Context context, final FingerPrintCallback fingerPrintCallback) { - if (!FingerPrintUtils.isFingerprintSupported()) { + this.fingerprintManager = FingerprintManagerCompat.from(context); + if (!isFingerprintSupported(fingerprintManager)) { // really not much to do when no fingerprint support found setInitOk(false); return; } - this.fingerprintManager = FingerprintManagerCompat.from(context); this.keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); this.fingerPrintCallback = fingerPrintCallback; @@ -129,21 +118,27 @@ public class FingerPrintHelper { setInitOk(true); } catch (final Exception e) { setInitOk(false); - fingerPrintCallback.onException(); + fingerPrintCallback.onFingerprintException(e); } } } - public boolean isFingerprintInitialized() { - return hasEnrolledFingerprints() && initOk; + public boolean isFingerprintSupported(FingerprintManagerCompat fingerprintManager) { + return Build.VERSION.SDK_INT >= BuildCompat.VERSION_CODE_M + && fingerprintManager.isHardwareDetected(); } - public void initEncryptData() { + public boolean isFingerprintInitialized() { + boolean isFingerprintInit = hasEnrolledFingerprints() && initOk; + if (!isFingerprintInit && fingerPrintCallback != null) { + fingerPrintCallback.onFingerprintException(new Exception("FingerPrint not initialized")); + } + return isFingerprintInit; + } + @RequiresApi(api = Build.VERSION_CODES.M) + public void initEncryptData() { if (!isFingerprintInitialized()) { - if (fingerPrintCallback != null) { - fingerPrintCallback.onException(); - } return; } try { @@ -158,16 +153,12 @@ public class FingerPrintHelper { } catch (final KeyPermanentlyInvalidatedException invalidKeyException) { fingerPrintCallback.onInvalidKeyException(); } catch (final Exception e) { - fingerPrintCallback.onException(); + fingerPrintCallback.onFingerprintException(e); } } public void encryptData(final String value) { - if (!isFingerprintInitialized()) { - if (fingerPrintCallback != null) { - fingerPrintCallback.onException(); - } return; } try { @@ -181,17 +172,13 @@ public class FingerPrintHelper { fingerPrintCallback.handleEncryptedResult(encryptedValue, ivSpecValue); } catch (final Exception e) { - fingerPrintCallback.onException(); + fingerPrintCallback.onFingerprintException(e); } - } + @RequiresApi(api = Build.VERSION_CODES.M) public void initDecryptData(final String ivSpecValue) { - if (!isFingerprintInitialized()) { - if (fingerPrintCallback != null) { - fingerPrintCallback.onException(false); - } return; } try { @@ -210,16 +197,12 @@ public class FingerPrintHelper { } catch (final KeyPermanentlyInvalidatedException invalidKeyException) { fingerPrintCallback.onInvalidKeyException(); } catch (final Exception e) { - fingerPrintCallback.onException(); + fingerPrintCallback.onFingerprintException(e); } } public void decryptData(final String encryptedValue) { - if (!isFingerprintInitialized()) { - if (fingerPrintCallback != null) { - fingerPrintCallback.onException(); - } return; } try { @@ -230,9 +213,8 @@ public class FingerPrintHelper { //final String encryptedString = Base64.encodeToString(encrypted, 0 /* flags */); fingerPrintCallback.handleDecryptedResult(decryptedString); - } catch (final Exception e) { - fingerPrintCallback.onException(); + fingerPrintCallback.onFingerprintException(e); } } @@ -267,28 +249,23 @@ public class FingerPrintHelper { keyGenerator.generateKey(); } } catch (final Exception e) { - fingerPrintCallback.onException(); + fingerPrintCallback.onFingerprintException(e); } } - public boolean isHardwareDetected() { - return FingerPrintUtils.isFingerprintSupported() - && fingerprintManager != null - && fingerprintManager.isHardwareDetected(); - } - @SuppressLint("NewApi") public boolean hasEnrolledFingerprints() { // fingerprint hardware supported and api level OK - return isHardwareDetected() - // fingerprints enrolled + return isFingerprintSupported(fingerprintManager) && fingerprintManager != null + && fingerprintManager.isHardwareDetected() + // fingerprints enrolled && fingerprintManager.hasEnrolledFingerprints() // and lockscreen configured && keyguardManager.isKeyguardSecure(); } - void setInitOk(final boolean initOk) { + private void setInitOk(final boolean initOk) { this.initOk = initOk; } diff --git a/app/src/main/java/com/keepassdroid/fingerprint/FingerPrintUtils.java b/app/src/main/java/com/keepassdroid/fingerprint/FingerPrintUtils.java deleted file mode 100644 index b2218c248..000000000 --- a/app/src/main/java/com/keepassdroid/fingerprint/FingerPrintUtils.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.keepassdroid.fingerprint; - -import android.os.Build; - -import com.keepassdroid.compat.BuildCompat; - -public class FingerPrintUtils { - - // Solve bug of VerifyError - public static boolean isFingerprintSupported() { - return Build.VERSION.SDK_INT >= BuildCompat.VERSION_CODE_M; - } -}