1 /*
2  * Copyright (C) 2011 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 package com.android.contacts.common;
17 
18 import com.google.common.annotations.VisibleForTesting;
19 
20 import android.content.Context;
21 import android.content.CursorLoader;
22 import android.net.Uri;
23 import android.provider.ContactsContract;
24 import android.provider.ContactsContract.CommonDataKinds.Phone;
25 import android.provider.ContactsContract.Contacts;
26 
27 /**
28  * Used to create {@link CursorLoader}s to load different groups of
29  * {@link com.android.contacts.list.ContactTileView}.
30  */
31 public final class ContactTileLoaderFactory {
32 
33     public final static int CONTACT_ID = 0;
34     public final static int DISPLAY_NAME = 1;
35     public final static int STARRED = 2;
36     public final static int PHOTO_URI = 3;
37     public final static int LOOKUP_KEY = 4;
38     public final static int CONTACT_PRESENCE = 5;
39     public final static int CONTACT_STATUS = 6;
40 
41     // Only used for StrequentPhoneOnlyLoader
42     public final static int PHONE_NUMBER = 5;
43     public final static int PHONE_NUMBER_TYPE = 6;
44     public final static int PHONE_NUMBER_LABEL = 7;
45     public final static int IS_DEFAULT_NUMBER = 8;
46     public final static int PINNED = 9;
47     // The _ID field returned for strequent items actually contains data._id instead of
48     // contacts._id because the query is performed on the data table. In order to obtain the
49     // contact id for strequent items, we thus have to use Phone.contact_id instead.
50     public final static int CONTACT_ID_FOR_DATA = 10;
51     public final static int DISPLAY_NAME_ALTERNATIVE = 11;
52 
53     private static final String[] COLUMNS = new String[] {
54         Contacts._ID, // ..........................................0
55         Contacts.DISPLAY_NAME, // .................................1
56         Contacts.STARRED, // ......................................2
57         Contacts.PHOTO_URI, // ....................................3
58         Contacts.LOOKUP_KEY, // ...................................4
59         Contacts.CONTACT_PRESENCE, // .............................5
60         Contacts.CONTACT_STATUS, // ...............................6
61     };
62 
63     /**
64      * Projection used for the {@link Contacts#CONTENT_STREQUENT_URI}
65      * query when {@link ContactsContract#STREQUENT_PHONE_ONLY} flag
66      * is set to true. The main difference is the lack of presence
67      * and status data and the addition of phone number and label.
68      */
69     @VisibleForTesting
70     public static final String[] COLUMNS_PHONE_ONLY = new String[] {
71         Contacts._ID, // ..........................................0
72         Contacts.DISPLAY_NAME_PRIMARY, // .........................1
73         Contacts.STARRED, // ......................................2
74         Contacts.PHOTO_URI, // ....................................3
75         Contacts.LOOKUP_KEY, // ...................................4
76         Phone.NUMBER, // ..........................................5
77         Phone.TYPE, // ............................................6
78         Phone.LABEL, // ...........................................7
79         Phone.IS_SUPER_PRIMARY, //.................................8
80         Contacts.PINNED, // .......................................9
81         Phone.CONTACT_ID, //.......................................10
82         Contacts.DISPLAY_NAME_ALTERNATIVE, // .....................11
83     };
84 
85     private static final String STARRED_ORDER = Contacts.DISPLAY_NAME+" COLLATE NOCASE ASC";
86 
createStrequentLoader(Context context)87     public static CursorLoader createStrequentLoader(Context context) {
88         return new CursorLoader(context, Contacts.CONTENT_STREQUENT_URI, COLUMNS, null, null,
89                 STARRED_ORDER);
90     }
91 
createStrequentPhoneOnlyLoader(Context context)92     public static CursorLoader createStrequentPhoneOnlyLoader(Context context) {
93         Uri uri = Contacts.CONTENT_STREQUENT_URI.buildUpon()
94                 .appendQueryParameter(ContactsContract.STREQUENT_PHONE_ONLY, "true").build();
95 
96         return new CursorLoader(context, uri, COLUMNS_PHONE_ONLY, null, null, null);
97     }
98 
createStarredLoader(Context context)99     public static CursorLoader createStarredLoader(Context context) {
100         return new CursorLoader(context, Contacts.CONTENT_URI, COLUMNS, Contacts.STARRED + "=?",
101                 new String[]{"1"},  STARRED_ORDER);
102     }
103 
createFrequentLoader(Context context)104     public static CursorLoader createFrequentLoader(Context context) {
105         return new CursorLoader(context, Contacts.CONTENT_FREQUENT_URI, COLUMNS,
106                  Contacts.STARRED + "=?", new String[]{"0"}, null);
107     }
108 }
109