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