blob: c501984a21989998316ca580d35a7c2f9b719cda [file] [log] [blame]
Walter Jang4715c042016-04-06 17:14:23 -07001/*
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 */
16package com.android.contacts.group;
17
18import android.content.Context;
19import android.content.CursorLoader;
20import android.database.Cursor;
21import android.net.Uri;
22import android.provider.ContactsContract;
23import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
24import android.provider.ContactsContract.Contacts;
25import android.provider.ContactsContract.Data;
26import android.provider.ContactsContract.Directory;
27import android.view.View;
28import android.view.ViewGroup;
29
30import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Walter Jang16c29592016-06-14 15:34:39 -070031import com.android.contacts.common.R;
Walter Jang4715c042016-04-06 17:14:23 -070032import com.android.contacts.common.list.ContactListItemView;
Walter Jange9ea4f02016-05-10 09:39:46 -070033import com.android.contacts.common.list.MultiSelectEntryContactListAdapter;
Walter Jang4715c042016-04-06 17:14:23 -070034import com.android.contacts.common.preference.ContactsPreferences;
35
36/** Group members cursor adapter. */
Walter Jang90f2fe52016-06-17 16:38:43 -070037public class GroupMembersAdapter extends MultiSelectEntryContactListAdapter {
Walter Jang4715c042016-04-06 17:14:23 -070038
Walter Jangf9779ef2016-05-25 12:23:05 -070039 public static class GroupMembersQuery {
Walter Jang4715c042016-04-06 17:14:23 -070040
41 private static final String[] PROJECTION_PRIMARY = new String[] {
42 Data.CONTACT_ID,
Walter Jang8b8b30c2016-05-12 19:07:13 -070043 Data.RAW_CONTACT_ID,
Walter Jang4715c042016-04-06 17:14:23 -070044 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 Jang8b8b30c2016-05-12 19:07:13 -070053 Data.RAW_CONTACT_ID,
Walter Jang4715c042016-04-06 17:14:23 -070054 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 Jang8b8b30c2016-05-12 19:07:13 -070062 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 Jang4715c042016-04-06 17:14:23 -070068 }
69
70 private final CharSequence mUnknownNameText;
71 private long mGroupId;
Walter Jang05e71472016-06-23 19:00:10 -070072 private boolean mDisplayDeleteButtons;
Walter Jang4715c042016-04-06 17:14:23 -070073
Walter Jang90f2fe52016-06-17 16:38:43 -070074 public GroupMembersAdapter(Context context) {
Walter Jang9434cd12016-06-22 18:22:49 -070075 super(context, GroupMembersQuery.CONTACT_ID);
John Shao2b0d2982016-07-26 14:50:05 -070076
Walter Jang16c29592016-06-14 15:34:39 -070077 mUnknownNameText = context.getText(R.string.missing_name);
Walter Jang4715c042016-04-06 17:14:23 -070078 }
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 Shao2b0d2982016-07-26 14:50:05 -070086 public Uri getContactUri(int position) {
Walter Jang4715c042016-04-06 17:14:23 -070087 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 Jang05e71472016-06-23 19:00:10 -070093 /** 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 Jang4715c042016-04-06 17:14:23 -0700108 @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 Janged454bb2016-04-11 13:23:25 -0700113 .appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
Walter Jang4715c042016-04-06 17:14:23 -0700114 .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 Janged454bb2016-04-11 13:23:25 -0700130 ? Contacts.SORT_KEY_PRIMARY : Contacts.SORT_KEY_ALTERNATIVE);
Walter Jang4715c042016-04-06 17:14:23 -0700131 }
132
133 @Override
134 public String getContactDisplayName(int position) {
135 return ((Cursor) getItem(position)).getString(GroupMembersQuery.CONTACT_DISPLAY_NAME);
136 }
137
Walter Jang4715c042016-04-06 17:14:23 -0700138 @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 Jang4715c042016-04-06 17:14:23 -0700144 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 Jangd89f89c2016-06-17 10:57:31 -0700151 bindSectionHeaderAndDivider(view, position);
Walter Jang72f99882016-05-26 09:01:31 -0700152 bindName(view, cursor);
Sean Midforda8aa3d82016-11-17 20:13:34 +0000153 bindPhoto(view, cursor, GroupMembersQuery.CONTACT_PHOTO_ID,
154 GroupMembersQuery.CONTACT_LOOKUP_KEY, GroupMembersQuery.CONTACT_DISPLAY_NAME);
Tingting Wang4e2656f2016-06-28 22:58:43 -0700155 bindDeleteButton(view, position);
Walter Jang4715c042016-04-06 17:14:23 -0700156 }
157
Walter Jangd89f89c2016-06-17 10:57:31 -0700158 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 Jang4715c042016-04-06 17:14:23 -0700168 private void bindName(ContactListItemView view, Cursor cursor) {
169 view.showDisplayName(cursor, GroupMembersQuery.CONTACT_DISPLAY_NAME,
170 getContactNameDisplayOrder());
171 }
172
Tingting Wang4e2656f2016-06-28 22:58:43 -0700173 private void bindDeleteButton(final ContactListItemView view, int position) {
Walter Jang05e71472016-06-23 19:00:10 -0700174 if (mDisplayDeleteButtons) {
Tingting Wang4e2656f2016-06-28 22:58:43 -0700175 view.getDeleteImageButton(getDeleteContactListener(), position);
Walter Jang05e71472016-06-23 19:00:10 -0700176 } else {
177 view.hideDeleteImageButton();
178 }
179 }
Walter Jang4715c042016-04-06 17:14:23 -0700180}