Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 3 | * |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 4 | * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 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 android.app.ListFragment; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 34 | import android.app.LoaderManager; |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 35 | import android.app.AlertDialog; |
| 36 | import android.app.AlertDialog.Builder; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 37 | import android.content.ContentResolver; |
| 38 | import android.content.ContentUris; |
| 39 | import android.content.Context; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 40 | import android.content.CursorLoader; |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 41 | import android.content.DialogInterface; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 42 | import android.content.Loader; |
| 43 | import android.database.Cursor; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 44 | import android.graphics.Bitmap; |
| 45 | import android.graphics.BitmapFactory; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 46 | import android.net.Uri; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 47 | import android.os.Bundle; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 48 | import android.provider.*; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 49 | import android.provider.ContactsContract.CommonDataKinds; |
| 50 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 51 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 52 | import android.provider.ContactsContract.Contacts; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 53 | import android.text.TextUtils; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 54 | import android.view.LayoutInflater; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 55 | import android.view.Menu; |
| 56 | import android.view.MenuItem; |
| 57 | import android.view.MenuInflater; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 58 | import android.view.View; |
| 59 | import android.view.ViewGroup; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 60 | import android.widget.CursorAdapter; |
| 61 | import android.widget.ImageView; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 62 | import android.widget.ListView; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 63 | import android.widget.TextView; |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 64 | import android.widget.Toast; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 65 | import android.widget.SearchView; |
Alexandre Savard | 3143399 | 2012-09-20 15:19:49 -0400 | [diff] [blame] | 66 | import android.widget.AdapterView; |
| 67 | import android.widget.AdapterView.OnItemLongClickListener; |
| 68 | import android.widget.AdapterView.OnItemSelectedListener; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 69 | import android.widget.SearchView.OnQueryTextListener; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 70 | import android.util.Log; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 71 | import java.io.InputStream; |
| 72 | import java.util.concurrent.ExecutorService; |
| 73 | import java.util.concurrent.Executors; |
| 74 | import java.util.List; |
| 75 | import java.util.ArrayList; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 76 | |
| 77 | import com.savoirfairelinux.sflphone.R; |
| 78 | |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 79 | public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 80 | { |
Alexandre Savard | 7a46f54 | 2012-09-20 11:23:33 -0400 | [diff] [blame] | 81 | final String TAG = "ConatctListFragment"; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 82 | ContactElementAdapter mAdapter; |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 83 | Context mContext; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 84 | String mCurFilter; |
| 85 | |
| 86 | // These are the Contacts rows that we will retrieve. |
| 87 | static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, |
| 88 | Contacts.PHOTO_ID, Contacts.LOOKUP_KEY }; |
| 89 | static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE }; |
| 90 | static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE }; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 91 | |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 92 | public static class InfosLoader implements Runnable |
| 93 | { |
| 94 | private View view; |
| 95 | private long cid; |
| 96 | private ContentResolver cr; |
| 97 | |
| 98 | public InfosLoader(Context context, View element, long contact_id) |
| 99 | { |
| 100 | cid = contact_id; |
| 101 | cr = context.getContentResolver(); |
| 102 | view = element; |
| 103 | } |
| 104 | |
| 105 | public static Bitmap loadContactPhoto(ContentResolver cr, long id) |
| 106 | { |
| 107 | Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); |
| 108 | InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); |
| 109 | if (input == null) { |
| 110 | return null; |
| 111 | } |
| 112 | return BitmapFactory.decodeStream(input); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void run() |
| 117 | { |
| 118 | final Bitmap photo_bmp = loadContactPhoto(cr, cid); |
| 119 | |
| 120 | Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI, |
| 121 | CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?", |
| 122 | new String[] { Long.toString(cid) }, |
| 123 | null); |
| 124 | |
| 125 | final List<String> numbers = new ArrayList<String>(); |
| 126 | while (phones.moveToNext()) { |
| 127 | String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER)); |
| 128 | // int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE)); |
| 129 | numbers.add(number); |
| 130 | } |
| 131 | phones.close(); |
| 132 | |
| 133 | final Bitmap bmp = photo_bmp; |
| 134 | view.post(new Runnable() |
| 135 | { |
| 136 | @Override |
| 137 | public void run() |
| 138 | { |
| 139 | } |
| 140 | }); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | public static class ContactElementAdapter extends CursorAdapter |
| 145 | { |
| 146 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
| 147 | |
| 148 | public ContactElementAdapter(Context context, Cursor c) |
| 149 | { |
| 150 | super(context, c, 0); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public View newView(Context context, Cursor cursor, ViewGroup parent) |
| 155 | { |
| 156 | LayoutInflater inflater = LayoutInflater.from(context); |
| 157 | View v = inflater.inflate(R.layout.call_element, parent, false); |
| 158 | bindView(v, context, cursor); |
| 159 | return v; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void bindView(final View view, Context context, Cursor cursor) |
| 164 | { |
| 165 | final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); |
| 166 | final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)); |
| 167 | // final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID)); |
| 168 | // final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI)); |
| 169 | |
| 170 | TextView display_name_txt = (TextView) view.findViewById(R.id.display_name); |
| 171 | display_name_txt.setText(display_name); |
| 172 | |
| 173 | ImageView photo_view = (ImageView) view.findViewById(R.id.photo); |
| 174 | photo_view.setVisibility(View.GONE); |
| 175 | |
| 176 | infos_fetcher.execute(new InfosLoader(context, view, contact_id)); |
| 177 | } |
| 178 | }; |
| 179 | |
Alexandre Savard | 6c0584e | 2012-09-20 09:42:59 -0400 | [diff] [blame] | 180 | public ContactListFragment() |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 181 | { |
| 182 | super(); |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 183 | mContext = getActivity(); |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 186 | @Override |
| 187 | public void onActivityCreated(Bundle savedInstanceState) |
| 188 | { |
| 189 | super.onActivityCreated(savedInstanceState); |
| 190 | |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 191 | // In order to onCreateOptionsMenu be called |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 192 | setHasOptionsMenu(true); |
| 193 | |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 194 | mAdapter = new ContactElementAdapter(getActivity(), null); |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 195 | setListAdapter(mAdapter); |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 196 | |
| 197 | getLoaderManager().initLoader(0, null, this); |
Alexandre Savard | 3143399 | 2012-09-20 15:19:49 -0400 | [diff] [blame] | 198 | |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 199 | final Context context = getActivity(); |
Alexandre Savard | 3143399 | 2012-09-20 15:19:49 -0400 | [diff] [blame] | 200 | ListView lv = getListView(); |
| 201 | lv.setOnItemLongClickListener(new OnItemLongClickListener() { |
| 202 | @Override |
| 203 | public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) { |
| 204 | Log.i(TAG, "On Long Click"); |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 205 | final CharSequence[] items = {"Make Call", "Send Message", "Add to Conference"}; |
| 206 | final SipCall.CallInfo info = new SipCall.CallInfo(); |
| 207 | info.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText(); |
| 208 | info.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText(); |
| 209 | final SipCall call = SipCall.getCallInstance(info); |
| 210 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 211 | builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName) |
| 212 | .setCancelable(true) |
| 213 | .setItems(items, new DialogInterface.OnClickListener() { |
| 214 | public void onClick(DialogInterface dialog, int item) { |
| 215 | Log.i(TAG, "Selected " + items[item]); |
| 216 | switch (item) { |
| 217 | case 0: |
| 218 | call.placeCall(); |
| 219 | break; |
| 220 | case 1: |
| 221 | call.sendTextMessage(); |
| 222 | // Need to hangup this call immediately since no way to do it after this action |
| 223 | call.hangup(); |
| 224 | break; |
| 225 | case 2: |
| 226 | call.addToConference(); |
| 227 | // Need to hangup this call immediately since no way to do it after this action |
| 228 | call.hangup(); |
| 229 | break; |
| 230 | default: |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | }); |
| 235 | AlertDialog alert = builder.create(); |
| 236 | alert.show(); |
| 237 | |
Alexandre Savard | 3143399 | 2012-09-20 15:19:49 -0400 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | }); |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) |
| 245 | { |
| 246 | return inflater.inflate(R.layout.call_element_list, container, false); |
| 247 | } |
| 248 | |
| 249 | @Override |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 250 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) |
| 251 | { |
| 252 | // Place an action bar item for searching |
| 253 | MenuItem item = menu.add("Search"); |
| 254 | item.setIcon(R.drawable.ic_menu_search); |
| 255 | item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); |
| 256 | SearchView sv = new SearchView(getActivity()); |
| 257 | sv.setOnQueryTextListener(this); |
| 258 | item.setActionView(sv); |
| 259 | } |
| 260 | |
| 261 | @Override |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 262 | public void onListItemClick(ListView l, View v, int position, long id) |
| 263 | { |
| 264 | // Insert desired behavior here. |
Alexandre Savard | 7a46f54 | 2012-09-20 11:23:33 -0400 | [diff] [blame] | 265 | SipCall.CallInfo callInfo = new SipCall.CallInfo(); |
| 266 | callInfo.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText(); |
| 267 | callInfo.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText(); |
| 268 | Log.i(TAG, "Contact clicked: " + callInfo.mDisplayName + ", Phone number: " + callInfo.mPhone); |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 269 | |
| 270 | int nbCallBefore = SipCall.getNbCalls(); |
Alexandre Savard | 7a46f54 | 2012-09-20 11:23:33 -0400 | [diff] [blame] | 271 | SipCall call = SipCall.getCallInstance(callInfo); |
| 272 | Log.i(TAG, "Number of calls " + SipCall.getNbCalls()); |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 273 | int nbCallAfter = SipCall.getNbCalls(); |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 274 | |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 275 | if(nbCallAfter > nbCallBefore) |
| 276 | call.placeCall(); |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public boolean onQueryTextChange(String newText) |
| 281 | { |
| 282 | // Called when the action bar search text has changed. Update |
| 283 | // the search filter, and restart the loader to do a new query |
| 284 | // with this filter. |
| 285 | String newFilter = !TextUtils.isEmpty(newText) ? newText : null; |
| 286 | // Don't do anything if the filter hasn't actually changed. |
| 287 | // Prefents restarting the loader when restoring state. |
| 288 | if (mCurFilter == null && newFilter == null) { return true; } |
| 289 | if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; } |
| 290 | mCurFilter = newFilter; |
| 291 | getLoaderManager().restartLoader(0, null, this); |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public boolean onQueryTextSubmit(String query) |
| 297 | { |
| 298 | // Return false to let the SearchView perform the default action |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public Loader<Cursor> onCreateLoader(int id, Bundle args) |
| 304 | { |
| 305 | Uri baseUri; |
| 306 | |
| 307 | if(mCurFilter != null) { |
| 308 | baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter)); |
| 309 | } else { |
| 310 | baseUri = Contacts.CONTENT_URI; |
| 311 | } |
| 312 | |
| 313 | // Now create and return a CursorLoader that will take care of |
| 314 | // creating a Cursor for the data being displayed. |
| 315 | String select = "((" + Contacts.DISPLAY_NAME |
| 316 | + " NOTNULL) AND (" |
| 317 | + Contacts.HAS_PHONE_NUMBER |
| 318 | + "=1) AND (" |
| 319 | + Contacts.DISPLAY_NAME |
| 320 | + " != '' ))"; |
| 321 | |
| 322 | return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION, |
| 323 | select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) |
| 328 | { |
| 329 | // Swap the new cursor in. |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 330 | mAdapter.swapCursor(data); |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void onLoaderReset(Loader<Cursor> loader) |
| 335 | { |
| 336 | // Thi is called when the last Cursor provided to onLoadFinished |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 337 | mAdapter.swapCursor(null); |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 338 | } |
| 339 | } |