Merge branch 'master' of github.com:andOTP/andOTP

This commit is contained in:
Jakob Nixdorf 2020-06-05 07:28:39 +02:00
commit 1929e5bdc1
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
2 changed files with 56 additions and 15 deletions

View file

@ -278,28 +278,37 @@ public class ManualEntryDialog {
public void afterTextChanged(Editable editable) {
if ((TextUtils.isEmpty(labelInput.getText()) && TextUtils.isEmpty(issuerInput.getText())) ||
(TextUtils.isEmpty(secretInput.getText()) && isNewEntry) ||
TextUtils.isEmpty(digitsInput.getText()) ||
Integer.parseInt(digitsInput.getText().toString()) == 0) {
!isNonZeroIntegerInput(digitsInput)) {
positiveButton.setEnabled(false);
} else {
Entry.OTPType type = (Entry.OTPType) typeInput.getSelectedItem();
if (type == Entry.OTPType.HOTP) {
if (TextUtils.isEmpty(counterInput.getText())) {
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
positiveButton.setEnabled(isZeroOrPositiveLongInput(counterInput));
} else if (type == Entry.OTPType.TOTP || type == Entry.OTPType.STEAM) {
if (TextUtils.isEmpty(periodInput.getText()) || Integer.parseInt(periodInput.getText().toString()) == 0) {
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
positiveButton.setEnabled(isNonZeroIntegerInput(periodInput));
} else {
positiveButton.setEnabled(true);
}
}
}
private boolean isNonZeroIntegerInput(EditText editText) {
try {
Editable text = editText.getText();
return !TextUtils.isEmpty(text) && (Integer.parseInt(text.toString()) != 0);
} catch (NumberFormatException e) {
return false;
}
}
private boolean isZeroOrPositiveLongInput(EditText editText) {
try {
Editable text = editText.getText();
return !TextUtils.isEmpty(text) && (Long.parseLong(text.toString()) >= 0);
} catch (NumberFormatException e) {
return false;
}
}
};
labelInput.addTextChangedListener(watcher);

View file

@ -34,6 +34,7 @@ import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MenuInflater;
@ -41,6 +42,7 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
@ -438,7 +440,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
container.setPaddingRelative(marginMedium, marginSmall, marginMedium, 0);
container.addView(input);
builder.setTitle(R.string.dialog_title_counter)
AlertDialog dialog = builder.setTitle(R.string.dialog_title_counter)
.setView(container)
.setPositiveButton(R.string.button_save, new DialogInterface.OnClickListener() {
@Override
@ -459,8 +461,38 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
})
.create()
.show();
.create();
addCounterValidationWatcher(input, dialog);
dialog.show();
}
private void addCounterValidationWatcher(EditText input, AlertDialog dialog) {
TextWatcher counterWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable input) {
Button positive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (positive != null) {
positive.setEnabled(isZeroOrPositiveLongInput(input));
}
}
private boolean isZeroOrPositiveLongInput(Editable input) {
try {
return !TextUtils.isEmpty(input) && (Long.parseLong(input.toString()) >= 0);
} catch (NumberFormatException e) {
return false;
}
}
};
input.addTextChangedListener(counterWatcher);
}
private boolean updateLastUsedAndFrequency(int position, int realIndex) {