GroupMemberLoader.java revision 5a8faeb4f17dab62557adc8cdc99284a9221ea35
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;
17
18import com.android.contacts.list.ContactListAdapter;
19import com.android.contacts.preference.ContactsPreferences;
20
21import android.content.Context;
22import android.content.CursorLoader;
23import android.net.Uri;
24import android.provider.ContactsContract;
25import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
26import android.provider.ContactsContract.Contacts;
27import android.provider.ContactsContract.Data;
28import android.provider.ContactsContract.Directory;
29
30import java.util.ArrayList;
31import java.util.List;
32
33/**
34 * Group Member loader. Loads all group members from the given groupId
35 */
36public final class GroupMemberLoader extends CursorLoader {
37
38    /**
39     * Projection map is taken from {@link ContactListAdapter}
40     */
41    private static final String[] PROJECTION_DATA = new String[] {
42        // TODO: Pull Projection_data out into util class
43        Data.CONTACT_ID,                        // 0
44        Data.RAW_CONTACT_ID,                    // 1
45        Data.DISPLAY_NAME_PRIMARY,              // 2
46        Data.DISPLAY_NAME_ALTERNATIVE,          // 3
47        Data.SORT_KEY_PRIMARY,                  // 4
48        Data.STARRED,                           // 5
49        Data.CONTACT_PRESENCE,                  // 6
50        Data.CONTACT_CHAT_CAPABILITY,           // 7
51        Data.PHOTO_ID,                          // 8
52        Data.PHOTO_URI,                         // 9
53        Data.PHOTO_THUMBNAIL_URI,               // 10
54        Data.LOOKUP_KEY,                        // 11
55        Data.PHONETIC_NAME,                     // 12
56        Data.HAS_PHONE_NUMBER,                  // 13
57        Data.CONTACT_STATUS,                    // 14
58    };
59
60    private final long mGroupId;
61
62    public static final int CONTACT_ID_COLUMN_INDEX = 0;
63    public static final int RAW_CONTACT_ID_COLUMN_INDEX = 1;
64    public static final int CONTACT_DISPLAY_NAME_PRIMARY_COLUMN_INDEX = 2;
65    public static final int CONTACT_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX = 3;
66    public static final int CONTACT_SORT_KEY_PRIMARY_COLUMN_INDEX = 4;
67    public static final int CONTACT_STARRED_COLUMN_INDEX = 5;
68    public static final int CONTACT_PRESENCE_STATUS_COLUMN_INDEX = 6;
69    public static final int CONTACT_CHAT_CAPABILITY_COLUMN_INDEX = 7;
70    public static final int CONTACT_PHOTO_ID_COLUMN_INDEX = 8;
71    public static final int CONTACT_PHOTO_URI_COLUMN_INDEX = 9;
72    public static final int CONTACT_PHOTO_THUMBNAIL_URI_COLUMN_INDEX = 10;
73    public static final int CONTACT_LOOKUP_KEY_COLUMN_INDEX = 11;
74    public static final int CONTACT_PHONETIC_NAME_COLUMN_INDEX = 12;
75    public static final int CONTACT_HAS_PHONE_COLUMN_INDEX = 13;
76    public static final int CONTACT_STATUS_COLUMN_INDEX = 14;
77
78    public GroupMemberLoader(Context context, long groupId) {
79        super(context);
80        mGroupId = groupId;
81        setUri(createUri());
82        setProjection(PROJECTION_DATA);
83        setSelection(createSelection());
84        setSelectionArgs(createSelectionArgs());
85
86        ContactsPreferences prefs = new ContactsPreferences(context);
87        if (prefs.getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
88            setSortOrder(Contacts.SORT_KEY_PRIMARY);
89        } else {
90            setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
91        }
92    }
93
94    private Uri createUri() {
95        Uri uri = Data.CONTENT_URI;
96        uri = uri.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
97                String.valueOf(Directory.DEFAULT)).build();
98        return uri;
99    }
100
101    private String createSelection() {
102        StringBuilder selection = new StringBuilder();
103        selection.append(Data.MIMETYPE + "=?" + " AND " + GroupMembership.GROUP_ROW_ID + "=?");
104        return selection.toString();
105    }
106
107    private String[] createSelectionArgs() {
108        List<String> selectionArgs = new ArrayList<String>();
109        selectionArgs.add(GroupMembership.CONTENT_ITEM_TYPE);
110        selectionArgs.add(String.valueOf(mGroupId));
111        return selectionArgs.toArray(new String[0]);
112    }
113}
114