summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorErik <epastern@google.com>2011-08-04 16:07:00 -0700
committerErik <epastern@google.com>2011-08-05 10:51:20 -0700
commit490556a764a879cd0eaff358e90705cc1335c92e (patch)
tree63371656f467c4b7b29e38654f4e452f260888be /common
parent311576adc68a068ab583d794e064329b0f71b8d1 (diff)
downloadandroid_frameworks_ex-490556a764a879cd0eaff358e90705cc1335c92e.tar.gz
android_frameworks_ex-490556a764a879cd0eaff358e90705cc1335c92e.tar.bz2
android_frameworks_ex-490556a764a879cd0eaff358e90705cc1335c92e.zip
b/5122753 Make Rfc822Validator a bit smarter
Change-Id: Iffea9735b906466077ce03a97ab49b614917dbf3
Diffstat (limited to 'common')
-rw-r--r--common/java/com/android/common/Rfc822Validator.java34
-rw-r--r--common/tests/src/com/android/common/Rfc822ValidatorTest.java32
2 files changed, 63 insertions, 3 deletions
diff --git a/common/java/com/android/common/Rfc822Validator.java b/common/java/com/android/common/Rfc822Validator.java
index 1b4a268..2db00ff 100644
--- a/common/java/com/android/common/Rfc822Validator.java
+++ b/common/java/com/android/common/Rfc822Validator.java
@@ -36,6 +36,7 @@ import java.util.regex.Pattern;
* a full understanding of the syntax it claims to correct.
* @hide
*/
+@Deprecated
public class Rfc822Validator implements AutoCompleteTextView.Validator {
/*
* Regex.EMAIL_ADDRESS_PATTERN hardcodes the TLD that we accept, but we
@@ -48,6 +49,7 @@ public class Rfc822Validator implements AutoCompleteTextView.Validator {
Pattern.compile("[^\\s@]+@([^\\s@\\.]+\\.)+[a-zA-z][a-zA-Z][a-zA-Z]*");
private String mDomain;
+ private boolean mRemoveInvalid = false;
/**
* Constructs a new validator that uses the specified domain name as
@@ -69,6 +71,18 @@ public class Rfc822Validator implements AutoCompleteTextView.Validator {
}
/**
+ * Specify if the validator should remove invalid tokens instead of trying
+ * to fix them. This can be used to strip results of incorrectly formatted
+ * tokens.
+ *
+ * @param remove true to remove tokens with the wrong format, false to
+ * attempt to fix them
+ */
+ public void setRemoveInvalid(boolean remove) {
+ mRemoveInvalid = remove;
+ }
+
+ /**
* @return a string in which all the characters that are illegal for the username
* or the domain name part of the email address have been removed.
*/
@@ -112,15 +126,29 @@ public class Rfc822Validator implements AutoCompleteTextView.Validator {
for (int i = 0; i < tokens.length; i++) {
String text = tokens[i].getAddress();
+
+ if (mRemoveInvalid && !isValid(text)) {
+ continue;
+ }
int index = text.indexOf('@');
if (index < 0) {
- // If there is no @, just append the domain of the account
- tokens[i].setAddress(removeIllegalCharacters(text) + "@" + mDomain);
+ // append the domain of the account if it exists
+ if (mDomain != null) {
+ tokens[i].setAddress(removeIllegalCharacters(text) + "@" + mDomain);
+ }
} else {
// Otherwise, remove the illegal characters on both sides of the '@'
String fix = removeIllegalCharacters(text.substring(0, index));
+ if (TextUtils.isEmpty(fix)) {
+ // if the address is empty after removing invalid chars
+ // don't use it
+ continue;
+ }
String domain = removeIllegalCharacters(text.substring(index + 1));
- tokens[i].setAddress(fix + "@" + (domain.length() != 0 ? domain : mDomain));
+ boolean emptyDomain = domain.length() == 0;
+ if (!emptyDomain || mDomain != null) {
+ tokens[i].setAddress(fix + "@" + (!emptyDomain ? domain : mDomain));
+ }
}
sb.append(tokens[i].toString());
diff --git a/common/tests/src/com/android/common/Rfc822ValidatorTest.java b/common/tests/src/com/android/common/Rfc822ValidatorTest.java
index 5118146..cbcc812 100644
--- a/common/tests/src/com/android/common/Rfc822ValidatorTest.java
+++ b/common/tests/src/com/android/common/Rfc822ValidatorTest.java
@@ -49,6 +49,38 @@ public class Rfc822ValidatorTest extends TestCase {
fixes.put("a", "<a@gmail.com>");
fixes.put("a b", "<ab@gmail.com>");
fixes.put("a@b", "<a@b>");
+ fixes.put("()~><@not.work", "");
+
+ for (Map.Entry<String, String> e : fixes.entrySet()) {
+ assertEquals(e.getValue(), validator.fixText(e.getKey()).toString());
+ }
+ }
+
+ @SmallTest
+ public void testEmailValidatorNullDomain() {
+ Rfc822Validator validator = new Rfc822Validator(null);
+
+ Map<String, String> fixes = new HashMap<String, String>();
+ fixes.put("a", "<a>");
+ fixes.put("a b", "<a b>");
+ fixes.put("a@b", "<a@b>");
+ fixes.put("a@b.com", "<a@b.com>"); // this one is correct
+
+ for (Map.Entry<String, String> e : fixes.entrySet()) {
+ assertEquals(e.getValue(), validator.fixText(e.getKey()).toString());
+ }
+ }
+
+ @SmallTest
+ public void testEmailValidatorRemoveInvalid() {
+ Rfc822Validator validator = new Rfc822Validator("google.com");
+ validator.setRemoveInvalid(true);
+
+ Map<String, String> fixes = new HashMap<String, String>();
+ fixes.put("a", "");
+ fixes.put("a b", "");
+ fixes.put("a@b", "");
+ fixes.put("a@b.com", "<a@b.com>"); // this one is correct
for (Map.Entry<String, String> e : fixes.entrySet()) {
assertEquals(e.getValue(), validator.fixText(e.getKey()).toString());