Fix inefficient EAS server deletes

* This also caused bug 5784532, which leads to looping syncs and
  battery drain

Bug: 5784532
Bug: 5806830
Change-Id: I892521ca3217138c1f3de0c038d520700cf3d3ea
diff --git a/src/com/android/exchange/adapter/EmailSyncAdapter.java b/src/com/android/exchange/adapter/EmailSyncAdapter.java
index 6c9d585..4c3f8c9 100644
--- a/src/com/android/exchange/adapter/EmailSyncAdapter.java
+++ b/src/com/android/exchange/adapter/EmailSyncAdapter.java
@@ -1333,7 +1333,23 @@
                     boolean readChange = false;
 
                     long mailbox = currentCursor.getLong(UPDATES_MAILBOX_KEY_COLUMN);
-                    if (mailbox != c.getLong(Message.LIST_MAILBOX_KEY_COLUMN)) {
+                    // If the message is now in the trash folder, it has been deleted by the user
+                    if (mailbox == trashMailboxId) {
+                         if (firstCommand) {
+                            s.start(Tags.SYNC_COMMANDS);
+                            firstCommand = false;
+                        }
+                        // Send the command to delete this message
+                        s.start(Tags.SYNC_DELETE).data(Tags.SYNC_SERVER_ID, serverId).end();
+                        // Mark the message as moved (so the copy will be deleted if/when the server
+                        // version is synced)
+                        int flags = c.getInt(Message.LIST_FLAGS_COLUMN);
+                        cv.put(MessageColumns.FLAGS,
+                                flags | EasSyncService.MESSAGE_FLAG_MOVED_MESSAGE);
+                        cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, id), cv,
+                                null, null);
+                        continue;
+                    } else if (mailbox != c.getLong(Message.LIST_MAILBOX_KEY_COLUMN)) {
                         // The message has moved to another mailbox; add a request for this
                         // Note: The Sync command doesn't handle moving messages, so we need
                         // to handle this as a "request" (similar to meeting response and
@@ -1406,24 +1422,6 @@
                         }
                     }
                     s.end().end(); // SYNC_APPLICATION_DATA, SYNC_CHANGE
-
-                    // If the message is now in the trash folder, it has been deleted by the user
-                    if (currentCursor.getLong(UPDATES_MAILBOX_KEY_COLUMN) == trashMailboxId) {
-                         if (firstCommand) {
-                            s.start(Tags.SYNC_COMMANDS);
-                            firstCommand = false;
-                        }
-                        // Send the command to delete this message
-                        s.start(Tags.SYNC_DELETE).data(Tags.SYNC_SERVER_ID, serverId).end();
-                        // Mark the message as moved (so the copy will be deleted if/when the server
-                        // version is synced)
-                        int flags = c.getInt(Message.LIST_FLAGS_COLUMN);
-                        cv.put(MessageColumns.FLAGS,
-                                flags | EasSyncService.MESSAGE_FLAG_MOVED_MESSAGE);
-                        cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, id), cv,
-                                null, null);
-                        continue;
-                    }
                 } finally {
                     currentCursor.close();
                 }