The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Parcel; |
Dmitri Plotnikov | e1cd679 | 2009-07-27 20:28:17 -0700 | [diff] [blame] | 22 | import android.provider.ContactsContract.Contacts; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 23 | import android.provider.ContactsContract.Data; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.BaseAdapter; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | |
| 31 | public abstract class ContactEntryAdapter<E extends ContactEntryAdapter.Entry> |
| 32 | extends BaseAdapter { |
| 33 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 34 | 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 Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 43 | public int type = -1; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 44 | public String label; |
| 45 | public String data; |
| 46 | public Uri uri; |
| 47 | public long id = 0; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 48 | public long contactId; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 49 | public int maxLines = 1; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 50 | public String mimetype; |
| 51 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Helper for making subclasses parcelable. |
| 54 | */ |
| 55 | protected void writeToParcel(Parcel p) { |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 56 | p.writeInt(type); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 57 | p.writeString(label); |
| 58 | p.writeString(data); |
| 59 | p.writeParcelable(uri, 0); |
| 60 | p.writeLong(id); |
| 61 | p.writeInt(maxLines); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 62 | p.writeString(mimetype); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 63 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 64 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Helper for making subclasses parcelable. |
| 67 | */ |
| 68 | protected void readFromParcel(Parcel p) { |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 69 | type = p.readInt(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 70 | label = p.readString(); |
| 71 | data = p.readString(); |
| 72 | uri = p.readParcelable(null); |
| 73 | id = p.readLong(); |
| 74 | maxLines = p.readInt(); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 75 | mimetype = p.readString(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 76 | } |
| 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 Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 88 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 89 | * @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 Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 99 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 100 | * @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 Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 175 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 176 | * @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 Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 200 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 201 | * @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 Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 248 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 249 | * @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 Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 256 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 257 | * @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 | } |