summaryrefslogtreecommitdiffstats
path: root/common/java/com/android/common/Rfc822Validator.java
diff options
context:
space:
mode:
authorYing Wang <wangying@google.com>2010-06-15 13:54:16 -0700
committerYing Wang <wangying@google.com>2010-06-15 13:58:21 -0700
commit823b6f3516076b92f78c3fc27037d24bb514e653 (patch)
tree580ce1a902841fdc55901cfc5b4ba29171e14cc6 /common/java/com/android/common/Rfc822Validator.java
parentb87362e0a6de2bf398fd4b135c66adcfbb42d8e7 (diff)
downloadandroid_frameworks_ex-823b6f3516076b92f78c3fc27037d24bb514e653.tar.gz
android_frameworks_ex-823b6f3516076b92f78c3fc27037d24bb514e653.tar.bz2
android_frameworks_ex-823b6f3516076b92f78c3fc27037d24bb514e653.zip
move android-common from framework/base to framework/ex
Also change the LOCAL_SDK_VERSION from current to 8. Change-Id: I68943b8b41622dab88c7b13d8c067b39205f028e
Diffstat (limited to 'common/java/com/android/common/Rfc822Validator.java')
-rw-r--r--common/java/com/android/common/Rfc822Validator.java134
1 files changed, 134 insertions, 0 deletions
diff --git a/common/java/com/android/common/Rfc822Validator.java b/common/java/com/android/common/Rfc822Validator.java
new file mode 100644
index 0000000..087e425
--- /dev/null
+++ b/common/java/com/android/common/Rfc822Validator.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.common;
+
+import android.text.TextUtils;
+import android.text.util.Rfc822Token;
+import android.text.util.Rfc822Tokenizer;
+import android.widget.AutoCompleteTextView;
+
+import java.util.regex.Pattern;
+
+/**
+ * This class works as a Validator for AutoCompleteTextView for
+ * email addresses. If a token does not appear to be a valid address,
+ * it is trimmed of characters that cannot legitimately appear in one
+ * and has the specified domain name added. It is meant for use with
+ * {@link Rfc822Token} and {@link Rfc822Tokenizer}.
+ *
+ * @deprecated In the future make sure we don't quietly alter the user's
+ * text in ways they did not intend. Meanwhile, hide this
+ * class from the public API because it does not even have
+ * a full understanding of the syntax it claims to correct.
+ * @hide
+ */
+public class Rfc822Validator implements AutoCompleteTextView.Validator {
+ /*
+ * Regex.EMAIL_ADDRESS_PATTERN hardcodes the TLD that we accept, but we
+ * want to make sure we will keep accepting email addresses with TLD's
+ * that don't exist at the time of this writing, so this regexp relaxes
+ * that constraint by accepting any kind of top level domain, not just
+ * ".com", ".fr", etc...
+ */
+ private static final Pattern EMAIL_ADDRESS_PATTERN =
+ Pattern.compile("[^\\s@]+@[^\\s@]+\\.[a-zA-z][a-zA-Z][a-zA-Z]*");
+
+ private String mDomain;
+
+ /**
+ * Constructs a new validator that uses the specified domain name as
+ * the default when none is specified.
+ */
+ public Rfc822Validator(String domain) {
+ mDomain = domain;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isValid(CharSequence text) {
+ Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(text);
+
+ return tokens.length == 1 &&
+ EMAIL_ADDRESS_PATTERN.
+ matcher(tokens[0].getAddress()).matches();
+ }
+
+ /**
+ * @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.
+ */
+ private String removeIllegalCharacters(String s) {
+ StringBuilder result = new StringBuilder();
+ int length = s.length();
+ for (int i = 0; i < length; i++) {
+ char c = s.charAt(i);
+
+ /*
+ * An RFC822 atom can contain any ASCII printing character
+ * except for periods and any of the following punctuation.
+ * A local-part can contain multiple atoms, concatenated by
+ * periods, so do allow periods here.
+ */
+
+ if (c <= ' ' || c > '~') {
+ continue;
+ }
+
+ if (c == '(' || c == ')' || c == '<' || c == '>' ||
+ c == '@' || c == ',' || c == ';' || c == ':' ||
+ c == '\\' || c == '"' || c == '[' || c == ']') {
+ continue;
+ }
+
+ result.append(c);
+ }
+ return result.toString();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public CharSequence fixText(CharSequence cs) {
+ // Return an empty string if the email address only contains spaces, \n or \t
+ if (TextUtils.getTrimmedLength(cs) == 0) return "";
+
+ Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(cs);
+ StringBuilder sb = new StringBuilder();
+
+ for (int i = 0; i < tokens.length; i++) {
+ String text = tokens[i].getAddress();
+ int index = text.indexOf('@');
+ if (index < 0) {
+ // If there is no @, just append the domain of the account
+ tokens[i].setAddress(removeIllegalCharacters(text) + "@" + mDomain);
+ } else {
+ // Otherwise, remove the illegal characters on both sides of the '@'
+ String fix = removeIllegalCharacters(text.substring(0, index));
+ String domain = removeIllegalCharacters(text.substring(index + 1));
+ tokens[i].setAddress(fix + "@" + (domain.length() != 0 ? domain : mDomain));
+ }
+
+ sb.append(tokens[i].toString());
+ if (i + 1 < tokens.length) {
+ sb.append(", ");
+ }
+ }
+
+ return sb;
+ }
+}