Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.adapters; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 2 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 3 | import java.lang.ref.WeakReference; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 4 | import java.util.ArrayList; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 5 | import java.util.Collections; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Locale; |
| 8 | import java.util.Set; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 9 | import java.util.concurrent.ExecutorService; |
| 10 | import java.util.concurrent.Executors; |
| 11 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 12 | import org.sflphone.R; |
| 13 | import org.sflphone.fragments.ContactListFragment; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 14 | import org.sflphone.model.CallContact; |
| 15 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 16 | import android.content.Context; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 17 | import android.util.SparseArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 18 | import android.view.LayoutInflater; |
| 19 | import android.view.View; |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 20 | import android.view.View.OnClickListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 21 | import android.view.ViewGroup; |
| 22 | import android.widget.BaseAdapter; |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 23 | import android.widget.ImageButton; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 24 | import android.widget.ImageView; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 25 | import android.widget.SectionIndexer; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 26 | import android.widget.TextView; |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 27 | import android.widget.Toast; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 28 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 29 | public class ContactsAdapter extends BaseAdapter implements SectionIndexer { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 30 | |
| 31 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 32 | Context mContext; |
| 33 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 34 | HashMap<String, Integer> alphaIndexer; |
| 35 | String[] sections; |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 36 | WeakReference<ContactListFragment> parent; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 37 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 38 | // private static final String TAG = ContactsAdapter.class.getSimpleName(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 39 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 40 | public ContactsAdapter(ContactListFragment contactListFragment) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 41 | super(); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 42 | mContext = contactListFragment.getActivity(); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 43 | alphaIndexer = new HashMap<String, Integer>(); |
| 44 | headers = new HeadersHolder(new ArrayList<CallContact>()); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 45 | parent = new WeakReference<ContactListFragment>(contactListFragment); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 46 | } |
| 47 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 48 | HeadersHolder headers; |
| 49 | private static final int TYPE_HEADER = 0; |
| 50 | private static final int TYPE_TRACK = 1; |
| 51 | |
| 52 | @Override |
| 53 | public View getView(int position, View convertView, ViewGroup parent) { |
| 54 | int type = getItemViewType(position); |
| 55 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 56 | // Log.i(TAG, "positon" + position + " type " + type); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 57 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 58 | |
| 59 | switch (type) { |
| 60 | case TYPE_TRACK: |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 61 | return getViewContact(position, inflater, convertView); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 62 | case TYPE_HEADER: |
| 63 | return getViewHeader(position, inflater, convertView); |
| 64 | } |
| 65 | return null; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 66 | } |
| 67 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 68 | private View getViewHeader(int position, LayoutInflater inflater, View convertView) { |
| 69 | if (convertView == null) { |
| 70 | convertView = inflater.inflate(R.layout.header, null); |
| 71 | } |
| 72 | TextView name = (TextView) convertView.findViewById(R.id.header_letter); |
| 73 | name.setText(headers.getSection(position)); |
| 74 | return convertView; |
| 75 | } |
| 76 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 77 | private View getViewContact(int position, LayoutInflater inflater, View convertView) { |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 78 | ContactView entryView; |
| 79 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 80 | if (convertView == null) { |
| 81 | convertView = inflater.inflate(R.layout.item_contact, null); |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 82 | |
| 83 | entryView = new ContactView(); |
| 84 | entryView.quick_starred = (ImageButton) convertView.findViewById(R.id.quick_starred); |
| 85 | entryView.quick_edit = (ImageButton) convertView.findViewById(R.id.quick_edit); |
| 86 | entryView.quick_discard = (ImageButton) convertView.findViewById(R.id.quick_discard); |
| 87 | entryView.quick_call = (ImageButton) convertView.findViewById(R.id.quick_call); |
| 88 | entryView.quick_msg = (ImageButton) convertView.findViewById(R.id.quick_message); |
| 89 | entryView.photo = (ImageView) convertView.findViewById(R.id.photo); |
| 90 | entryView.display_name = (TextView) convertView.findViewById(R.id.display_name); |
| 91 | convertView.setTag(entryView); |
| 92 | } else { |
| 93 | entryView = (ContactView) convertView.getTag(); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 94 | } |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 95 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 96 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 97 | final CallContact item = headers.getCallContact(position); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 98 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 99 | entryView.display_name.setText(item.getmDisplayName()); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 100 | |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 101 | if(item.hasPhoto()){ |
| 102 | entryView.photo.setImageBitmap(item.getPhoto()); |
| 103 | } else { |
| 104 | infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, item)); |
| 105 | } |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 106 | |
| 107 | entryView.quick_call.setOnClickListener(new OnClickListener() { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 108 | |
| 109 | @Override |
| 110 | public void onClick(View v) { |
| 111 | parent.get().mCallbacks.onCallContact(item); |
| 112 | |
| 113 | } |
| 114 | }); |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 115 | |
Alexandre Lision | e168d4a | 2013-10-24 12:32:59 -0400 | [diff] [blame] | 116 | entryView.quick_msg.setOnClickListener(new OnClickListener() { |
| 117 | |
| 118 | @Override |
| 119 | public void onClick(View v) { |
| 120 | parent.get().mCallbacks.onTextContact(item); |
| 121 | } |
| 122 | }); |
| 123 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 124 | entryView.quick_starred.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 125 | |
| 126 | @Override |
| 127 | public void onClick(View v) { |
| 128 | Toast.makeText(mContext, "Coming soon", Toast.LENGTH_SHORT).show(); |
| 129 | } |
| 130 | }); |
| 131 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 132 | entryView.quick_edit.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 133 | |
| 134 | @Override |
| 135 | public void onClick(View v) { |
| 136 | parent.get().mCallbacks.onEditContact(item); |
| 137 | |
| 138 | } |
| 139 | }); |
| 140 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 141 | entryView.quick_discard.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 142 | |
| 143 | @Override |
| 144 | public void onClick(View v) { |
| 145 | Toast.makeText(mContext, "Coming soon", Toast.LENGTH_SHORT).show(); |
| 146 | |
| 147 | } |
| 148 | }); |
| 149 | |
Alexandre Lision | e168d4a | 2013-10-24 12:32:59 -0400 | [diff] [blame] | 150 | entryView.quick_edit.setClickable(false); |
| 151 | entryView.quick_discard.setClickable(false); |
| 152 | entryView.quick_starred.setClickable(false); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 153 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 154 | return convertView; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 155 | } |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 156 | |
| 157 | /********************* |
| 158 | * ViewHolder Pattern |
| 159 | *********************/ |
| 160 | public class ContactView { |
| 161 | ImageButton quick_starred, quick_edit, quick_discard, quick_call, quick_msg; |
| 162 | ImageView photo; |
| 163 | TextView display_name; |
| 164 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 165 | |
| 166 | @Override |
| 167 | public int getCount() { |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 168 | return headers.size() + headers.getSections().size(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public int getItemViewType(int pos) { |
| 173 | return (headers.contains(pos) ? TYPE_HEADER : TYPE_TRACK); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int getViewTypeCount() { |
| 178 | return 2; |
| 179 | } |
| 180 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 181 | @Override |
| 182 | public long getItemId(int position) { |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | public void removeAll() { |
| 187 | headers.clear(); |
| 188 | notifyDataSetChanged(); |
| 189 | } |
| 190 | |
| 191 | public void add(CallContact tr) { |
| 192 | headers.add(tr); |
| 193 | headers.buildIndex(); |
| 194 | } |
| 195 | |
| 196 | public void addAll(ArrayList<CallContact> tr) { |
| 197 | headers = new HeadersHolder(tr); |
| 198 | notifyDataSetChanged(); |
| 199 | } |
| 200 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 201 | @Override |
| 202 | public int getPositionForSection(int section) { |
| 203 | return headers.getPositionFor(section); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public int getSectionForPosition(int position) { |
| 208 | return headers.getSectionFor(position); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Object[] getSections() { |
| 213 | return headers.getSectionsArray(); |
| 214 | } |
| 215 | |
| 216 | public int getRealPosition(int pos) { |
| 217 | return headers.getTrackPosition(pos); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public CallContact getItem(int index) { |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 222 | return headers.getCallContact(index); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 223 | } |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 224 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 225 | public class HeadersHolder { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 226 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 227 | public static final String TAG = "HeadersHolder"; |
| 228 | HashMap<String, Integer> alphaIndexer; |
| 229 | ArrayList<CallContact> contacts; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 230 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 231 | String[] sectionsArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 232 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 233 | public String[] getSectionsArray() { |
| 234 | return sectionsArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 235 | } |
| 236 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 237 | int headersCount; |
| 238 | private SparseArray<Item> items = new SparseArray<Item>(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 239 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 240 | public SparseArray<Item> getItems() { |
| 241 | return items; |
| 242 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 243 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 244 | public void setItems(SparseArray<Item> items) { |
| 245 | this.items = items; |
| 246 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 247 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 248 | public SparseArray<Section> getSections() { |
| 249 | return sections; |
| 250 | } |
| 251 | |
| 252 | public void setSections(SparseArray<Section> sections) { |
| 253 | this.sections = sections; |
| 254 | } |
| 255 | |
| 256 | private SparseArray<Section> sections = new SparseArray<Section>(); |
| 257 | |
| 258 | public HeadersHolder(ArrayList<CallContact> a) { |
| 259 | alphaIndexer = new HashMap<String, Integer>(); |
| 260 | contacts = a; |
| 261 | headersCount = 0; |
| 262 | buildIndex(); |
| 263 | } |
| 264 | |
| 265 | // public int getHeadersCount() { |
| 266 | // return headersCount; |
| 267 | // } |
| 268 | |
| 269 | private class Item { |
| 270 | public Item(int rpos, int headersCount2, CallContact track) { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 271 | // Log.i(TAG, "Creating Item"); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 272 | |
| 273 | sectionNumber = headersCount2; |
| 274 | realPos = rpos; |
| 275 | tr = track; |
| 276 | |
| 277 | } |
| 278 | |
| 279 | public int realPos; |
| 280 | public int sectionNumber; |
| 281 | public CallContact tr; |
| 282 | } |
| 283 | |
| 284 | private class Section { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 285 | // public int startPosition; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 286 | public int number; |
| 287 | public String header; |
| 288 | |
| 289 | public Section(int i, int headersCount, String str) { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 290 | // Log.i(TAG, "Creating section"); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 291 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 292 | // startPosition = i + headersCount; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 293 | number = headersCount; |
| 294 | header = str; |
| 295 | |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public String toString() { |
| 300 | return header; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | public void buildIndex() { |
| 305 | |
| 306 | for (int x = 0; x < contacts.size(); x++) { |
| 307 | String s = contacts.get(x).getmDisplayName(); |
| 308 | String ch = s.substring(0, 1); |
| 309 | ch = ch.toUpperCase(Locale.CANADA); |
| 310 | if (!alphaIndexer.containsKey(ch)) { |
| 311 | sections.put(x + headersCount, new Section(x, headersCount, ch)); |
| 312 | headersCount++; |
| 313 | } |
| 314 | Integer result = alphaIndexer.put(ch, x + headersCount); |
| 315 | items.put(x + headersCount, new Item(x, headersCount, contacts.get(x))); |
| 316 | if (result == null) { |
| 317 | |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | Set<String> sect = alphaIndexer.keySet(); |
| 322 | |
| 323 | // create a list from the set to sort |
| 324 | ArrayList<String> sectionList = new ArrayList<String>(sect); |
| 325 | Collections.sort(sectionList); |
| 326 | sectionsArray = new String[sectionList.size()]; |
| 327 | sectionList.toArray(sectionsArray); |
| 328 | |
| 329 | } |
| 330 | |
| 331 | public HashMap<String, Integer> getAlphaIndexer() { |
| 332 | return alphaIndexer; |
| 333 | } |
| 334 | |
| 335 | public int getPositionFor(int section) { |
| 336 | if (section == sectionsArray.length) |
| 337 | return sectionsArray.length; |
| 338 | return alphaIndexer.get(sectionsArray[section]); |
| 339 | } |
| 340 | |
| 341 | public int getSectionFor(int position) { |
| 342 | return (null != items.get(position)) ? items.get(position).sectionNumber : sections.get(position).number; |
| 343 | } |
| 344 | |
| 345 | public boolean contains(int pos) { |
| 346 | if (sections.get(pos) != null) { |
| 347 | return true; |
| 348 | } |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | public CallContact getCallContact(int position) { |
| 353 | |
| 354 | if (items.get(position) == null) |
| 355 | return null; |
| 356 | |
| 357 | return items.get(position).tr; |
| 358 | |
| 359 | } |
| 360 | |
| 361 | public int size() { |
| 362 | return contacts.size(); |
| 363 | } |
| 364 | |
| 365 | public void clear() { |
| 366 | contacts.clear(); |
| 367 | |
| 368 | } |
| 369 | |
| 370 | public void add(CallContact tr) { |
| 371 | contacts.add(tr); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | public void addAll(ArrayList<CallContact> tr) { |
| 376 | contacts.clear(); |
| 377 | contacts.addAll(tr); |
| 378 | |
| 379 | } |
| 380 | |
| 381 | public ArrayList<CallContact> getTracks() { |
| 382 | return contacts; |
| 383 | } |
| 384 | |
| 385 | public int getTrackPosition(int pos) { |
| 386 | if (sections.get(pos) != null) { |
| 387 | return items.get(pos + 1).realPos; |
| 388 | } |
| 389 | return items.get(pos).realPos; |
| 390 | } |
| 391 | |
| 392 | public CharSequence getSection(int position) { |
| 393 | return sections.get(position).header; |
| 394 | } |
| 395 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | } |