diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-12-09 06:26:25 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-12-09 06:26:25 +0000 |
commit | aab6dc9f075fd8576ffc78c1f983b8fb1d3e77fa (patch) | |
tree | 3845e7aad9e502bf5a91eb8705c8005f06990105 /java/com/android/dialer/contactsfragment/ContactsCursorLoader.java | |
parent | 20ebcdca46e6be68050cd44087f0f768f5dae5c6 (diff) | |
parent | 73b51d5771b31c932a589abc9bb0fe64c52fe102 (diff) |
Merge "Implemented adding a new favorites contact flow NUI."
Diffstat (limited to 'java/com/android/dialer/contactsfragment/ContactsCursorLoader.java')
-rw-r--r-- | java/com/android/dialer/contactsfragment/ContactsCursorLoader.java | 57 |
1 files changed, 43 insertions, 14 deletions
diff --git a/java/com/android/dialer/contactsfragment/ContactsCursorLoader.java b/java/com/android/dialer/contactsfragment/ContactsCursorLoader.java index a22f7eb39..e55f95149 100644 --- a/java/com/android/dialer/contactsfragment/ContactsCursorLoader.java +++ b/java/com/android/dialer/contactsfragment/ContactsCursorLoader.java @@ -18,7 +18,10 @@ package com.android.dialer.contactsfragment; import android.content.Context; import android.content.CursorLoader; +import android.net.Uri; import android.provider.ContactsContract.Contacts; +import android.text.TextUtils; +import com.android.contacts.common.preference.ContactsPreferences; /** Cursor Loader for {@link ContactsFragment}. */ final class ContactsCursorLoader extends CursorLoader { @@ -47,26 +50,52 @@ final class ContactsCursorLoader extends CursorLoader { Contacts.LOOKUP_KEY, // 4 }; - private ContactsCursorLoader(Context context, String[] contactProjection, String sortKey) { + ContactsCursorLoader(Context context, boolean hasPhoneNumbers) { super( context, - Contacts.CONTENT_URI - .buildUpon() - .appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true") - .build(), - contactProjection, - contactProjection[CONTACT_DISPLAY_NAME] + " IS NOT NULL", + buildUri(""), + getProjection(context), + getWhere(context, hasPhoneNumbers), null, - sortKey + " ASC"); + getSortKey(context) + " ASC"); } - public static ContactsCursorLoader createInstanceDisplayNamePrimary( - Context context, String sortKey) { - return new ContactsCursorLoader(context, CONTACTS_PROJECTION_DISPLAY_NAME_PRIMARY, sortKey); + private static String[] getProjection(Context context) { + ContactsPreferences contactsPrefs = new ContactsPreferences(context); + boolean displayOrderPrimary = + (contactsPrefs.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY); + return displayOrderPrimary + ? CONTACTS_PROJECTION_DISPLAY_NAME_PRIMARY + : CONTACTS_PROJECTION_DISPLAY_NAME_ALTERNATIVE; } - public static ContactsCursorLoader createInstanceDisplayNameAlternative( - Context context, String sortKey) { - return new ContactsCursorLoader(context, CONTACTS_PROJECTION_DISPLAY_NAME_ALTERNATIVE, sortKey); + private static String getWhere(Context context, boolean hasPhoneNumbers) { + String where = getProjection(context)[CONTACT_DISPLAY_NAME] + " IS NOT NULL"; + if (hasPhoneNumbers) { + where += " AND " + Contacts.HAS_PHONE_NUMBER + "=1"; + } + return where; + } + + private static String getSortKey(Context context) { + ContactsPreferences contactsPrefs = new ContactsPreferences(context); + boolean sortOrderPrimary = + (contactsPrefs.getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY); + return sortOrderPrimary ? Contacts.SORT_KEY_PRIMARY : Contacts.SORT_KEY_ALTERNATIVE; + } + + /** Update cursor loader to filter contacts based on the provided query. */ + public void setQuery(String query) { + setUri(buildUri(query)); + } + + private static Uri buildUri(String query) { + Uri.Builder baseUri; + if (TextUtils.isEmpty(query)) { + baseUri = Contacts.CONTENT_URI.buildUpon(); + } else { + baseUri = Contacts.CONTENT_FILTER_URI.buildUpon().appendPath(query); + } + return baseUri.appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true").build(); } } |