blob: e36882b80cb0ce240cb34cac9cd9dba4f3b02a71 [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;
Adrien Béraudffd32412012-08-07 18:39:23 -040071
72/**
73 * Main list of Call Elements.
74 * We don't manage contacts ourself so they are
75 */
Alexandre Savard9b742d52012-09-20 13:57:44 -040076public class CallElementList extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
Adrien Béraudffd32412012-08-07 18:39:23 -040077{
Alexandre Savardd1976532012-10-26 15:32:45 -040078 private static final String TAG = "CallElementList";
79 private static final String CURRENT_STATE_LABEL = " CURRENT STATE: ";
Alexandre Savardf17e3172012-10-25 16:09:09 -040080 private ContactManager mContactManager;
Alexandre Savardd1976532012-10-26 15:32:45 -040081 private CallElementAdapter mAdapter;
Alexandre Savardf17e3172012-10-25 16:09:09 -040082 private String mCurFilter;
83 private SFLPhoneHome sflphoneHome;
Alexandre Savard817dc502012-10-22 11:47:29 -040084 private ISipService service;
Alexandre Savard35d208a2012-10-31 15:15:06 -040085 private AccountSelectionButton mAccountSelectionButton;
Adrien Béraudffd32412012-08-07 18:39:23 -040086
Alexandre Savarda1404652012-09-20 13:34:08 -040087 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 };
Adrien Béraudffd32412012-08-07 18:39:23 -040091
Alexandre Savard60f0f1c2012-11-01 09:40:34 -040092 public String getSelectedAccount() {
93 return mAccountSelectionButton.getText().toString();
94 }
95
Alexandre Savarda1404652012-09-20 13:34:08 -040096 /**
97 * Runnable that fill information in a contact card asynchroniously.
98 */
99 public static class InfosLoader implements Runnable
100 {
101 private View view;
102 private long cid;
103 private ContentResolver cr;
Adrien Béraudffd32412012-08-07 18:39:23 -0400104
Alexandre Savarda1404652012-09-20 13:34:08 -0400105 public InfosLoader(Context context, View element, long contact_id)
Alexandre Savard38d6a152012-09-18 13:33:02 -0400106 {
Alexandre Savarda1404652012-09-20 13:34:08 -0400107 cid = contact_id;
108 cr = context.getContentResolver();
109 view = element;
Alexandre Savard38d6a152012-09-18 13:33:02 -0400110 }
Alexandre Savarda1404652012-09-20 13:34:08 -0400111
112 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
113 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
114 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
115 if (input == null) {
116 return null;
117 }
118 return BitmapFactory.decodeStream(input);
119 }
120
121 @Override
122 public void run()
123 {
124 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
125
126 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
127 CONTACTS_PHONES_PROJECTION,
128 CommonDataKinds.Phone.CONTACT_ID + " = ?",
129 new String[] { Long.toString(cid) }, null);
130
131 final List<String> numbers = new ArrayList<String>();
132 while (phones.moveToNext()) {
133 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
134 //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
135 numbers.add(number);
136 }
137 phones.close();
138 // TODO: same for SIP adresses.
139
140 final Bitmap bmp = photo_bmp;
141 view.post(new Runnable()
142 {
143 @Override
144 public void run()
145 {
146 /*
147 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
148 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
149
150 if (photo_bmp != null) {
151 photo_view.setImageBitmap(bmp);
152 photo_view.setVisibility(View.VISIBLE);
153 } else {
154 photo_view.setVisibility(View.GONE);
155 }
156
157 if (numbers.size() > 0) {
158 String phonestxt = numbers.get(0);
159 for (int i = 1, n = numbers.size(); i < n; i++)
160 phonestxt += "\n" + numbers.get(i);
161 phones_txt.setText(phonestxt);
162 phones_txt.setVisibility(View.VISIBLE);
163 } else
164 phones_txt.setVisibility(View.GONE);
165 */
166 }
167 });
168 }
169 }
170
171 /**
172 * A CursorAdapter that creates and update call elements using corresponding contact infos.
173 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
174 */
175 public static class CallElementAdapter extends ArrayAdapter
176 {
177 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
178 private Context mContext;
179 private final List mCallList;
180
181 public CallElementAdapter(Context context, List callList)
182 {
183 super(context, R.layout.call_element, callList);
184 mContext = context;
185 mCallList = callList;
186 }
187
188 @Override
189 public View getView(int position, View convertView, ViewGroup parent)
190 {
191 View rowView = convertView;
192 CallElementView entryView = null;
193
194 if(rowView == null)
195 {
196 // Get a new instance of the row layout view
197 LayoutInflater inflater = LayoutInflater.from(mContext);
198 rowView = inflater.inflate(R.layout.call_element, null);
199
200 // Hold the view objects in an object
201 // so they don't need to be re-fetched
202 entryView = new CallElementView();
203 entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
204 entryView.button = (Button) rowView.findViewById(R.id.button2);
205 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
206 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
207 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
Alexandre Savardd1976532012-10-26 15:32:45 -0400208 entryView.state = (TextView) rowView.findViewById(R.id.callstate);
Alexandre Savarda1404652012-09-20 13:34:08 -0400209
210 // Cache the view obects in the tag
211 // so they can be re-accessed later
212 rowView.setTag(entryView);
213 } else {
214 entryView = (CallElementView) rowView.getTag();
215 }
216
217 // Transfer the stock data from the data object
218 // to the view objects
219 SipCall call = (SipCall) mCallList.get(position);
Alexandre Savardd1976532012-10-26 15:32:45 -0400220 call.setAssociatedRowView(rowView);
221 entryView.displayName.setText(call.getDisplayName());
222 entryView.phones.setText(call.getPhone());
223 entryView.state.setText(CURRENT_STATE_LABEL + call.getCallStateString());
Alexandre Savarda1404652012-09-20 13:34:08 -0400224
225 return rowView;
226 }
227 };
228
229 public static class CallElementView
230 {
231 protected ImageButton toggleButton;
232 protected Button button;
233 protected ImageView photo;
234 protected TextView displayName;
Alexandre Savardd1976532012-10-26 15:32:45 -0400235 protected TextView phones;
236 protected TextView state;
Alexandre Savarda1404652012-09-20 13:34:08 -0400237 }
238
Alexandre Savardf17e3172012-10-25 16:09:09 -0400239 public CallElementList(ISipService s, SFLPhoneHome home)
Alexandre Savard817dc502012-10-22 11:47:29 -0400240 {
241 super();
242 service = s;
Alexandre Savardf17e3172012-10-25 16:09:09 -0400243 sflphoneHome = home;
Alexandre Savard817dc502012-10-22 11:47:29 -0400244 }
245
246 public void setService(ISipService s)
247 {
248 service = s;
Alexandre Savard35d208a2012-10-31 15:15:06 -0400249 mAccountSelectionButton.setSipService(service, (Context)getActivity());
Alexandre Savard817dc502012-10-22 11:47:29 -0400250 }
251
Alexandre Savarda1404652012-09-20 13:34:08 -0400252 public void addCall(SipCall c)
253 {
254 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
255 mAdapter.add(c);
256 }
257
Alexandre Savard9b742d52012-09-20 13:57:44 -0400258 public void removeCall(SipCall c)
259 {
260 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
261 mAdapter.remove(c);
262 }
263
264
265 @Override
266 public void onActivityCreated(Bundle savedInstanceState)
267 {
268 super.onActivityCreated(savedInstanceState);
269
270 // Give some text to display if there is no data. In a real
271 // application this would come from a resource.
272 //setEmptyText("No phone numbers");
273
274 // We have a menu item to show in action bar.
275 setHasOptionsMenu(true);
276
277
278 // Create an empty adapter we will use to display the loaded data.
Alexandre Savardd1976532012-10-26 15:32:45 -0400279 ArrayList calls = new ArrayList();
Alexandre Savard9b742d52012-09-20 13:57:44 -0400280 mAdapter = new CallElementAdapter(getActivity(), calls);
281 setListAdapter(mAdapter);
282
283 // Start out with a progress indicator.
284 //setListShown(false);
285
286 // Prepare the loader. Either re-connect with an existing one,
287 // or start a new one.
288 // getLoaderManager().initLoader(0, null, this)
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400289
290 final Context context = getActivity();
291 ListView lv = getListView();
292 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
293 @Override
294 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
295 Log.i(TAG, "On Long Click");
296 final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"};
297 final SipCall call = (SipCall) mAdapter.getItem(pos);
298 AlertDialog.Builder builder = new AlertDialog.Builder(context);
299 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
300 .setCancelable(true)
301 .setItems(items, new DialogInterface.OnClickListener() {
302 public void onClick(DialogInterface dialog, int item) {
303 Log.i(TAG, "Selected " + items[item]);
304 switch (item) {
305 case 0:
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400306 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400307 break;
308 case 1:
309 call.sendTextMessage();
310 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400311 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400312 break;
313 case 2:
314 call.addToConference();
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 default:
319 break;
320 }
321 }
322 });
323 AlertDialog alert = builder.create();
324 alert.show();
325
326 return true;
327 }
328 });
Alexandre Savard9b742d52012-09-20 13:57:44 -0400329 }
330
331 @Override
332 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Savard9b742d52012-09-20 13:57:44 -0400333 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
Alexandre Savard817dc502012-10-22 11:47:29 -0400334
Alexandre Savard35d208a2012-10-31 15:15:06 -0400335 mAccountSelectionButton = (AccountSelectionButton) inflatedView.findViewById(R.id.account_selection_button);
Alexandre Savard817dc502012-10-22 11:47:29 -0400336
Alexandre Savard9b742d52012-09-20 13:57:44 -0400337 return inflatedView;
338 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400339
Alexandre Savard9b742d52012-09-20 13:57:44 -0400340 public void onListItemClick(ListView l, View v, int position, long id)
341 {
342 // Insert desired behavior here.
Alexandre Savard6b7f4182012-09-20 14:10:42 -0400343 SipCall call = (SipCall) mAdapter.getItem(position);
Alexandre Savarddf544262012-10-25 14:24:08 -0400344 Log.i(TAG, "Call Clicked: " + call.getCallId());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400345
Alexandre Savarddf544262012-10-25 14:24:08 -0400346 call.launchCallActivity(getActivity());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400347
348 sflphoneHome.onSelectedCallAction(call);
Alexandre Savard9b742d52012-09-20 13:57:44 -0400349 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400350
351 @Override
352 public Loader<Cursor> onCreateLoader(int id, Bundle args)
353 {
354
355 //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
356
357 // This is called when a new Loader needs to be created. This
358 // sample only has one Loader, so we don't care about the ID.
359 // First, pick the base URI to use depending on whether we are
360 // currently filtering.
361 Uri baseUri;
362
363 if (mCurFilter != null) {
364 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
365 } else {
366 baseUri = Contacts.CONTENT_URI;
367 }
368
369 // Now create and return a CursorLoader that will take care of
370 // creating a Cursor for the data being displayed.
371 String select = "((" + Contacts.DISPLAY_NAME
372 + " NOTNULL) AND ("
373 + Contacts.HAS_PHONE_NUMBER
374 + "=1) AND ("
375 + Contacts.DISPLAY_NAME
376 + " != '' ))";
377 //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
378
379 return new CursorLoader(getActivity(),
380 baseUri,
381 CONTACTS_SUMMARY_PROJECTION,
382 select,
383 null,
384 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
385 }
386
387 @Override
388 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
389 {
390 // Swap the new cursor in. (The framework will take care of closing the
391 // old cursor once we return.)
Alexandre Savarda1404652012-09-20 13:34:08 -0400392 // mAdapter.swapCursor(data);
Adrien Béraudffd32412012-08-07 18:39:23 -0400393
394 // The list should now be shown.
395 /*
396 if (isResumed()) {
397 setListShown(true);
398 } else {
399 setListShownNoAnimation(true);
400 }*/
401 }
402
403 @Override
404 public void onLoaderReset(Loader<Cursor> loader)
405 {
406 // This is called when the last Cursor provided to onLoadFinished()
407 // above is about to be closed. We need to make sure we are no
408 // longer using it.
Alexandre Savarda1404652012-09-20 13:34:08 -0400409 // mAdapter.swapCursor(null);
Adrien Béraudffd32412012-08-07 18:39:23 -0400410 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400411}