Delete PIM data during folder sync/reset

Bug: 3361000
Change-Id: I95305467c17c5c4b75b96dfb06c3c3511eb3f15a
diff --git a/src/com/android/exchange/ExchangeService.java b/src/com/android/exchange/ExchangeService.java
index e9e2af7..5f7fad8 100644
--- a/src/com/android/exchange/ExchangeService.java
+++ b/src/com/android/exchange/ExchangeService.java
@@ -462,24 +462,10 @@
          * @throws RemoteException
          */
         public void deleteAccountPIMData(long accountId) throws RemoteException {
-            ExchangeService exchangeService = INSTANCE;
-            if (exchangeService == null) return;
             // Stop any running syncs
-            exchangeService.stopAccountSyncs(accountId, true);
-            Mailbox mailbox =
-                Mailbox.restoreMailboxOfType(exchangeService, accountId, Mailbox.TYPE_CONTACTS);
-            if (mailbox != null) {
-                EasSyncService service = new EasSyncService(exchangeService, mailbox);
-                ContactsSyncAdapter adapter = new ContactsSyncAdapter(service);
-                adapter.wipe();
-            }
-            mailbox =
-                Mailbox.restoreMailboxOfType(exchangeService, accountId, Mailbox.TYPE_CALENDAR);
-            if (mailbox != null) {
-                EasSyncService service = new EasSyncService(exchangeService, mailbox);
-                CalendarSyncAdapter adapter = new CalendarSyncAdapter(service);
-                adapter.wipe();
-            }
+            ExchangeService.stopAccountSyncs(accountId);
+            // Delete the data
+            ExchangeService.deleteAccountPIMData(accountId);
         }
     };
 
@@ -514,6 +500,25 @@
         }
     }
 
+    public static void deleteAccountPIMData(long accountId) {
+        ExchangeService exchangeService = INSTANCE;
+        if (exchangeService == null) return;
+        Mailbox mailbox =
+            Mailbox.restoreMailboxOfType(exchangeService, accountId, Mailbox.TYPE_CONTACTS);
+        if (mailbox != null) {
+            EasSyncService service = new EasSyncService(exchangeService, mailbox);
+            ContactsSyncAdapter adapter = new ContactsSyncAdapter(service);
+            adapter.wipe();
+        }
+        mailbox =
+            Mailbox.restoreMailboxOfType(exchangeService, accountId, Mailbox.TYPE_CALENDAR);
+        if (mailbox != null) {
+            EasSyncService service = new EasSyncService(exchangeService, mailbox);
+            CalendarSyncAdapter adapter = new CalendarSyncAdapter(service);
+            adapter.wipe();
+        }
+    }
+
     class AccountObserver extends ContentObserver {
         String mSyncableEasMailboxSelector = null;
         String mEasAccountSelector = null;
diff --git a/src/com/android/exchange/adapter/FolderSyncParser.java b/src/com/android/exchange/adapter/FolderSyncParser.java
index ae20fd9..84c4be1 100644
--- a/src/com/android/exchange/adapter/FolderSyncParser.java
+++ b/src/com/android/exchange/adapter/FolderSyncParser.java
@@ -129,6 +129,9 @@
                     if (status == Eas.FOLDER_STATUS_INVALID_KEY) {
                         mAccount.mSyncKey = "0";
                         mService.errorLog("Bad sync key; RESET and delete all folders");
+                        // Delete PIM data first
+                        ExchangeService.deleteAccountPIMData(mAccountId);
+                        // Then, delete mailboxes
                         mContentResolver.delete(Mailbox.CONTENT_URI, ALL_BUT_ACCOUNT_MAILBOX,
                                 new String[] {Long.toString(mAccountId)});
                         // Stop existing syncs and reconstruct _main