blob: 9605747ddbb5aa2059a5c58b71538f01c72958db [file] [log] [blame]
Frank Sposaro5f34b2e2011-06-21 15:54:30 -07001/*
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;
19
20import android.content.Context;
21import android.content.CursorLoader;
22import android.net.Uri;
23import android.provider.ContactsContract;
24import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
25import android.provider.ContactsContract.Contacts;
26import android.provider.ContactsContract.Data;
27import android.provider.ContactsContract.Directory;
28
29import java.util.ArrayList;
30import java.util.List;
31
32/**
33 * Group Member loader. Loads all group members from the given groupId
34 */
35public final class GroupMemberLoader extends CursorLoader {
36
37 /**
38 * Projection map is taken from {@link ContactListAdapter}
39 */
40 private final String[] PROJECTION_DATA = new String[] {
41 // TODO: Pull Projection_data out into util class
42 Data.CONTACT_ID, // 0
Katherine Kuan2d851cc2011-07-05 16:23:27 -070043 Data.RAW_CONTACT_ID, // 1
44 Data.DISPLAY_NAME_PRIMARY, // 2
45 Data.DISPLAY_NAME_ALTERNATIVE, // 3
46 Data.SORT_KEY_PRIMARY, // 4
47 Data.STARRED, // 5
48 Data.CONTACT_PRESENCE, // 6
49 Data.CONTACT_CHAT_CAPABILITY, // 7
50 Data.PHOTO_ID, // 8
Dave Santoro0a2a5db2011-06-29 00:37:06 -070051 Data.PHOTO_URI, // 9
52 Data.PHOTO_THUMBNAIL_URI, // 10
53 Data.LOOKUP_KEY, // 11
54 Data.PHONETIC_NAME, // 12
55 Data.HAS_PHONE_NUMBER, // 13
Frank Sposaro595d6ea2011-07-20 11:03:26 -070056 Data.CONTACT_STATUS, // 14
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070057 };
58
59 private final long mGroupId;
60
61 public static final int CONTACT_ID_COLUMN_INDEX = 0;
Katherine Kuan2d851cc2011-07-05 16:23:27 -070062 public static final int RAW_CONTACT_ID_COLUMN_INDEX = 1;
63 public static final int CONTACT_DISPLAY_NAME_PRIMARY_COLUMN_INDEX = 2;
64 public static final int CONTACT_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX = 3;
65 public static final int CONTACT_SORT_KEY_PRIMARY_COLUMN_INDEX = 4;
66 public static final int CONTACT_STARRED_COLUMN_INDEX = 5;
67 public static final int CONTACT_PRESENCE_STATUS_COLUMN_INDEX = 6;
68 public static final int CONTACT_CHAT_CAPABILITY_COLUMN_INDEX = 7;
69 public static final int CONTACT_PHOTO_ID_COLUMN_INDEX = 8;
70 public static final int CONTACT_PHOTO_URI_COLUMN_INDEX = 9;
Dave Santoro0a2a5db2011-06-29 00:37:06 -070071 public static final int CONTACT_PHOTO_THUMBNAIL_URI_COLUMN_INDEX = 10;
Frank Sposaro3a987532011-07-15 14:01:26 -070072 public static final int CONTACT_LOOKUP_KEY_COLUMN_INDEX = 11;
73 public static final int CONTACT_PHONETIC_NAME_COLUMN_INDEX = 12;
74 public static final int CONTACT_HAS_PHONE_COLUMN_INDEX = 13;
Frank Sposaro595d6ea2011-07-20 11:03:26 -070075 public static final int CONTACT_STATUS_COLUMN_INDEX = 14;
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070076
77 public GroupMemberLoader(Context context, long groupId) {
78 super(context);
79 mGroupId = groupId;
80 setUri(createUri());
81 setProjection(PROJECTION_DATA);
82 setSelection(createSelection());
83 setSelectionArgs(createSelectionArgs());
84 setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
85 }
86
87 private Uri createUri() {
88 Uri uri = Data.CONTENT_URI;
89 uri = uri.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
90 String.valueOf(Directory.DEFAULT)).build();
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070091 return uri;
92 }
93
94 private String createSelection() {
95 StringBuilder selection = new StringBuilder();
96 selection.append(Data.MIMETYPE + "=?" + " AND " + GroupMembership.GROUP_ROW_ID + "=?");
97 return selection.toString();
98 }
99
100 private String[] createSelectionArgs() {
101 List<String> selectionArgs = new ArrayList<String>();
102 selectionArgs.add(GroupMembership.CONTENT_ITEM_TYPE);
103 selectionArgs.add(String.valueOf(mGroupId));
104 return selectionArgs.toArray(new String[0]);
105 }
106}