Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame^] | 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 | */ |
| 16 | package com.android.contacts; |
| 17 | |
| 18 | import com.android.contacts.list.ContactListAdapter; |
| 19 | |
| 20 | import android.content.Context; |
| 21 | import android.content.CursorLoader; |
| 22 | import android.net.Uri; |
| 23 | import android.provider.ContactsContract; |
| 24 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
| 25 | import android.provider.ContactsContract.Contacts; |
| 26 | import android.provider.ContactsContract.Data; |
| 27 | import android.provider.ContactsContract.Directory; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * Group Member loader. Loads all group members from the given groupId |
| 34 | */ |
| 35 | public 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 |
| 43 | Data.DISPLAY_NAME_PRIMARY, // 1 |
| 44 | Data.DISPLAY_NAME_ALTERNATIVE, // 2 |
| 45 | Data.SORT_KEY_PRIMARY, // 3 |
| 46 | Data.STARRED, // 4 |
| 47 | Data.CONTACT_PRESENCE, // 5 |
| 48 | Data.CONTACT_CHAT_CAPABILITY, // 6 |
| 49 | Data.PHOTO_ID, // 7 |
| 50 | Data.PHOTO_THUMBNAIL_URI, // 8 |
| 51 | Data.LOOKUP_KEY, // 9 |
| 52 | Data.PHONETIC_NAME, // 10 |
| 53 | Data.HAS_PHONE_NUMBER, // 11 |
| 54 | }; |
| 55 | |
| 56 | private final long mGroupId; |
| 57 | |
| 58 | public static final int CONTACT_ID_COLUMN_INDEX = 0; |
| 59 | public static final int CONTACT_DISPLAY_NAME_PRIMARY_COLUMN_INDEX = 1; |
| 60 | public static final int CONTACT_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX = 2; |
| 61 | public static final int CONTACT_SORT_KEY_PRIMARY_COLUMN_INDEX = 3; |
| 62 | public static final int CONTACT_STARRED_COLUMN_INDEX = 4; |
| 63 | public static final int CONTACT_PRESENCE_STATUS_COLUMN_INDEX = 5; |
| 64 | public static final int CONTACT_CHAT_CAPABILITY_COLUMN_INDEX = 6; |
| 65 | public static final int CONTACT_PHOTO_ID_COLUMN_INDEX = 7; |
| 66 | public static final int CONTACT_PHOTO_URI_COLUMN_INDEX = 8; |
| 67 | public static final int CONTACT_LOOKUP_KEY_COLUMN_INDEX = 9; |
| 68 | public static final int CONTACT_PHONETIC_NAME_COLUMN_INDEX = 10; |
| 69 | public static final int CONTACT_HAS_PHONE_COLUMN_INDEX = 11; |
| 70 | |
| 71 | public GroupMemberLoader(Context context, long groupId) { |
| 72 | super(context); |
| 73 | mGroupId = groupId; |
| 74 | setUri(createUri()); |
| 75 | setProjection(PROJECTION_DATA); |
| 76 | setSelection(createSelection()); |
| 77 | setSelectionArgs(createSelectionArgs()); |
| 78 | setSortOrder(Contacts.SORT_KEY_ALTERNATIVE); |
| 79 | } |
| 80 | |
| 81 | private Uri createUri() { |
| 82 | Uri uri = Data.CONTENT_URI; |
| 83 | uri = uri.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 84 | String.valueOf(Directory.DEFAULT)).build(); |
| 85 | // TODO: Bring back dataRestriction |
| 86 | // uri = applyDataRestriction(uri); |
| 87 | return uri; |
| 88 | } |
| 89 | |
| 90 | private String createSelection() { |
| 91 | StringBuilder selection = new StringBuilder(); |
| 92 | selection.append(Data.MIMETYPE + "=?" + " AND " + GroupMembership.GROUP_ROW_ID + "=?"); |
| 93 | return selection.toString(); |
| 94 | } |
| 95 | |
| 96 | private String[] createSelectionArgs() { |
| 97 | List<String> selectionArgs = new ArrayList<String>(); |
| 98 | selectionArgs.add(GroupMembership.CONTENT_ITEM_TYPE); |
| 99 | selectionArgs.add(String.valueOf(mGroupId)); |
| 100 | return selectionArgs.toArray(new String[0]); |
| 101 | } |
| 102 | } |