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