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 */
16package com.android.contacts.common;
17
18import com.google.common.annotations.VisibleForTesting;
19
20import android.content.Context;
21import android.content.CursorLoader;
22import android.net.Uri;
23import android.provider.ContactsContract;
24import android.provider.ContactsContract.CommonDataKinds.Phone;
25import 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 */
31public 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
52    private static final String[] COLUMNS = new String[] {
53        Contacts._ID, // ..........................................0
54        Contacts.DISPLAY_NAME, // .................................1
55        Contacts.STARRED, // ......................................2
56        Contacts.PHOTO_URI, // ....................................3
57        Contacts.LOOKUP_KEY, // ...................................4
58        Contacts.CONTACT_PRESENCE, // .............................5
59        Contacts.CONTACT_STATUS, // ...............................6
60    };
61
62    /**
63     * Projection used for the {@link Contacts#CONTENT_STREQUENT_URI}
64     * query when {@link ContactsContract#STREQUENT_PHONE_ONLY} flag
65     * is set to true. The main difference is the lack of presence
66     * and status data and the addition of phone number and label.
67     */
68    @VisibleForTesting
69    public static final String[] COLUMNS_PHONE_ONLY = new String[] {
70        Contacts._ID, // ..........................................0
71        Contacts.DISPLAY_NAME, // .................................1
72        Contacts.STARRED, // ......................................2
73        Contacts.PHOTO_URI, // ....................................3
74        Contacts.LOOKUP_KEY, // ...................................4
75        Phone.NUMBER, // ..........................................5
76        Phone.TYPE, // ............................................6
77        Phone.LABEL, // ...........................................7
78        Phone.IS_SUPER_PRIMARY, //.................................8
79        Contacts.PINNED, // .......................................9
80        Phone.CONTACT_ID //........................................10
81    };
82
83    private static final String STARRED_ORDER = Contacts.DISPLAY_NAME+" COLLATE NOCASE ASC";
84
85    public static CursorLoader createStrequentLoader(Context context) {
86        return new CursorLoader(context, Contacts.CONTENT_STREQUENT_URI, COLUMNS, null, null,
87                STARRED_ORDER);
88    }
89
90    public static CursorLoader createStrequentPhoneOnlyLoader(Context context) {
91        Uri uri = Contacts.CONTENT_STREQUENT_URI.buildUpon()
92                .appendQueryParameter(ContactsContract.STREQUENT_PHONE_ONLY, "true").build();
93
94        return new CursorLoader(context, uri, COLUMNS_PHONE_ONLY, null, null, null);
95    }
96
97    public static CursorLoader createStarredLoader(Context context) {
98        return new CursorLoader(context, Contacts.CONTENT_URI, COLUMNS, Contacts.STARRED + "=?",
99                new String[]{"1"},  STARRED_ORDER);
100    }
101
102    public static CursorLoader createFrequentLoader(Context context) {
103        return new CursorLoader(context, Contacts.CONTENT_FREQUENT_URI, COLUMNS,
104                 Contacts.STARRED + "=?", new String[]{"0"}, null);
105    }
106}
107