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