diff --git a/app/src/main/java/ee/ria/DigiDoc/android/Constants.java b/app/src/main/java/ee/ria/DigiDoc/android/Constants.java index e1d450d77..a03d6624b 100644 --- a/app/src/main/java/ee/ria/DigiDoc/android/Constants.java +++ b/app/src/main/java/ee/ria/DigiDoc/android/Constants.java @@ -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 diff --git a/app/src/main/java/ee/ria/DigiDoc/android/crypto/create/Processor.java b/app/src/main/java/ee/ria/DigiDoc/android/crypto/create/Processor.java index ee36dd010..cffd9f20a 100644 --- a/app/src/main/java/ee/ria/DigiDoc/android/crypto/create/Processor.java +++ b/app/src/main/java/ee/ria/DigiDoc/android/crypto/create/Processor.java @@ -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; @@ -392,8 +392,8 @@ final class Processor implements ObservableTransformer { 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( diff --git a/app/src/main/java/ee/ria/DigiDoc/android/signature/update/mobileid/MobileIdView.java b/app/src/main/java/ee/ria/DigiDoc/android/signature/update/mobileid/MobileIdView.java index 0c93fd179..73a1ab2b6 100644 --- a/app/src/main/java/ee/ria/DigiDoc/android/signature/update/mobileid/MobileIdView.java +++ b/app/src/main/java/ee/ria/DigiDoc/android/signature/update/mobileid/MobileIdView.java @@ -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; @@ -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()) && diff --git a/app/src/main/java/ee/ria/DigiDoc/android/signature/update/smartid/SmartIdView.java b/app/src/main/java/ee/ria/DigiDoc/android/signature/update/smartid/SmartIdView.java index a791ff8fb..2b0ca9118 100644 --- a/app/src/main/java/ee/ria/DigiDoc/android/signature/update/smartid/SmartIdView.java +++ b/app/src/main/java/ee/ria/DigiDoc/android/signature/update/smartid/SmartIdView.java @@ -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; @@ -174,9 +175,15 @@ public Observable 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; } @@ -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() { diff --git a/app/src/main/java/ee/ria/DigiDoc/android/utils/validator/PersonalCodeValidator.java b/app/src/main/java/ee/ria/DigiDoc/android/utils/validator/PersonalCodeValidator.java index 250dc3063..3e49129b7 100644 --- a/app/src/main/java/ee/ria/DigiDoc/android/utils/validator/PersonalCodeValidator.java +++ b/app/src/main/java/ee/ria/DigiDoc/android/utils/validator/PersonalCodeValidator.java @@ -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; @@ -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)) || diff --git a/common-lib/src/main/java/ee/ria/DigiDoc/common/TextUtil.java b/common-lib/src/main/java/ee/ria/DigiDoc/common/TextUtil.java index ab27d2857..5b0025b7c 100644 --- a/common-lib/src/main/java/ee/ria/DigiDoc/common/TextUtil.java +++ b/common-lib/src/main/java/ee/ria/DigiDoc/common/TextUtil.java @@ -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) {