Normalize the whole email address when comparing.

b/11336576

Change-Id: I5e79c0d37337f848b385697799d45bf922f86ff1
diff --git a/src/com/android/mail/providers/ReplyFromAccount.java b/src/com/android/mail/providers/ReplyFromAccount.java
index 1fa984e..875d22e 100644
--- a/src/com/android/mail/providers/ReplyFromAccount.java
+++ b/src/com/android/mail/providers/ReplyFromAccount.java
@@ -111,13 +111,14 @@
             List<ReplyFromAccount> replyFromAccounts) {
         Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(possibleCustomFrom);
         if (tokens != null && tokens.length > 0) {
-            String parsedFromAddress = tokens[0].getAddress();
-            if (TextUtils.equals(account.getEmailAddress(), parsedFromAddress)) {
+            String parsedFromAddress = Utils.normalizeEmailAddress(tokens[0].getAddress());
+            if (TextUtils.equals(Utils.normalizeEmailAddress(account.getEmailAddress()),
+                    parsedFromAddress)) {
                 return true;
             }
             for (ReplyFromAccount replyFromAccount : replyFromAccounts) {
-                if (TextUtils.equals(replyFromAccount.address, parsedFromAddress)
-                        && replyFromAccount.isCustomFrom) {
+                if (TextUtils.equals(Utils.normalizeEmailAddress(replyFromAccount.address),
+                        parsedFromAddress) && replyFromAccount.isCustomFrom) {
                     return true;
                 }
             }
diff --git a/src/com/android/mail/utils/Utils.java b/src/com/android/mail/utils/Utils.java
index 32c8de7..4247555 100644
--- a/src/com/android/mail/utils/Utils.java
+++ b/src/com/android/mail/utils/Utils.java
@@ -1408,4 +1408,32 @@
         }
     }
 
+    /**
+     * Email addresses are supposed to be treated as case-insensitive for the host-part and
+     * case-sensitive for the local-part, but nobody really wants email addresses to match
+     * case-sensitive on the local-part, so just smash everything to lower case.
+     * @param email Hello@Example.COM
+     * @return hello@example.com
+     */
+    public static String normalizeEmailAddress(String email) {
+        /*
+        // The RFC5321 version
+        if (TextUtils.isEmpty(email)) {
+            return email;
+        }
+        String[] parts = email.split("@");
+        if (parts.length != 2) {
+            LogUtils.d(LOG_TAG, "Tried to normalize a malformed email address: ", email);
+            return email;
+        }
+
+        return parts[0] + "@" + parts[1].toLowerCase(Locale.US);
+        */
+        if (TextUtils.isEmpty(email)) {
+            return email;
+        } else {
+            // Doing this for other locales might really screw things up, so do US-version only
+            return email.toLowerCase(Locale.US);
+        }
+    }
 }
diff --git a/tests/src/com/android/mail/utils/NormalizeEmailAddressTest.java b/tests/src/com/android/mail/utils/NormalizeEmailAddressTest.java
new file mode 100644
index 0000000..3778a1f
--- /dev/null
+++ b/tests/src/com/android/mail/utils/NormalizeEmailAddressTest.java
@@ -0,0 +1,27 @@
+// Copyright 2013 Google Inc. All Rights Reserved.
+
+package com.android.mail.utils;
+
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.mail.utils.Utils;
+
+@SmallTest
+public class NormalizeEmailAddressTest extends AndroidTestCase {
+    public void testNormalizeEmailAddress() {
+        final String emailAddress = "user@example.com";
+
+        assertEquals(Utils.normalizeEmailAddress("User@EXAMPLE.COM"), emailAddress);
+
+        assertEquals(Utils.normalizeEmailAddress("User@example.com"), emailAddress);
+
+        assertEquals(Utils.normalizeEmailAddress("User@exaMple.com"), emailAddress);
+
+        assertEquals(Utils.normalizeEmailAddress(null), null);
+
+        assertEquals(Utils.normalizeEmailAddress(""), "");
+
+        assertEquals(Utils.normalizeEmailAddress("Not an EMAIL address"), "not an email address");
+    }
+}
\ No newline at end of file