Skip to content

Commit

Permalink
Support Latvian personal codes
Browse files Browse the repository at this point in the history
  • Loading branch information
martenrebane committed Oct 28, 2024
1 parent a0e4d39 commit 7f3fe3b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/ee/ria/DigiDoc/android/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public final class Constants {
/**
* Personal code
*/
public static final int MAXIMUM_PERSONAL_CODE_LENGTH = 11;
public static final int MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH = 11;
public static final int MAXIMUM_LATVIAN_PERSONAL_CODE_LENGTH = 12;

/**
* CAN number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static android.app.Activity.RESULT_OK;
import static android.view.accessibility.AccessibilityEvent.TYPE_ANNOUNCEMENT;
import static ee.ria.DigiDoc.android.Constants.MAXIMUM_PERSONAL_CODE_LENGTH;
import static ee.ria.DigiDoc.android.Constants.MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH;
import static ee.ria.DigiDoc.android.Constants.RC_CRYPTO_CREATE_DATA_FILE_ADD;
import static ee.ria.DigiDoc.android.Constants.RC_CRYPTO_CREATE_INITIAL;
import static ee.ria.DigiDoc.android.Constants.SAVE_FILE;
Expand Down Expand Up @@ -392,8 +392,8 @@ final class Processor implements ObservableTransformer<Intent, Result> {
recipientsSearch = upstream -> upstream.switchMap(intent -> {
if (intent.query() == null || intent.query().isEmpty()) {
return Observable.just(Result.RecipientsSearchResult.clear());
} else if (intent.query().length() >= MAXIMUM_PERSONAL_CODE_LENGTH &&
StringUtils.isNumeric(intent.query()) && !PersonalCodeValidator.validatePersonalCode(intent.query())) {
} else if (intent.query().length() >= MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH &&
StringUtils.isNumeric(intent.query()) && !PersonalCodeValidator.validateEstonianPersonalCode(intent.query())) {
AccessibilityUtils.sendAccessibilityEvent(
application.getApplicationContext(), TYPE_ANNOUNCEMENT, R.string.crypto_recipients_search_result_invalid_personal_code);
return Observable.just(Result.RecipientsSearchResult.failure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static ee.ria.DigiDoc.common.TextUtil.getSymbolsFilter;

import android.content.Context;
import android.graphics.Rect;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
Expand Down Expand Up @@ -154,7 +153,7 @@ public boolean positiveButtonEnabled() {
Editable phoneNumber = phoneNoView.getText();
Editable personalCode = personalCodeView.getText();
if (phoneNumber != null && personalCode != null) {
PersonalCodeValidator.validatePersonalCode(personalCodeView);
PersonalCodeValidator.validateEstonianPersonalCode(personalCodeView);

return isCountryCodeCorrect(phoneNumber.toString()) &&
isPhoneNumberCorrect(phoneNumber.toString()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
package ee.ria.DigiDoc.android.signature.update.smartid;

import static com.jakewharton.rxbinding4.widget.RxTextView.afterTextChangeEvents;
import static ee.ria.DigiDoc.android.Constants.MAXIMUM_PERSONAL_CODE_LENGTH;
import static ee.ria.DigiDoc.android.Constants.MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH;
import static ee.ria.DigiDoc.android.Constants.MAXIMUM_LATVIAN_PERSONAL_CODE_LENGTH;
import static ee.ria.DigiDoc.android.accessibility.AccessibilityUtils.removeAccessibilityStateChanged;
import static ee.ria.DigiDoc.android.utils.ErrorMessageUtil.setTextViewError;
import static ee.ria.DigiDoc.android.utils.TextUtil.removeTextWatcher;
Expand Down Expand Up @@ -174,9 +175,15 @@ public Observable<Object> positiveButtonState() {
public boolean positiveButtonEnabled() {
Editable personalCode = personalCodeView.getText();
if (personalCode != null) {
PersonalCodeValidator.validatePersonalCode(personalCodeView);
return countryView.getSelectedItemPosition() != 0 ||
isPersonalCodeCorrect(personalCode.toString());
if (countryView.getSelectedItemPosition() == 0) {
PersonalCodeValidator.validateEstonianPersonalCode(personalCodeView);
} else if (countryView.getSelectedItemPosition() == 2) {
PersonalCodeValidator.validateLatvianPersonalCode(personalCodeView);
}
return ((countryView.getSelectedItemPosition() == 0 || countryView.getSelectedItemPosition() == 1) &&
personalCode.toString().length() == MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH) ||
(countryView.getSelectedItemPosition() == 2 &&
personalCode.toString().length() == MAXIMUM_LATVIAN_PERSONAL_CODE_LENGTH);
}
return false;
}
Expand Down Expand Up @@ -207,7 +214,7 @@ private void checkPersonalCodeValidity() {
}

private boolean isPersonalCodeCorrect(String personalCode) {
return personalCode.length() == MAXIMUM_PERSONAL_CODE_LENGTH;
return personalCode.length() == MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH;
}

private void checkForDoneButtonClick() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ee.ria.DigiDoc.android.utils.validator;

import static ee.ria.DigiDoc.android.Constants.MAXIMUM_PERSONAL_CODE_LENGTH;
import static ee.ria.DigiDoc.android.Constants.MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH;
import static ee.ria.DigiDoc.android.Constants.MAXIMUM_LATVIAN_PERSONAL_CODE_LENGTH;

import android.util.Log;
import android.widget.EditText;
Expand All @@ -16,21 +17,33 @@

public class PersonalCodeValidator {

public static void validatePersonalCode(EditText personalCode) {
public static void validateEstonianPersonalCode(EditText personalCode) {
if (personalCode.getText() != null &&
personalCode.length() >= MAXIMUM_PERSONAL_CODE_LENGTH &&
personalCode.length() >= MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH &&
!isPersonalCodeValid(personalCode.getText().toString())) {
personalCode.getText()
.delete(MAXIMUM_PERSONAL_CODE_LENGTH - 1, personalCode.length());
.delete(MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH - 1, personalCode.length());
}
}

public static boolean validatePersonalCode(String personalCode) {
public static boolean validateEstonianPersonalCode(String personalCode) {
return personalCode == null ||
personalCode.length() < MAXIMUM_PERSONAL_CODE_LENGTH ||
personalCode.length() < MAXIMUM_ESTONIAN_PERSONAL_CODE_LENGTH ||
isPersonalCodeValid(personalCode);
}

public static void validateLatvianPersonalCode(EditText personalCode) {
String regex = "^\\d{6}-\\d{5}$";

if (personalCode.getText() != null &&
personalCode.length() >= MAXIMUM_LATVIAN_PERSONAL_CODE_LENGTH &&
!personalCode.getText().toString().matches(regex)
) {
personalCode.getText()
.delete(MAXIMUM_LATVIAN_PERSONAL_CODE_LENGTH - 1, personalCode.length());
}
}

public static boolean isPersonalCodeValid(String personalCode) {
return (isPersonalCodeLengthValid(personalCode) && isBirthDateValid(personalCode) &&
isChecksumValid(personalCode)) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class TextUtil {

public static final String DIGITS = "0123456789";
public static final String PERSONAL_CODE_SYMBOLS = DIGITS;
public static final String PERSONAL_CODE_SYMBOLS = DIGITS + "-";
public static final String PHONE_SYMBOLS = DIGITS + "+-/()*+.,#";

public static String splitTextAndJoin(String text, String delimiter, String joinDelimiter) {
Expand Down

0 comments on commit 7f3fe3b

Please sign in to comment.