Excluding accounts that don't support contact sync

Bug: 2663372
Change-Id: I11958cdb9461e94b176bcb7b118bd5f027b5549a
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 5e9df0d..cbff3ae 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -34,6 +34,7 @@
     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />
     <uses-permission android:name="android.permission.VIBRATE" />
+    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
 
     <application
         android:label="@string/contactsList"
diff --git a/src/com/android/contacts/model/AccountTypes.java b/src/com/android/contacts/model/AccountTypes.java
index acf9569..973820a 100644
--- a/src/com/android/contacts/model/AccountTypes.java
+++ b/src/com/android/contacts/model/AccountTypes.java
@@ -247,16 +247,28 @@
         final AccountManager am = mAccountManager;
         final Account[] accounts = am.getAccounts();
         final ArrayList<Account> matching = Lists.newArrayList();
+        final IContentService cs = ContentResolver.getContentService();
 
         for (Account account : accounts) {
-            // Ensure we have details loaded for each account
-            final BaseAccountType accountType = getInflatedSource(account.type,
-                    BaseAccountType.LEVEL_SUMMARY);
-            final boolean hasContacts = accountType != null;
-            final boolean matchesWritable =
+            boolean syncable = false;
+            try {
+                int isSyncable = cs.getIsSyncable(account, ContactsContract.AUTHORITY);
+                if (isSyncable > 0) {
+                    syncable = true;
+                }
+            } catch (RemoteException e) {
+                Log.e(TAG, "Cannot obtain sync flag for account: " + account, e);
+            }
+            if (syncable) {
+                // Ensure we have details loaded for each account
+                final BaseAccountType accountType = getInflatedSource(account.type,
+                        BaseAccountType.LEVEL_SUMMARY);
+                final boolean hasContacts = accountType != null;
+                final boolean matchesWritable =
                     (!writableOnly || (writableOnly && !accountType.readOnly));
-            if (hasContacts && matchesWritable) {
-                matching.add(account);
+                if (hasContacts && matchesWritable) {
+                    matching.add(account);
+                }
             }
         }
         return matching;