blob: 9f111653b12890bd44cc1651f68e5ffa38485726 [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
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080034 protected ArrayList<ArrayList<E>> mSections;
35 protected LayoutInflater mInflater;
36 protected Context mContext;
37 protected boolean mSeparators;
38
39 /**
40 * Base class for adapter entries.
41 */
42 public static class Entry {
Evan Millar54a5c9f2009-06-23 17:41:09 -070043 public int type = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080044 public String label;
45 public String data;
46 public Uri uri;
47 public long id = 0;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070048 public long contactId;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049 public int maxLines = 1;
Evan Millar45e0ed32009-06-01 16:44:38 -070050 public String mimetype;
51
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052 /**
53 * Helper for making subclasses parcelable.
54 */
55 protected void writeToParcel(Parcel p) {
Evan Millar54a5c9f2009-06-23 17:41:09 -070056 p.writeInt(type);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057 p.writeString(label);
58 p.writeString(data);
59 p.writeParcelable(uri, 0);
60 p.writeLong(id);
61 p.writeInt(maxLines);
Evan Millar45e0ed32009-06-01 16:44:38 -070062 p.writeString(mimetype);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080063 }
Evan Millar45e0ed32009-06-01 16:44:38 -070064
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080065 /**
66 * Helper for making subclasses parcelable.
67 */
68 protected void readFromParcel(Parcel p) {
Evan Millar54a5c9f2009-06-23 17:41:09 -070069 type = p.readInt();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070 label = p.readString();
71 data = p.readString();
72 uri = p.readParcelable(null);
73 id = p.readLong();
74 maxLines = p.readInt();
Evan Millar45e0ed32009-06-01 16:44:38 -070075 mimetype = p.readString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080076 }
77 }
78
79 ContactEntryAdapter(Context context, ArrayList<ArrayList<E>> sections, boolean separators) {
80 mContext = context;
81 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
82 mSections = sections;
83 mSeparators = separators;
84 }
85
86 /**
87 * Resets the section data.
Evan Millar45e0ed32009-06-01 16:44:38 -070088 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080089 * @param sections the section data
90 */
91 public final void setSections(ArrayList<ArrayList<E>> sections, boolean separators) {
92 mSections = sections;
93 mSeparators = separators;
94 notifyDataSetChanged();
95 }
96
97 /**
98 * Resets the section data and returns the position of the given entry.
Evan Millar45e0ed32009-06-01 16:44:38 -070099 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100 * @param sections the section data
101 * @param entry the entry to return the position for
102 * @return the position of entry, or -1 if it isn't found
103 */
104 public final int setSections(ArrayList<ArrayList<E>> sections, E entry) {
105 mSections = sections;
106 notifyDataSetChanged();
107
108 int numSections = mSections.size();
109 int position = 0;
110 for (int i = 0; i < numSections; i++) {
111 ArrayList<E> section = mSections.get(i);
112 int sectionSize = section.size();
113 for (int j = 0; j < sectionSize; j++) {
114 E e = section.get(j);
115 if (e.equals(entry)) {
116 position += j;
117 return position;
118 }
119 }
120 position += sectionSize;
121 }
122 return -1;
123 }
124
125 /**
126 * @see android.widget.ListAdapter#getCount()
127 */
128 public final int getCount() {
129 return countEntries(mSections, mSeparators);
130 }
131
132 /**
133 * @see android.widget.ListAdapter#hasSeparators()
134 */
135 @Override
136 public final boolean areAllItemsEnabled() {
137 return mSeparators == false;
138 }
139
140 /**
141 * @see android.widget.ListAdapter#isSeparator(int)
142 */
143 @Override
144 public final boolean isEnabled(int position) {
145 if (!mSeparators) {
146 return true;
147 }
148
149 int numSections = mSections.size();
150 for (int i = 0; i < numSections; i++) {
151 ArrayList<E> section = mSections.get(i);
152 int sectionSize = section.size();
153 if (sectionSize == 1) {
154 // The section only contains a separator and nothing else, skip it
155 continue;
156 }
157 if (position == 0) {
158 // The first item in a section is always the separator
159 return false;
160 }
161 position -= sectionSize;
162 }
163 return true;
164 }
165
166 /**
167 * @see android.widget.ListAdapter#getItem(int)
168 */
169 public final Object getItem(int position) {
170 return getEntry(mSections, position, mSeparators);
171 }
172
173 /**
174 * Get the entry for the given position.
Evan Millar45e0ed32009-06-01 16:44:38 -0700175 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800176 * @param sections the list of sections
177 * @param position the position for the desired entry
178 * @return the ContactEntry for the given position
179 */
180 public final static <T extends Entry> T getEntry(ArrayList<ArrayList<T>> sections,
181 int position, boolean separators) {
182 int numSections = sections.size();
183 for (int i = 0; i < numSections; i++) {
184 ArrayList<T> section = sections.get(i);
185 int sectionSize = section.size();
186 if (separators && sectionSize == 1) {
187 // The section only contains a separator and nothing else, skip it
188 continue;
189 }
190 if (position < section.size()) {
191 return section.get(position);
192 }
193 position -= section.size();
194 }
195 return null;
196 }
197
198 /**
199 * Get the count of entries in all sections
Evan Millar45e0ed32009-06-01 16:44:38 -0700200 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800201 * @param sections the list of sections
202 * @return the count of entries in all sections
203 */
204 public static <T extends Entry> int countEntries(ArrayList<ArrayList<T>> sections,
205 boolean separators) {
206 int count = 0;
207 int numSections = sections.size();
208 for (int i = 0; i < numSections; i++) {
209 ArrayList<T> section = sections.get(i);
210 int sectionSize = section.size();
211 if (separators && sectionSize == 1) {
212 // The section only contains a separator and nothing else, skip it
213 continue;
214 }
215 count += sections.get(i).size();
216 }
217 return count;
218 }
219
220 /**
221 * @see android.widget.ListAdapter#getItemId(int)
222 */
223 public final long getItemId(int position) {
224 Entry entry = getEntry(mSections, position, mSeparators);
225 if (entry != null) {
226 return entry.id;
227 } else {
228 return -1;
229 }
230 }
231
232 /**
233 * @see android.widget.ListAdapter#getView(int, View, ViewGroup)
234 */
235 public View getView(int position, View convertView, ViewGroup parent) {
236 View v;
237 if (convertView == null) {
238 v = newView(position, parent);
239 } else {
240 v = convertView;
241 }
242 bindView(v, getEntry(mSections, position, mSeparators));
243 return v;
244 }
245
246 /**
247 * Create a new view for an entry.
Evan Millar45e0ed32009-06-01 16:44:38 -0700248 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800249 * @parent the parent ViewGroup
250 * @return the newly created view
251 */
252 protected abstract View newView(int position, ViewGroup parent);
253
254 /**
255 * Binds the data from an entry to a view.
Evan Millar45e0ed32009-06-01 16:44:38 -0700256 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800257 * @param view the view to display the entry in
258 * @param entry the data to bind
259 */
260 protected abstract void bindView(View view, E entry);
261}