blob: a47b20af1fa78e0b9a1c505958a13200ebb776ae [file] [log] [blame]
alisiond295ec22013-05-17 10:12:13 -04001package com.savoirfairelinux.sflphone.adapters;
2
alisiond295ec22013-05-17 10:12:13 -04003import java.util.ArrayList;
alisionf7053602013-07-09 10:25:20 -04004import java.util.Collections;
5import java.util.HashMap;
6import java.util.Locale;
7import java.util.Set;
alisiond295ec22013-05-17 10:12:13 -04008import java.util.concurrent.ExecutorService;
9import java.util.concurrent.Executors;
10
alisiond295ec22013-05-17 10:12:13 -040011import android.content.Context;
alisionf7053602013-07-09 10:25:20 -040012import android.util.SparseArray;
alisiond295ec22013-05-17 10:12:13 -040013import android.view.LayoutInflater;
14import android.view.View;
15import android.view.ViewGroup;
16import android.widget.BaseAdapter;
17import android.widget.ImageView;
alisionf7053602013-07-09 10:25:20 -040018import android.widget.SectionIndexer;
alisiond295ec22013-05-17 10:12:13 -040019import android.widget.TextView;
20
21import com.savoirfairelinux.sflphone.R;
22import com.savoirfairelinux.sflphone.model.CallContact;
23
alisionf7053602013-07-09 10:25:20 -040024public class ContactsAdapter extends BaseAdapter implements SectionIndexer {
alisiond295ec22013-05-17 10:12:13 -040025
26 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
alisiond295ec22013-05-17 10:12:13 -040027 Context mContext;
28
alisionf7053602013-07-09 10:25:20 -040029 HashMap<String, Integer> alphaIndexer;
30 String[] sections;
31
Alexandre Lision6e8931e2013-09-19 16:49:34 -040032// private static final String TAG = ContactsAdapter.class.getSimpleName();
alisiond295ec22013-05-17 10:12:13 -040033
34 public ContactsAdapter(Context context) {
35 super();
36 mContext = context;
alisionf7053602013-07-09 10:25:20 -040037 alphaIndexer = new HashMap<String, Integer>();
38 headers = new HeadersHolder(new ArrayList<CallContact>());
alisiond295ec22013-05-17 10:12:13 -040039 }
40
alisionf7053602013-07-09 10:25:20 -040041 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;
alisiond295ec22013-05-17 10:12:13 -040059 }
60
alisionf7053602013-07-09 10:25:20 -040061 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 Lision6e8931e2013-09-19 16:49:34 -040081 infos_fetcher.execute(new ContactPictureTask(mContext, photo_view, item.getId()));
alisionf7053602013-07-09 10:25:20 -040082
83 return convertView;
alisiond295ec22013-05-17 10:12:13 -040084 }
85
86 @Override
87 public int getCount() {
alisionf7053602013-07-09 10:25:20 -040088 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);
alisiond295ec22013-05-17 10:12:13 -0400141 }
142
143 @Override
144 public CallContact getItem(int index) {
alisionf7053602013-07-09 10:25:20 -0400145 return headers.getCallContact(index);
alisiond295ec22013-05-17 10:12:13 -0400146 }
alisionf7053602013-07-09 10:25:20 -0400147
148 public class HeadersHolder {
alisiond295ec22013-05-17 10:12:13 -0400149
alisionf7053602013-07-09 10:25:20 -0400150 public static final String TAG = "HeadersHolder";
151 HashMap<String, Integer> alphaIndexer;
152 ArrayList<CallContact> contacts;
alisiond295ec22013-05-17 10:12:13 -0400153
alisionf7053602013-07-09 10:25:20 -0400154 String[] sectionsArray;
alisiond295ec22013-05-17 10:12:13 -0400155
alisionf7053602013-07-09 10:25:20 -0400156 public String[] getSectionsArray() {
157 return sectionsArray;
alisiond295ec22013-05-17 10:12:13 -0400158 }
159
alisionf7053602013-07-09 10:25:20 -0400160 int headersCount;
161 private SparseArray<Item> items = new SparseArray<Item>();
alisiond295ec22013-05-17 10:12:13 -0400162
alisionf7053602013-07-09 10:25:20 -0400163 public SparseArray<Item> getItems() {
164 return items;
165 }
alisiond295ec22013-05-17 10:12:13 -0400166
alisionf7053602013-07-09 10:25:20 -0400167 public void setItems(SparseArray<Item> items) {
168 this.items = items;
169 }
alisiond295ec22013-05-17 10:12:13 -0400170
alisionf7053602013-07-09 10:25:20 -0400171 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 Lision6e8931e2013-09-19 16:49:34 -0400208// public int startPosition;
alisionf7053602013-07-09 10:25:20 -0400209 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 Lision6e8931e2013-09-19 16:49:34 -0400215// startPosition = i + headersCount;
alisionf7053602013-07-09 10:25:20 -0400216 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
alisiond295ec22013-05-17 10:12:13 -0400319 }
320
321}