blob: 9012362ec1e659d49564c509fa5af42e9b578f4f [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;
Isaac Katzenelson4b9b26e2011-09-23 15:01:00 -070019import com.android.contacts.preference.ContactsPreferences;
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070020
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 */
Daisuke Miyakawa5a8faeb2011-10-31 16:39:14 -070041 private static final String[] PROJECTION_DATA = new String[] {
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070042 // TODO: Pull Projection_data out into util class
43 Data.CONTACT_ID, // 0
Katherine Kuan2d851cc2011-07-05 16:23:27 -070044 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
Dave Santoro0a2a5db2011-06-29 00:37:06 -070052 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
Frank Sposaro595d6ea2011-07-20 11:03:26 -070057 Data.CONTACT_STATUS, // 14
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070058 };
59
60 private final long mGroupId;
61
62 public static final int CONTACT_ID_COLUMN_INDEX = 0;
Katherine Kuan2d851cc2011-07-05 16:23:27 -070063 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;
Dave Santoro0a2a5db2011-06-29 00:37:06 -070072 public static final int CONTACT_PHOTO_THUMBNAIL_URI_COLUMN_INDEX = 10;
Frank Sposaro3a987532011-07-15 14:01:26 -070073 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;
Frank Sposaro595d6ea2011-07-20 11:03:26 -070076 public static final int CONTACT_STATUS_COLUMN_INDEX = 14;
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070077
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());
Isaac Katzenelson4b9b26e2011-09-23 15:01:00 -070085
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 }
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070092 }
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();
Frank Sposaro5f34b2e2011-06-21 15:54:30 -070098 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}