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