Merge "Two fixes for custom filter activity."
diff --git a/src/com/android/contacts/list/AccountFilterActivity.java b/src/com/android/contacts/list/AccountFilterActivity.java
index 02abb53..8e0f9b8 100644
--- a/src/com/android/contacts/list/AccountFilterActivity.java
+++ b/src/com/android/contacts/list/AccountFilterActivity.java
@@ -68,10 +68,6 @@
 
     private ListView mListView;
 
-    private static final String[] ID_PROJECTION = new String[] {BaseColumns._ID};
-    private static final Uri RAW_CONTACTS_URI_LIMIT_1 = RawContacts.CONTENT_URI.buildUpon()
-            .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, "1").build();
-
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -124,7 +120,7 @@
         List<AccountWithDataSet> accounts = accountTypes.getAccounts(false);
         for (AccountWithDataSet account : accounts) {
             AccountType accountType = accountTypes.getAccountType(account.type, account.dataSet);
-            if (accountType.isExtension() && !hasAccountData(context, account)) {
+            if (accountType.isExtension() && !account.hasData(context)) {
                 // Hide extensions with no raw_contacts.
                 continue;
             }
@@ -149,29 +145,6 @@
         return result;
     }
 
-    private static boolean hasAccountData(Context context, AccountWithDataSet account) {
-        final String BASE_SELECTION =
-                RawContacts.ACCOUNT_TYPE + " = ?" + " AND " + RawContacts.ACCOUNT_NAME + " = ?";
-        final String selection;
-        final String[] args;
-        if (TextUtils.isEmpty(account.dataSet)) {
-            selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " IS NULL";
-            args = new String[] {account.type, account.name};
-        } else {
-            selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " = ?";
-            args = new String[] {account.type, account.name, account.dataSet};
-        }
-
-        final Cursor c = context.getContentResolver().query(RAW_CONTACTS_URI_LIMIT_1,
-                ID_PROJECTION, selection, args, null);
-        if (c == null) return false;
-        try {
-            return c.moveToFirst();
-        } finally {
-            c.close();
-        }
-    }
-
     private class MyLoaderCallbacks implements LoaderCallbacks<List<ContactListFilter>> {
         @Override
         public Loader<List<ContactListFilter>> onCreateLoader(int id, Bundle args) {
diff --git a/src/com/android/contacts/list/CustomContactListFilterActivity.java b/src/com/android/contacts/list/CustomContactListFilterActivity.java
index 26610dc..1ac1d32 100644
--- a/src/com/android/contacts/list/CustomContactListFilterActivity.java
+++ b/src/com/android/contacts/list/CustomContactListFilterActivity.java
@@ -132,6 +132,12 @@
 
             final AccountSet accounts = new AccountSet();
             for (AccountWithDataSet account : accountTypes.getAccounts(false)) {
+                final AccountType accountType = accountTypes.getAccountTypeForAccount(account);
+                if (accountType.isExtension() && !account.hasData(context)) {
+                    // Extension with no data -- skip.
+                    continue;
+                }
+
                 AccountDisplay accountDisplay =
                         new AccountDisplay(resolver, account.name, account.type, account.dataSet);
 
diff --git a/src/com/android/contacts/model/AccountTypeManager.java b/src/com/android/contacts/model/AccountTypeManager.java
index d28d5bb..d60f355 100644
--- a/src/com/android/contacts/model/AccountTypeManager.java
+++ b/src/com/android/contacts/model/AccountTypeManager.java
@@ -85,7 +85,15 @@
 
     public abstract List<AccountWithDataSet> getAccounts(boolean writableOnly);
 
-    public abstract AccountType getAccountType(String accountType, String dataSet);
+    public abstract AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet);
+
+    public final AccountType getAccountType(String accountType, String dataSet) {
+        return getAccountType(AccountTypeWithDataSet.get(accountType, dataSet));
+    }
+
+    public final AccountType getAccountTypeForAccount(AccountWithDataSet account) {
+        return getAccountType(account.getAccountTypeWithDataSet());
+    }
 
     /**
      * @return Unmodifiable map from {@link AccountTypeWithDataSet}s to {@link AccountType}s
@@ -482,11 +490,10 @@
      * Return {@link AccountType} for the given account type and data set.
      */
     @Override
-    public AccountType getAccountType(String accountType, String dataSet) {
+    public AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet) {
         ensureAccountsLoaded();
         synchronized (this) {
-            AccountType type = mAccountTypesWithDataSets.get(
-                    AccountTypeWithDataSet.get(accountType, dataSet));
+            AccountType type = mAccountTypesWithDataSets.get(accountTypeWithDataSet);
             return type != null ? type : mFallbackAccountType;
         }
     }
@@ -506,7 +513,7 @@
             Map<AccountTypeWithDataSet, AccountType> accountTypesByTypeAndDataSet) {
         HashMap<AccountTypeWithDataSet, AccountType> result = Maps.newHashMap();
         for (AccountWithDataSet account : accounts) {
-            AccountTypeWithDataSet accountTypeWithDataSet = account.getAccountTypeAndWithDataSet();
+            AccountTypeWithDataSet accountTypeWithDataSet = account.getAccountTypeWithDataSet();
             AccountType type = accountTypesByTypeAndDataSet.get(accountTypeWithDataSet);
             if (type == null) continue; // just in case
             if (result.containsKey(accountTypeWithDataSet)) continue;
diff --git a/src/com/android/contacts/model/AccountWithDataSet.java b/src/com/android/contacts/model/AccountWithDataSet.java
index f607737..55af795 100644
--- a/src/com/android/contacts/model/AccountWithDataSet.java
+++ b/src/com/android/contacts/model/AccountWithDataSet.java
@@ -19,7 +19,14 @@
 import com.android.internal.util.Objects;
 
 import android.accounts.Account;
+import android.content.Context;
+import android.database.Cursor;
+import android.net.Uri;
 import android.os.Parcel;
+import android.provider.BaseColumns;
+import android.provider.ContactsContract;
+import android.provider.ContactsContract.RawContacts;
+import android.text.TextUtils;
 
 /**
  * Wrapper for an account that includes a data set (which may be null).
@@ -29,6 +36,11 @@
     public final String dataSet;
     private final AccountTypeWithDataSet mAccountTypeWithDataSet;
 
+    private static final String[] ID_PROJECTION = new String[] {BaseColumns._ID};
+    private static final Uri RAW_CONTACTS_URI_LIMIT_1 = RawContacts.CONTENT_URI.buildUpon()
+            .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, "1").build();
+
+
     public AccountWithDataSet(String name, String type, String dataSet) {
         super(name, type);
         this.dataSet = dataSet;
@@ -41,10 +53,37 @@
         mAccountTypeWithDataSet = AccountTypeWithDataSet.get(type, dataSet);
     }
 
-    public AccountTypeWithDataSet getAccountTypeAndWithDataSet() {
+    public AccountTypeWithDataSet getAccountTypeWithDataSet() {
         return mAccountTypeWithDataSet;
     }
 
+    /**
+     * Return {@code true} if this account has any contacts in the database.
+     * Touches DB.  Don't use in the UI thread.
+     */
+    public boolean hasData(Context context) {
+        final String BASE_SELECTION =
+                RawContacts.ACCOUNT_TYPE + " = ?" + " AND " + RawContacts.ACCOUNT_NAME + " = ?";
+        final String selection;
+        final String[] args;
+        if (TextUtils.isEmpty(dataSet)) {
+            selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " IS NULL";
+            args = new String[] {type, name};
+        } else {
+            selection = BASE_SELECTION + " AND " + RawContacts.DATA_SET + " = ?";
+            args = new String[] {type, name, dataSet};
+        }
+
+        final Cursor c = context.getContentResolver().query(RAW_CONTACTS_URI_LIMIT_1,
+                ID_PROJECTION, selection, args, null);
+        if (c == null) return false;
+        try {
+            return c.moveToFirst();
+        } finally {
+            c.close();
+        }
+    }
+
     @Override
     public boolean equals(Object o) {
         return (o instanceof AccountWithDataSet) && super.equals(o)
diff --git a/tests/src/com/android/contacts/tests/mocks/MockAccountTypeManager.java b/tests/src/com/android/contacts/tests/mocks/MockAccountTypeManager.java
index 2be662d..7a04ae3 100644
--- a/tests/src/com/android/contacts/tests/mocks/MockAccountTypeManager.java
+++ b/tests/src/com/android/contacts/tests/mocks/MockAccountTypeManager.java
@@ -25,6 +25,8 @@
 import java.util.List;
 import java.util.Map;
 
+import libcore.util.Objects;
+
 /**
  * A mock {@link AccountTypeManager} class.
  */
@@ -39,9 +41,10 @@
     }
 
     @Override
-    public AccountType getAccountType(String accountType, String dataSet) {
+    public AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet) {
         for (AccountType type : mTypes) {
-            if (accountType.equals(type.accountType)) {
+            if (Objects.equal(accountTypeWithDataSet.accountType, type.accountType)
+                    && Objects.equal(accountTypeWithDataSet.dataSet, type.dataSet)) {
                 return type;
             }
         }