Support Multiple Contact Folders

Bug: 10893901
Change-Id: Ibea2faea80299f9dd20cb35efca11ebe7dbd4463
diff --git a/src/com/android/exchange/service/ContactsSyncAdapterService.java b/src/com/android/exchange/service/ContactsSyncAdapterService.java
index d99cfcd..e2dd7e3 100644
--- a/src/com/android/exchange/service/ContactsSyncAdapterService.java
+++ b/src/com/android/exchange/service/ContactsSyncAdapterService.java
@@ -128,7 +128,7 @@
                 final Cursor mailboxCursor = cr.query(Mailbox.CONTENT_URI, Mailbox.ID_PROJECTION,
                         ACCOUNT_AND_TYPE_CONTACTS, new String[] {Long.toString(accountId)}, null);
                 try {
-                     if (mailboxCursor.moveToFirst()) {
+                    while (mailboxCursor.moveToNext()) {
                          LogUtils.i(TAG, "Contact sync requested for " + account.name);
                          // TODO: Currently just bouncing this to Email sync; eventually streamline.
                         final long mailboxId = mailboxCursor.getLong(Mailbox.ID_PROJECTION_COLUMN);
diff --git a/src/com/android/exchange/service/EasCalendarSyncHandler.java b/src/com/android/exchange/service/EasCalendarSyncHandler.java
index d5c8415..8884739 100644
--- a/src/com/android/exchange/service/EasCalendarSyncHandler.java
+++ b/src/com/android/exchange/service/EasCalendarSyncHandler.java
@@ -171,17 +171,6 @@
     }
 
     @Override
-    protected String getSyncKey() {
-        if (mMailbox == null) {
-            return null;
-        }
-        if (mMailbox.mSyncKey == null) {
-            mMailbox.mSyncKey = "0";
-        }
-        return mMailbox.mSyncKey;
-    }
-
-    @Override
     protected String getFolderClassName() {
         return "Calendar";
     }
diff --git a/src/com/android/exchange/service/EasContactsSyncHandler.java b/src/com/android/exchange/service/EasContactsSyncHandler.java
index cc593e1..8daf3eb 100644
--- a/src/com/android/exchange/service/EasContactsSyncHandler.java
+++ b/src/com/android/exchange/service/EasContactsSyncHandler.java
@@ -1,6 +1,5 @@
 package com.android.exchange.service;
 
-import android.content.ContentProviderClient;
 import android.content.ContentProviderOperation;
 import android.content.ContentResolver;
 import android.content.ContentUris;
@@ -12,7 +11,6 @@
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.RemoteException;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.CommonDataKinds.Email;
 import android.provider.ContactsContract.CommonDataKinds.Event;
@@ -28,7 +26,6 @@
 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
 import android.provider.ContactsContract.CommonDataKinds.Website;
 import android.provider.ContactsContract.Groups;
-import android.provider.SyncStateContract;
 import android.text.TextUtils;
 import android.util.Base64;
 
@@ -162,40 +159,6 @@
     }
 
     @Override
-    protected String getSyncKey() {
-        // mMailbox.mSyncKey is bogus since state is stored by the contacts provider, so we
-        // need to fetch the data from there.
-        // However, we need for that value to be reasonable, so we set it here once we fetch it.
-        final ContentProviderClient client = mContentResolver.acquireContentProviderClient(
-                ContactsContract.AUTHORITY_URI);
-        try {
-            final byte[] data = SyncStateContract.Helpers.get(client,
-                    ContactsContract.SyncState.CONTENT_URI, mAccountManagerAccount);
-            if (data == null || data.length == 0) {
-                // We don't have a sync key yet, initialize it.
-                // TODO: Should we leave it and just let the first successful sync set it?
-                /*
-                mMailbox.mSyncKey = "0";
-                SyncStateContract.Helpers.set(client, ContactsContract.SyncState.CONTENT_URI,
-                            mAccountManagerAccount, "0".getBytes());
-                // Make sure ungrouped contacts for Exchange are visible by default.
-                final ContentValues cv = new ContentValues(3);
-                cv.put(Groups.ACCOUNT_NAME, mAccount.mEmailAddress);
-                cv.put(Groups.ACCOUNT_TYPE, Eas.EXCHANGE_ACCOUNT_MANAGER_TYPE);
-                cv.put(ContactsContract.Settings.UNGROUPED_VISIBLE, true);
-                client.insert(addCallerIsSyncAdapterParameter(Settings.CONTENT_URI), cv);
-                */
-                mMailbox.mSyncKey = "0";
-            } else {
-                mMailbox.mSyncKey = new String(data);
-            }
-            return mMailbox.mSyncKey;
-        } catch (final RemoteException e) {
-            return null;
-        }
-    }
-
-    @Override
     protected String getFolderClassName() {
         return "Contacts";
     }
diff --git a/src/com/android/exchange/service/EasMailboxSyncHandler.java b/src/com/android/exchange/service/EasMailboxSyncHandler.java
index 3364a78..ab297a4 100644
--- a/src/com/android/exchange/service/EasMailboxSyncHandler.java
+++ b/src/com/android/exchange/service/EasMailboxSyncHandler.java
@@ -96,18 +96,6 @@
     }
 
     @Override
-    protected String getSyncKey() {
-        if (mMailbox == null) {
-            return null;
-        }
-        if (mMailbox.mSyncKey == null) {
-            // TODO: Write to DB? Probably not, and just let successful sync do that.
-            mMailbox.mSyncKey = "0";
-        }
-        return mMailbox.mSyncKey;
-    }
-
-    @Override
     protected String getFolderClassName() {
         return "Email";
     }
diff --git a/src/com/android/exchange/service/EasSyncHandler.java b/src/com/android/exchange/service/EasSyncHandler.java
index 0054163..29f55b5 100644
--- a/src/com/android/exchange/service/EasSyncHandler.java
+++ b/src/com/android/exchange/service/EasSyncHandler.java
@@ -156,7 +156,15 @@
      * @return The sync key for the object being synced. "0" means this is the first sync. If
      *      there is an error in getting the sync key, this function returns null.
      */
-    protected abstract String getSyncKey();
+    protected String getSyncKey() {
+        if (mMailbox == null) {
+            return null;
+        }
+        if (mMailbox.mSyncKey == null) {
+            mMailbox.mSyncKey = "0";
+        }
+        return mMailbox.mSyncKey;
+    }
 
     /**
      * Get the folder class name for this mailbox.