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; |
Alexandre Savard | f1e32cf | 2012-09-20 17:45:16 -0400 | [diff] [blame] | 41 | import android.app.AlertDialog; |
| 42 | import android.app.AlertDialog.Builder; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 43 | import android.content.ContentResolver; |
| 44 | import android.content.ContentUris; |
| 45 | import android.content.Context; |
| 46 | import android.content.CursorLoader; |
Alexandre Savard | f1e32cf | 2012-09-20 17:45:16 -0400 | [diff] [blame] | 47 | import android.content.DialogInterface; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 48 | import android.content.Loader; |
| 49 | import android.database.Cursor; |
| 50 | import android.graphics.Bitmap; |
| 51 | import android.graphics.BitmapFactory; |
| 52 | import android.net.Uri; |
| 53 | import android.os.Bundle; |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 54 | import android.os.RemoteException; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 55 | import android.provider.*; |
| 56 | import android.provider.ContactsContract.CommonDataKinds; |
| 57 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 58 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 59 | import android.provider.ContactsContract.Contacts; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 60 | import android.util.Log; |
| 61 | import android.view.*; |
Alexandre Savard | f1e32cf | 2012-09-20 17:45:16 -0400 | [diff] [blame] | 62 | import android.widget.AdapterView.OnItemLongClickListener; |
| 63 | import android.widget.AdapterView.OnItemSelectedListener; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 64 | import android.widget.*; |
Alexandre Savard | 38d6a15 | 2012-09-18 13:33:02 -0400 | [diff] [blame] | 65 | import java.util.List; |
| 66 | import java.util.ArrayList; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 67 | |
| 68 | import com.savoirfairelinux.sflphone.R; |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 69 | import com.savoirfairelinux.sflphone.service.ISipService; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Main list of Call Elements. |
| 73 | * We don't manage contacts ourself so they are |
| 74 | */ |
Alexandre Savard | 9b742d5 | 2012-09-20 13:57:44 -0400 | [diff] [blame] | 75 | public class CallElementList extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 76 | { |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 77 | final String TAG = "CallElementList"; |
| 78 | ContactManager mContactManager; |
| 79 | ArrayAdapter mAdapter; |
| 80 | String mCurFilter; |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 81 | private ISipService service; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 82 | |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 83 | static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, |
| 84 | Contacts.PHOTO_ID, Contacts.LOOKUP_KEY }; |
| 85 | static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE }; |
| 86 | static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE }; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 87 | |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 88 | /** |
| 89 | * Runnable that fill information in a contact card asynchroniously. |
| 90 | */ |
| 91 | public static class InfosLoader implements Runnable |
| 92 | { |
| 93 | private View view; |
| 94 | private long cid; |
| 95 | private ContentResolver cr; |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 96 | |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 97 | public InfosLoader(Context context, View element, long contact_id) |
Alexandre Savard | 38d6a15 | 2012-09-18 13:33:02 -0400 | [diff] [blame] | 98 | { |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 99 | cid = contact_id; |
| 100 | cr = context.getContentResolver(); |
| 101 | view = element; |
Alexandre Savard | 38d6a15 | 2012-09-18 13:33:02 -0400 | [diff] [blame] | 102 | } |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 103 | |
| 104 | public static Bitmap loadContactPhoto(ContentResolver cr, long id) { |
| 105 | Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); |
| 106 | InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); |
| 107 | if (input == null) { |
| 108 | return null; |
| 109 | } |
| 110 | return BitmapFactory.decodeStream(input); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void run() |
| 115 | { |
| 116 | final Bitmap photo_bmp = loadContactPhoto(cr, cid); |
| 117 | |
| 118 | Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI, |
| 119 | CONTACTS_PHONES_PROJECTION, |
| 120 | CommonDataKinds.Phone.CONTACT_ID + " = ?", |
| 121 | new String[] { Long.toString(cid) }, null); |
| 122 | |
| 123 | final List<String> numbers = new ArrayList<String>(); |
| 124 | while (phones.moveToNext()) { |
| 125 | String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER)); |
| 126 | //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE)); |
| 127 | numbers.add(number); |
| 128 | } |
| 129 | phones.close(); |
| 130 | // TODO: same for SIP adresses. |
| 131 | |
| 132 | final Bitmap bmp = photo_bmp; |
| 133 | view.post(new Runnable() |
| 134 | { |
| 135 | @Override |
| 136 | public void run() |
| 137 | { |
| 138 | /* |
| 139 | ImageView photo_view = (ImageView) view.findViewById(R.id.photo); |
| 140 | TextView phones_txt = (TextView) view.findViewById(R.id.phones); |
| 141 | |
| 142 | if (photo_bmp != null) { |
| 143 | photo_view.setImageBitmap(bmp); |
| 144 | photo_view.setVisibility(View.VISIBLE); |
| 145 | } else { |
| 146 | photo_view.setVisibility(View.GONE); |
| 147 | } |
| 148 | |
| 149 | if (numbers.size() > 0) { |
| 150 | String phonestxt = numbers.get(0); |
| 151 | for (int i = 1, n = numbers.size(); i < n; i++) |
| 152 | phonestxt += "\n" + numbers.get(i); |
| 153 | phones_txt.setText(phonestxt); |
| 154 | phones_txt.setVisibility(View.VISIBLE); |
| 155 | } else |
| 156 | phones_txt.setVisibility(View.GONE); |
| 157 | */ |
| 158 | } |
| 159 | }); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * A CursorAdapter that creates and update call elements using corresponding contact infos. |
| 165 | * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc. |
| 166 | */ |
| 167 | public static class CallElementAdapter extends ArrayAdapter |
| 168 | { |
| 169 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
| 170 | private Context mContext; |
| 171 | private final List mCallList; |
| 172 | |
| 173 | public CallElementAdapter(Context context, List callList) |
| 174 | { |
| 175 | super(context, R.layout.call_element, callList); |
| 176 | mContext = context; |
| 177 | mCallList = callList; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public View getView(int position, View convertView, ViewGroup parent) |
| 182 | { |
| 183 | View rowView = convertView; |
| 184 | CallElementView entryView = null; |
| 185 | |
| 186 | if(rowView == null) |
| 187 | { |
| 188 | // Get a new instance of the row layout view |
| 189 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 190 | rowView = inflater.inflate(R.layout.call_element, null); |
| 191 | |
| 192 | // Hold the view objects in an object |
| 193 | // so they don't need to be re-fetched |
| 194 | entryView = new CallElementView(); |
| 195 | entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1); |
| 196 | entryView.button = (Button) rowView.findViewById(R.id.button2); |
| 197 | entryView.photo = (ImageView) rowView.findViewById(R.id.photo); |
| 198 | entryView.displayName = (TextView) rowView.findViewById(R.id.display_name); |
| 199 | entryView.phones = (TextView) rowView.findViewById(R.id.phones); |
| 200 | |
| 201 | // Cache the view obects in the tag |
| 202 | // so they can be re-accessed later |
| 203 | rowView.setTag(entryView); |
| 204 | } else { |
| 205 | entryView = (CallElementView) rowView.getTag(); |
| 206 | } |
| 207 | |
| 208 | // Transfer the stock data from the data object |
| 209 | // to the view objects |
| 210 | SipCall call = (SipCall) mCallList.get(position); |
| 211 | entryView.displayName.setText(call.mCallInfo.mDisplayName); |
| 212 | entryView.phones.setText(call.mCallInfo.mPhone); |
| 213 | |
| 214 | return rowView; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | public static class CallElementView |
| 219 | { |
| 220 | protected ImageButton toggleButton; |
| 221 | protected Button button; |
| 222 | protected ImageView photo; |
| 223 | protected TextView displayName; |
| 224 | protected TextView phones; |
| 225 | } |
| 226 | |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 227 | public CallElementList(ISipService s) |
| 228 | { |
| 229 | super(); |
| 230 | service = s; |
| 231 | } |
| 232 | |
| 233 | public void setService(ISipService s) |
| 234 | { |
| 235 | service = s; |
| 236 | } |
| 237 | |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 238 | public void addCall(SipCall c) |
| 239 | { |
| 240 | Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName); |
| 241 | mAdapter.add(c); |
| 242 | } |
| 243 | |
Alexandre Savard | 9b742d5 | 2012-09-20 13:57:44 -0400 | [diff] [blame] | 244 | public void removeCall(SipCall c) |
| 245 | { |
| 246 | Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName); |
| 247 | mAdapter.remove(c); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | @Override |
| 252 | public void onActivityCreated(Bundle savedInstanceState) |
| 253 | { |
| 254 | super.onActivityCreated(savedInstanceState); |
| 255 | |
| 256 | // Give some text to display if there is no data. In a real |
| 257 | // application this would come from a resource. |
| 258 | //setEmptyText("No phone numbers"); |
| 259 | |
| 260 | // We have a menu item to show in action bar. |
| 261 | setHasOptionsMenu(true); |
| 262 | |
| 263 | |
| 264 | // Create an empty adapter we will use to display the loaded data. |
| 265 | List calls = new ArrayList(); |
| 266 | mAdapter = new CallElementAdapter(getActivity(), calls); |
| 267 | setListAdapter(mAdapter); |
| 268 | |
| 269 | // Start out with a progress indicator. |
| 270 | //setListShown(false); |
| 271 | |
| 272 | // Prepare the loader. Either re-connect with an existing one, |
| 273 | // or start a new one. |
| 274 | // getLoaderManager().initLoader(0, null, this) |
Alexandre Savard | f1e32cf | 2012-09-20 17:45:16 -0400 | [diff] [blame] | 275 | |
| 276 | final Context context = getActivity(); |
| 277 | ListView lv = getListView(); |
| 278 | lv.setOnItemLongClickListener(new OnItemLongClickListener() { |
| 279 | @Override |
| 280 | public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) { |
| 281 | Log.i(TAG, "On Long Click"); |
| 282 | final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"}; |
| 283 | final SipCall call = (SipCall) mAdapter.getItem(pos); |
| 284 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 285 | builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName) |
| 286 | .setCancelable(true) |
| 287 | .setItems(items, new DialogInterface.OnClickListener() { |
| 288 | public void onClick(DialogInterface dialog, int item) { |
| 289 | Log.i(TAG, "Selected " + items[item]); |
| 290 | switch (item) { |
| 291 | case 0: |
Alexandre Savard | 4f42ade | 2012-10-24 18:03:31 -0400 | [diff] [blame^] | 292 | call.notifyServiceHangup(service); |
Alexandre Savard | f1e32cf | 2012-09-20 17:45:16 -0400 | [diff] [blame] | 293 | break; |
| 294 | case 1: |
| 295 | call.sendTextMessage(); |
| 296 | // Need to hangup this call immediately since no way to do it after this action |
Alexandre Savard | 4f42ade | 2012-10-24 18:03:31 -0400 | [diff] [blame^] | 297 | call.notifyServiceHangup(service); |
Alexandre Savard | f1e32cf | 2012-09-20 17:45:16 -0400 | [diff] [blame] | 298 | break; |
| 299 | case 2: |
| 300 | call.addToConference(); |
| 301 | // Need to hangup this call immediately since no way to do it after this action |
Alexandre Savard | 4f42ade | 2012-10-24 18:03:31 -0400 | [diff] [blame^] | 302 | call.notifyServiceHangup(service); |
Alexandre Savard | f1e32cf | 2012-09-20 17:45:16 -0400 | [diff] [blame] | 303 | break; |
| 304 | default: |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | }); |
| 309 | AlertDialog alert = builder.create(); |
| 310 | alert.show(); |
| 311 | |
| 312 | return true; |
| 313 | } |
| 314 | }); |
Alexandre Savard | 9b742d5 | 2012-09-20 13:57:44 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 319 | //LayoutInflater newInflater = inflater.cloneInContext(new ContextThemeWrapper(getActivity(), R.style.)); |
| 320 | View inflatedView = inflater.inflate(R.layout.call_element_list, container, false); |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 321 | |
| 322 | Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button); |
| 323 | accountSelectionButton.setOnClickListener(new View.OnClickListener() { |
| 324 | public void onClick(View v) { |
| 325 | /* |
| 326 | ArrayList<String> list = new ArrayList<String>(); |
| 327 | list.add("IP2IP"); |
| 328 | list.add("SFL-181"); |
| 329 | AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list); |
| 330 | accountSelectionDialog.show(); |
| 331 | */ |
| 332 | try { |
| 333 | if(service != null) { |
| 334 | ArrayList<String> list = (ArrayList<String>)service.getAccountList(); |
| 335 | if(list != null) { |
| 336 | AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list); |
| 337 | accountSelectionDialog.show(); |
| 338 | } |
| 339 | else { Log.i(TAG, "Could not get account list"); } |
| 340 | } |
| 341 | else { Log.i(TAG, "Could not get service"); } |
| 342 | } |
| 343 | catch (RemoteException e) { |
| 344 | Log.e(TAG, "Remote exception", e); |
| 345 | } |
| 346 | } |
| 347 | }); |
| 348 | |
Alexandre Savard | 9b742d5 | 2012-09-20 13:57:44 -0400 | [diff] [blame] | 349 | return inflatedView; |
| 350 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 351 | |
Alexandre Savard | 9b742d5 | 2012-09-20 13:57:44 -0400 | [diff] [blame] | 352 | public void onListItemClick(ListView l, View v, int position, long id) |
| 353 | { |
| 354 | // Insert desired behavior here. |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 355 | Log.i(TAG, "Item clicked: " + id); |
Alexandre Savard | 6b7f418 | 2012-09-20 14:10:42 -0400 | [diff] [blame] | 356 | SipCall call = (SipCall) mAdapter.getItem(position); |
Alexandre Savard | 4f42ade | 2012-10-24 18:03:31 -0400 | [diff] [blame^] | 357 | call.notifyServiceHangup(service); |
Alexandre Savard | 9b742d5 | 2012-09-20 13:57:44 -0400 | [diff] [blame] | 358 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 359 | |
| 360 | @Override |
| 361 | public Loader<Cursor> onCreateLoader(int id, Bundle args) |
| 362 | { |
| 363 | |
| 364 | //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); |
| 365 | |
| 366 | // This is called when a new Loader needs to be created. This |
| 367 | // sample only has one Loader, so we don't care about the ID. |
| 368 | // First, pick the base URI to use depending on whether we are |
| 369 | // currently filtering. |
| 370 | Uri baseUri; |
| 371 | |
| 372 | if (mCurFilter != null) { |
| 373 | baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter)); |
| 374 | } else { |
| 375 | baseUri = Contacts.CONTENT_URI; |
| 376 | } |
| 377 | |
| 378 | // Now create and return a CursorLoader that will take care of |
| 379 | // creating a Cursor for the data being displayed. |
| 380 | String select = "((" + Contacts.DISPLAY_NAME |
| 381 | + " NOTNULL) AND (" |
| 382 | + Contacts.HAS_PHONE_NUMBER |
| 383 | + "=1) AND (" |
| 384 | + Contacts.DISPLAY_NAME |
| 385 | + " != '' ))"; |
| 386 | //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))"; |
| 387 | |
| 388 | return new CursorLoader(getActivity(), |
| 389 | baseUri, |
| 390 | CONTACTS_SUMMARY_PROJECTION, |
| 391 | select, |
| 392 | null, |
| 393 | Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) |
| 398 | { |
| 399 | // Swap the new cursor in. (The framework will take care of closing the |
| 400 | // old cursor once we return.) |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 401 | // mAdapter.swapCursor(data); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 402 | |
| 403 | // The list should now be shown. |
| 404 | /* |
| 405 | if (isResumed()) { |
| 406 | setListShown(true); |
| 407 | } else { |
| 408 | setListShownNoAnimation(true); |
| 409 | }*/ |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void onLoaderReset(Loader<Cursor> loader) |
| 414 | { |
| 415 | // This is called when the last Cursor provided to onLoadFinished() |
| 416 | // above is about to be closed. We need to make sure we are no |
| 417 | // longer using it. |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 418 | // mAdapter.swapCursor(null); |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 419 | } |
Adrien Béraud | ffd3241 | 2012-08-07 18:39:23 -0400 | [diff] [blame] | 420 | } |