blob: 6bf6d5e5ce931d8b7d8eb20035a799c5da27e7c9 [file] [log] [blame]
Sean Midfordff6f1bb2016-10-12 09:48: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.list;
17
18import android.content.ContentUris;
19import android.content.Context;
20import android.content.CursorLoader;
21import android.database.Cursor;
22import android.net.Uri;
23import android.net.Uri.Builder;
24import android.os.Bundle;
25import android.provider.ContactsContract;
26import android.text.TextUtils;
27import android.view.View;
28import android.view.ViewGroup;
29
30import android.provider.ContactsContract.CommonDataKinds.Phone;
31
Sean Midforda8aa3d82016-11-17 20:13:34 +000032import com.android.contacts.common.ContactPhotoManager;
Sean Midfordff6f1bb2016-10-12 09:48:23 -070033import com.android.contacts.common.list.ContactListItemView;
34import com.android.contacts.common.list.MultiSelectEntryContactListAdapter;
35import com.android.contacts.common.preference.ContactsPreferences;
36import com.android.contacts.group.GroupUtil;
37
38/** Phone Numbers multi-select cursor adapter. */
39public class MultiSelectPhoneNumbersListAdapter extends MultiSelectEntryContactListAdapter {
40
41 public static class PhoneQuery {
42 public static final String[] PROJECTION_PRIMARY = new String[] {
43 Phone._ID, // 0
44 Phone.TYPE, // 1
45 Phone.LABEL, // 2
46 Phone.NUMBER, // 3
47 Phone.CONTACT_ID, // 4
48 Phone.LOOKUP_KEY, // 5
49 Phone.PHOTO_ID, // 6
50 Phone.DISPLAY_NAME_PRIMARY, // 7
51 Phone.PHOTO_THUMBNAIL_URI, // 8
52 };
53
54 public static final String[] PROJECTION_ALTERNATIVE = new String[] {
55 Phone._ID, // 0
56 Phone.TYPE, // 1
57 Phone.LABEL, // 2
58 Phone.NUMBER, // 3
59 Phone.CONTACT_ID, // 4
60 Phone.LOOKUP_KEY, // 5
61 Phone.PHOTO_ID, // 6
62 Phone.DISPLAY_NAME_ALTERNATIVE, // 7
63 Phone.PHOTO_THUMBNAIL_URI, // 8
64 };
65
66 public static final int PHONE_ID = 0;
67 public static final int PHONE_TYPE = 1;
68 public static final int PHONE_LABEL = 2;
69 public static final int PHONE_NUMBER = 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 MultiSelectPhoneNumbersListAdapter(Context context) {
81 super(context, PhoneQuery.PHONE_ID);
82
83 mUnknownNameText = context.getText(android.R.string.unknownName);
84 }
85
86 public void setArguments(Bundle bundle) {
Sean Midford4b2ccd22016-10-14 13:03:49 -070087 mContactIdsFilter = bundle.getLongArray(UiIntentActions.SELECTION_ITEM_LIST);
Sean Midfordff6f1bb2016-10-12 09:48:23 -070088 }
89
90 @Override
91 public void configureLoader(CursorLoader loader, long directoryId) {
92 final Builder builder;
93 if (isSearchMode()) {
94 builder = Phone.CONTENT_FILTER_URI.buildUpon();
95 final String query = getQueryString();
96 builder.appendPath(TextUtils.isEmpty(query) ? "" : query);
97 } else {
98 builder = Phone.CONTENT_URI.buildUpon();
99 if (isSectionHeaderDisplayEnabled()) {
100 builder.appendQueryParameter(Phone.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(PhoneQuery.PROJECTION_PRIMARY);
114 } else {
115 loader.setProjection(PhoneQuery.PROJECTION_ALTERNATIVE);
116 }
117
118 if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
119 loader.setSortOrder(Phone.SORT_KEY_PRIMARY);
120 } else {
121 loader.setSortOrder(Phone.SORT_KEY_ALTERNATIVE);
122 }
123 }
124
125 @Override
126 public String getContactDisplayName(int position) {
127 return ((Cursor) getItem(position)).getString(PhoneQuery.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(PhoneQuery.PHONE_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(PhoneQuery.CONTACT_ID);
155 if (cursor.moveToPrevious() && !cursor.isBeforeFirst()) {
156 final long previousContactId = cursor.getLong(PhoneQuery.CONTACT_ID);
157 if (currentContactId == previousContactId) {
158 isFirstEntry = false;
159 }
160 }
161 cursor.moveToPosition(position);
162
163 bindViewId(view, cursor, PhoneQuery.PHONE_ID);
Sean Midfordff6f1bb2016-10-12 09:48:23 -0700164 if (isFirstEntry) {
165 bindName(view, cursor);
Sean Midforda8aa3d82016-11-17 20:13:34 +0000166 bindPhoto(view, cursor, PhoneQuery.PHOTO_ID, PhoneQuery.LOOKUP_KEY,
167 PhoneQuery.DISPLAY_NAME);
Sean Midfordff6f1bb2016-10-12 09:48:23 -0700168 } else {
169 unbindName(view);
170 view.removePhotoView(true, false);
171 }
172 bindPhoneNumber(view, cursor);
173 }
174
175 protected void unbindName(final ContactListItemView view) {
176 view.hideDisplayName();
177 }
178
179 protected void bindPhoneNumber(ContactListItemView view, Cursor cursor) {
180 CharSequence label = null;
181 if (!cursor.isNull(PhoneQuery.PHONE_TYPE)) {
182 final int type = cursor.getInt(PhoneQuery.PHONE_TYPE);
183 final String customLabel = cursor.getString(PhoneQuery.PHONE_LABEL);
184
185 // TODO cache
186 label = Phone.getTypeLabel(getContext().getResources(), type, customLabel);
187 }
188 view.setLabel(label);
189 view.showData(cursor, PhoneQuery.PHONE_NUMBER);
190 }
191
Sean Midfordff6f1bb2016-10-12 09:48:23 -0700192 protected void bindName(final ContactListItemView view, Cursor cursor) {
193 view.showDisplayName(cursor, PhoneQuery.DISPLAY_NAME, getContactNameDisplayOrder());
194 }
195}