blob: 5307e42d78e1a0cefd502caa2ba043f710750e60 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2007 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
19import android.content.Context;
20import android.net.Uri;
21import android.os.Parcel;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070022import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070023import android.provider.ContactsContract.Data;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080024import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.BaseAdapter;
28
29import java.util.ArrayList;
30
31public abstract class ContactEntryAdapter<E extends ContactEntryAdapter.Entry>
32 extends BaseAdapter {
33
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070034 public static final String[] CONTACT_PROJECTION = new String[] {
35 Contacts.DISPLAY_NAME, // 0
36 Contacts.STARRED, //1
37 Contacts.PHOTO_ID, //2
Evan Millar2c1cc832009-07-13 11:08:06 -070038 Data._ID, //3
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070039 Data.RAW_CONTACT_ID, //4
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -070040 Data.RES_PACKAGE, //5
Evan Millar2c1cc832009-07-13 11:08:06 -070041 Data.MIMETYPE, //6
42 Data.IS_PRIMARY, //7
43 Data.IS_SUPER_PRIMARY, //8
44 Data.DATA1, //9
45 Data.DATA2, //10
46 Data.DATA3, //11
47 Data.DATA4, //12
48 Data.DATA5, //13
49 Data.DATA6, //14
50 Data.DATA7, //15
51 Data.DATA8, //16
52 Data.DATA9, //17
53 Data.DATA10, //18
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080054 };
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070055 public static final int CONTACT_DISPLAY_NAME_COLUMN = 0;
56 public static final int CONTACT_STARRED_COLUMN = 1;
57 public static final int CONTACT_PHOTO_ID = 2;
Evan Millar2c1cc832009-07-13 11:08:06 -070058 public static final int DATA_ID_COLUMN = 3;
59 public static final int DATA_CONTACT_ID_COLUMN = 4;
60 public static final int DATA_PACKAGE_COLUMN = 5;
61 public static final int DATA_MIMETYPE_COLUMN = 6;
62 public static final int DATA_IS_PRIMARY_COLUMN = 7;
63 public static final int DATA_IS_SUPER_PRIMARY_COLUMN = 8;
64 public static final int DATA_1_COLUMN = 9;
65 public static final int DATA_2_COLUMN = 10;
66 public static final int DATA_3_COLUMN = 11;
67 public static final int DATA_4_COLUMN = 12;
68 public static final int DATA_5_COLUMN = 13;
69 public static final int DATA_6_COLUMN = 14;
70 public static final int DATA_7_COLUMN = 15;
71 public static final int DATA_8_COLUMN = 16;
72 public static final int DATA_9_COLUMN = 17;
73 public static final int DATA_10_COLUMN = 18;
Evan Millar45e0ed32009-06-01 16:44:38 -070074
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075 protected ArrayList<ArrayList<E>> mSections;
76 protected LayoutInflater mInflater;
77 protected Context mContext;
78 protected boolean mSeparators;
79
80 /**
81 * Base class for adapter entries.
82 */
83 public static class Entry {
Evan Millar54a5c9f2009-06-23 17:41:09 -070084 public int type = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080085 public String label;
86 public String data;
87 public Uri uri;
88 public long id = 0;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070089 public long contactId;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080090 public int maxLines = 1;
Evan Millar45e0ed32009-06-01 16:44:38 -070091 public String mimetype;
92
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080093 /**
94 * Helper for making subclasses parcelable.
95 */
96 protected void writeToParcel(Parcel p) {
Evan Millar54a5c9f2009-06-23 17:41:09 -070097 p.writeInt(type);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098 p.writeString(label);
99 p.writeString(data);
100 p.writeParcelable(uri, 0);
101 p.writeLong(id);
102 p.writeInt(maxLines);
Evan Millar45e0ed32009-06-01 16:44:38 -0700103 p.writeString(mimetype);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800104 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700105
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106 /**
107 * Helper for making subclasses parcelable.
108 */
109 protected void readFromParcel(Parcel p) {
Evan Millar54a5c9f2009-06-23 17:41:09 -0700110 type = p.readInt();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800111 label = p.readString();
112 data = p.readString();
113 uri = p.readParcelable(null);
114 id = p.readLong();
115 maxLines = p.readInt();
Evan Millar45e0ed32009-06-01 16:44:38 -0700116 mimetype = p.readString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117 }
118 }
119
120 ContactEntryAdapter(Context context, ArrayList<ArrayList<E>> sections, boolean separators) {
121 mContext = context;
122 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
123 mSections = sections;
124 mSeparators = separators;
125 }
126
127 /**
128 * Resets the section data.
Evan Millar45e0ed32009-06-01 16:44:38 -0700129 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800130 * @param sections the section data
131 */
132 public final void setSections(ArrayList<ArrayList<E>> sections, boolean separators) {
133 mSections = sections;
134 mSeparators = separators;
135 notifyDataSetChanged();
136 }
137
138 /**
139 * Resets the section data and returns the position of the given entry.
Evan Millar45e0ed32009-06-01 16:44:38 -0700140 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800141 * @param sections the section data
142 * @param entry the entry to return the position for
143 * @return the position of entry, or -1 if it isn't found
144 */
145 public final int setSections(ArrayList<ArrayList<E>> sections, E entry) {
146 mSections = sections;
147 notifyDataSetChanged();
148
149 int numSections = mSections.size();
150 int position = 0;
151 for (int i = 0; i < numSections; i++) {
152 ArrayList<E> section = mSections.get(i);
153 int sectionSize = section.size();
154 for (int j = 0; j < sectionSize; j++) {
155 E e = section.get(j);
156 if (e.equals(entry)) {
157 position += j;
158 return position;
159 }
160 }
161 position += sectionSize;
162 }
163 return -1;
164 }
165
166 /**
167 * @see android.widget.ListAdapter#getCount()
168 */
169 public final int getCount() {
170 return countEntries(mSections, mSeparators);
171 }
172
173 /**
174 * @see android.widget.ListAdapter#hasSeparators()
175 */
176 @Override
177 public final boolean areAllItemsEnabled() {
178 return mSeparators == false;
179 }
180
181 /**
182 * @see android.widget.ListAdapter#isSeparator(int)
183 */
184 @Override
185 public final boolean isEnabled(int position) {
186 if (!mSeparators) {
187 return true;
188 }
189
190 int numSections = mSections.size();
191 for (int i = 0; i < numSections; i++) {
192 ArrayList<E> section = mSections.get(i);
193 int sectionSize = section.size();
194 if (sectionSize == 1) {
195 // The section only contains a separator and nothing else, skip it
196 continue;
197 }
198 if (position == 0) {
199 // The first item in a section is always the separator
200 return false;
201 }
202 position -= sectionSize;
203 }
204 return true;
205 }
206
207 /**
208 * @see android.widget.ListAdapter#getItem(int)
209 */
210 public final Object getItem(int position) {
211 return getEntry(mSections, position, mSeparators);
212 }
213
214 /**
215 * Get the entry for the given position.
Evan Millar45e0ed32009-06-01 16:44:38 -0700216 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800217 * @param sections the list of sections
218 * @param position the position for the desired entry
219 * @return the ContactEntry for the given position
220 */
221 public final static <T extends Entry> T getEntry(ArrayList<ArrayList<T>> sections,
222 int position, boolean separators) {
223 int numSections = sections.size();
224 for (int i = 0; i < numSections; i++) {
225 ArrayList<T> section = sections.get(i);
226 int sectionSize = section.size();
227 if (separators && sectionSize == 1) {
228 // The section only contains a separator and nothing else, skip it
229 continue;
230 }
231 if (position < section.size()) {
232 return section.get(position);
233 }
234 position -= section.size();
235 }
236 return null;
237 }
238
239 /**
240 * Get the count of entries in all sections
Evan Millar45e0ed32009-06-01 16:44:38 -0700241 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800242 * @param sections the list of sections
243 * @return the count of entries in all sections
244 */
245 public static <T extends Entry> int countEntries(ArrayList<ArrayList<T>> sections,
246 boolean separators) {
247 int count = 0;
248 int numSections = sections.size();
249 for (int i = 0; i < numSections; i++) {
250 ArrayList<T> section = sections.get(i);
251 int sectionSize = section.size();
252 if (separators && sectionSize == 1) {
253 // The section only contains a separator and nothing else, skip it
254 continue;
255 }
256 count += sections.get(i).size();
257 }
258 return count;
259 }
260
261 /**
262 * @see android.widget.ListAdapter#getItemId(int)
263 */
264 public final long getItemId(int position) {
265 Entry entry = getEntry(mSections, position, mSeparators);
266 if (entry != null) {
267 return entry.id;
268 } else {
269 return -1;
270 }
271 }
272
273 /**
274 * @see android.widget.ListAdapter#getView(int, View, ViewGroup)
275 */
276 public View getView(int position, View convertView, ViewGroup parent) {
277 View v;
278 if (convertView == null) {
279 v = newView(position, parent);
280 } else {
281 v = convertView;
282 }
283 bindView(v, getEntry(mSections, position, mSeparators));
284 return v;
285 }
286
287 /**
288 * Create a new view for an entry.
Evan Millar45e0ed32009-06-01 16:44:38 -0700289 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800290 * @parent the parent ViewGroup
291 * @return the newly created view
292 */
293 protected abstract View newView(int position, ViewGroup parent);
294
295 /**
296 * Binds the data from an entry to a view.
Evan Millar45e0ed32009-06-01 16:44:38 -0700297 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800298 * @param view the view to display the entry in
299 * @param entry the data to bind
300 */
301 protected abstract void bindView(View view, E entry);
302}