Adrien BĂ©raud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 3 | * |
| 4 | * Author: Adrien Beraud <adrien.beraud@gmail.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 | package com.savoirfairelinux.sflphone.client; |
| 32 | |
| 33 | import java.io.InputStream; |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | import java.util.concurrent.ExecutorService; |
| 37 | import java.util.concurrent.Executors; |
| 38 | |
| 39 | import android.app.ListFragment; |
| 40 | import android.app.LoaderManager; |
| 41 | import android.content.ContentResolver; |
| 42 | import android.content.ContentUris; |
| 43 | import android.content.Context; |
| 44 | import android.content.CursorLoader; |
| 45 | import android.content.Loader; |
| 46 | import android.database.Cursor; |
| 47 | import android.graphics.Bitmap; |
| 48 | import android.graphics.BitmapFactory; |
| 49 | import android.net.Uri; |
| 50 | import android.os.Bundle; |
| 51 | import android.provider.*; |
| 52 | import android.provider.ContactsContract.CommonDataKinds; |
| 53 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 54 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 55 | import android.provider.ContactsContract.Contacts; |
| 56 | import android.text.TextUtils; |
| 57 | import android.util.Log; |
| 58 | import android.view.*; |
| 59 | import android.widget.*; |
| 60 | import android.widget.SearchView.OnQueryTextListener; |
| 61 | |
| 62 | import com.savoirfairelinux.sflphone.R; |
| 63 | |
| 64 | /** |
| 65 | * Main list of Call Elements. |
| 66 | * We don't manage contacts ourself so they are |
| 67 | */ |
| 68 | public class CallElementList extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> |
| 69 | { |
| 70 | CursorAdapter mAdapter; |
| 71 | String mCurFilter; |
| 72 | |
| 73 | // These are the Contacts rows that we will retrieve. |
| 74 | static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, |
| 75 | Contacts.PHOTO_ID, |
| 76 | Contacts.LOOKUP_KEY }; |
| 77 | |
| 78 | static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE }; |
| 79 | static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE }; |
| 80 | |
| 81 | /** |
| 82 | * Runnable that fill information in a contact card asynchroniously. |
| 83 | */ |
| 84 | public static class InfosLoader implements Runnable |
| 85 | { |
| 86 | private View view; |
| 87 | private long cid; |
| 88 | private ContentResolver cr; |
| 89 | |
| 90 | public InfosLoader(Context context, View element, long contact_id) |
| 91 | { |
| 92 | cid = contact_id; |
| 93 | cr = context.getContentResolver(); |
| 94 | view = element; |
| 95 | } |
| 96 | |
| 97 | public static Bitmap loadContactPhoto(ContentResolver cr, long id) { |
| 98 | Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); |
| 99 | InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); |
| 100 | if (input == null) { |
| 101 | return null; |
| 102 | } |
| 103 | return BitmapFactory.decodeStream(input); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void run() |
| 108 | { |
| 109 | final Bitmap photo_bmp = loadContactPhoto(cr, cid); |
| 110 | |
| 111 | Cursor phones = cr.query( CommonDataKinds.Phone.CONTENT_URI, |
| 112 | CONTACTS_PHONES_PROJECTION, |
| 113 | CommonDataKinds.Phone.CONTACT_ID + " = ?", |
| 114 | new String[] { Long.toString(cid) }, |
| 115 | null); |
| 116 | final List<String> numbers = new ArrayList<String>(); |
| 117 | while (phones.moveToNext()) { |
| 118 | String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER)); |
| 119 | //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE)); |
| 120 | numbers.add(number); |
| 121 | } |
| 122 | phones.close(); |
| 123 | // TODO: same for SIP adresses. |
| 124 | |
| 125 | final Bitmap bmp = photo_bmp; |
| 126 | view.post(new Runnable() |
| 127 | { |
| 128 | @Override |
| 129 | public void run() |
| 130 | { |
| 131 | ImageView photo_view = (ImageView) view.findViewById(R.id.photo); |
| 132 | TextView phones_txt = (TextView) view.findViewById(R.id.phones); |
| 133 | |
| 134 | if (photo_bmp != null) { |
| 135 | photo_view.setImageBitmap(bmp); |
| 136 | photo_view.setVisibility(View.VISIBLE); |
| 137 | } else { |
| 138 | photo_view.setVisibility(View.GONE); |
| 139 | } |
| 140 | |
| 141 | if (numbers.size() > 0) { |
| 142 | String phonestxt = numbers.get(0); |
| 143 | for (int i = 1, n = numbers.size(); i < n; i++) |
| 144 | phonestxt += "\n" + numbers.get(i); |
| 145 | phones_txt.setText(phonestxt); |
| 146 | phones_txt.setVisibility(View.VISIBLE); |
| 147 | } else |
| 148 | phones_txt.setVisibility(View.GONE); |
| 149 | } |
| 150 | }); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * A CursorAdapter that creates and update call elements using corresponding contact infos. |
| 158 | * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc. |
| 159 | */ |
| 160 | class CallElementAdapter extends CursorAdapter |
| 161 | { |
| 162 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
| 163 | |
| 164 | public CallElementAdapter(Context context, Cursor c) |
| 165 | { |
| 166 | super(context, c, 0); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public View newView(Context context, Cursor cursor, ViewGroup parent) |
| 171 | { |
| 172 | LayoutInflater inflater = LayoutInflater.from(context); |
| 173 | View v = inflater.inflate(R.layout.call_element, parent, false); |
| 174 | bindView(v, context, cursor); |
| 175 | return v; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void bindView(final View view, Context context, Cursor cursor) |
| 180 | { |
| 181 | final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); |
| 182 | final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)); |
| 183 | //final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID)); |
| 184 | //final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI)); |
| 185 | |
| 186 | TextView display_name_txt = (TextView) view.findViewById(R.id.display_name); |
| 187 | display_name_txt.setText(display_name); |
| 188 | |
| 189 | ImageView photo_view = (ImageView) view.findViewById(R.id.photo); |
| 190 | photo_view.setVisibility(View.GONE); |
| 191 | |
| 192 | infos_fetcher.execute(new InfosLoader(getActivity(), view, contact_id)); |
| 193 | } |
| 194 | |
| 195 | }; |
| 196 | |
| 197 | @Override |
| 198 | public void onActivityCreated(Bundle savedInstanceState) |
| 199 | { |
| 200 | super.onActivityCreated(savedInstanceState); |
| 201 | |
| 202 | // Give some text to display if there is no data. In a real |
| 203 | // application this would come from a resource. |
| 204 | //setEmptyText("No phone numbers"); |
| 205 | |
| 206 | // We have a menu item to show in action bar. |
| 207 | setHasOptionsMenu(true); |
| 208 | |
| 209 | // Create an empty adapter we will use to display the loaded data. |
| 210 | mAdapter = new CallElementAdapter(getActivity(), null); |
| 211 | setListAdapter(mAdapter); |
| 212 | |
| 213 | // Start out with a progress indicator. |
| 214 | //setListShown(false); |
| 215 | |
| 216 | // Prepare the loader. Either re-connect with an existing one, |
| 217 | // or start a new one. |
| 218 | getLoaderManager().initLoader(0, null, this); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 223 | //LayoutInflater newInflater = inflater.cloneInContext(new ContextThemeWrapper(getActivity(), R.style.)); |
| 224 | View inflatedView = inflater.inflate(R.layout.call_element_list, container, false); |
| 225 | return inflatedView; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) |
| 230 | { |
| 231 | // Place an action bar item for searching. |
| 232 | MenuItem item = menu.add("Search"); |
| 233 | //item.setIcon(android.R.drawable.ic_menu_search); |
| 234 | item.setIcon(R.drawable.ic_menu_search); |
| 235 | item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); |
| 236 | SearchView sv = new SearchView(getActivity()); |
| 237 | sv.setOnQueryTextListener(this); |
| 238 | item.setActionView(sv); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public boolean onQueryTextChange(String newText) |
| 243 | { |
| 244 | // Called when the action bar search text has changed. Update |
| 245 | // the search filter, and restart the loader to do a new query |
| 246 | // with this filter. |
| 247 | String newFilter = !TextUtils.isEmpty(newText) ? newText : null; |
| 248 | // Don't do anything if the filter hasn't actually changed. |
| 249 | // Prevents restarting the loader when restoring state. |
| 250 | if (mCurFilter == null && newFilter == null) { return true; } |
| 251 | if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; } |
| 252 | mCurFilter = newFilter; |
| 253 | getLoaderManager().restartLoader(0, null, this); |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public boolean onQueryTextSubmit(String query) |
| 259 | { |
| 260 | // Don't care about this. |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void onListItemClick(ListView l, View v, int position, long id) |
| 266 | { |
| 267 | // Insert desired behavior here. |
| 268 | Log.i("FragmentComplexList", "Item clicked: " + id); |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public Loader<Cursor> onCreateLoader(int id, Bundle args) |
| 273 | { |
| 274 | |
| 275 | //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); |
| 276 | |
| 277 | // This is called when a new Loader needs to be created. This |
| 278 | // sample only has one Loader, so we don't care about the ID. |
| 279 | // First, pick the base URI to use depending on whether we are |
| 280 | // currently filtering. |
| 281 | Uri baseUri; |
| 282 | |
| 283 | if (mCurFilter != null) { |
| 284 | baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter)); |
| 285 | } else { |
| 286 | baseUri = Contacts.CONTENT_URI; |
| 287 | } |
| 288 | |
| 289 | // Now create and return a CursorLoader that will take care of |
| 290 | // creating a Cursor for the data being displayed. |
| 291 | String select = "((" + Contacts.DISPLAY_NAME |
| 292 | + " NOTNULL) AND (" |
| 293 | + Contacts.HAS_PHONE_NUMBER |
| 294 | + "=1) AND (" |
| 295 | + Contacts.DISPLAY_NAME |
| 296 | + " != '' ))"; |
| 297 | //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))"; |
| 298 | |
| 299 | return new CursorLoader(getActivity(), |
| 300 | baseUri, |
| 301 | CONTACTS_SUMMARY_PROJECTION, |
| 302 | select, |
| 303 | null, |
| 304 | Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) |
| 309 | { |
| 310 | // Swap the new cursor in. (The framework will take care of closing the |
| 311 | // old cursor once we return.) |
| 312 | mAdapter.swapCursor(data); |
| 313 | |
| 314 | // The list should now be shown. |
| 315 | /* |
| 316 | if (isResumed()) { |
| 317 | setListShown(true); |
| 318 | } else { |
| 319 | setListShownNoAnimation(true); |
| 320 | }*/ |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void onLoaderReset(Loader<Cursor> loader) |
| 325 | { |
| 326 | // This is called when the last Cursor provided to onLoadFinished() |
| 327 | // above is about to be closed. We need to make sure we are no |
| 328 | // longer using it. |
| 329 | mAdapter.swapCursor(null); |
| 330 | } |
| 331 | |
| 332 | } |