Merge pull request #238 from michaelschattgen/feature-togglepassword

Add checkbox to toggle visibility in password fields
This commit is contained in:
Alexander Bakker 2019-11-26 21:55:45 +01:00 committed by GitHub
commit 82135c8b9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 5 deletions

View file

@ -9,10 +9,13 @@ import android.os.Build;
import android.text.Editable; import android.text.Editable;
import android.text.InputType; import android.text.InputType;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText; import android.widget.EditText;
import android.widget.NumberPicker; import android.widget.NumberPicker;
import android.widget.TextView; import android.widget.TextView;
@ -78,6 +81,18 @@ public class Dialogs {
View view = activity.getLayoutInflater().inflate(R.layout.dialog_password, null); View view = activity.getLayoutInflater().inflate(R.layout.dialog_password, null);
EditText textPassword = view.findViewById(R.id.text_password); EditText textPassword = view.findViewById(R.id.text_password);
EditText textPasswordConfirm = view.findViewById(R.id.text_password_confirm); EditText textPasswordConfirm = view.findViewById(R.id.text_password_confirm);
CheckBox switchToggleVisibility = view.findViewById(R.id.check_toggle_visibility);
switchToggleVisibility.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener) (buttonView, isChecked) -> {
if (isChecked) {
textPassword.setTransformationMethod(null);
textPassword.clearFocus();
textPasswordConfirm.setEnabled(false);
} else {
textPassword.setTransformationMethod(new PasswordTransformationMethod());
textPasswordConfirm.setEnabled(true);
}
});
AlertDialog dialog = new AlertDialog.Builder(activity) AlertDialog dialog = new AlertDialog.Builder(activity)
.setTitle(R.string.set_password) .setTitle(R.string.set_password)
@ -94,7 +109,7 @@ public class Dialogs {
// replace the default listener // replace the default listener
button.setOnClickListener(v -> { button.setOnClickListener(v -> {
if (!EditTextHelper.areEditTextsEqual(textPassword, textPasswordConfirm)) { if (!EditTextHelper.areEditTextsEqual(textPassword, textPasswordConfirm) && !switchToggleVisibility.isChecked()) {
return; return;
} }
@ -119,7 +134,7 @@ public class Dialogs {
TextWatcher watcher = new TextWatcher() { TextWatcher watcher = new TextWatcher() {
public void onTextChanged(CharSequence c, int start, int before, int count) { public void onTextChanged(CharSequence c, int start, int before, int count) {
boolean equal = EditTextHelper.areEditTextsEqual(textPassword, textPasswordConfirm); boolean equal = EditTextHelper.areEditTextsEqual(textPassword, textPasswordConfirm);
buttonOK.get().setEnabled(equal); buttonOK.get().setEnabled(equal || switchToggleVisibility.isChecked());
} }
public void beforeTextChanged(CharSequence c, int start, int count, int after) { public void beforeTextChanged(CharSequence c, int start, int count, int after) {

View file

@ -6,9 +6,12 @@ import android.content.Intent;
import android.hardware.fingerprint.FingerprintManager; import android.hardware.fingerprint.FingerprintManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
@ -34,6 +37,7 @@ public class CustomAuthenticatedSlide extends Fragment implements FingerprintUiH
private int _cryptType; private int _cryptType;
private EditText _textPassword; private EditText _textPassword;
private EditText _textPasswordConfirm; private EditText _textPasswordConfirm;
private CheckBox _checkPasswordVisibility;
private int _bgColor; private int _bgColor;
private LinearLayout _boxFingerprint; private LinearLayout _boxFingerprint;
@ -50,6 +54,7 @@ public class CustomAuthenticatedSlide extends Fragment implements FingerprintUiH
final View view = inflater.inflate(R.layout.fragment_authenticated_slide, container, false); final View view = inflater.inflate(R.layout.fragment_authenticated_slide, container, false);
_textPassword = view.findViewById(R.id.text_password); _textPassword = view.findViewById(R.id.text_password);
_textPasswordConfirm = view.findViewById(R.id.text_password_confirm); _textPasswordConfirm = view.findViewById(R.id.text_password_confirm);
_checkPasswordVisibility = view.findViewById(R.id.check_toggle_visibility);
_boxFingerprint = view.findViewById(R.id.box_fingerprint); _boxFingerprint = view.findViewById(R.id.box_fingerprint);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@ -58,6 +63,17 @@ public class CustomAuthenticatedSlide extends Fragment implements FingerprintUiH
insertPoint.addView(_imgFingerprint, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); insertPoint.addView(_imgFingerprint, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
} }
_checkPasswordVisibility.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
_textPassword.setTransformationMethod(null);
_textPassword.clearFocus();
_textPasswordConfirm.setEnabled(false);
} else {
_textPassword.setTransformationMethod(new PasswordTransformationMethod());
_textPasswordConfirm.setEnabled(true);
}
});
_textFingerprint = view.findViewById(R.id.text_fingerprint); _textFingerprint = view.findViewById(R.id.text_fingerprint);
view.findViewById(R.id.main).setBackgroundColor(_bgColor); view.findViewById(R.id.main).setBackgroundColor(_bgColor);
return view; return view;
@ -147,7 +163,11 @@ public class CustomAuthenticatedSlide extends Fragment implements FingerprintUiH
} }
// intentional fallthrough // intentional fallthrough
case CustomAuthenticationSlide.CRYPT_TYPE_PASS: case CustomAuthenticationSlide.CRYPT_TYPE_PASS:
return EditTextHelper.areEditTextsEqual(_textPassword, _textPasswordConfirm); if (EditTextHelper.getEditTextChars(_textPassword).length > 0) {
return EditTextHelper.areEditTextsEqual(_textPassword, _textPasswordConfirm) || _checkPasswordVisibility.isChecked();
}
return false;
default: default:
throw new RuntimeException(); throw new RuntimeException();
} }
@ -156,7 +176,7 @@ public class CustomAuthenticatedSlide extends Fragment implements FingerprintUiH
@Override @Override
public void onUserIllegallyRequestedNextPage() { public void onUserIllegallyRequestedNextPage() {
String message; String message;
if (!EditTextHelper.areEditTextsEqual(_textPassword, _textPasswordConfirm)) { if (!EditTextHelper.areEditTextsEqual(_textPassword, _textPasswordConfirm) && !_checkPasswordVisibility.isChecked()) {
message = getString(R.string.password_equality_error); message = getString(R.string.password_equality_error);
} else if (!_fingerAuthenticated) { } else if (!_fingerAuthenticated) {
message = getString(R.string.register_fingerprint); message = getString(R.string.register_fingerprint);

View file

@ -4,8 +4,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:paddingStart="20dp" android:paddingStart="20dp"
android:paddingBottom="10dp"
android:paddingEnd="20dp" android:paddingEnd="20dp"
android:paddingTop="20dp"> android:paddingTop="10dp">
<EditText <EditText
android:id="@+id/text_password" android:id="@+id/text_password"
android:hint="@string/password" android:hint="@string/password"
@ -18,4 +19,11 @@
android:inputType="textPassword" android:inputType="textPassword"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/check_toggle_visibility"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/show_password" />
</LinearLayout> </LinearLayout>

View file

@ -59,6 +59,13 @@
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"/> android:layout_alignParentEnd="true"/>
<CheckBox
android:id="@+id/check_toggle_visibility"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/show_password" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout

View file

@ -83,6 +83,7 @@
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="password">Password</string> <string name="password">Password</string>
<string name="confirm_password">Confirm password</string> <string name="confirm_password">Confirm password</string>
<string name="show_password">Show password</string>
<string name="new_profile">New profile</string> <string name="new_profile">New profile</string>
<string name="add_new_profile">Add new profile</string> <string name="add_new_profile">Add new profile</string>
<string name="unlock_vault_error">Couldn\'t unlock vault</string> <string name="unlock_vault_error">Couldn\'t unlock vault</string>