blob: 4491f695855a57b7966905b1263f2b71f8aba724 [file] [log] [blame]
Alexandre Lision064e1e02013-10-01 16:18:42 -04001package org.sflphone.adapters;
alisiond295ec22013-05-17 10:12:13 -04002
Alexandre Lisione1c96db2013-10-04 14:34:21 -04003import java.lang.ref.WeakReference;
alisiond295ec22013-05-17 10:12:13 -04004import java.util.ArrayList;
alisionf7053602013-07-09 10:25:20 -04005import java.util.Collections;
6import java.util.HashMap;
7import java.util.Locale;
8import java.util.Set;
alisiond295ec22013-05-17 10:12:13 -04009import java.util.concurrent.ExecutorService;
10import java.util.concurrent.Executors;
11
Alexandre Lisione1c96db2013-10-04 14:34:21 -040012import org.sflphone.R;
13import org.sflphone.fragments.ContactListFragment;
Alexandre Lision064e1e02013-10-01 16:18:42 -040014import org.sflphone.model.CallContact;
15
Alexandre Lisione1c96db2013-10-04 14:34:21 -040016import android.app.Fragment;
alisiond295ec22013-05-17 10:12:13 -040017import android.content.Context;
Alexandre Lisione1c96db2013-10-04 14:34:21 -040018import android.util.Log;
alisionf7053602013-07-09 10:25:20 -040019import android.util.SparseArray;
alisiond295ec22013-05-17 10:12:13 -040020import android.view.LayoutInflater;
21import android.view.View;
Alexandre Lisione1c96db2013-10-04 14:34:21 -040022import android.view.View.OnClickListener;
alisiond295ec22013-05-17 10:12:13 -040023import android.view.ViewGroup;
24import android.widget.BaseAdapter;
25import android.widget.ImageView;
alisionf7053602013-07-09 10:25:20 -040026import android.widget.SectionIndexer;
alisiond295ec22013-05-17 10:12:13 -040027import android.widget.TextView;
28
alisionf7053602013-07-09 10:25:20 -040029public class ContactsAdapter extends BaseAdapter implements SectionIndexer {
alisiond295ec22013-05-17 10:12:13 -040030
31 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
alisiond295ec22013-05-17 10:12:13 -040032 Context mContext;
33
alisionf7053602013-07-09 10:25:20 -040034 HashMap<String, Integer> alphaIndexer;
35 String[] sections;
Alexandre Lisione1c96db2013-10-04 14:34:21 -040036 WeakReference<ContactListFragment> parent;
alisionf7053602013-07-09 10:25:20 -040037
Alexandre Lisione1c96db2013-10-04 14:34:21 -040038 // private static final String TAG = ContactsAdapter.class.getSimpleName();
alisiond295ec22013-05-17 10:12:13 -040039
Alexandre Lisione1c96db2013-10-04 14:34:21 -040040 public ContactsAdapter(ContactListFragment contactListFragment) {
alisiond295ec22013-05-17 10:12:13 -040041 super();
Alexandre Lisione1c96db2013-10-04 14:34:21 -040042 mContext = contactListFragment.getActivity();
alisionf7053602013-07-09 10:25:20 -040043 alphaIndexer = new HashMap<String, Integer>();
44 headers = new HeadersHolder(new ArrayList<CallContact>());
Alexandre Lisione1c96db2013-10-04 14:34:21 -040045 parent = new WeakReference<ContactListFragment>(contactListFragment);
alisiond295ec22013-05-17 10:12:13 -040046 }
47
alisionf7053602013-07-09 10:25:20 -040048 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 Lisione1c96db2013-10-04 14:34:21 -040056 // Log.i(TAG, "positon" + position + " type " + type);
alisionf7053602013-07-09 10:25:20 -040057 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
58
59 switch (type) {
60 case TYPE_TRACK:
Alexandre Lisione1c96db2013-10-04 14:34:21 -040061 return getViewContact(position, inflater, convertView);
alisionf7053602013-07-09 10:25:20 -040062 case TYPE_HEADER:
63 return getViewHeader(position, inflater, convertView);
64 }
65 return null;
alisiond295ec22013-05-17 10:12:13 -040066 }
67
alisionf7053602013-07-09 10:25:20 -040068 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 Lisione1c96db2013-10-04 14:34:21 -040077 private View getViewContact(int position, LayoutInflater inflater, View convertView) {
alisionf7053602013-07-09 10:25:20 -040078
79 if (convertView == null) {
80 convertView = inflater.inflate(R.layout.item_contact, null);
81 }
82
Alexandre Lisione1c96db2013-10-04 14:34:21 -040083 final CallContact item = headers.getCallContact(position);
alisionf7053602013-07-09 10:25:20 -040084
85 ((TextView) convertView.findViewById(R.id.display_name)).setText(item.getmDisplayName());
86 ImageView photo_view = (ImageView) convertView.findViewById(R.id.photo);
87
Alexandre Lision6e8931e2013-09-19 16:49:34 -040088 infos_fetcher.execute(new ContactPictureTask(mContext, photo_view, item.getId()));
alisionf7053602013-07-09 10:25:20 -040089
Alexandre Lisione1c96db2013-10-04 14:34:21 -040090 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
alisionf7053602013-07-09 10:25:20 -0400107 return convertView;
alisiond295ec22013-05-17 10:12:13 -0400108 }
109
110 @Override
111 public int getCount() {
alisionf7053602013-07-09 10:25:20 -0400112 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
alisionf7053602013-07-09 10:25:20 -0400125 @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
alisionf7053602013-07-09 10:25:20 -0400145 @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);
alisiond295ec22013-05-17 10:12:13 -0400162 }
163
164 @Override
165 public CallContact getItem(int index) {
alisionf7053602013-07-09 10:25:20 -0400166 return headers.getCallContact(index);
alisiond295ec22013-05-17 10:12:13 -0400167 }
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400168
alisionf7053602013-07-09 10:25:20 -0400169 public class HeadersHolder {
alisiond295ec22013-05-17 10:12:13 -0400170
alisionf7053602013-07-09 10:25:20 -0400171 public static final String TAG = "HeadersHolder";
172 HashMap<String, Integer> alphaIndexer;
173 ArrayList<CallContact> contacts;
alisiond295ec22013-05-17 10:12:13 -0400174
alisionf7053602013-07-09 10:25:20 -0400175 String[] sectionsArray;
alisiond295ec22013-05-17 10:12:13 -0400176
alisionf7053602013-07-09 10:25:20 -0400177 public String[] getSectionsArray() {
178 return sectionsArray;
alisiond295ec22013-05-17 10:12:13 -0400179 }
180
alisionf7053602013-07-09 10:25:20 -0400181 int headersCount;
182 private SparseArray<Item> items = new SparseArray<Item>();
alisiond295ec22013-05-17 10:12:13 -0400183
alisionf7053602013-07-09 10:25:20 -0400184 public SparseArray<Item> getItems() {
185 return items;
186 }
alisiond295ec22013-05-17 10:12:13 -0400187
alisionf7053602013-07-09 10:25:20 -0400188 public void setItems(SparseArray<Item> items) {
189 this.items = items;
190 }
alisiond295ec22013-05-17 10:12:13 -0400191
alisionf7053602013-07-09 10:25:20 -0400192 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 Lisione1c96db2013-10-04 14:34:21 -0400215 // Log.i(TAG, "Creating Item");
alisionf7053602013-07-09 10:25:20 -0400216
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 Lisione1c96db2013-10-04 14:34:21 -0400229 // public int startPosition;
alisionf7053602013-07-09 10:25:20 -0400230 public int number;
231 public String header;
232
233 public Section(int i, int headersCount, String str) {
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400234 // Log.i(TAG, "Creating section");
alisionf7053602013-07-09 10:25:20 -0400235
Alexandre Lisione1c96db2013-10-04 14:34:21 -0400236 // startPosition = i + headersCount;
alisionf7053602013-07-09 10:25:20 -0400237 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
alisiond295ec22013-05-17 10:12:13 -0400340 }
341
342}