blob: 2281ec6ffada21fd8759ee0c04fbacbf3910ebc2 [file] [log] [blame]
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -07001/*
2 * Copyright (C) 2006 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
17package com.android.contacts;
18
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070019import android.content.Context;
20import android.database.Cursor;
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -070021import android.graphics.drawable.Drawable;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070022import android.net.Uri;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070023import android.provider.ContactsContract.CommonDataKinds.Email;
24import android.provider.ContactsContract.CommonDataKinds.Nickname;
25import android.provider.ContactsContract.CommonDataKinds.Phone;
26import android.provider.ContactsContract.CommonDataKinds.StructuredName;
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -070027import android.provider.ContactsContract.Contacts.Data;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070028import android.provider.ContactsContract.RawContacts;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070029import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.AdapterView;
33import android.widget.ArrayAdapter;
34import android.widget.ImageView;
35import android.widget.ListView;
36import android.widget.TextView;
37
Chiao Cheng0d5588d2012-11-26 15:34:14 -080038import com.android.contacts.common.model.AccountTypeManager;
Chiao Cheng428f0082012-11-13 18:38:56 -080039import com.android.contacts.common.model.account.AccountType;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070040
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070041import java.util.ArrayList;
42import java.util.Collections;
43import java.util.HashMap;
44import java.util.List;
45
46/**
47 * A list view for constituent contacts of an aggregate. Shows the contact name, source icon
48 * and additional data such as a nickname, email address or phone number, whichever
49 * is available.
50 */
51public class SplitAggregateView extends ListView {
52
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -070053 private static final String TAG = "SplitAggregateView";
54
Dmitri Plotnikovb902a462009-09-21 10:25:54 -070055 private interface SplitQuery {
56 String[] COLUMNS = new String[] {
Dave Santoro2b3f3c52011-07-26 17:35:42 -070057 Data.MIMETYPE, RawContacts.ACCOUNT_TYPE, RawContacts.DATA_SET, Data.RAW_CONTACT_ID,
58 Data.IS_PRIMARY, StructuredName.DISPLAY_NAME, Nickname.NAME, Email.DATA,
59 Phone.NUMBER
Dmitri Plotnikovb902a462009-09-21 10:25:54 -070060 };
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070061
Dmitri Plotnikovb902a462009-09-21 10:25:54 -070062 int MIMETYPE = 0;
63 int ACCOUNT_TYPE = 1;
Dave Santoro2b3f3c52011-07-26 17:35:42 -070064 int DATA_SET = 2;
65 int RAW_CONTACT_ID = 3;
66 int IS_PRIMARY = 4;
67 int DISPLAY_NAME = 5;
68 int NICKNAME = 6;
69 int EMAIL = 7;
70 int PHONE = 8;
Dmitri Plotnikovb902a462009-09-21 10:25:54 -070071 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070072
73 private final Uri mAggregateUri;
74 private OnContactSelectedListener mListener;
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -080075 private AccountTypeManager mAccountTypes;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070076
77 /**
78 * Listener interface that gets the contact ID of the user-selected contact.
79 */
80 public interface OnContactSelectedListener {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -070081 void onContactSelected(long rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070082 }
83
84 /**
85 * Constructor.
86 */
87 public SplitAggregateView(Context context, Uri aggregateUri) {
88 super(context);
89
90 mAggregateUri = aggregateUri;
91
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -080092 mAccountTypes = AccountTypeManager.getInstance(context);
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -070093
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -070094 final List<RawContactInfo> list = loadData();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070095
96 setAdapter(new SplitAggregateAdapter(context, list));
97 setOnItemClickListener(new OnItemClickListener() {
98
99 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700100 mListener.onContactSelected(list.get(position).rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700101 }
102 });
103 }
104
105 /**
106 * Sets a contact selection listener.
107 */
108 public void setOnContactSelectedListener(OnContactSelectedListener listener) {
109 mListener = listener;
110 }
111
112 /**
113 * Contact information loaded from the content provider.
114 */
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700115 private static class RawContactInfo implements Comparable<RawContactInfo> {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700116 final long rawContactId;
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -0700117 String accountType;
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700118 String dataSet;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700119 String name;
120 String phone;
121 String email;
122 String nickname;
123
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700124 public RawContactInfo(long rawContactId) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700125 this.rawContactId = rawContactId;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700126 }
127
128 public String getAdditionalData() {
129 if (nickname != null) {
130 return nickname;
131 }
132
133 if (email != null) {
134 return email;
135 }
136
137 if (phone != null) {
138 return phone;
139 }
140
141 return "";
142 }
143
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700144 public int compareTo(RawContactInfo another) {
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -0700145 String thisAccount = accountType != null ? accountType : "";
146 String thatAccount = another.accountType != null ? another.accountType : "";
147 return thisAccount.compareTo(thatAccount);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700148 }
149 }
150
151 /**
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700152 * Loads data from the content provider, organizes it into {@link RawContactInfo} objects
153 * and returns a sorted list of {@link RawContactInfo}'s.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700154 */
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700155 private List<RawContactInfo> loadData() {
156 HashMap<Long, RawContactInfo> rawContactInfos = new HashMap<Long, RawContactInfo>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700157 Uri dataUri = Uri.withAppendedPath(mAggregateUri, Data.CONTENT_DIRECTORY);
158 Cursor cursor = getContext().getContentResolver().query(dataUri,
Dmitri Plotnikovb902a462009-09-21 10:25:54 -0700159 SplitQuery.COLUMNS, null, null, null);
Jay Shrauner13c42f42014-01-27 17:06:27 -0800160 if (cursor == null) {
161 return Collections.emptyList();
162 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700163 try {
164 while (cursor.moveToNext()) {
Dmitri Plotnikovb902a462009-09-21 10:25:54 -0700165 long rawContactId = cursor.getLong(SplitQuery.RAW_CONTACT_ID);
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700166 RawContactInfo info = rawContactInfos.get(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700167 if (info == null) {
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700168 info = new RawContactInfo(rawContactId);
169 rawContactInfos.put(rawContactId, info);
Dmitri Plotnikovb902a462009-09-21 10:25:54 -0700170 info.accountType = cursor.getString(SplitQuery.ACCOUNT_TYPE);
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700171 info.dataSet = cursor.getString(SplitQuery.DATA_SET);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700172 }
173
Dmitri Plotnikovb902a462009-09-21 10:25:54 -0700174 String mimetype = cursor.getString(SplitQuery.MIMETYPE);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700175 if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) {
176 loadStructuredName(cursor, info);
177 } else if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
178 loadPhoneNumber(cursor, info);
179 } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
180 loadEmail(cursor, info);
181 } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
182 loadNickname(cursor, info);
183 }
184 }
185 } finally {
186 cursor.close();
187 }
188
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700189 List<RawContactInfo> list = new ArrayList<RawContactInfo>(rawContactInfos.values());
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700190 Collections.sort(list);
191 return list;
192 }
193
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700194 private void loadStructuredName(Cursor cursor, RawContactInfo info) {
Dmitri Plotnikovb902a462009-09-21 10:25:54 -0700195 info.name = cursor.getString(SplitQuery.DISPLAY_NAME);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700196 }
197
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700198 private void loadNickname(Cursor cursor, RawContactInfo info) {
Dmitri Plotnikovb902a462009-09-21 10:25:54 -0700199 if (info.nickname == null || cursor.getInt(SplitQuery.IS_PRIMARY) != 0) {
200 info.nickname = cursor.getString(SplitQuery.NICKNAME);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700201 }
202 }
203
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700204 private void loadEmail(Cursor cursor, RawContactInfo info) {
Dmitri Plotnikovb902a462009-09-21 10:25:54 -0700205 if (info.email == null || cursor.getInt(SplitQuery.IS_PRIMARY) != 0) {
206 info.email = cursor.getString(SplitQuery.EMAIL);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700207 }
208 }
209
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700210 private void loadPhoneNumber(Cursor cursor, RawContactInfo info) {
Dmitri Plotnikovb902a462009-09-21 10:25:54 -0700211 if (info.phone == null || cursor.getInt(SplitQuery.IS_PRIMARY) != 0) {
212 info.phone = cursor.getString(SplitQuery.PHONE);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700213 }
214 }
215
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -0700216 private static class SplitAggregateItemCache {
217 TextView name;
218 TextView additionalData;
219 ImageView sourceIcon;
220 }
221
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700222 /**
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700223 * List adapter for the list of {@link RawContactInfo} objects.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700224 */
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700225 private class SplitAggregateAdapter extends ArrayAdapter<RawContactInfo> {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700226
227 private LayoutInflater mInflater;
228
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700229 public SplitAggregateAdapter(Context context, List<RawContactInfo> sources) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700230 super(context, 0, sources);
231 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
232 }
233
234 @Override
235 public View getView(int position, View convertView, ViewGroup parent) {
236 if (convertView == null) {
237 convertView = mInflater.inflate(R.layout.split_aggregate_list_item, parent, false);
238 }
239
240 SplitAggregateItemCache cache = (SplitAggregateItemCache)convertView.getTag();
241 if (cache == null) {
242 cache = new SplitAggregateItemCache();
243 cache.name = (TextView)convertView.findViewById(R.id.name);
244 cache.additionalData = (TextView)convertView.findViewById(R.id.additionalData);
245 cache.sourceIcon = (ImageView)convertView.findViewById(R.id.sourceIcon);
246 convertView.setTag(cache);
247 }
248
Dmitri Plotnikovac86cde2009-09-17 18:55:08 -0700249 final RawContactInfo info = getItem(position);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700250 cache.name.setText(info.name);
251 cache.additionalData.setText(info.getAdditionalData());
252
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -0700253 Drawable icon = null;
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700254 AccountType accountType = mAccountTypes.getAccountType(info.accountType, info.dataSet);
Dmitri Plotnikov69f9e6f2011-01-03 15:47:24 -0800255 if (accountType != null) {
256 icon = accountType.getDisplayIcon(getContext());
Dmitri Plotnikov6698cf22009-08-19 18:03:00 -0700257 }
258 if (icon != null) {
259 cache.sourceIcon.setImageDrawable(icon);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700260 } else {
261 cache.sourceIcon.setImageResource(R.drawable.unknown_source);
262 }
263 return convertView;
264 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700265 }
266}