From 1686c61b697fb1961afd1589b7415b016a6338f4 Mon Sep 17 00:00:00 2001 From: Joshua Soberg Date: Wed, 27 Jan 2021 21:02:19 -0500 Subject: [PATCH] #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. --- .../Activities/AuthenticateActivity.java | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java index fd416588..eab82481 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/AuthenticateActivity.java @@ -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());