blob: cf27f0a392e41eb5b08bbe9ca9dd72852dc801fa [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;
Adrien Béraudffd32412012-08-07 18:39:23 -040070
71/**
72 * Main list of Call Elements.
73 * We don't manage contacts ourself so they are
74 */
Alexandre Savard9b742d52012-09-20 13:57:44 -040075public class CallElementList extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
Adrien Béraudffd32412012-08-07 18:39:23 -040076{
Alexandre Savardd1976532012-10-26 15:32:45 -040077 private static final String TAG = "CallElementList";
78 private static final String CURRENT_STATE_LABEL = " CURRENT STATE: ";
Alexandre Savardf17e3172012-10-25 16:09:09 -040079 private ContactManager mContactManager;
Alexandre Savardd1976532012-10-26 15:32:45 -040080 private CallElementAdapter mAdapter;
Alexandre Savardf17e3172012-10-25 16:09:09 -040081 private String mCurFilter;
82 private SFLPhoneHome sflphoneHome;
Alexandre Savard817dc502012-10-22 11:47:29 -040083 private ISipService service;
Adrien Béraudffd32412012-08-07 18:39:23 -040084
Alexandre Savarda1404652012-09-20 13:34:08 -040085 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
86 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
87 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
88 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Adrien Béraudffd32412012-08-07 18:39:23 -040089
Alexandre Savarda1404652012-09-20 13:34:08 -040090 /**
91 * Runnable that fill information in a contact card asynchroniously.
92 */
93 public static class InfosLoader implements Runnable
94 {
95 private View view;
96 private long cid;
97 private ContentResolver cr;
Adrien Béraudffd32412012-08-07 18:39:23 -040098
Alexandre Savarda1404652012-09-20 13:34:08 -040099 public InfosLoader(Context context, View element, long contact_id)
Alexandre Savard38d6a152012-09-18 13:33:02 -0400100 {
Alexandre Savarda1404652012-09-20 13:34:08 -0400101 cid = contact_id;
102 cr = context.getContentResolver();
103 view = element;
Alexandre Savard38d6a152012-09-18 13:33:02 -0400104 }
Alexandre Savarda1404652012-09-20 13:34:08 -0400105
106 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
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,
122 CommonDataKinds.Phone.CONTACT_ID + " = ?",
123 new String[] { Long.toString(cid) }, 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 // TODO: same for SIP adresses.
133
134 final Bitmap bmp = photo_bmp;
135 view.post(new Runnable()
136 {
137 @Override
138 public void run()
139 {
140 /*
141 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
142 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
143
144 if (photo_bmp != null) {
145 photo_view.setImageBitmap(bmp);
146 photo_view.setVisibility(View.VISIBLE);
147 } else {
148 photo_view.setVisibility(View.GONE);
149 }
150
151 if (numbers.size() > 0) {
152 String phonestxt = numbers.get(0);
153 for (int i = 1, n = numbers.size(); i < n; i++)
154 phonestxt += "\n" + numbers.get(i);
155 phones_txt.setText(phonestxt);
156 phones_txt.setVisibility(View.VISIBLE);
157 } else
158 phones_txt.setVisibility(View.GONE);
159 */
160 }
161 });
162 }
163 }
164
165 /**
166 * A CursorAdapter that creates and update call elements using corresponding contact infos.
167 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
168 */
169 public static class CallElementAdapter extends ArrayAdapter
170 {
171 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
172 private Context mContext;
173 private final List mCallList;
174
175 public CallElementAdapter(Context context, List callList)
176 {
177 super(context, R.layout.call_element, callList);
178 mContext = context;
179 mCallList = callList;
180 }
181
182 @Override
183 public View getView(int position, View convertView, ViewGroup parent)
184 {
185 View rowView = convertView;
186 CallElementView entryView = null;
187
188 if(rowView == null)
189 {
190 // Get a new instance of the row layout view
191 LayoutInflater inflater = LayoutInflater.from(mContext);
192 rowView = inflater.inflate(R.layout.call_element, null);
193
194 // Hold the view objects in an object
195 // so they don't need to be re-fetched
196 entryView = new CallElementView();
197 entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
198 entryView.button = (Button) rowView.findViewById(R.id.button2);
199 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
200 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
201 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
Alexandre Savardd1976532012-10-26 15:32:45 -0400202 entryView.state = (TextView) rowView.findViewById(R.id.callstate);
Alexandre Savarda1404652012-09-20 13:34:08 -0400203
204 // Cache the view obects in the tag
205 // so they can be re-accessed later
206 rowView.setTag(entryView);
207 } else {
208 entryView = (CallElementView) rowView.getTag();
209 }
210
211 // Transfer the stock data from the data object
212 // to the view objects
213 SipCall call = (SipCall) mCallList.get(position);
Alexandre Savardd1976532012-10-26 15:32:45 -0400214 call.setAssociatedRowView(rowView);
215 entryView.displayName.setText(call.getDisplayName());
216 entryView.phones.setText(call.getPhone());
217 entryView.state.setText(CURRENT_STATE_LABEL + call.getCallStateString());
Alexandre Savarda1404652012-09-20 13:34:08 -0400218
219 return rowView;
220 }
221 };
222
223 public static class CallElementView
224 {
225 protected ImageButton toggleButton;
226 protected Button button;
227 protected ImageView photo;
228 protected TextView displayName;
Alexandre Savardd1976532012-10-26 15:32:45 -0400229 protected TextView phones;
230 protected TextView state;
Alexandre Savarda1404652012-09-20 13:34:08 -0400231 }
232
Alexandre Savardf17e3172012-10-25 16:09:09 -0400233 public CallElementList(ISipService s, SFLPhoneHome home)
Alexandre Savard817dc502012-10-22 11:47:29 -0400234 {
235 super();
236 service = s;
Alexandre Savardf17e3172012-10-25 16:09:09 -0400237 sflphoneHome = home;
Alexandre Savard817dc502012-10-22 11:47:29 -0400238 }
239
240 public void setService(ISipService s)
241 {
242 service = s;
243 }
244
Alexandre Savarda1404652012-09-20 13:34:08 -0400245 public void addCall(SipCall c)
246 {
247 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
248 mAdapter.add(c);
249 }
250
Alexandre Savard9b742d52012-09-20 13:57:44 -0400251 public void removeCall(SipCall c)
252 {
253 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
254 mAdapter.remove(c);
255 }
256
257
258 @Override
259 public void onActivityCreated(Bundle savedInstanceState)
260 {
261 super.onActivityCreated(savedInstanceState);
262
263 // Give some text to display if there is no data. In a real
264 // application this would come from a resource.
265 //setEmptyText("No phone numbers");
266
267 // We have a menu item to show in action bar.
268 setHasOptionsMenu(true);
269
270
271 // Create an empty adapter we will use to display the loaded data.
Alexandre Savardd1976532012-10-26 15:32:45 -0400272 ArrayList calls = new ArrayList();
Alexandre Savard9b742d52012-09-20 13:57:44 -0400273 mAdapter = new CallElementAdapter(getActivity(), calls);
274 setListAdapter(mAdapter);
275
276 // Start out with a progress indicator.
277 //setListShown(false);
278
279 // Prepare the loader. Either re-connect with an existing one,
280 // or start a new one.
281 // getLoaderManager().initLoader(0, null, this)
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400282
283 final Context context = getActivity();
284 ListView lv = getListView();
285 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
286 @Override
287 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
288 Log.i(TAG, "On Long Click");
289 final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"};
290 final SipCall call = (SipCall) mAdapter.getItem(pos);
291 AlertDialog.Builder builder = new AlertDialog.Builder(context);
292 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
293 .setCancelable(true)
294 .setItems(items, new DialogInterface.OnClickListener() {
295 public void onClick(DialogInterface dialog, int item) {
296 Log.i(TAG, "Selected " + items[item]);
297 switch (item) {
298 case 0:
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400299 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400300 break;
301 case 1:
302 call.sendTextMessage();
303 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400304 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400305 break;
306 case 2:
307 call.addToConference();
308 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400309 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400310 break;
311 default:
312 break;
313 }
314 }
315 });
316 AlertDialog alert = builder.create();
317 alert.show();
318
319 return true;
320 }
321 });
Alexandre Savard9b742d52012-09-20 13:57:44 -0400322 }
323
324 @Override
325 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
326 //LayoutInflater newInflater = inflater.cloneInContext(new ContextThemeWrapper(getActivity(), R.style.));
327 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
Alexandre Savard817dc502012-10-22 11:47:29 -0400328
329 Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button);
330 accountSelectionButton.setOnClickListener(new View.OnClickListener() {
331 public void onClick(View v) {
332 /*
333 ArrayList<String> list = new ArrayList<String>();
334 list.add("IP2IP");
335 list.add("SFL-181");
336 AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list);
337 accountSelectionDialog.show();
338 */
339 try {
340 if(service != null) {
341 ArrayList<String> list = (ArrayList<String>)service.getAccountList();
342 if(list != null) {
343 AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list);
344 accountSelectionDialog.show();
345 }
346 else { Log.i(TAG, "Could not get account list"); }
347 }
348 else { Log.i(TAG, "Could not get service"); }
349 }
350 catch (RemoteException e) {
351 Log.e(TAG, "Remote exception", e);
352 }
353 }
354 });
355
Alexandre Savard9b742d52012-09-20 13:57:44 -0400356 return inflatedView;
357 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400358
Alexandre Savard9b742d52012-09-20 13:57:44 -0400359 public void onListItemClick(ListView l, View v, int position, long id)
360 {
361 // Insert desired behavior here.
Alexandre Savard6b7f4182012-09-20 14:10:42 -0400362 SipCall call = (SipCall) mAdapter.getItem(position);
Alexandre Savarddf544262012-10-25 14:24:08 -0400363 Log.i(TAG, "Call Clicked: " + call.getCallId());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400364
Alexandre Savarddf544262012-10-25 14:24:08 -0400365 call.launchCallActivity(getActivity());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400366
367 sflphoneHome.onSelectedCallAction(call);
Alexandre Savard9b742d52012-09-20 13:57:44 -0400368 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400369
370 @Override
371 public Loader<Cursor> onCreateLoader(int id, Bundle args)
372 {
373
374 //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
375
376 // This is called when a new Loader needs to be created. This
377 // sample only has one Loader, so we don't care about the ID.
378 // First, pick the base URI to use depending on whether we are
379 // currently filtering.
380 Uri baseUri;
381
382 if (mCurFilter != null) {
383 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
384 } else {
385 baseUri = Contacts.CONTENT_URI;
386 }
387
388 // Now create and return a CursorLoader that will take care of
389 // creating a Cursor for the data being displayed.
390 String select = "((" + Contacts.DISPLAY_NAME
391 + " NOTNULL) AND ("
392 + Contacts.HAS_PHONE_NUMBER
393 + "=1) AND ("
394 + Contacts.DISPLAY_NAME
395 + " != '' ))";
396 //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
397
398 return new CursorLoader(getActivity(),
399 baseUri,
400 CONTACTS_SUMMARY_PROJECTION,
401 select,
402 null,
403 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
404 }
405
406 @Override
407 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
408 {
409 // Swap the new cursor in. (The framework will take care of closing the
410 // old cursor once we return.)
Alexandre Savarda1404652012-09-20 13:34:08 -0400411 // mAdapter.swapCursor(data);
Adrien Béraudffd32412012-08-07 18:39:23 -0400412
413 // The list should now be shown.
414 /*
415 if (isResumed()) {
416 setListShown(true);
417 } else {
418 setListShownNoAnimation(true);
419 }*/
420 }
421
422 @Override
423 public void onLoaderReset(Loader<Cursor> loader)
424 {
425 // This is called when the last Cursor provided to onLoadFinished()
426 // above is about to be closed. We need to make sure we are no
427 // longer using it.
Alexandre Savarda1404652012-09-20 13:34:08 -0400428 // mAdapter.swapCursor(null);
Adrien Béraudffd32412012-08-07 18:39:23 -0400429 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400430}