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