Only make mailbox syncing expedited if the incoming request was

b/10970795
In CalendarSyncAdapterService and ContactsSyncAdapterService,
when we get an onPerformSync(), we just make a new sync request
for a particular exchange mailbox. The trouble is that if the
new request is "expedited", and the original request was not,
then the original request will be canceled. When this happens,
after the second sync completes, the original will be started
again. This can put us in a rapidly spinning infinite loop.

With this CL, our new sync request will only be expedited if
the incoming request was also expedited. This should prevent the
infinite loop. It unfortunately means that background calendar
and contacts syncs can take extra long to start, because we must
wait for the sync manager to decide to run the original sync,
and then wait again for it to perform the actual mailbox sync.
That's a good enough trade off for now.

Change-Id: I51689f4a54b3b9bae2f797730287e30eb39f4649
diff --git a/src/com/android/exchange/service/CalendarSyncAdapterService.java b/src/com/android/exchange/service/CalendarSyncAdapterService.java
index 15c52ea..c8f00cf 100644
--- a/src/com/android/exchange/service/CalendarSyncAdapterService.java
+++ b/src/com/android/exchange/service/CalendarSyncAdapterService.java
@@ -102,10 +102,11 @@
         }
 
         final long extrasMailboxId = extras.getLong(Mailbox.SYNC_EXTRA_MAILBOX_ID, 0);
+        final boolean expedited = extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
         if (extrasMailboxId != 0) {
             // If we've been given a mailbox, just sync that one and be done. Don't
             // do a query for the rest of the calendar mailboxes.
-            syncMailbox(account, extrasMailboxId);
+            syncMailbox(account, expedited, extrasMailboxId);
             return;
         }
 
@@ -129,7 +130,7 @@
                     while (mailboxCursor.moveToNext()) {
                         // TODO: Currently just bouncing this to Email sync; eventually streamline.
                         final long mailboxId = mailboxCursor.getLong(Mailbox.ID_PROJECTION_COLUMN);
-                        syncMailbox(account, mailboxId);
+                        syncMailbox(account, expedited, mailboxId);
                     }
                 } finally {
                     mailboxCursor.close();
@@ -140,12 +141,14 @@
         }
     }
 
-    private static void syncMailbox(Account account, long mailboxId) {
+    private static void syncMailbox(Account account, boolean expedited, long mailboxId) {
         // TODO: Should we be using the existing extras and just adding our bits?
         final Bundle mailboxExtras = new Bundle(4);
         mailboxExtras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
         mailboxExtras.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, true);
-        mailboxExtras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
+        if (expedited) {
+            mailboxExtras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
+        }
         mailboxExtras.putLong(Mailbox.SYNC_EXTRA_MAILBOX_ID, mailboxId);
         ContentResolver.requestSync(account, EmailContent.AUTHORITY, mailboxExtras);
         LogUtils.i(TAG, "requestSync CalendarSyncAdapter %s, %s",
diff --git a/src/com/android/exchange/service/ContactsSyncAdapterService.java b/src/com/android/exchange/service/ContactsSyncAdapterService.java
index abb1521..19986fb 100644
--- a/src/com/android/exchange/service/ContactsSyncAdapterService.java
+++ b/src/com/android/exchange/service/ContactsSyncAdapterService.java
@@ -124,10 +124,11 @@
         }
 
         final long extrasMailboxId = extras.getLong(Mailbox.SYNC_EXTRA_MAILBOX_ID, 0);
+        final boolean expedited = extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
         if (extrasMailboxId != 0) {
             // If we've been given a mailbox, just sync that one and be done. Don't
             // do a query for the rest of the calendar mailboxes.
-            syncMailbox(account, extrasMailboxId);
+            syncMailbox(account, expedited, extrasMailboxId);
             return;
         }
 
@@ -142,7 +143,7 @@
                     while (mailboxCursor.moveToNext()) {
                         // TODO: Currently just bouncing this to Email sync; eventually streamline.
                         final long mailboxId = mailboxCursor.getLong(Mailbox.ID_PROJECTION_COLUMN);
-                        syncMailbox(account, mailboxId);
+                        syncMailbox(account, expedited, mailboxId);
                     }
                 } finally {
                     mailboxCursor.close();
@@ -153,12 +154,14 @@
         }
     }
 
-    private static void syncMailbox(Account account, long mailboxId) {
+    private static void syncMailbox(Account account, boolean expedited, long mailboxId) {
         // TODO: Should we be using the existing extras and just adding our bits?
         final Bundle mailboxExtras = new Bundle(4);
         mailboxExtras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
         mailboxExtras.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, true);
-        mailboxExtras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
+        if (expedited) {
+            mailboxExtras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
+        }
         mailboxExtras.putLong(Mailbox.SYNC_EXTRA_MAILBOX_ID, mailboxId);
         ContentResolver.requestSync(account, EmailContent.AUTHORITY, mailboxExtras);
         LogUtils.i(TAG, "requestSync ContactsSyncAdapter %s, %s",