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 | |
Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame] | 18 | import android.content.Context; |
| 19 | import android.content.CursorLoader; |
| 20 | import android.net.Uri; |
| 21 | import android.provider.ContactsContract; |
| 22 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
| 23 | import android.provider.ContactsContract.Contacts; |
| 24 | import android.provider.ContactsContract.Data; |
| 25 | import android.provider.ContactsContract.Directory; |
| 26 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 27 | import com.android.contacts.preference.ContactsPreferences; |
| 28 | |
Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame] | 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 | |
Daisuke Miyakawa | a939372 | 2011-10-31 13:51:38 -0700 | [diff] [blame] | 37 | public static class GroupEditorQuery { |
| 38 | private static final String[] PROJECTION = new String[] { |
| 39 | Data.CONTACT_ID, // 0 |
| 40 | Data.RAW_CONTACT_ID, // 1 |
| 41 | Data.DISPLAY_NAME_PRIMARY, // 2 |
| 42 | Data.PHOTO_URI, // 3 |
| 43 | Data.LOOKUP_KEY, // 4 |
| 44 | }; |
| 45 | |
| 46 | public static final int CONTACT_ID = 0; |
| 47 | public static final int RAW_CONTACT_ID = 1; |
| 48 | public static final int CONTACT_DISPLAY_NAME_PRIMARY = 2; |
| 49 | public static final int CONTACT_PHOTO_URI = 3; |
| 50 | public static final int CONTACT_LOOKUP_KEY = 4; |
| 51 | } |
| 52 | |
| 53 | public static class GroupDetailQuery { |
| 54 | private static final String[] PROJECTION = new String[] { |
Katherine Kuan | 8102959 | 2011-11-14 11:21:09 -0800 | [diff] [blame] | 55 | Data.CONTACT_ID, // 0 |
Daisuke Miyakawa | a939372 | 2011-10-31 13:51:38 -0700 | [diff] [blame] | 56 | Data.PHOTO_URI, // 1 |
| 57 | Data.LOOKUP_KEY, // 2 |
| 58 | Data.DISPLAY_NAME_PRIMARY, // 3 |
| 59 | Data.CONTACT_PRESENCE, // 4 |
| 60 | Data.CONTACT_STATUS, // 5 |
| 61 | }; |
| 62 | |
Katherine Kuan | 8102959 | 2011-11-14 11:21:09 -0800 | [diff] [blame] | 63 | public static final int CONTACT_ID = 0; |
Daisuke Miyakawa | a939372 | 2011-10-31 13:51:38 -0700 | [diff] [blame] | 64 | public static final int CONTACT_PHOTO_URI = 1; |
| 65 | public static final int CONTACT_LOOKUP_KEY = 2; |
| 66 | public static final int CONTACT_DISPLAY_NAME_PRIMARY = 3; |
| 67 | public static final int CONTACT_PRESENCE_STATUS = 4; |
| 68 | public static final int CONTACT_STATUS = 5; |
| 69 | } |
Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame] | 70 | |
| 71 | private final long mGroupId; |
| 72 | |
Daisuke Miyakawa | a939372 | 2011-10-31 13:51:38 -0700 | [diff] [blame] | 73 | /** |
| 74 | * @return GroupMemberLoader object which can be used in group editor. |
| 75 | */ |
| 76 | public static GroupMemberLoader constructLoaderForGroupEditorQuery( |
| 77 | Context context, long groupId) { |
| 78 | return new GroupMemberLoader(context, groupId, GroupEditorQuery.PROJECTION); |
| 79 | } |
Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame] | 80 | |
Daisuke Miyakawa | a939372 | 2011-10-31 13:51:38 -0700 | [diff] [blame] | 81 | /** |
| 82 | * @return GroupMemberLoader object used in group detail page. |
| 83 | */ |
| 84 | public static GroupMemberLoader constructLoaderForGroupDetailQuery( |
| 85 | Context context, long groupId) { |
| 86 | return new GroupMemberLoader(context, groupId, GroupDetailQuery.PROJECTION); |
| 87 | } |
| 88 | |
| 89 | private GroupMemberLoader(Context context, long groupId, String[] projection) { |
Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame] | 90 | super(context); |
| 91 | mGroupId = groupId; |
| 92 | setUri(createUri()); |
Daisuke Miyakawa | a939372 | 2011-10-31 13:51:38 -0700 | [diff] [blame] | 93 | setProjection(projection); |
Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame] | 94 | setSelection(createSelection()); |
| 95 | setSelectionArgs(createSelectionArgs()); |
Isaac Katzenelson | 4b9b26e | 2011-09-23 15:01:00 -0700 | [diff] [blame] | 96 | |
| 97 | ContactsPreferences prefs = new ContactsPreferences(context); |
| 98 | if (prefs.getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) { |
| 99 | setSortOrder(Contacts.SORT_KEY_PRIMARY); |
| 100 | } else { |
| 101 | setSortOrder(Contacts.SORT_KEY_ALTERNATIVE); |
| 102 | } |
Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private Uri createUri() { |
| 106 | Uri uri = Data.CONTENT_URI; |
| 107 | uri = uri.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 108 | String.valueOf(Directory.DEFAULT)).build(); |
Frank Sposaro | 5f34b2e | 2011-06-21 15:54:30 -0700 | [diff] [blame] | 109 | return uri; |
| 110 | } |
| 111 | |
| 112 | private String createSelection() { |
| 113 | StringBuilder selection = new StringBuilder(); |
| 114 | selection.append(Data.MIMETYPE + "=?" + " AND " + GroupMembership.GROUP_ROW_ID + "=?"); |
| 115 | return selection.toString(); |
| 116 | } |
| 117 | |
| 118 | private String[] createSelectionArgs() { |
| 119 | List<String> selectionArgs = new ArrayList<String>(); |
| 120 | selectionArgs.add(GroupMembership.CONTENT_ITEM_TYPE); |
| 121 | selectionArgs.add(String.valueOf(mGroupId)); |
| 122 | return selectionArgs.toArray(new String[0]); |
| 123 | } |
| 124 | } |