Dont show invisible contacts in frequent.

Bug 6000492

Change-Id: I0e316f294feca805038611e0449fddcbda0b2deb
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index 1cba421..021202f 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -5099,9 +5099,10 @@
                             mDbHelper.get().getMimeTypeId(SipAddress.CONTENT_ITEM_TYPE);
                     qb.appendWhere(DbQueryUtils.concatenateClauses(
                             selection,
-                            Contacts.STARRED + "=0 OR " + Contacts.STARRED + " IS NULL",
+                            "(" + Contacts.STARRED + "=0 OR " + Contacts.STARRED + " IS NULL",
                             DataColumns.MIMETYPE_ID + " IN (" +
-                            phoneMimeTypeId + ", " + sipMimeTypeId + ")"));
+                            phoneMimeTypeId + ", " + sipMimeTypeId + ")) AND (" +
+                            RawContacts.CONTACT_ID + " IN " + Tables.DEFAULT_DIRECTORY + ")"));
                     frequentInnerQuery =
                             qb.buildQuery(subProjection, null, null, null,
                             TIMES_USED_SORT_COLUMN + " DESC", "25");
@@ -5111,8 +5112,12 @@
                     qb.appendWhere(DbQueryUtils.concatenateClauses(
                             selection,
                             "(" + Contacts.STARRED + " =0 OR " + Contacts.STARRED + " IS NULL)"));
+                    // Note frequentInnerQuery is a grouping query, so the "IN default_directory"
+                    // selection needs to be in HAVING, not in WHERE.
+                    final String HAVING =
+                            RawContacts.CONTACT_ID + " IN " + Tables.DEFAULT_DIRECTORY;
                     frequentInnerQuery = qb.buildQuery(subProjection,
-                            null, Contacts._ID, null, null, "25");
+                            null, Contacts._ID, HAVING, null, "25");
                 }
 
                 // We need to wrap the inner queries in an extra select, because they contain