• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.contacts.common.preference.ContactsPreferences;
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     ContactsPreferences contactsPrefs = new ContactsPreferences(context);
65     boolean displayOrderPrimary =
66         (contactsPrefs.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY);
67     return displayOrderPrimary
68         ? CONTACTS_PROJECTION_DISPLAY_NAME_PRIMARY
69         : CONTACTS_PROJECTION_DISPLAY_NAME_ALTERNATIVE;
70   }
71 
getWhere(Context context, boolean hasPhoneNumbers)72   private static String getWhere(Context context, boolean hasPhoneNumbers) {
73     String where = getProjection(context)[CONTACT_DISPLAY_NAME] + " IS NOT NULL";
74     if (hasPhoneNumbers) {
75       where += " AND " + Contacts.HAS_PHONE_NUMBER + "=1";
76     }
77     return where;
78   }
79 
getSortKey(Context context)80   private static String getSortKey(Context context) {
81     ContactsPreferences contactsPrefs = new ContactsPreferences(context);
82     boolean sortOrderPrimary =
83         (contactsPrefs.getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY);
84     return sortOrderPrimary ? Contacts.SORT_KEY_PRIMARY : Contacts.SORT_KEY_ALTERNATIVE;
85   }
86 
87   /** Update cursor loader to filter contacts based on the provided query. */
setQuery(String query)88   public void setQuery(String query) {
89     setUri(buildUri(query));
90   }
91 
buildUri(String query)92   private static Uri buildUri(String query) {
93     Uri.Builder baseUri;
94     if (TextUtils.isEmpty(query)) {
95       baseUri = Contacts.CONTENT_URI.buildUpon();
96     } else {
97       baseUri = Contacts.CONTENT_FILTER_URI.buildUpon().appendPath(query);
98     }
99     return baseUri.appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true").build();
100   }
101 }
102