#635 - More code cleanup in AuthenticateActivity

Get rid of the need for separate checks for setting layout hints and input type. Add some clarifying comments.
This commit is contained in:
Joshua Soberg 2021-01-27 21:02:19 -05:00
parent 56ad0630b4
commit 1686c61b69

View file

@ -62,10 +62,9 @@ public class AuthenticateActivity extends ThemedActivity
private AuthMethod authMethod;
private String newEncryption = "";
private boolean oldPassword = false;
private String password;
private boolean oldPassword = false;
private TextInputLayout passwordLayout;
private TextInputEditText passwordInput;
@Override
@ -82,6 +81,18 @@ public class AuthenticateActivity extends ThemedActivity
oldPassword = true;
}
// If our password is still empty at this point, we can't do anything.
if (password.isEmpty()) {
int missingPwResId = (authMethod == AuthMethod.PASSWORD)
? R.string.auth_toast_password_missing : R.string.auth_toast_pin_missing;
Toast.makeText(this, missingPwResId, Toast.LENGTH_LONG).show();
finishWithResult(true, null);
}
// If we're not using password or pin for auth method, we have nothing to authenticate here.
if (authMethod != AuthMethod.PASSWORD && authMethod != AuthMethod.PIN) {
finishWithResult(true, null);
}
setTitle(R.string.auth_activity_title);
setContentView(R.layout.activity_container);
initToolbar();
@ -105,20 +116,6 @@ public class AuthenticateActivity extends ThemedActivity
initPasswordLayoutView(v);
initPasswordInputView(v);
initUnlockButtonView(v);
if (authMethod == AuthMethod.PASSWORD) {
if (password.isEmpty())
finishFromEmptyPassword(R.string.auth_toast_password_missing);
else
setupPasswordFields(R.string.auth_hint_password, (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
} else if (authMethod == AuthMethod.PIN) {
if (password.isEmpty())
finishFromEmptyPassword(R.string.auth_toast_pin_missing);
else
setupPasswordFields(R.string.auth_hint_pin, (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD));
} else {
finishWithResult(true, null);
}
}
private void initPasswordLabelView(View v) {
@ -128,7 +125,10 @@ public class AuthenticateActivity extends ThemedActivity
}
private void initPasswordLayoutView(View v) {
passwordLayout = v.findViewById(R.id.passwordLayout);
TextInputLayout passwordLayout = v.findViewById(R.id.passwordLayout);
int hintResId = (authMethod == AuthMethod.PASSWORD) ? R.string.auth_hint_password : R.string.auth_hint_pin;
passwordLayout.setHint(getString(hintResId));
if (settings.getBlockAccessibility())
passwordLayout.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
@ -139,6 +139,10 @@ public class AuthenticateActivity extends ThemedActivity
private void initPasswordInputView(View v) {
passwordInput = v.findViewById(R.id.passwordEdit);
int inputType = (authMethod == AuthMethod.PASSWORD)
? (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)
: (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
passwordInput.setInputType(inputType);
passwordInput.setTransformationMethod(new PasswordTransformationMethod());
passwordInput.setOnEditorActionListener(this);
}
@ -148,16 +152,6 @@ public class AuthenticateActivity extends ThemedActivity
unlockButton.setOnClickListener(this);
}
private void finishFromEmptyPassword(@StringRes int missingPwResId) {
Toast.makeText(this, missingPwResId, Toast.LENGTH_LONG).show();
finishWithResult(true, null);
}
private void setupPasswordFields(@StringRes int hintResId, int inputType) {
passwordLayout.setHint(getString(hintResId));
passwordInput.setInputType(inputType);
}
@Override
public void onClick(View view) {
checkPassword(passwordInput.getText().toString());