Code factoring

This commit is contained in:
Jeremy committed 2018-01-28 16:37:18 +01:00
1 parent 9ac060ea05
commit 21c657c107
3 files changed
+75 -55

No files matched your search

@@ -39,7 +39,6 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
@@ -96,8 +95,9 @@ public class PasswordActivity extends LockingActivity
private EditText passwordView;
private EditText keyFileView;
private Button confirmButtonView;
private CheckBox checkboxPasswordView;
private CheckBox checkboxKeyfileView;
private CompoundButton checkboxPasswordView;
private CompoundButton checkboxKeyfileView;
private CompoundButton checkboxDefaultDatabaseView;
private KeyFileHelper keyFileHelper;
@@ -171,13 +171,9 @@ public class PasswordActivity extends LockingActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefsNoBackup = PrefsUtil.getNoBackupSharedPreferences(getApplicationContext());
boolean mRememberKeyfile = prefs.getBoolean(getString(R.string.keyfile_key),
getResources().getBoolean(R.bool.keyfile_default));
setContentView(R.layout.password);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
@@ -194,8 +190,9 @@ public class PasswordActivity extends LockingActivity
filenameView = (TextView) findViewById(R.id.filename);
passwordView = (EditText) findViewById(R.id.password);
keyFileView = (EditText) findViewById(R.id.pass_keyfile);
checkboxPasswordView = (CheckBox) findViewById(R.id.password_checkbox);
checkboxKeyfileView = (CheckBox) findViewById(R.id.keyfile_checkox);
checkboxPasswordView = (CompoundButton) findViewById(R.id.password_checkbox);
checkboxKeyfileView = (CompoundButton) findViewById(R.id.keyfile_checkox);
checkboxDefaultDatabaseView = (CompoundButton) findViewById(R.id.default_database);
passwordView.addTextChangedListener(new TextWatcher() {
@Override
@@ -222,12 +219,15 @@ public class PasswordActivity extends LockingActivity
}
});
new UriIntentInitTask(this,
prefs.getBoolean(getString(R.string.keyfile_key),
getResources().getBoolean(R.bool.keyfile_default)))
.execute(getIntent());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fingerPrintAnimatedVector = new FingerPrintAnimatedVector(this,
(ImageView) fingerprintImageView);
}
new UriIntentInitTask(this, mRememberKeyfile).execute(i);
}
@Override
@@ -248,15 +248,13 @@ public class PasswordActivity extends LockingActivity
populateView();
Button confirmButton = (Button) findViewById(R.id.pass_ok);
confirmButton.setOnClickListener(new OkClickHandler());
confirmButtonView.setOnClickListener(new OkClickHandler());
if (password != null) {
passwordView.setText(password);
}
CompoundButton defaultCheck = (CompoundButton) findViewById(R.id.default_database);
defaultCheck.setOnCheckedChangeListener(new DefaultCheckChange());
checkboxDefaultDatabaseView.setOnCheckedChangeListener(new DefaultCheckChange());
View browseView = findViewById(R.id.browse_button);
keyFileHelper = new KeyFileHelper(PasswordActivity.this);
@@ -278,8 +276,7 @@ public class PasswordActivity extends LockingActivity
if (mDbUri!=null
&& !EmptyUtils.isNullOrEmpty(mDbUri.getPath())
&& UriUtil.equalsDefaultfile(mDbUri, defaultFilename)) {
CompoundButton checkbox = (CompoundButton) findViewById(R.id.default_database);
checkbox.setChecked(true);
checkboxDefaultDatabaseView.setChecked(true);
}
}
@@ -327,6 +324,7 @@ public class PasswordActivity extends LockingActivity
@RequiresApi(api = Build.VERSION_CODES.M)
private void initForFingerprint() {
mode = -1;
fingerPrintHelper = new FingerPrintHelper(this, this);
// when text entered we can enable the logon/purchase button and if required update encryption/decryption mode
@@ -528,7 +526,6 @@ public class PasswordActivity extends LockingActivity
public void handleEncryptedResult(
final String value,
final String ivSpec) {
prefsNoBackup.edit()
.putString(getPreferenceKeyValue(), value)
.putString(getPreferenceKeyIvSpec(), ivSpec)
@@ -547,8 +544,13 @@ public class PasswordActivity extends LockingActivity
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onInvalidKeyException() {
public void onInvalidKeyException(Exception e) {
showError(R.string.fingerprint_invalid_key);
prefsNoBackup.edit()
.remove(getPreferenceKeyValue())
.remove(getPreferenceKeyIvSpec())
.apply();
e.printStackTrace();
checkFingerprintAvailability(); // restarts listening
}
@@ -556,8 +558,8 @@ public class PasswordActivity extends LockingActivity
@Override
public void onFingerPrintException(Exception e) {
showError(R.string.fingerprint_error);
checkFingerprintAvailability();
e.printStackTrace();
checkFingerprintAvailability();
}
private void showError(final int messageId) {
@@ -41,6 +41,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
@@ -88,13 +89,6 @@ public class FingerPrintHelper {
}
}
public interface FingerPrintCallback {
void handleEncryptedResult(String value, String ivSpec);
void handleDecryptedResult(String value);
void onInvalidKeyException();
void onFingerPrintException(Exception e);
}
@TargetApi(BuildCompat.VERSION_CODE_M)
public FingerPrintHelper(
final Context context,
@@ -163,7 +157,7 @@ public class FingerPrintHelper {
} catch (final UnrecoverableKeyException unrecoverableKeyException) {
deleteEntryKey();
} catch (final KeyPermanentlyInvalidatedException invalidKeyException) {
fingerPrintCallback.onInvalidKeyException();
fingerPrintCallback.onInvalidKeyException(invalidKeyException);
} catch (final Exception e) {
fingerPrintCallback.onFingerPrintException(e);
}
@@ -207,7 +201,7 @@ public class FingerPrintHelper {
startListening();
} catch (final KeyPermanentlyInvalidatedException invalidKeyException) {
fingerPrintCallback.onInvalidKeyException();
fingerPrintCallback.onInvalidKeyException(invalidKeyException);
} catch (final UnrecoverableKeyException unrecoverableKeyException) {
deleteEntryKey();
} catch (final Exception e) {
@@ -227,6 +221,8 @@ public class FingerPrintHelper {
//final String encryptedString = Base64.encodeToString(encrypted, 0 /* flags */);
fingerPrintCallback.handleDecryptedResult(decryptedString);
} catch (final BadPaddingException badPaddingException) {
fingerPrintCallback.onInvalidKeyException(badPaddingException);
} catch (final Exception e) {
fingerPrintCallback.onFingerPrintException(e);
}
@@ -274,7 +270,6 @@ public class FingerPrintHelper {
| CertificateException
| NoSuchAlgorithmException
| IOException e) {
e.printStackTrace();
fingerPrintCallback.onFingerPrintException(e);
}
}
@@ -293,4 +288,40 @@ public class FingerPrintHelper {
this.initOk = initOk;
}
/**
* Remove entry key in keystore
*/
public static void deleteEntryKeyInKeystoreForFingerprints(final Context context,
final FingerPrintErrorCallback fingerPrintCallback) {
FingerPrintHelper fingerPrintHelper = new FingerPrintHelper(
context, new FingerPrintCallback() {
@Override
public void handleEncryptedResult(String value, String ivSpec) {}
@Override
public void handleDecryptedResult(String value) {}
@Override
public void onInvalidKeyException(Exception e) {
fingerPrintCallback.onInvalidKeyException(e);
}
@Override
public void onFingerPrintException(Exception e) {
fingerPrintCallback.onFingerPrintException(e);
}
});
fingerPrintHelper.deleteEntryKey();
}
public interface FingerPrintErrorCallback {
void onInvalidKeyException(Exception e);
void onFingerPrintException(Exception e);
}
public interface FingerPrintCallback extends FingerPrintErrorCallback {
void handleEncryptedResult(String value, String ivSpec);
void handleDecryptedResult(String value);
}
}
@@ -136,7 +136,18 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat {
@Override
public void onClick(DialogInterface dialog,
int which) {
deleteAllKeysForFingerprints();
FingerPrintHelper.deleteEntryKeyInKeystoreForFingerprints(
getContext(),
new FingerPrintHelper.FingerPrintErrorCallback() {
@Override
public void onInvalidKeyException(Exception e) {}
@Override
public void onFingerPrintException(Exception e) {
Toast.makeText(getContext(), R.string.fingerprint_error, Toast.LENGTH_SHORT).show();
}
});
PrefsUtil.deleteAllValuesFromNoBackupPreferences(getContext());
}
})
.setNegativeButton(
@@ -185,30 +196,6 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat {
}
}
/**
* Delete all entries of NoBackup preference and remove entry key in keystore
*/
private void deleteAllKeysForFingerprints() {
FingerPrintHelper fingerPrintHelper = new FingerPrintHelper(
getContext(), new FingerPrintHelper.FingerPrintCallback() {
@Override
public void handleEncryptedResult(String value, String ivSpec) {}
@Override
public void handleDecryptedResult(String value) {}
@Override
public void onInvalidKeyException() {}
@Override
public void onFingerPrintException(Exception e) {
Toast.makeText(getContext(), R.string.fingerprint_error, Toast.LENGTH_SHORT).show();
}
});
fingerPrintHelper.deleteEntryKey();
PrefsUtil.deleteAllValuesFromNoBackupPreferences(getContext());
}
@Override
public void onDisplayPreferenceDialog(Preference preference) {
// Try if the preference is one of our custom Preferences