Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.adapters; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 33 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 34 | import java.lang.ref.WeakReference; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.concurrent.ExecutorService; |
| 37 | import java.util.concurrent.Executors; |
| 38 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 39 | import org.sflphone.R; |
| 40 | import org.sflphone.fragments.ContactListFragment; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 41 | import org.sflphone.model.CallContact; |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 42 | import org.sflphone.views.stickylistheaders.StickyListHeadersAdapter; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 43 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 44 | import android.content.Context; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 45 | import android.view.LayoutInflater; |
| 46 | import android.view.View; |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 47 | import android.view.View.OnClickListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 48 | import android.view.ViewGroup; |
| 49 | import android.widget.BaseAdapter; |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 50 | import android.widget.ImageButton; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 51 | import android.widget.ImageView; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 52 | import android.widget.SectionIndexer; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 53 | import android.widget.TextView; |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 54 | import android.widget.Toast; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 55 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 56 | public class ContactsAdapter extends BaseAdapter implements StickyListHeadersAdapter, SectionIndexer { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 57 | |
| 58 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 59 | Context mContext; |
| 60 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 61 | private ArrayList<CallContact> mContacts; |
| 62 | private int[] mSectionIndices; |
| 63 | private Character[] mSectionLetters; |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 64 | WeakReference<ContactListFragment> parent; |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 65 | private LayoutInflater mInflater; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 66 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 67 | // private static final String TAG = ContactsAdapter.class.getSimpleName(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 68 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 69 | public ContactsAdapter(ContactListFragment contactListFragment) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 70 | super(); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 71 | mContext = contactListFragment.getActivity(); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 72 | mInflater = LayoutInflater.from(mContext); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 73 | parent = new WeakReference<ContactListFragment>(contactListFragment); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 74 | mContacts = new ArrayList<CallContact>(); |
| 75 | mSectionIndices = getSectionIndices(); |
| 76 | mSectionLetters = getSectionLetters(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 79 | public static final int TYPE_HEADER = 0; |
| 80 | public static final int TYPE_CONTACT = 1; |
| 81 | |
| 82 | private int[] getSectionIndices() { |
| 83 | ArrayList<Integer> sectionIndices = new ArrayList<Integer>(); |
| 84 | if (mContacts.isEmpty()) |
| 85 | return new int[0]; |
| 86 | char lastFirstChar = mContacts.get(0).getmDisplayName().charAt(0); |
| 87 | sectionIndices.add(0); |
| 88 | for (int i = 1; i < mContacts.size(); i++) { |
| 89 | if (mContacts.get(i).getmDisplayName().charAt(0) != lastFirstChar) { |
| 90 | lastFirstChar = mContacts.get(i).getmDisplayName().charAt(0); |
| 91 | sectionIndices.add(i); |
| 92 | } |
| 93 | } |
| 94 | int[] sections = new int[sectionIndices.size()]; |
| 95 | for (int i = 0; i < sectionIndices.size(); i++) { |
| 96 | sections[i] = sectionIndices.get(i); |
| 97 | } |
| 98 | return sections; |
| 99 | } |
| 100 | |
| 101 | private Character[] getSectionLetters() { |
| 102 | Character[] letters = new Character[mSectionIndices.length]; |
| 103 | for (int i = 0; i < mSectionIndices.length; i++) { |
| 104 | letters[i] = mContacts.get(mSectionIndices[i]).getmDisplayName().charAt(0); |
| 105 | } |
| 106 | return letters; |
| 107 | } |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 108 | |
| 109 | @Override |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 110 | public View getView(int position, View convertView, ViewGroup root) { |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 111 | ContactView entryView; |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 112 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 113 | if (convertView == null) { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 114 | convertView = mInflater.inflate(R.layout.item_contact, null); |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 115 | |
| 116 | entryView = new ContactView(); |
| 117 | entryView.quick_starred = (ImageButton) convertView.findViewById(R.id.quick_starred); |
| 118 | entryView.quick_edit = (ImageButton) convertView.findViewById(R.id.quick_edit); |
| 119 | entryView.quick_discard = (ImageButton) convertView.findViewById(R.id.quick_discard); |
| 120 | entryView.quick_call = (ImageButton) convertView.findViewById(R.id.quick_call); |
| 121 | entryView.quick_msg = (ImageButton) convertView.findViewById(R.id.quick_message); |
| 122 | entryView.photo = (ImageView) convertView.findViewById(R.id.photo); |
| 123 | entryView.display_name = (TextView) convertView.findViewById(R.id.display_name); |
| 124 | convertView.setTag(entryView); |
| 125 | } else { |
| 126 | entryView = (ContactView) convertView.getTag(); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 129 | final CallContact item = mContacts.get(position); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 130 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 131 | entryView.display_name.setText(item.getmDisplayName()); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 132 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 133 | if (item.hasPhoto()) { |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 134 | entryView.photo.setImageBitmap(item.getPhoto()); |
| 135 | } else { |
| 136 | infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, item)); |
| 137 | } |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 138 | |
| 139 | entryView.quick_call.setOnClickListener(new OnClickListener() { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 140 | |
| 141 | @Override |
| 142 | public void onClick(View v) { |
| 143 | parent.get().mCallbacks.onCallContact(item); |
| 144 | |
| 145 | } |
| 146 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 147 | |
Alexandre Lision | e168d4a | 2013-10-24 12:32:59 -0400 | [diff] [blame] | 148 | entryView.quick_msg.setOnClickListener(new OnClickListener() { |
| 149 | |
| 150 | @Override |
| 151 | public void onClick(View v) { |
| 152 | parent.get().mCallbacks.onTextContact(item); |
| 153 | } |
| 154 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 155 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 156 | entryView.quick_starred.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 157 | |
| 158 | @Override |
| 159 | public void onClick(View v) { |
| 160 | Toast.makeText(mContext, "Coming soon", Toast.LENGTH_SHORT).show(); |
| 161 | } |
| 162 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 163 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 164 | entryView.quick_edit.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 165 | |
| 166 | @Override |
| 167 | public void onClick(View v) { |
| 168 | parent.get().mCallbacks.onEditContact(item); |
| 169 | |
| 170 | } |
| 171 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 172 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 173 | entryView.quick_discard.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 174 | |
| 175 | @Override |
| 176 | public void onClick(View v) { |
| 177 | Toast.makeText(mContext, "Coming soon", Toast.LENGTH_SHORT).show(); |
| 178 | |
| 179 | } |
| 180 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 181 | |
Alexandre Lision | e168d4a | 2013-10-24 12:32:59 -0400 | [diff] [blame] | 182 | entryView.quick_edit.setClickable(false); |
| 183 | entryView.quick_discard.setClickable(false); |
| 184 | entryView.quick_starred.setClickable(false); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 185 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 186 | return convertView; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 187 | } |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 188 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 189 | /********************* |
| 190 | * ViewHolder Pattern |
| 191 | *********************/ |
| 192 | public class ContactView { |
| 193 | ImageButton quick_starred, quick_edit, quick_discard, quick_call, quick_msg; |
| 194 | ImageView photo; |
| 195 | TextView display_name; |
| 196 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 197 | |
| 198 | @Override |
| 199 | public int getCount() { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 200 | return mContacts.size(); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public int getViewTypeCount() { |
| 205 | return 2; |
| 206 | } |
| 207 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 208 | @Override |
| 209 | public long getItemId(int position) { |
| 210 | return 0; |
| 211 | } |
| 212 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 213 | @Override |
| 214 | public View getHeaderView(int position, View convertView, ViewGroup parent) { |
| 215 | HeaderViewHolder holder; |
| 216 | |
| 217 | if (convertView == null) { |
| 218 | holder = new HeaderViewHolder(); |
| 219 | convertView = mInflater.inflate(R.layout.header, parent, false); |
| 220 | holder.text = (TextView) convertView.findViewById(R.id.header_letter); |
| 221 | convertView.setTag(holder); |
| 222 | } else { |
| 223 | holder = (HeaderViewHolder) convertView.getTag(); |
| 224 | } |
| 225 | |
| 226 | // set header text as first char in name |
| 227 | char headerChar = mContacts.get(position).getmDisplayName().subSequence(0, 1).charAt(0); |
| 228 | |
| 229 | holder.text.setText("" + headerChar); |
| 230 | |
| 231 | return convertView; |
| 232 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 235 | class HeaderViewHolder { |
| 236 | TextView text; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 239 | @Override |
| 240 | public long getHeaderId(int position) { |
| 241 | // return the first character of the name as ID because this is what |
| 242 | // headers are based upon |
| 243 | return mContacts.get(position).getmDisplayName().subSequence(0, 1).charAt(0); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 244 | } |
| 245 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 246 | @Override |
| 247 | public int getPositionForSection(int section) { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 248 | if (section >= mSectionIndices.length) { |
| 249 | section = mSectionIndices.length - 1; |
| 250 | } else if (section < 0) { |
| 251 | section = 0; |
| 252 | } |
| 253 | return mSectionIndices[section]; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public int getSectionForPosition(int position) { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 258 | for (int i = 0; i < mSectionIndices.length; i++) { |
| 259 | if (position < mSectionIndices[i]) { |
| 260 | return i - 1; |
| 261 | } |
| 262 | } |
| 263 | return mSectionIndices.length - 1; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public Object[] getSections() { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 268 | return mSectionLetters; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 272 | public CallContact getItem(int position) { |
| 273 | return mContacts.get(position); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 274 | } |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 275 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 276 | public void clear() { |
| 277 | mContacts = new ArrayList<CallContact>(); |
| 278 | mSectionIndices = new int[0]; |
| 279 | mSectionLetters = new Character[0]; |
| 280 | notifyDataSetChanged(); |
| 281 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 282 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 283 | public void restore() { |
| 284 | mContacts = new ArrayList<CallContact>(); |
| 285 | mSectionIndices = getSectionIndices(); |
| 286 | mSectionLetters = getSectionLetters(); |
| 287 | notifyDataSetChanged(); |
| 288 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 289 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 290 | public void addAll(ArrayList<CallContact> tmp) { |
| 291 | mContacts.addAll(tmp); |
| 292 | mSectionIndices = getSectionIndices(); |
| 293 | mSectionLetters = getSectionLetters(); |
| 294 | notifyDataSetChanged(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | } |