Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.group; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.CursorLoader; |
| 20 | import android.database.Cursor; |
| 21 | import android.net.Uri; |
| 22 | import android.provider.ContactsContract; |
| 23 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
| 24 | import android.provider.ContactsContract.Contacts; |
| 25 | import android.provider.ContactsContract.Data; |
| 26 | import android.provider.ContactsContract.Directory; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | |
| 30 | import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest; |
Walter Jang | 16c2959 | 2016-06-14 15:34:39 -0700 | [diff] [blame] | 31 | import com.android.contacts.common.R; |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 32 | import com.android.contacts.common.list.ContactListItemView; |
Walter Jang | e9ea4f0 | 2016-05-10 09:39:46 -0700 | [diff] [blame] | 33 | import com.android.contacts.common.list.MultiSelectEntryContactListAdapter; |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 34 | import com.android.contacts.common.preference.ContactsPreferences; |
| 35 | |
| 36 | /** Group members cursor adapter. */ |
Walter Jang | 90f2fe5 | 2016-06-17 16:38:43 -0700 | [diff] [blame] | 37 | public class GroupMembersAdapter extends MultiSelectEntryContactListAdapter { |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 38 | |
Walter Jang | f9779ef | 2016-05-25 12:23:05 -0700 | [diff] [blame] | 39 | public static class GroupMembersQuery { |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 40 | |
| 41 | private static final String[] PROJECTION_PRIMARY = new String[] { |
| 42 | Data.CONTACT_ID, |
Walter Jang | 8b8b30c | 2016-05-12 19:07:13 -0700 | [diff] [blame] | 43 | Data.RAW_CONTACT_ID, |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 44 | Data.PHOTO_ID, |
| 45 | Data.LOOKUP_KEY, |
| 46 | Data.CONTACT_PRESENCE, |
| 47 | Data.CONTACT_STATUS, |
| 48 | Data.DISPLAY_NAME_PRIMARY, |
| 49 | }; |
| 50 | |
| 51 | private static final String[] PROJECTION_ALTERNATIVE = new String[] { |
| 52 | Data.CONTACT_ID, |
Walter Jang | 8b8b30c | 2016-05-12 19:07:13 -0700 | [diff] [blame] | 53 | Data.RAW_CONTACT_ID, |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 54 | Data.PHOTO_ID, |
| 55 | Data.LOOKUP_KEY, |
| 56 | Data.CONTACT_PRESENCE, |
| 57 | Data.CONTACT_STATUS, |
| 58 | Data.DISPLAY_NAME_ALTERNATIVE, |
| 59 | }; |
| 60 | |
| 61 | public static final int CONTACT_ID = 0; |
Walter Jang | 8b8b30c | 2016-05-12 19:07:13 -0700 | [diff] [blame] | 62 | public static final int RAW_CONTACT_ID = 1; |
| 63 | public static final int CONTACT_PHOTO_ID = 2; |
| 64 | public static final int CONTACT_LOOKUP_KEY = 3; |
| 65 | public static final int CONTACT_PRESENCE = 4; |
| 66 | public static final int CONTACT_STATUS = 5; |
| 67 | public static final int CONTACT_DISPLAY_NAME = 6; |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | private final CharSequence mUnknownNameText; |
| 71 | private long mGroupId; |
Walter Jang | 05e7147 | 2016-06-23 19:00:10 -0700 | [diff] [blame] | 72 | private boolean mDisplayDeleteButtons; |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 73 | |
Walter Jang | 90f2fe5 | 2016-06-17 16:38:43 -0700 | [diff] [blame] | 74 | public GroupMembersAdapter(Context context) { |
Walter Jang | 9434cd1 | 2016-06-22 18:22:49 -0700 | [diff] [blame] | 75 | super(context, GroupMembersQuery.CONTACT_ID); |
John Shao | 2b0d298 | 2016-07-26 14:50:05 -0700 | [diff] [blame] | 76 | |
Walter Jang | 16c2959 | 2016-06-14 15:34:39 -0700 | [diff] [blame] | 77 | mUnknownNameText = context.getText(R.string.missing_name); |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** Sets the ID of the group whose members will be displayed. */ |
| 81 | public void setGroupId(long groupId) { |
| 82 | mGroupId = groupId; |
| 83 | } |
| 84 | |
| 85 | /** Returns the lookup Uri for the contact at the given position in the underlying cursor. */ |
John Shao | 2b0d298 | 2016-07-26 14:50:05 -0700 | [diff] [blame] | 86 | public Uri getContactUri(int position) { |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 87 | final Cursor cursor = (Cursor) getItem(position); |
| 88 | final long contactId = cursor.getLong(GroupMembersQuery.CONTACT_ID); |
| 89 | final String lookupKey = cursor.getString(GroupMembersQuery.CONTACT_LOOKUP_KEY); |
| 90 | return Contacts.getLookupUri(contactId, lookupKey); |
| 91 | } |
| 92 | |
Walter Jang | 05e7147 | 2016-06-23 19:00:10 -0700 | [diff] [blame] | 93 | /** Returns the ID of the contact at the given position in the underlying cursor. */ |
| 94 | public long getContactId(int position) { |
| 95 | final Cursor cursor = (Cursor) getItem(position); |
| 96 | return cursor.getLong(GroupMembersQuery.CONTACT_ID); |
| 97 | } |
| 98 | |
| 99 | public void setDisplayDeleteButtons(boolean displayDeleteButtons) { |
| 100 | mDisplayDeleteButtons = displayDeleteButtons; |
| 101 | notifyDataSetChanged(); |
| 102 | } |
| 103 | |
| 104 | public boolean getDisplayDeleteButtons() { |
| 105 | return mDisplayDeleteButtons; |
| 106 | } |
| 107 | |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 108 | @Override |
| 109 | public void configureLoader(CursorLoader loader, long directoryId) { |
| 110 | loader.setUri(Data.CONTENT_URI.buildUpon() |
| 111 | .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 112 | String.valueOf(Directory.DEFAULT)) |
Walter Jang | ed454bb | 2016-04-11 13:23:25 -0700 | [diff] [blame] | 113 | .appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true") |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 114 | .build()); |
| 115 | |
| 116 | loader.setSelection(Data.MIMETYPE + "=?" + " AND " + GroupMembership.GROUP_ROW_ID + "=?"); |
| 117 | |
| 118 | final String[] selectionArgs = new String[2]; |
| 119 | selectionArgs[0] = GroupMembership.CONTENT_ITEM_TYPE; |
| 120 | selectionArgs[1] = String.valueOf(mGroupId); |
| 121 | loader.setSelectionArgs(selectionArgs); |
| 122 | |
| 123 | loader.setProjection( |
| 124 | getContactNameDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY |
| 125 | ? GroupMembersQuery.PROJECTION_PRIMARY |
| 126 | : GroupMembersQuery.PROJECTION_ALTERNATIVE); |
| 127 | |
| 128 | loader.setSortOrder( |
| 129 | getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY |
Walter Jang | ed454bb | 2016-04-11 13:23:25 -0700 | [diff] [blame] | 130 | ? Contacts.SORT_KEY_PRIMARY : Contacts.SORT_KEY_ALTERNATIVE); |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String getContactDisplayName(int position) { |
| 135 | return ((Cursor) getItem(position)).getString(GroupMembersQuery.CONTACT_DISPLAY_NAME); |
| 136 | } |
| 137 | |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 138 | @Override |
| 139 | protected ContactListItemView newView(Context context, int partition, Cursor cursor, |
| 140 | int position, ViewGroup parent) { |
| 141 | final ContactListItemView view = |
| 142 | super.newView(context, partition, cursor, position, parent); |
| 143 | view.setUnknownNameText(mUnknownNameText); |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 144 | return view; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | protected void bindView(View v, int partition, Cursor cursor, int position) { |
| 149 | super.bindView(v, partition, cursor, position); |
| 150 | final ContactListItemView view = (ContactListItemView) v; |
Walter Jang | d89f89c | 2016-06-17 10:57:31 -0700 | [diff] [blame] | 151 | bindSectionHeaderAndDivider(view, position); |
Walter Jang | 72f9988 | 2016-05-26 09:01:31 -0700 | [diff] [blame] | 152 | bindName(view, cursor); |
Sean Midford | a8aa3d8 | 2016-11-17 20:13:34 +0000 | [diff] [blame] | 153 | bindPhoto(view, cursor, GroupMembersQuery.CONTACT_PHOTO_ID, |
| 154 | GroupMembersQuery.CONTACT_LOOKUP_KEY, GroupMembersQuery.CONTACT_DISPLAY_NAME); |
Tingting Wang | 4e2656f | 2016-06-28 22:58:43 -0700 | [diff] [blame] | 155 | bindDeleteButton(view, position); |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Walter Jang | d89f89c | 2016-06-17 10:57:31 -0700 | [diff] [blame] | 158 | protected void bindSectionHeaderAndDivider(ContactListItemView view, int position) { |
| 159 | view.setIsSectionHeaderEnabled(isSectionHeaderDisplayEnabled()); |
| 160 | if (isSectionHeaderDisplayEnabled()) { |
| 161 | final Placement placement = getItemPlacementInSection(position); |
| 162 | view.setSectionHeader(placement.sectionHeader); |
| 163 | } else { |
| 164 | view.setSectionHeader(null); |
| 165 | } |
| 166 | } |
| 167 | |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 168 | private void bindName(ContactListItemView view, Cursor cursor) { |
| 169 | view.showDisplayName(cursor, GroupMembersQuery.CONTACT_DISPLAY_NAME, |
| 170 | getContactNameDisplayOrder()); |
| 171 | } |
| 172 | |
Tingting Wang | 4e2656f | 2016-06-28 22:58:43 -0700 | [diff] [blame] | 173 | private void bindDeleteButton(final ContactListItemView view, int position) { |
Walter Jang | 05e7147 | 2016-06-23 19:00:10 -0700 | [diff] [blame] | 174 | if (mDisplayDeleteButtons) { |
Tingting Wang | 4e2656f | 2016-06-28 22:58:43 -0700 | [diff] [blame] | 175 | view.getDeleteImageButton(getDeleteContactListener(), position); |
Walter Jang | 05e7147 | 2016-06-23 19:00:10 -0700 | [diff] [blame] | 176 | } else { |
| 177 | view.hideDeleteImageButton(); |
| 178 | } |
| 179 | } |
Walter Jang | 4715c04 | 2016-04-06 17:14:23 -0700 | [diff] [blame] | 180 | } |