Handle case of duplicate messages when using Fetch (EAS 2.5)

* It turns out that it's possible to have two messages with the
  same server id and mailbox; this causes our HTML-loading code
  for EAS 2.5 to loop indefinitely.
* It's unclear whether the cause is ours or Hotmail's, but the
  fix simply deletes the duplicate when it's detected

Bug: 5789201

Change-Id: I42e7cd9a4b3b3693dae38ac93941bb69f31b0051
diff --git a/src/com/android/exchange/adapter/EmailSyncAdapter.java b/src/com/android/exchange/adapter/EmailSyncAdapter.java
index d889bef..042ec32 100644
--- a/src/com/android/exchange/adapter/EmailSyncAdapter.java
+++ b/src/com/android/exchange/adapter/EmailSyncAdapter.java
@@ -875,6 +875,9 @@
             Cursor c = mContentResolver.query(Message.CONTENT_URI, projection,
                     WHERE_SERVER_ID_AND_MAILBOX_KEY, mBindArguments, null);
             if (c == null) throw new ProviderUnavailableException();
+            if (c.getCount() > 1) {
+                userLog("Multiple messages with the same serverId/mailbox: " + serverId);
+            }
             return c;
         }
 
@@ -1074,6 +1077,13 @@
                 try {
                     if (c.moveToFirst()) {
                         id = c.getString(EmailContent.ID_PROJECTION_COLUMN);
+                        while (c.moveToNext()) {
+                            // This shouldn't happen, but clean up if it does
+                            Long dupId =
+                                    Long.parseLong(c.getString(EmailContent.ID_PROJECTION_COLUMN));
+                            userLog("Delete duplicate with id: " + dupId);
+                            deletedEmails.add(dupId);
+                        }
                     }
                 } finally {
                     c.close();