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; |
| 5 | import java.util.concurrent.ExecutorService; |
| 6 | import java.util.concurrent.Executors; |
| 7 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 8 | import org.sflphone.R; |
| 9 | import org.sflphone.fragments.ContactListFragment; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 10 | import org.sflphone.model.CallContact; |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 11 | import org.sflphone.views.stickylistheaders.StickyListHeadersAdapter; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 12 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 13 | import android.content.Context; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 14 | import android.view.LayoutInflater; |
| 15 | import android.view.View; |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 16 | import android.view.View.OnClickListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 17 | import android.view.ViewGroup; |
| 18 | import android.widget.BaseAdapter; |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 19 | import android.widget.ImageButton; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 20 | import android.widget.ImageView; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 21 | import android.widget.SectionIndexer; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 22 | import android.widget.TextView; |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 23 | import android.widget.Toast; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 24 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 25 | public class ContactsAdapter extends BaseAdapter implements StickyListHeadersAdapter, SectionIndexer { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 26 | |
| 27 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 28 | Context mContext; |
| 29 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 30 | private ArrayList<CallContact> mContacts; |
| 31 | private int[] mSectionIndices; |
| 32 | private Character[] mSectionLetters; |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 33 | WeakReference<ContactListFragment> parent; |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 34 | private LayoutInflater mInflater; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 35 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 36 | // private static final String TAG = ContactsAdapter.class.getSimpleName(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 37 | |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 38 | public ContactsAdapter(ContactListFragment contactListFragment) { |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 39 | super(); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 40 | mContext = contactListFragment.getActivity(); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 41 | mInflater = LayoutInflater.from(mContext); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 42 | parent = new WeakReference<ContactListFragment>(contactListFragment); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 43 | mContacts = new ArrayList<CallContact>(); |
| 44 | mSectionIndices = getSectionIndices(); |
| 45 | mSectionLetters = getSectionLetters(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 48 | public static final int TYPE_HEADER = 0; |
| 49 | public static final int TYPE_CONTACT = 1; |
| 50 | |
| 51 | private int[] getSectionIndices() { |
| 52 | ArrayList<Integer> sectionIndices = new ArrayList<Integer>(); |
| 53 | if (mContacts.isEmpty()) |
| 54 | return new int[0]; |
| 55 | char lastFirstChar = mContacts.get(0).getmDisplayName().charAt(0); |
| 56 | sectionIndices.add(0); |
| 57 | for (int i = 1; i < mContacts.size(); i++) { |
| 58 | if (mContacts.get(i).getmDisplayName().charAt(0) != lastFirstChar) { |
| 59 | lastFirstChar = mContacts.get(i).getmDisplayName().charAt(0); |
| 60 | sectionIndices.add(i); |
| 61 | } |
| 62 | } |
| 63 | int[] sections = new int[sectionIndices.size()]; |
| 64 | for (int i = 0; i < sectionIndices.size(); i++) { |
| 65 | sections[i] = sectionIndices.get(i); |
| 66 | } |
| 67 | return sections; |
| 68 | } |
| 69 | |
| 70 | private Character[] getSectionLetters() { |
| 71 | Character[] letters = new Character[mSectionIndices.length]; |
| 72 | for (int i = 0; i < mSectionIndices.length; i++) { |
| 73 | letters[i] = mContacts.get(mSectionIndices[i]).getmDisplayName().charAt(0); |
| 74 | } |
| 75 | return letters; |
| 76 | } |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 77 | |
| 78 | @Override |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 79 | public View getView(int position, View convertView, ViewGroup root) { |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 80 | ContactView entryView; |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 81 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 82 | if (convertView == null) { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 83 | convertView = mInflater.inflate(R.layout.item_contact, null); |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 84 | |
| 85 | entryView = new ContactView(); |
| 86 | entryView.quick_starred = (ImageButton) convertView.findViewById(R.id.quick_starred); |
| 87 | entryView.quick_edit = (ImageButton) convertView.findViewById(R.id.quick_edit); |
| 88 | entryView.quick_discard = (ImageButton) convertView.findViewById(R.id.quick_discard); |
| 89 | entryView.quick_call = (ImageButton) convertView.findViewById(R.id.quick_call); |
| 90 | entryView.quick_msg = (ImageButton) convertView.findViewById(R.id.quick_message); |
| 91 | entryView.photo = (ImageView) convertView.findViewById(R.id.photo); |
| 92 | entryView.display_name = (TextView) convertView.findViewById(R.id.display_name); |
| 93 | convertView.setTag(entryView); |
| 94 | } else { |
| 95 | entryView = (ContactView) convertView.getTag(); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 98 | final CallContact item = mContacts.get(position); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 99 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 100 | entryView.display_name.setText(item.getmDisplayName()); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 101 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 102 | if (item.hasPhoto()) { |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 103 | entryView.photo.setImageBitmap(item.getPhoto()); |
| 104 | } else { |
| 105 | infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, item)); |
| 106 | } |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 107 | |
| 108 | entryView.quick_call.setOnClickListener(new OnClickListener() { |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 109 | |
| 110 | @Override |
| 111 | public void onClick(View v) { |
| 112 | parent.get().mCallbacks.onCallContact(item); |
| 113 | |
| 114 | } |
| 115 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 116 | |
Alexandre Lision | e168d4a | 2013-10-24 12:32:59 -0400 | [diff] [blame] | 117 | entryView.quick_msg.setOnClickListener(new OnClickListener() { |
| 118 | |
| 119 | @Override |
| 120 | public void onClick(View v) { |
| 121 | parent.get().mCallbacks.onTextContact(item); |
| 122 | } |
| 123 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 124 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 125 | entryView.quick_starred.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 126 | |
| 127 | @Override |
| 128 | public void onClick(View v) { |
| 129 | Toast.makeText(mContext, "Coming soon", Toast.LENGTH_SHORT).show(); |
| 130 | } |
| 131 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 132 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 133 | entryView.quick_edit.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 134 | |
| 135 | @Override |
| 136 | public void onClick(View v) { |
| 137 | parent.get().mCallbacks.onEditContact(item); |
| 138 | |
| 139 | } |
| 140 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 141 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 142 | entryView.quick_discard.setOnClickListener(new OnClickListener() { |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 143 | |
| 144 | @Override |
| 145 | public void onClick(View v) { |
| 146 | Toast.makeText(mContext, "Coming soon", Toast.LENGTH_SHORT).show(); |
| 147 | |
| 148 | } |
| 149 | }); |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 150 | |
Alexandre Lision | e168d4a | 2013-10-24 12:32:59 -0400 | [diff] [blame] | 151 | entryView.quick_edit.setClickable(false); |
| 152 | entryView.quick_discard.setClickable(false); |
| 153 | entryView.quick_starred.setClickable(false); |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 154 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 155 | return convertView; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 156 | } |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 157 | |
Alexandre Lision | d5dbcdf | 2013-10-07 14:13:09 -0400 | [diff] [blame] | 158 | /********************* |
| 159 | * ViewHolder Pattern |
| 160 | *********************/ |
| 161 | public class ContactView { |
| 162 | ImageButton quick_starred, quick_edit, quick_discard, quick_call, quick_msg; |
| 163 | ImageView photo; |
| 164 | TextView display_name; |
| 165 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 166 | |
| 167 | @Override |
| 168 | public int getCount() { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 169 | return mContacts.size(); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public int getViewTypeCount() { |
| 174 | return 2; |
| 175 | } |
| 176 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 177 | @Override |
| 178 | public long getItemId(int position) { |
| 179 | return 0; |
| 180 | } |
| 181 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 182 | @Override |
| 183 | public View getHeaderView(int position, View convertView, ViewGroup parent) { |
| 184 | HeaderViewHolder holder; |
| 185 | |
| 186 | if (convertView == null) { |
| 187 | holder = new HeaderViewHolder(); |
| 188 | convertView = mInflater.inflate(R.layout.header, parent, false); |
| 189 | holder.text = (TextView) convertView.findViewById(R.id.header_letter); |
| 190 | convertView.setTag(holder); |
| 191 | } else { |
| 192 | holder = (HeaderViewHolder) convertView.getTag(); |
| 193 | } |
| 194 | |
| 195 | // set header text as first char in name |
| 196 | char headerChar = mContacts.get(position).getmDisplayName().subSequence(0, 1).charAt(0); |
| 197 | |
| 198 | holder.text.setText("" + headerChar); |
| 199 | |
| 200 | return convertView; |
| 201 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 204 | class HeaderViewHolder { |
| 205 | TextView text; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 208 | @Override |
| 209 | public long getHeaderId(int position) { |
| 210 | // return the first character of the name as ID because this is what |
| 211 | // headers are based upon |
| 212 | return mContacts.get(position).getmDisplayName().subSequence(0, 1).charAt(0); |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 213 | } |
| 214 | |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 215 | @Override |
| 216 | public int getPositionForSection(int section) { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 217 | if (section >= mSectionIndices.length) { |
| 218 | section = mSectionIndices.length - 1; |
| 219 | } else if (section < 0) { |
| 220 | section = 0; |
| 221 | } |
| 222 | return mSectionIndices[section]; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public int getSectionForPosition(int position) { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 227 | for (int i = 0; i < mSectionIndices.length; i++) { |
| 228 | if (position < mSectionIndices[i]) { |
| 229 | return i - 1; |
| 230 | } |
| 231 | } |
| 232 | return mSectionIndices.length - 1; |
alision | f705360 | 2013-07-09 10:25:20 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Object[] getSections() { |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 237 | return mSectionLetters; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 241 | public CallContact getItem(int position) { |
| 242 | return mContacts.get(position); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 243 | } |
Alexandre Lision | e1c96db | 2013-10-04 14:34:21 -0400 | [diff] [blame] | 244 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 245 | public void clear() { |
| 246 | mContacts = new ArrayList<CallContact>(); |
| 247 | mSectionIndices = new int[0]; |
| 248 | mSectionLetters = new Character[0]; |
| 249 | notifyDataSetChanged(); |
| 250 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 251 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 252 | public void restore() { |
| 253 | mContacts = new ArrayList<CallContact>(); |
| 254 | mSectionIndices = getSectionIndices(); |
| 255 | mSectionLetters = getSectionLetters(); |
| 256 | notifyDataSetChanged(); |
| 257 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 258 | |
Alexandre Lision | 691a1d6 | 2013-11-12 10:06:27 -0500 | [diff] [blame] | 259 | public void addAll(ArrayList<CallContact> tmp) { |
| 260 | mContacts.addAll(tmp); |
| 261 | mSectionIndices = getSectionIndices(); |
| 262 | mSectionLetters = getSectionLetters(); |
| 263 | notifyDataSetChanged(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | } |