blob: cb2f24979f23bc79bfb955900b3a7255f68594e0 [file] [log] [blame]
Adrien Béraudffd32412012-08-07 18:39:23 -04001/*
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 */
31package com.savoirfairelinux.sflphone.client;
32
33import java.io.InputStream;
34import java.util.ArrayList;
35import java.util.List;
36import java.util.concurrent.ExecutorService;
37import java.util.concurrent.Executors;
38
39import android.app.ListFragment;
40import android.app.LoaderManager;
Alexandre Savardf1e32cf2012-09-20 17:45:16 -040041import android.app.AlertDialog;
42import android.app.AlertDialog.Builder;
Adrien Béraudffd32412012-08-07 18:39:23 -040043import android.content.ContentResolver;
44import android.content.ContentUris;
45import android.content.Context;
46import android.content.CursorLoader;
Alexandre Savardf1e32cf2012-09-20 17:45:16 -040047import android.content.DialogInterface;
Adrien Béraudffd32412012-08-07 18:39:23 -040048import android.content.Loader;
49import android.database.Cursor;
50import android.graphics.Bitmap;
51import android.graphics.BitmapFactory;
52import android.net.Uri;
53import android.os.Bundle;
Alexandre Savard817dc502012-10-22 11:47:29 -040054import android.os.RemoteException;
Adrien Béraudffd32412012-08-07 18:39:23 -040055import android.provider.*;
56import android.provider.ContactsContract.CommonDataKinds;
57import android.provider.ContactsContract.CommonDataKinds.Phone;
58import android.provider.ContactsContract.CommonDataKinds.SipAddress;
59import android.provider.ContactsContract.Contacts;
Adrien Béraudffd32412012-08-07 18:39:23 -040060import android.util.Log;
61import android.view.*;
Alexandre Savardf1e32cf2012-09-20 17:45:16 -040062import android.widget.AdapterView.OnItemLongClickListener;
63import android.widget.AdapterView.OnItemSelectedListener;
Adrien Béraudffd32412012-08-07 18:39:23 -040064import android.widget.*;
Alexandre Savard38d6a152012-09-18 13:33:02 -040065import java.util.List;
66import java.util.ArrayList;
Adrien Béraudffd32412012-08-07 18:39:23 -040067
68import com.savoirfairelinux.sflphone.R;
Alexandre Savard817dc502012-10-22 11:47:29 -040069import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savard35d208a2012-10-31 15:15:06 -040070import com.savoirfairelinux.sflphone.utils.AccountSelectionButton;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040071import com.savoirfairelinux.sflphone.utils.AccountList;
Adrien Béraudffd32412012-08-07 18:39:23 -040072
73/**
74 * Main list of Call Elements.
75 * We don't manage contacts ourself so they are
76 */
Alexandre Savard9b742d52012-09-20 13:57:44 -040077public class CallElementList extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
Adrien Béraudffd32412012-08-07 18:39:23 -040078{
Alexandre Savardd1976532012-10-26 15:32:45 -040079 private static final String TAG = "CallElementList";
80 private static final String CURRENT_STATE_LABEL = " CURRENT STATE: ";
Alexandre Savardf17e3172012-10-25 16:09:09 -040081 private ContactManager mContactManager;
Alexandre Savardd1976532012-10-26 15:32:45 -040082 private CallElementAdapter mAdapter;
Alexandre Savardf17e3172012-10-25 16:09:09 -040083 private String mCurFilter;
84 private SFLPhoneHome sflphoneHome;
Alexandre Savard817dc502012-10-22 11:47:29 -040085 private ISipService service;
Alexandre Savard35d208a2012-10-31 15:15:06 -040086 private AccountSelectionButton mAccountSelectionButton;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040087 private AccountList mAccountList;
Adrien Béraudffd32412012-08-07 18:39:23 -040088
Alexandre Savarda1404652012-09-20 13:34:08 -040089 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
90 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
91 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
92 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Adrien Béraudffd32412012-08-07 18:39:23 -040093
Alexandre Savard60f0f1c2012-11-01 09:40:34 -040094 public String getSelectedAccount() {
95 return mAccountSelectionButton.getText().toString();
96 }
97
Alexandre Savarda1404652012-09-20 13:34:08 -040098 /**
99 * Runnable that fill information in a contact card asynchroniously.
100 */
101 public static class InfosLoader implements Runnable
102 {
103 private View view;
104 private long cid;
105 private ContentResolver cr;
Adrien Béraudffd32412012-08-07 18:39:23 -0400106
Alexandre Savarda1404652012-09-20 13:34:08 -0400107 public InfosLoader(Context context, View element, long contact_id)
Alexandre Savard38d6a152012-09-18 13:33:02 -0400108 {
Alexandre Savarda1404652012-09-20 13:34:08 -0400109 cid = contact_id;
110 cr = context.getContentResolver();
111 view = element;
Alexandre Savard38d6a152012-09-18 13:33:02 -0400112 }
Alexandre Savarda1404652012-09-20 13:34:08 -0400113
114 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
115 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
116 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
117 if (input == null) {
118 return null;
119 }
120 return BitmapFactory.decodeStream(input);
121 }
122
123 @Override
124 public void run()
125 {
126 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
127
128 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
129 CONTACTS_PHONES_PROJECTION,
130 CommonDataKinds.Phone.CONTACT_ID + " = ?",
131 new String[] { Long.toString(cid) }, null);
132
133 final List<String> numbers = new ArrayList<String>();
134 while (phones.moveToNext()) {
135 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
136 //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
137 numbers.add(number);
138 }
139 phones.close();
140 // TODO: same for SIP adresses.
141
142 final Bitmap bmp = photo_bmp;
143 view.post(new Runnable()
144 {
145 @Override
146 public void run()
147 {
148 /*
149 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
150 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
151
152 if (photo_bmp != null) {
153 photo_view.setImageBitmap(bmp);
154 photo_view.setVisibility(View.VISIBLE);
155 } else {
156 photo_view.setVisibility(View.GONE);
157 }
158
159 if (numbers.size() > 0) {
160 String phonestxt = numbers.get(0);
161 for (int i = 1, n = numbers.size(); i < n; i++)
162 phonestxt += "\n" + numbers.get(i);
163 phones_txt.setText(phonestxt);
164 phones_txt.setVisibility(View.VISIBLE);
165 } else
166 phones_txt.setVisibility(View.GONE);
167 */
168 }
169 });
170 }
171 }
172
173 /**
174 * A CursorAdapter that creates and update call elements using corresponding contact infos.
175 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
176 */
177 public static class CallElementAdapter extends ArrayAdapter
178 {
179 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
180 private Context mContext;
181 private final List mCallList;
182
183 public CallElementAdapter(Context context, List callList)
184 {
185 super(context, R.layout.call_element, callList);
186 mContext = context;
187 mCallList = callList;
188 }
189
190 @Override
191 public View getView(int position, View convertView, ViewGroup parent)
192 {
193 View rowView = convertView;
194 CallElementView entryView = null;
195
196 if(rowView == null)
197 {
198 // Get a new instance of the row layout view
199 LayoutInflater inflater = LayoutInflater.from(mContext);
200 rowView = inflater.inflate(R.layout.call_element, null);
201
202 // Hold the view objects in an object
203 // so they don't need to be re-fetched
204 entryView = new CallElementView();
205 entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
206 entryView.button = (Button) rowView.findViewById(R.id.button2);
207 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
208 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
209 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
Alexandre Savardd1976532012-10-26 15:32:45 -0400210 entryView.state = (TextView) rowView.findViewById(R.id.callstate);
Alexandre Savarda1404652012-09-20 13:34:08 -0400211
212 // Cache the view obects in the tag
213 // so they can be re-accessed later
214 rowView.setTag(entryView);
215 } else {
216 entryView = (CallElementView) rowView.getTag();
217 }
218
219 // Transfer the stock data from the data object
220 // to the view objects
221 SipCall call = (SipCall) mCallList.get(position);
Alexandre Savardd1976532012-10-26 15:32:45 -0400222 call.setAssociatedRowView(rowView);
223 entryView.displayName.setText(call.getDisplayName());
224 entryView.phones.setText(call.getPhone());
225 entryView.state.setText(CURRENT_STATE_LABEL + call.getCallStateString());
Alexandre Savarda1404652012-09-20 13:34:08 -0400226
227 return rowView;
228 }
229 };
230
231 public static class CallElementView
232 {
233 protected ImageButton toggleButton;
234 protected Button button;
235 protected ImageView photo;
236 protected TextView displayName;
Alexandre Savardd1976532012-10-26 15:32:45 -0400237 protected TextView phones;
238 protected TextView state;
Alexandre Savarda1404652012-09-20 13:34:08 -0400239 }
240
Alexandre Savardf17e3172012-10-25 16:09:09 -0400241 public CallElementList(ISipService s, SFLPhoneHome home)
Alexandre Savard817dc502012-10-22 11:47:29 -0400242 {
243 super();
244 service = s;
Alexandre Savardf17e3172012-10-25 16:09:09 -0400245 sflphoneHome = home;
Alexandre Savard817dc502012-10-22 11:47:29 -0400246 }
247
248 public void setService(ISipService s)
249 {
250 service = s;
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400251 }
252
253 public void setAccountList(AccountList accountList) {
254 mAccountList = accountList;
Alexandre Savard817dc502012-10-22 11:47:29 -0400255 }
256
Alexandre Savarda1404652012-09-20 13:34:08 -0400257 public void addCall(SipCall c)
258 {
259 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
260 mAdapter.add(c);
261 }
262
Alexandre Savard9b742d52012-09-20 13:57:44 -0400263 public void removeCall(SipCall c)
264 {
265 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
266 mAdapter.remove(c);
267 }
268
269
270 @Override
271 public void onActivityCreated(Bundle savedInstanceState)
272 {
273 super.onActivityCreated(savedInstanceState);
274
275 // Give some text to display if there is no data. In a real
276 // application this would come from a resource.
277 //setEmptyText("No phone numbers");
278
279 // We have a menu item to show in action bar.
280 setHasOptionsMenu(true);
281
282
283 // Create an empty adapter we will use to display the loaded data.
Alexandre Savardd1976532012-10-26 15:32:45 -0400284 ArrayList calls = new ArrayList();
Alexandre Savard9b742d52012-09-20 13:57:44 -0400285 mAdapter = new CallElementAdapter(getActivity(), calls);
286 setListAdapter(mAdapter);
287
288 // Start out with a progress indicator.
289 //setListShown(false);
290
291 // Prepare the loader. Either re-connect with an existing one,
292 // or start a new one.
293 // getLoaderManager().initLoader(0, null, this)
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400294
295 final Context context = getActivity();
296 ListView lv = getListView();
297 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
298 @Override
299 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
300 Log.i(TAG, "On Long Click");
301 final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"};
302 final SipCall call = (SipCall) mAdapter.getItem(pos);
303 AlertDialog.Builder builder = new AlertDialog.Builder(context);
304 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
305 .setCancelable(true)
306 .setItems(items, new DialogInterface.OnClickListener() {
307 public void onClick(DialogInterface dialog, int item) {
308 Log.i(TAG, "Selected " + items[item]);
309 switch (item) {
310 case 0:
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400311 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400312 break;
313 case 1:
314 call.sendTextMessage();
315 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400316 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400317 break;
318 case 2:
319 call.addToConference();
320 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400321 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400322 break;
323 default:
324 break;
325 }
326 }
327 });
328 AlertDialog alert = builder.create();
329 alert.show();
330
331 return true;
332 }
333 });
Alexandre Savard9b742d52012-09-20 13:57:44 -0400334 }
335
336 @Override
337 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Savard9b742d52012-09-20 13:57:44 -0400338 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
Alexandre Savard817dc502012-10-22 11:47:29 -0400339
Alexandre Savard35d208a2012-10-31 15:15:06 -0400340 mAccountSelectionButton = (AccountSelectionButton) inflatedView.findViewById(R.id.account_selection_button);
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400341 mAccountList.addManagementUI(mAccountSelectionButton);
Alexandre Savard817dc502012-10-22 11:47:29 -0400342
Alexandre Savard9b742d52012-09-20 13:57:44 -0400343 return inflatedView;
344 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400345
Alexandre Savard9b742d52012-09-20 13:57:44 -0400346 public void onListItemClick(ListView l, View v, int position, long id)
347 {
348 // Insert desired behavior here.
Alexandre Savard6b7f4182012-09-20 14:10:42 -0400349 SipCall call = (SipCall) mAdapter.getItem(position);
Alexandre Savarddf544262012-10-25 14:24:08 -0400350 Log.i(TAG, "Call Clicked: " + call.getCallId());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400351
Alexandre Savarddf544262012-10-25 14:24:08 -0400352 call.launchCallActivity(getActivity());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400353
354 sflphoneHome.onSelectedCallAction(call);
Alexandre Savard9b742d52012-09-20 13:57:44 -0400355 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400356
357 @Override
358 public Loader<Cursor> onCreateLoader(int id, Bundle args)
359 {
360
361 //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
362
363 // This is called when a new Loader needs to be created. This
364 // sample only has one Loader, so we don't care about the ID.
365 // First, pick the base URI to use depending on whether we are
366 // currently filtering.
367 Uri baseUri;
368
369 if (mCurFilter != null) {
370 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
371 } else {
372 baseUri = Contacts.CONTENT_URI;
373 }
374
375 // Now create and return a CursorLoader that will take care of
376 // creating a Cursor for the data being displayed.
377 String select = "((" + Contacts.DISPLAY_NAME
378 + " NOTNULL) AND ("
379 + Contacts.HAS_PHONE_NUMBER
380 + "=1) AND ("
381 + Contacts.DISPLAY_NAME
382 + " != '' ))";
383 //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
384
385 return new CursorLoader(getActivity(),
386 baseUri,
387 CONTACTS_SUMMARY_PROJECTION,
388 select,
389 null,
390 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
391 }
392
393 @Override
394 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
395 {
396 // Swap the new cursor in. (The framework will take care of closing the
397 // old cursor once we return.)
Alexandre Savarda1404652012-09-20 13:34:08 -0400398 // mAdapter.swapCursor(data);
Adrien Béraudffd32412012-08-07 18:39:23 -0400399
400 // The list should now be shown.
401 /*
402 if (isResumed()) {
403 setListShown(true);
404 } else {
405 setListShownNoAnimation(true);
406 }*/
407 }
408
409 @Override
410 public void onLoaderReset(Loader<Cursor> loader)
411 {
412 // This is called when the last Cursor provided to onLoadFinished()
413 // above is about to be closed. We need to make sure we are no
414 // longer using it.
Alexandre Savarda1404652012-09-20 13:34:08 -0400415 // mAdapter.swapCursor(null);
Adrien Béraudffd32412012-08-07 18:39:23 -0400416 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400417}