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 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 16 | import android.app.Fragment; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 17 | import android.content.Context; |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 18 | import android.util.Log; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 19 | import android.util.SparseArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 20 | import android.view.LayoutInflater; |
| 21 | import android.view.View; |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 22 | import android.view.View.OnClickListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 23 | import android.view.ViewGroup; |
| 24 | import android.widget.BaseAdapter; |
| 25 | import android.widget.ImageView; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 26 | import android.widget.SectionIndexer; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 27 | import android.widget.TextView; |
| 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) { |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 78 | |
| 79 | if (convertView == null) { |
| 80 | convertView = inflater.inflate(R.layout.item_contact, null); |
| 81 | } |
| 82 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 83 | final CallContact item = headers.getCallContact(position); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 84 | |
| 85 | ((TextView) convertView.findViewById(R.id.display_name)).setText(item.getmDisplayName()); |
| 86 | ImageView photo_view = (ImageView) convertView.findViewById(R.id.photo); |
| 87 | |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 88 | infos_fetcher.execute(new ContactPictureTask(mContext, photo_view, item.getId())); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 89 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 90 | convertView.findViewById(R.id.quick_call).setOnClickListener(new OnClickListener() { |
| 91 | |
| 92 | @Override |
| 93 | public void onClick(View v) { |
| 94 | parent.get().mCallbacks.onCallContact(item); |
| 95 | |
| 96 | } |
| 97 | }); |
| 98 | |
| 99 | convertView.findViewById(R.id.quick_message).setOnClickListener(new OnClickListener() { |
| 100 | |
| 101 | @Override |
| 102 | public void onClick(View v) { |
| 103 | parent.get().mCallbacks.onTextContact(item); |
| 104 | } |
| 105 | }); |
| 106 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 107 | return convertView; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int getCount() { |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 112 | return headers.size() + headers.getSections().size(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public int getItemViewType(int pos) { |
| 117 | return (headers.contains(pos) ? TYPE_HEADER : TYPE_TRACK); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public int getViewTypeCount() { |
| 122 | return 2; |
| 123 | } |
| 124 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 125 | @Override |
| 126 | public long getItemId(int position) { |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | public void removeAll() { |
| 131 | headers.clear(); |
| 132 | notifyDataSetChanged(); |
| 133 | } |
| 134 | |
| 135 | public void add(CallContact tr) { |
| 136 | headers.add(tr); |
| 137 | headers.buildIndex(); |
| 138 | } |
| 139 | |
| 140 | public void addAll(ArrayList<CallContact> tr) { |
| 141 | headers = new HeadersHolder(tr); |
| 142 | notifyDataSetChanged(); |
| 143 | } |
| 144 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 145 | @Override |
| 146 | public int getPositionForSection(int section) { |
| 147 | return headers.getPositionFor(section); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int getSectionForPosition(int position) { |
| 152 | return headers.getSectionFor(position); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Object[] getSections() { |
| 157 | return headers.getSectionsArray(); |
| 158 | } |
| 159 | |
| 160 | public int getRealPosition(int pos) { |
| 161 | return headers.getTrackPosition(pos); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public CallContact getItem(int index) { |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 166 | return headers.getCallContact(index); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 167 | } |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 168 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 169 | public class HeadersHolder { |
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 static final String TAG = "HeadersHolder"; |
| 172 | HashMap<String, Integer> alphaIndexer; |
| 173 | ArrayList<CallContact> contacts; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 174 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 175 | String[] sectionsArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 176 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 177 | public String[] getSectionsArray() { |
| 178 | return sectionsArray; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 179 | } |
| 180 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 181 | int headersCount; |
| 182 | private SparseArray<Item> items = new SparseArray<Item>(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 183 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 184 | public SparseArray<Item> getItems() { |
| 185 | return items; |
| 186 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 187 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 188 | public void setItems(SparseArray<Item> items) { |
| 189 | this.items = items; |
| 190 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 191 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 192 | public SparseArray<Section> getSections() { |
| 193 | return sections; |
| 194 | } |
| 195 | |
| 196 | public void setSections(SparseArray<Section> sections) { |
| 197 | this.sections = sections; |
| 198 | } |
| 199 | |
| 200 | private SparseArray<Section> sections = new SparseArray<Section>(); |
| 201 | |
| 202 | public HeadersHolder(ArrayList<CallContact> a) { |
| 203 | alphaIndexer = new HashMap<String, Integer>(); |
| 204 | contacts = a; |
| 205 | headersCount = 0; |
| 206 | buildIndex(); |
| 207 | } |
| 208 | |
| 209 | // public int getHeadersCount() { |
| 210 | // return headersCount; |
| 211 | // } |
| 212 | |
| 213 | private class Item { |
| 214 | public Item(int rpos, int headersCount2, CallContact track) { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 215 | // Log.i(TAG, "Creating Item"); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 216 | |
| 217 | sectionNumber = headersCount2; |
| 218 | realPos = rpos; |
| 219 | tr = track; |
| 220 | |
| 221 | } |
| 222 | |
| 223 | public int realPos; |
| 224 | public int sectionNumber; |
| 225 | public CallContact tr; |
| 226 | } |
| 227 | |
| 228 | private class Section { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 229 | // public int startPosition; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 230 | public int number; |
| 231 | public String header; |
| 232 | |
| 233 | public Section(int i, int headersCount, String str) { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 234 | // Log.i(TAG, "Creating section"); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 235 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame^] | 236 | // startPosition = i + headersCount; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 237 | number = headersCount; |
| 238 | header = str; |
| 239 | |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public String toString() { |
| 244 | return header; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | public void buildIndex() { |
| 249 | |
| 250 | for (int x = 0; x < contacts.size(); x++) { |
| 251 | String s = contacts.get(x).getmDisplayName(); |
| 252 | String ch = s.substring(0, 1); |
| 253 | ch = ch.toUpperCase(Locale.CANADA); |
| 254 | if (!alphaIndexer.containsKey(ch)) { |
| 255 | sections.put(x + headersCount, new Section(x, headersCount, ch)); |
| 256 | headersCount++; |
| 257 | } |
| 258 | Integer result = alphaIndexer.put(ch, x + headersCount); |
| 259 | items.put(x + headersCount, new Item(x, headersCount, contacts.get(x))); |
| 260 | if (result == null) { |
| 261 | |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | Set<String> sect = alphaIndexer.keySet(); |
| 266 | |
| 267 | // create a list from the set to sort |
| 268 | ArrayList<String> sectionList = new ArrayList<String>(sect); |
| 269 | Collections.sort(sectionList); |
| 270 | sectionsArray = new String[sectionList.size()]; |
| 271 | sectionList.toArray(sectionsArray); |
| 272 | |
| 273 | } |
| 274 | |
| 275 | public HashMap<String, Integer> getAlphaIndexer() { |
| 276 | return alphaIndexer; |
| 277 | } |
| 278 | |
| 279 | public int getPositionFor(int section) { |
| 280 | if (section == sectionsArray.length) |
| 281 | return sectionsArray.length; |
| 282 | return alphaIndexer.get(sectionsArray[section]); |
| 283 | } |
| 284 | |
| 285 | public int getSectionFor(int position) { |
| 286 | return (null != items.get(position)) ? items.get(position).sectionNumber : sections.get(position).number; |
| 287 | } |
| 288 | |
| 289 | public boolean contains(int pos) { |
| 290 | if (sections.get(pos) != null) { |
| 291 | return true; |
| 292 | } |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | public CallContact getCallContact(int position) { |
| 297 | |
| 298 | if (items.get(position) == null) |
| 299 | return null; |
| 300 | |
| 301 | return items.get(position).tr; |
| 302 | |
| 303 | } |
| 304 | |
| 305 | public int size() { |
| 306 | return contacts.size(); |
| 307 | } |
| 308 | |
| 309 | public void clear() { |
| 310 | contacts.clear(); |
| 311 | |
| 312 | } |
| 313 | |
| 314 | public void add(CallContact tr) { |
| 315 | contacts.add(tr); |
| 316 | |
| 317 | } |
| 318 | |
| 319 | public void addAll(ArrayList<CallContact> tr) { |
| 320 | contacts.clear(); |
| 321 | contacts.addAll(tr); |
| 322 | |
| 323 | } |
| 324 | |
| 325 | public ArrayList<CallContact> getTracks() { |
| 326 | return contacts; |
| 327 | } |
| 328 | |
| 329 | public int getTrackPosition(int pos) { |
| 330 | if (sections.get(pos) != null) { |
| 331 | return items.get(pos + 1).realPos; |
| 332 | } |
| 333 | return items.get(pos).realPos; |
| 334 | } |
| 335 | |
| 336 | public CharSequence getSection(int position) { |
| 337 | return sections.get(position).header; |
| 338 | } |
| 339 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | } |