Sean Midford | ff6f1bb | 2016-10-12 09:48: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.list; |
| 17 | |
| 18 | import android.content.ContentUris; |
| 19 | import android.content.Context; |
| 20 | import android.content.CursorLoader; |
| 21 | import android.database.Cursor; |
| 22 | import android.net.Uri; |
| 23 | import android.net.Uri.Builder; |
| 24 | import android.os.Bundle; |
| 25 | import android.provider.ContactsContract; |
| 26 | import android.text.TextUtils; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | |
| 30 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 31 | |
Sean Midford | a8aa3d8 | 2016-11-17 20:13:34 +0000 | [diff] [blame] | 32 | import com.android.contacts.common.ContactPhotoManager; |
Sean Midford | ff6f1bb | 2016-10-12 09:48:23 -0700 | [diff] [blame] | 33 | import com.android.contacts.common.list.ContactListItemView; |
| 34 | import com.android.contacts.common.list.MultiSelectEntryContactListAdapter; |
| 35 | import com.android.contacts.common.preference.ContactsPreferences; |
| 36 | import com.android.contacts.group.GroupUtil; |
| 37 | |
| 38 | /** Email addresses multi-select cursor adapter. */ |
| 39 | public class MultiSelectEmailAddressesListAdapter extends MultiSelectEntryContactListAdapter { |
| 40 | |
| 41 | protected static class EmailQuery { |
| 42 | public static final String[] PROJECTION_PRIMARY = new String[] { |
| 43 | Email._ID, // 0 |
| 44 | Email.TYPE, // 1 |
| 45 | Email.LABEL, // 2 |
| 46 | Email.ADDRESS, // 3 |
| 47 | Email.CONTACT_ID, // 4 |
| 48 | Email.LOOKUP_KEY, // 5 |
| 49 | Email.PHOTO_ID, // 6 |
| 50 | Email.DISPLAY_NAME_PRIMARY, // 7 |
| 51 | Email.PHOTO_THUMBNAIL_URI, // 8 |
| 52 | }; |
| 53 | |
| 54 | public static final String[] PROJECTION_ALTERNATIVE = new String[] { |
| 55 | Email._ID, // 0 |
| 56 | Email.TYPE, // 1 |
| 57 | Email.LABEL, // 2 |
| 58 | Email.ADDRESS, // 3 |
| 59 | Email.CONTACT_ID, // 4 |
| 60 | Email.LOOKUP_KEY, // 5 |
| 61 | Email.PHOTO_ID, // 6 |
| 62 | Email.DISPLAY_NAME_ALTERNATIVE, // 7 |
| 63 | Email.PHOTO_THUMBNAIL_URI, // 8 |
| 64 | }; |
| 65 | |
| 66 | public static final int EMAIL_ID = 0; |
| 67 | public static final int EMAIL_TYPE = 1; |
| 68 | public static final int EMAIL_LABEL = 2; |
| 69 | public static final int EMAIL_ADDRESS = 3; |
| 70 | public static final int CONTACT_ID = 4; |
| 71 | public static final int LOOKUP_KEY = 5; |
| 72 | public static final int PHOTO_ID = 6; |
| 73 | public static final int DISPLAY_NAME = 7; |
| 74 | public static final int PHOTO_URI = 8; |
| 75 | } |
| 76 | |
| 77 | private final CharSequence mUnknownNameText; |
| 78 | private long[] mContactIdsFilter = null; |
| 79 | |
| 80 | public MultiSelectEmailAddressesListAdapter(Context context) { |
| 81 | super(context, EmailQuery.EMAIL_ID); |
| 82 | |
| 83 | mUnknownNameText = context.getText(android.R.string.unknownName); |
| 84 | } |
| 85 | |
| 86 | public void setArguments(Bundle bundle) { |
Sean Midford | 4b2ccd2 | 2016-10-14 13:03:49 -0700 | [diff] [blame] | 87 | mContactIdsFilter = bundle.getLongArray(UiIntentActions.SELECTION_ITEM_LIST); |
Sean Midford | ff6f1bb | 2016-10-12 09:48:23 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void configureLoader(CursorLoader loader, long directoryId) { |
| 92 | final Builder builder; |
| 93 | if (isSearchMode()) { |
| 94 | builder = Email.CONTENT_FILTER_URI.buildUpon(); |
| 95 | final String query = getQueryString(); |
| 96 | builder.appendPath(TextUtils.isEmpty(query) ? "" : query); |
| 97 | } else { |
| 98 | builder = Email.CONTENT_URI.buildUpon(); |
| 99 | if (isSectionHeaderDisplayEnabled()) { |
| 100 | builder.appendQueryParameter(Email.EXTRA_ADDRESS_BOOK_INDEX, "true"); |
| 101 | } |
| 102 | } |
| 103 | builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 104 | String.valueOf(directoryId)); |
| 105 | loader.setUri(builder.build()); |
| 106 | |
| 107 | if (mContactIdsFilter != null) { |
| 108 | loader.setSelection(ContactsContract.Data.CONTACT_ID |
| 109 | + " IN (" + GroupUtil.convertArrayToString(mContactIdsFilter) + ")"); |
| 110 | } |
| 111 | |
| 112 | if (getContactNameDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY) { |
| 113 | loader.setProjection(EmailQuery.PROJECTION_PRIMARY); |
| 114 | } else { |
| 115 | loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE); |
| 116 | } |
| 117 | |
| 118 | if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) { |
| 119 | loader.setSortOrder(Email.SORT_KEY_PRIMARY); |
| 120 | } else { |
| 121 | loader.setSortOrder(Email.SORT_KEY_ALTERNATIVE); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public String getContactDisplayName(int position) { |
| 127 | return ((Cursor) getItem(position)).getString(EmailQuery.DISPLAY_NAME); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Builds a {@link Data#CONTENT_URI} for the current cursor position. |
| 132 | */ |
| 133 | public Uri getDataUri(int position) { |
| 134 | final long id = ((Cursor) getItem(position)).getLong(EmailQuery.EMAIL_ID); |
| 135 | return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | protected ContactListItemView newView( |
| 140 | Context context, int partition, Cursor cursor, int position, ViewGroup parent) { |
| 141 | final ContactListItemView view = super.newView(context, partition, cursor, position, parent); |
| 142 | view.setUnknownNameText(mUnknownNameText); |
| 143 | view.setQuickContactEnabled(isQuickContactEnabled()); |
| 144 | return view; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | protected void bindView(View itemView, int partition, Cursor cursor, int position) { |
| 149 | super.bindView(itemView, partition, cursor, position); |
| 150 | final ContactListItemView view = (ContactListItemView)itemView; |
| 151 | |
| 152 | cursor.moveToPosition(position); |
| 153 | boolean isFirstEntry = true; |
| 154 | final long currentContactId = cursor.getLong(EmailQuery.CONTACT_ID); |
| 155 | if (cursor.moveToPrevious() && !cursor.isBeforeFirst()) { |
| 156 | final long previousContactId = cursor.getLong(EmailQuery.CONTACT_ID); |
| 157 | if (currentContactId == previousContactId) { |
| 158 | isFirstEntry = false; |
| 159 | } |
| 160 | } |
| 161 | cursor.moveToPosition(position); |
| 162 | |
| 163 | bindViewId(view, cursor, EmailQuery.EMAIL_ID); |
Sean Midford | ff6f1bb | 2016-10-12 09:48:23 -0700 | [diff] [blame] | 164 | if (isFirstEntry) { |
| 165 | bindName(view, cursor); |
Sean Midford | a8aa3d8 | 2016-11-17 20:13:34 +0000 | [diff] [blame] | 166 | bindPhoto(view, cursor, EmailQuery.PHOTO_ID, EmailQuery.LOOKUP_KEY, |
| 167 | EmailQuery.DISPLAY_NAME); |
Sean Midford | ff6f1bb | 2016-10-12 09:48:23 -0700 | [diff] [blame] | 168 | } else { |
| 169 | unbindName(view); |
| 170 | view.removePhotoView(true, false); |
| 171 | } |
| 172 | bindEmailAddress(view, cursor); |
| 173 | } |
| 174 | |
| 175 | protected void unbindName(final ContactListItemView view) { |
| 176 | view.hideDisplayName(); |
| 177 | } |
| 178 | |
| 179 | protected void bindEmailAddress(ContactListItemView view, Cursor cursor) { |
| 180 | CharSequence label = null; |
| 181 | if (!cursor.isNull(EmailQuery.EMAIL_TYPE)) { |
| 182 | final int type = cursor.getInt(EmailQuery.EMAIL_TYPE); |
| 183 | final String customLabel = cursor.getString(EmailQuery.EMAIL_LABEL); |
| 184 | |
| 185 | // TODO cache |
| 186 | label = Email.getTypeLabel(getContext().getResources(), type, customLabel); |
| 187 | } |
| 188 | view.setLabel(label); |
| 189 | view.showData(cursor, EmailQuery.EMAIL_ADDRESS); |
| 190 | } |
| 191 | |
Sean Midford | ff6f1bb | 2016-10-12 09:48:23 -0700 | [diff] [blame] | 192 | protected void bindName(final ContactListItemView view, Cursor cursor) { |
| 193 | view.showDisplayName(cursor, EmailQuery.DISPLAY_NAME, getContactNameDisplayOrder()); |
| 194 | } |
| 195 | } |