1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.dialer.contactsfragment; 18 19 import android.content.Context; 20 import android.content.CursorLoader; 21 import android.net.Uri; 22 import android.provider.ContactsContract.Contacts; 23 import android.text.TextUtils; 24 import com.android.dialer.contacts.ContactsComponent; 25 26 /** Cursor Loader for {@link ContactsFragment}. */ 27 final class ContactsCursorLoader extends CursorLoader { 28 29 public static final int CONTACT_ID = 0; 30 public static final int CONTACT_DISPLAY_NAME = 1; 31 public static final int CONTACT_PHOTO_ID = 2; 32 public static final int CONTACT_PHOTO_URI = 3; 33 public static final int CONTACT_LOOKUP_KEY = 4; 34 35 public static final String[] CONTACTS_PROJECTION_DISPLAY_NAME_PRIMARY = 36 new String[] { 37 Contacts._ID, // 0 38 Contacts.DISPLAY_NAME_PRIMARY, // 1 39 Contacts.PHOTO_ID, // 2 40 Contacts.PHOTO_THUMBNAIL_URI, // 3 41 Contacts.LOOKUP_KEY, // 4 42 }; 43 44 public static final String[] CONTACTS_PROJECTION_DISPLAY_NAME_ALTERNATIVE = 45 new String[] { 46 Contacts._ID, // 0 47 Contacts.DISPLAY_NAME_ALTERNATIVE, // 1 48 Contacts.PHOTO_ID, // 2 49 Contacts.PHOTO_THUMBNAIL_URI, // 3 50 Contacts.LOOKUP_KEY, // 4 51 }; 52 ContactsCursorLoader(Context context, boolean hasPhoneNumbers)53 ContactsCursorLoader(Context context, boolean hasPhoneNumbers) { 54 super( 55 context, 56 buildUri(""), 57 getProjection(context), 58 getWhere(context, hasPhoneNumbers), 59 null, 60 getSortKey(context) + " ASC"); 61 } 62 getProjection(Context context)63 private static String[] getProjection(Context context) { 64 switch (ContactsComponent.get(context).contactDisplayPreferences().getDisplayOrder()) { 65 case PRIMARY: 66 return CONTACTS_PROJECTION_DISPLAY_NAME_PRIMARY; 67 case ALTERNATIVE: 68 return CONTACTS_PROJECTION_DISPLAY_NAME_ALTERNATIVE; 69 } 70 throw new AssertionError("exhaustive switch"); 71 } 72 getWhere(Context context, boolean hasPhoneNumbers)73 private static String getWhere(Context context, boolean hasPhoneNumbers) { 74 String where = getProjection(context)[CONTACT_DISPLAY_NAME] + " IS NOT NULL"; 75 if (hasPhoneNumbers) { 76 where += " AND " + Contacts.HAS_PHONE_NUMBER + "=1"; 77 } 78 return where; 79 } 80 getSortKey(Context context)81 private static String getSortKey(Context context) { 82 83 switch (ContactsComponent.get(context).contactDisplayPreferences().getSortOrder()) { 84 case BY_PRIMARY: 85 return Contacts.SORT_KEY_PRIMARY; 86 case BY_ALTERNATIVE: 87 return Contacts.SORT_KEY_ALTERNATIVE; 88 } 89 throw new AssertionError("exhaustive switch"); 90 } 91 92 /** Update cursor loader to filter contacts based on the provided query. */ setQuery(String query)93 public void setQuery(String query) { 94 setUri(buildUri(query)); 95 } 96 buildUri(String query)97 private static Uri buildUri(String query) { 98 Uri.Builder baseUri; 99 if (TextUtils.isEmpty(query)) { 100 baseUri = Contacts.CONTENT_URI.buildUpon(); 101 } else { 102 baseUri = Contacts.CONTENT_FILTER_URI.buildUpon().appendPath(query); 103 } 104 return baseUri.appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true").build(); 105 } 106 } 107