Remove thread when typing

This commit is contained in:
J-Jamet
2018-01-26 17:11:22 +01:00
parent d921a0ae1a
commit c091ffb5e1
2 changed files with 14 additions and 16 deletions

View File

@@ -60,7 +60,6 @@ import com.keepassdroid.settings.PrefsUtil;
import com.keepassdroid.utils.EmptyUtils; import com.keepassdroid.utils.EmptyUtils;
import com.keepassdroid.utils.MenuUtil; import com.keepassdroid.utils.MenuUtil;
import com.keepassdroid.utils.UriUtil; import com.keepassdroid.utils.UriUtil;
import com.keepassdroid.utils.Util;
import com.keepassdroid.view.FingerPrintDialog; import com.keepassdroid.view.FingerPrintDialog;
import com.keepassdroid.view.KeyFileHelper; import com.keepassdroid.view.KeyFileHelper;
import com.kunzisoft.keepass.KeePass; import com.kunzisoft.keepass.KeePass;
@@ -92,6 +91,7 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp
private int mode; 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_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 private static final String PREF_KEY_IV_PREFIX = "ivFor_"; // key is a combination of db file name and this prefix
private String prefFingerprintKey;
private View fingerprintContainerView; private View fingerprintContainerView;
private View fingerprintImageView; private View fingerprintImageView;
@@ -238,6 +238,8 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
prefFingerprintKey = getPreferenceKeyValue();
// If the application was shutdown make sure to clear the password field, if it // If the application was shutdown make sure to clear the password field, if it
// was saved in the instance state // was saved in the instance state
if (App.isShutdown()) { if (App.isShutdown()) {
@@ -317,11 +319,7 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp
final boolean validInput = s.length() > 0; final boolean validInput = s.length() > 0;
// encrypt or decrypt mode based on how much input or not // encrypt or decrypt mode based on how much input or not
setFingerPrintTextView(validInput ? R.string.store_with_fingerprint : R.string.scanning_fingerprint); setFingerPrintTextView(validInput ? R.string.store_with_fingerprint : R.string.scanning_fingerprint);
new Thread(new Runnable() { mode = validInput ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
public void run() {
mode = validInput ? toggleMode(Cipher.ENCRYPT_MODE) : toggleMode(Cipher.DECRYPT_MODE);
}
}).start();
} }
} }
}); });
@@ -363,7 +361,7 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp
} else if (mode == Cipher.DECRYPT_MODE) { } else if (mode == Cipher.DECRYPT_MODE) {
// retrieve the encrypted value from preferences // retrieve the encrypted value from preferences
final String encryptedValue = prefsNoBackup.getString(getPreferenceKeyValue(), null); final String encryptedValue = prefsNoBackup.getString(prefFingerprintKey, null);
if (encryptedValue != null) { if (encryptedValue != null) {
fingerPrintHelper.decryptData(encryptedValue); fingerPrintHelper.decryptData(encryptedValue);
} }
@@ -476,7 +474,7 @@ public class PasswordActivity extends LockingActivity implements FingerPrintHelp
setFingerPrintAlphaImageView(1f); setFingerPrintAlphaImageView(1f);
// fingerprint available but no stored password found yet for this DB so show info don't listen // fingerprint available but no stored password found yet for this DB so show info don't listen
if (prefsNoBackup.getString(getPreferenceKeyValue(), null) == null) { if (prefsNoBackup.getString(prefFingerprintKey, null) == null) {
setFingerPrintTextView(R.string.no_password_stored); setFingerPrintTextView(R.string.no_password_stored);
} }
// all is set here so we can confirm to user and start listening for fingerprints // all is set here so we can confirm to user and start listening for fingerprints

View File

@@ -45,7 +45,7 @@ import javax.crypto.spec.IvParameterSpec;
public class FingerPrintHelper { public class FingerPrintHelper {
private static final String ALIAS_KEY = "example-key"; private static final String FINGERPRINT_KEYSTORE_KEY = "example-key";
private FingerprintManagerCompat fingerprintManager; private FingerprintManagerCompat fingerprintManager;
private KeyStore keyStore = null; private KeyStore keyStore = null;
@@ -146,7 +146,7 @@ public class FingerPrintHelper {
try { try {
createNewKeyIfNeeded(false); // no need to keep deleting existing keys createNewKeyIfNeeded(false); // no need to keep deleting existing keys
keyStore.load(null); keyStore.load(null);
final SecretKey key = (SecretKey) keyStore.getKey(ALIAS_KEY, null); final SecretKey key = (SecretKey) keyStore.getKey(FINGERPRINT_KEYSTORE_KEY, null);
cipher.init(Cipher.ENCRYPT_MODE, key); cipher.init(Cipher.ENCRYPT_MODE, key);
stopListening(); stopListening();
@@ -187,7 +187,7 @@ public class FingerPrintHelper {
try { try {
createNewKeyIfNeeded(false); createNewKeyIfNeeded(false);
keyStore.load(null); keyStore.load(null);
final SecretKey key = (SecretKey) keyStore.getKey(ALIAS_KEY, null); final SecretKey key = (SecretKey) keyStore.getKey(FINGERPRINT_KEYSTORE_KEY, null);
// important to restore spec here that was used for decryption // important to restore spec here that was used for decryption
final byte[] iv = Base64.decode(ivSpecValue, Base64.DEFAULT); final byte[] iv = Base64.decode(ivSpecValue, Base64.DEFAULT);
@@ -231,18 +231,18 @@ public class FingerPrintHelper {
try { try {
keyStore.load(null); keyStore.load(null);
if (allowDeleteExisting if (allowDeleteExisting
&& keyStore.containsAlias(ALIAS_KEY)) { && keyStore.containsAlias(FINGERPRINT_KEYSTORE_KEY)) {
keyStore.deleteEntry(ALIAS_KEY); keyStore.deleteEntry(FINGERPRINT_KEYSTORE_KEY);
} }
// Create new key if needed // Create new key if needed
if (!keyStore.containsAlias(ALIAS_KEY)) { if (!keyStore.containsAlias(FINGERPRINT_KEYSTORE_KEY)) {
// Set the alias of the entry in Android KeyStore where the key will appear // Set the alias of the entry in Android KeyStore where the key will appear
// and the constrains (purposes) in the constructor of the Builder // and the constrains (purposes) in the constructor of the Builder
keyGenerator.init( keyGenerator.init(
new KeyGenParameterSpec.Builder( new KeyGenParameterSpec.Builder(
ALIAS_KEY, FINGERPRINT_KEYSTORE_KEY,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT) KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
@@ -260,7 +260,7 @@ public class FingerPrintHelper {
private void deleteEntryKey() { private void deleteEntryKey() {
try { try {
keyStore.deleteEntry(ALIAS_KEY); keyStore.deleteEntry(FINGERPRINT_KEYSTORE_KEY);
} catch (KeyStoreException e) { } catch (KeyStoreException e) {
fingerPrintCallback.onFingerPrintException(e); fingerPrintCallback.onFingerPrintException(e);
} }