alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.adapters; |
| 2 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 3 | import java.util.ArrayList; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 4 | import java.util.Collections; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.Locale; |
| 7 | import java.util.Set; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 8 | import java.util.concurrent.ExecutorService; |
| 9 | import java.util.concurrent.Executors; |
| 10 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 11 | import android.content.Context; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 12 | import android.util.Log; |
| 13 | import android.util.SparseArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 14 | import android.view.LayoutInflater; |
| 15 | import android.view.View; |
| 16 | import android.view.ViewGroup; |
| 17 | import android.widget.BaseAdapter; |
| 18 | import android.widget.ImageView; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 19 | import android.widget.SectionIndexer; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 20 | import android.widget.TextView; |
| 21 | |
| 22 | import com.savoirfairelinux.sflphone.R; |
| 23 | import com.savoirfairelinux.sflphone.model.CallContact; |
| 24 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 25 | public class ContactsAdapter extends BaseAdapter implements SectionIndexer { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 26 | |
| 27 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 28 | Context mContext; |
| 29 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 30 | HashMap<String, Integer> alphaIndexer; |
| 31 | String[] sections; |
| 32 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 33 | private static final String TAG = ContactsAdapter.class.getSimpleName(); |
| 34 | |
| 35 | public ContactsAdapter(Context context) { |
| 36 | super(); |
| 37 | mContext = context; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 38 | alphaIndexer = new HashMap<String, Integer>(); |
| 39 | headers = new HeadersHolder(new ArrayList<CallContact>()); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 40 | } |
| 41 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 42 | HeadersHolder headers; |
| 43 | private static final int TYPE_HEADER = 0; |
| 44 | private static final int TYPE_TRACK = 1; |
| 45 | |
| 46 | @Override |
| 47 | public View getView(int position, View convertView, ViewGroup parent) { |
| 48 | int type = getItemViewType(position); |
| 49 | |
| 50 | // Log.i(TAG, "positon" + position + " type " + type); |
| 51 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 52 | |
| 53 | switch (type) { |
| 54 | case TYPE_TRACK: |
| 55 | return getViewTrack(position, inflater, convertView); |
| 56 | case TYPE_HEADER: |
| 57 | return getViewHeader(position, inflater, convertView); |
| 58 | } |
| 59 | return null; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 60 | } |
| 61 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 62 | private View getViewHeader(int position, LayoutInflater inflater, View convertView) { |
| 63 | if (convertView == null) { |
| 64 | convertView = inflater.inflate(R.layout.header, null); |
| 65 | } |
| 66 | TextView name = (TextView) convertView.findViewById(R.id.header_letter); |
| 67 | name.setText(headers.getSection(position)); |
| 68 | return convertView; |
| 69 | } |
| 70 | |
| 71 | private View getViewTrack(int position, LayoutInflater inflater, View convertView) { |
| 72 | |
| 73 | if (convertView == null) { |
| 74 | convertView = inflater.inflate(R.layout.item_contact, null); |
| 75 | } |
| 76 | |
| 77 | CallContact item = headers.getCallContact(position); |
| 78 | |
| 79 | ((TextView) convertView.findViewById(R.id.display_name)).setText(item.getmDisplayName()); |
| 80 | ImageView photo_view = (ImageView) convertView.findViewById(R.id.photo); |
| 81 | |
| 82 | infos_fetcher.execute(new ContactPictureLoader(mContext, photo_view, item.getId())); |
| 83 | |
| 84 | return convertView; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int getCount() { |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 89 | return headers.size() + headers.getSections().size(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int getItemViewType(int pos) { |
| 94 | return (headers.contains(pos) ? TYPE_HEADER : TYPE_TRACK); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public int getViewTypeCount() { |
| 99 | return 2; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | @Override |
| 105 | public long getItemId(int position) { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | public void removeAll() { |
| 110 | headers.clear(); |
| 111 | notifyDataSetChanged(); |
| 112 | } |
| 113 | |
| 114 | public void add(CallContact tr) { |
| 115 | headers.add(tr); |
| 116 | headers.buildIndex(); |
| 117 | } |
| 118 | |
| 119 | public void addAll(ArrayList<CallContact> tr) { |
| 120 | headers = new HeadersHolder(tr); |
| 121 | notifyDataSetChanged(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | @Override |
| 126 | public int getPositionForSection(int section) { |
| 127 | return headers.getPositionFor(section); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public int getSectionForPosition(int position) { |
| 132 | return headers.getSectionFor(position); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Object[] getSections() { |
| 137 | return headers.getSectionsArray(); |
| 138 | } |
| 139 | |
| 140 | public int getRealPosition(int pos) { |
| 141 | return headers.getTrackPosition(pos); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CallContact getItem(int index) { |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 146 | return headers.getCallContact(index); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 147 | } |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 148 | |
| 149 | public class HeadersHolder { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 150 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 151 | public static final String TAG = "HeadersHolder"; |
| 152 | HashMap<String, Integer> alphaIndexer; |
| 153 | ArrayList<CallContact> contacts; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 154 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 155 | String[] sectionsArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 156 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 157 | public String[] getSectionsArray() { |
| 158 | return sectionsArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 159 | } |
| 160 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 161 | int headersCount; |
| 162 | private SparseArray<Item> items = new SparseArray<Item>(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 163 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 164 | public SparseArray<Item> getItems() { |
| 165 | return items; |
| 166 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 167 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 168 | public void setItems(SparseArray<Item> items) { |
| 169 | this.items = items; |
| 170 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 171 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 172 | public SparseArray<Section> getSections() { |
| 173 | return sections; |
| 174 | } |
| 175 | |
| 176 | public void setSections(SparseArray<Section> sections) { |
| 177 | this.sections = sections; |
| 178 | } |
| 179 | |
| 180 | private SparseArray<Section> sections = new SparseArray<Section>(); |
| 181 | |
| 182 | public HeadersHolder(ArrayList<CallContact> a) { |
| 183 | alphaIndexer = new HashMap<String, Integer>(); |
| 184 | contacts = a; |
| 185 | headersCount = 0; |
| 186 | buildIndex(); |
| 187 | } |
| 188 | |
| 189 | // public int getHeadersCount() { |
| 190 | // return headersCount; |
| 191 | // } |
| 192 | |
| 193 | private class Item { |
| 194 | public Item(int rpos, int headersCount2, CallContact track) { |
| 195 | // Log.i(TAG, "Creating Item"); |
| 196 | |
| 197 | sectionNumber = headersCount2; |
| 198 | realPos = rpos; |
| 199 | tr = track; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | public int realPos; |
| 204 | public int sectionNumber; |
| 205 | public CallContact tr; |
| 206 | } |
| 207 | |
| 208 | private class Section { |
| 209 | public int startPosition; |
| 210 | public int number; |
| 211 | public String header; |
| 212 | |
| 213 | public Section(int i, int headersCount, String str) { |
| 214 | // Log.i(TAG, "Creating section"); |
| 215 | |
| 216 | startPosition = i + headersCount; |
| 217 | number = headersCount; |
| 218 | header = str; |
| 219 | |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public String toString() { |
| 224 | return header; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | public void buildIndex() { |
| 229 | |
| 230 | for (int x = 0; x < contacts.size(); x++) { |
| 231 | String s = contacts.get(x).getmDisplayName(); |
| 232 | String ch = s.substring(0, 1); |
| 233 | ch = ch.toUpperCase(Locale.CANADA); |
| 234 | if (!alphaIndexer.containsKey(ch)) { |
| 235 | sections.put(x + headersCount, new Section(x, headersCount, ch)); |
| 236 | headersCount++; |
| 237 | } |
| 238 | Integer result = alphaIndexer.put(ch, x + headersCount); |
| 239 | items.put(x + headersCount, new Item(x, headersCount, contacts.get(x))); |
| 240 | if (result == null) { |
| 241 | |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | Set<String> sect = alphaIndexer.keySet(); |
| 246 | |
| 247 | // create a list from the set to sort |
| 248 | ArrayList<String> sectionList = new ArrayList<String>(sect); |
| 249 | Collections.sort(sectionList); |
| 250 | sectionsArray = new String[sectionList.size()]; |
| 251 | sectionList.toArray(sectionsArray); |
| 252 | |
| 253 | } |
| 254 | |
| 255 | public HashMap<String, Integer> getAlphaIndexer() { |
| 256 | return alphaIndexer; |
| 257 | } |
| 258 | |
| 259 | public int getPositionFor(int section) { |
| 260 | if (section == sectionsArray.length) |
| 261 | return sectionsArray.length; |
| 262 | return alphaIndexer.get(sectionsArray[section]); |
| 263 | } |
| 264 | |
| 265 | public int getSectionFor(int position) { |
| 266 | return (null != items.get(position)) ? items.get(position).sectionNumber : sections.get(position).number; |
| 267 | } |
| 268 | |
| 269 | public boolean contains(int pos) { |
| 270 | if (sections.get(pos) != null) { |
| 271 | return true; |
| 272 | } |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | public CallContact getCallContact(int position) { |
| 277 | |
| 278 | if (items.get(position) == null) |
| 279 | return null; |
| 280 | |
| 281 | return items.get(position).tr; |
| 282 | |
| 283 | } |
| 284 | |
| 285 | public int size() { |
| 286 | return contacts.size(); |
| 287 | } |
| 288 | |
| 289 | public void clear() { |
| 290 | contacts.clear(); |
| 291 | |
| 292 | } |
| 293 | |
| 294 | public void add(CallContact tr) { |
| 295 | contacts.add(tr); |
| 296 | |
| 297 | } |
| 298 | |
| 299 | public void addAll(ArrayList<CallContact> tr) { |
| 300 | contacts.clear(); |
| 301 | contacts.addAll(tr); |
| 302 | |
| 303 | } |
| 304 | |
| 305 | public ArrayList<CallContact> getTracks() { |
| 306 | return contacts; |
| 307 | } |
| 308 | |
| 309 | public int getTrackPosition(int pos) { |
| 310 | if (sections.get(pos) != null) { |
| 311 | return items.get(pos + 1).realPos; |
| 312 | } |
| 313 | return items.get(pos).realPos; |
| 314 | } |
| 315 | |
| 316 | public CharSequence getSection(int position) { |
| 317 | return sections.get(position).header; |
| 318 | } |
| 319 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | } |