blob: 2f8b109219a5111cd9fdf295aabe6da65c46a579 [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;
Evan Millar66388be2009-05-28 15:41:07 -070022import android.provider.ContactsContract.Aggregates;
Jeff Sharkey06bd5a82009-06-15 20:06:48 -070023import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070024import android.provider.ContactsContract.Data;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080025import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.BaseAdapter;
29
30import java.util.ArrayList;
31
32public abstract class ContactEntryAdapter<E extends ContactEntryAdapter.Entry>
33 extends BaseAdapter {
34
Evan Millar66388be2009-05-28 15:41:07 -070035 public static final String[] AGGREGATE_PROJECTION = new String[] {
36 Aggregates.DISPLAY_NAME, // 0
37 Aggregates.STARRED, //1
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070038 Data._ID, //2
39 Data.CONTACT_ID, //3
Jeff Sharkey06bd5a82009-06-15 20:06:48 -070040 Contacts.PACKAGE, //4
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070041 Data.MIMETYPE, //5
42 Data.IS_PRIMARY, //6
43 Data.IS_SUPER_PRIMARY, //7
44 Data.DATA1, //8
45 Data.DATA2, //9
46 Data.DATA3, //10
47 Data.DATA4, //11
48 Data.DATA5, //12
49 Data.DATA6, //13
50 Data.DATA7, //14
51 Data.DATA8, //15
52 Data.DATA9, //16
53 Data.DATA10, //17
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080054 };
Evan Millar66388be2009-05-28 15:41:07 -070055 public static final int AGGREGATE_DISPLAY_NAME_COLUMN = 0;
56 public static final int AGGREGATE_STARRED_COLUMN = 1;
57 public static final int DATA_ID_COLUMN = 2;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070058 public static final int DATA_CONTACT_ID_COLUMN = 3;
59 public static final int DATA_PACKAGE_COLUMN = 4;
60 public static final int DATA_MIMETYPE_COLUMN = 5;
61 public static final int DATA_IS_PRIMARY_COLUMN = 6;
62 public static final int DATA_IS_SUPER_PRIMARY_COLUMN = 7;
63 public static final int DATA_1_COLUMN = 8;
64 public static final int DATA_2_COLUMN = 9;
65 public static final int DATA_3_COLUMN = 10;
66 public static final int DATA_4_COLUMN = 11;
67 public static final int DATA_5_COLUMN = 12;
68 public static final int DATA_6_COLUMN = 13;
69 public static final int DATA_7_COLUMN = 14;
70 public static final int DATA_8_COLUMN = 15;
71 public static final int DATA_9_COLUMN = 16;
72 public static final int DATA_10_COLUMN = 17;
Evan Millar45e0ed32009-06-01 16:44:38 -070073
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080074 protected ArrayList<ArrayList<E>> mSections;
75 protected LayoutInflater mInflater;
76 protected Context mContext;
77 protected boolean mSeparators;
78
79 /**
80 * Base class for adapter entries.
81 */
82 public static class Entry {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080083 public String label;
84 public String data;
85 public Uri uri;
86 public long id = 0;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070087 public long contactId;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088 public int maxLines = 1;
Evan Millar45e0ed32009-06-01 16:44:38 -070089 public String mimetype;
90
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091 /**
92 * Helper for making subclasses parcelable.
93 */
94 protected void writeToParcel(Parcel p) {
95 p.writeString(label);
96 p.writeString(data);
97 p.writeParcelable(uri, 0);
98 p.writeLong(id);
99 p.writeInt(maxLines);
Evan Millar45e0ed32009-06-01 16:44:38 -0700100 p.writeString(mimetype);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800101 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700102
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800103 /**
104 * Helper for making subclasses parcelable.
105 */
106 protected void readFromParcel(Parcel p) {
107 label = p.readString();
108 data = p.readString();
109 uri = p.readParcelable(null);
110 id = p.readLong();
111 maxLines = p.readInt();
Evan Millar45e0ed32009-06-01 16:44:38 -0700112 mimetype = p.readString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113 }
114 }
115
116 ContactEntryAdapter(Context context, ArrayList<ArrayList<E>> sections, boolean separators) {
117 mContext = context;
118 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
119 mSections = sections;
120 mSeparators = separators;
121 }
122
123 /**
124 * Resets the section data.
Evan Millar45e0ed32009-06-01 16:44:38 -0700125 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800126 * @param sections the section data
127 */
128 public final void setSections(ArrayList<ArrayList<E>> sections, boolean separators) {
129 mSections = sections;
130 mSeparators = separators;
131 notifyDataSetChanged();
132 }
133
134 /**
135 * Resets the section data and returns the position of the given entry.
Evan Millar45e0ed32009-06-01 16:44:38 -0700136 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800137 * @param sections the section data
138 * @param entry the entry to return the position for
139 * @return the position of entry, or -1 if it isn't found
140 */
141 public final int setSections(ArrayList<ArrayList<E>> sections, E entry) {
142 mSections = sections;
143 notifyDataSetChanged();
144
145 int numSections = mSections.size();
146 int position = 0;
147 for (int i = 0; i < numSections; i++) {
148 ArrayList<E> section = mSections.get(i);
149 int sectionSize = section.size();
150 for (int j = 0; j < sectionSize; j++) {
151 E e = section.get(j);
152 if (e.equals(entry)) {
153 position += j;
154 return position;
155 }
156 }
157 position += sectionSize;
158 }
159 return -1;
160 }
161
162 /**
163 * @see android.widget.ListAdapter#getCount()
164 */
165 public final int getCount() {
166 return countEntries(mSections, mSeparators);
167 }
168
169 /**
170 * @see android.widget.ListAdapter#hasSeparators()
171 */
172 @Override
173 public final boolean areAllItemsEnabled() {
174 return mSeparators == false;
175 }
176
177 /**
178 * @see android.widget.ListAdapter#isSeparator(int)
179 */
180 @Override
181 public final boolean isEnabled(int position) {
182 if (!mSeparators) {
183 return true;
184 }
185
186 int numSections = mSections.size();
187 for (int i = 0; i < numSections; i++) {
188 ArrayList<E> section = mSections.get(i);
189 int sectionSize = section.size();
190 if (sectionSize == 1) {
191 // The section only contains a separator and nothing else, skip it
192 continue;
193 }
194 if (position == 0) {
195 // The first item in a section is always the separator
196 return false;
197 }
198 position -= sectionSize;
199 }
200 return true;
201 }
202
203 /**
204 * @see android.widget.ListAdapter#getItem(int)
205 */
206 public final Object getItem(int position) {
207 return getEntry(mSections, position, mSeparators);
208 }
209
210 /**
211 * Get the entry for the given position.
Evan Millar45e0ed32009-06-01 16:44:38 -0700212 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800213 * @param sections the list of sections
214 * @param position the position for the desired entry
215 * @return the ContactEntry for the given position
216 */
217 public final static <T extends Entry> T getEntry(ArrayList<ArrayList<T>> sections,
218 int position, boolean separators) {
219 int numSections = sections.size();
220 for (int i = 0; i < numSections; i++) {
221 ArrayList<T> section = sections.get(i);
222 int sectionSize = section.size();
223 if (separators && sectionSize == 1) {
224 // The section only contains a separator and nothing else, skip it
225 continue;
226 }
227 if (position < section.size()) {
228 return section.get(position);
229 }
230 position -= section.size();
231 }
232 return null;
233 }
234
235 /**
236 * Get the count of entries in all sections
Evan Millar45e0ed32009-06-01 16:44:38 -0700237 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800238 * @param sections the list of sections
239 * @return the count of entries in all sections
240 */
241 public static <T extends Entry> int countEntries(ArrayList<ArrayList<T>> sections,
242 boolean separators) {
243 int count = 0;
244 int numSections = sections.size();
245 for (int i = 0; i < numSections; i++) {
246 ArrayList<T> section = sections.get(i);
247 int sectionSize = section.size();
248 if (separators && sectionSize == 1) {
249 // The section only contains a separator and nothing else, skip it
250 continue;
251 }
252 count += sections.get(i).size();
253 }
254 return count;
255 }
256
257 /**
258 * @see android.widget.ListAdapter#getItemId(int)
259 */
260 public final long getItemId(int position) {
261 Entry entry = getEntry(mSections, position, mSeparators);
262 if (entry != null) {
263 return entry.id;
264 } else {
265 return -1;
266 }
267 }
268
269 /**
270 * @see android.widget.ListAdapter#getView(int, View, ViewGroup)
271 */
272 public View getView(int position, View convertView, ViewGroup parent) {
273 View v;
274 if (convertView == null) {
275 v = newView(position, parent);
276 } else {
277 v = convertView;
278 }
279 bindView(v, getEntry(mSections, position, mSeparators));
280 return v;
281 }
282
283 /**
284 * Create a new view for an entry.
Evan Millar45e0ed32009-06-01 16:44:38 -0700285 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800286 * @parent the parent ViewGroup
287 * @return the newly created view
288 */
289 protected abstract View newView(int position, ViewGroup parent);
290
291 /**
292 * Binds the data from an entry to a view.
Evan Millar45e0ed32009-06-01 16:44:38 -0700293 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800294 * @param view the view to display the entry in
295 * @param entry the data to bind
296 */
297 protected abstract void bindView(View view, E entry);
298}