blob: 4e68522e9b4dbfac31ffedd23aad17619d1924b6 [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
Emeric Vigier1f1ced32012-11-02 16:56:32 -040039import android.app.Activity;
Adrien Béraudffd32412012-08-07 18:39:23 -040040import android.app.ListFragment;
41import android.app.LoaderManager;
Alexandre Savardf1e32cf2012-09-20 17:45:16 -040042import android.app.AlertDialog;
43import android.app.AlertDialog.Builder;
Adrien Béraudffd32412012-08-07 18:39:23 -040044import android.content.ContentResolver;
45import android.content.ContentUris;
46import android.content.Context;
47import android.content.CursorLoader;
Alexandre Savardf1e32cf2012-09-20 17:45:16 -040048import android.content.DialogInterface;
Adrien Béraudffd32412012-08-07 18:39:23 -040049import android.content.Loader;
50import android.database.Cursor;
51import android.graphics.Bitmap;
52import android.graphics.BitmapFactory;
53import android.net.Uri;
54import android.os.Bundle;
Alexandre Savard817dc502012-10-22 11:47:29 -040055import android.os.RemoteException;
Adrien Béraudffd32412012-08-07 18:39:23 -040056import android.provider.*;
57import android.provider.ContactsContract.CommonDataKinds;
58import android.provider.ContactsContract.CommonDataKinds.Phone;
59import android.provider.ContactsContract.CommonDataKinds.SipAddress;
60import android.provider.ContactsContract.Contacts;
Adrien Béraudffd32412012-08-07 18:39:23 -040061import android.util.Log;
62import android.view.*;
Alexandre Savardf1e32cf2012-09-20 17:45:16 -040063import android.widget.AdapterView.OnItemLongClickListener;
64import android.widget.AdapterView.OnItemSelectedListener;
Adrien Béraudffd32412012-08-07 18:39:23 -040065import android.widget.*;
Alexandre Savard38d6a152012-09-18 13:33:02 -040066import java.util.List;
67import java.util.ArrayList;
Adrien Béraudffd32412012-08-07 18:39:23 -040068
69import com.savoirfairelinux.sflphone.R;
Alexandre Savard817dc502012-10-22 11:47:29 -040070import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savard35d208a2012-10-31 15:15:06 -040071import com.savoirfairelinux.sflphone.utils.AccountSelectionButton;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040072import com.savoirfairelinux.sflphone.utils.AccountList;
Adrien Béraudffd32412012-08-07 18:39:23 -040073
74/**
75 * Main list of Call Elements.
76 * We don't manage contacts ourself so they are
77 */
Alexandre Savard9b742d52012-09-20 13:57:44 -040078public class CallElementList extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
Adrien Béraudffd32412012-08-07 18:39:23 -040079{
Alexandre Savardd1976532012-10-26 15:32:45 -040080 private static final String TAG = "CallElementList";
81 private static final String CURRENT_STATE_LABEL = " CURRENT STATE: ";
Alexandre Savardf17e3172012-10-25 16:09:09 -040082 private ContactManager mContactManager;
Alexandre Savardd1976532012-10-26 15:32:45 -040083 private CallElementAdapter mAdapter;
Alexandre Savardf17e3172012-10-25 16:09:09 -040084 private String mCurFilter;
85 private SFLPhoneHome sflphoneHome;
Emeric Vigier0038a612012-11-06 18:51:19 -050086 private SFLphoneApplication sflphoneApplication;
Alexandre Savard817dc502012-10-22 11:47:29 -040087 private ISipService service;
Alexandre Savard35d208a2012-10-31 15:15:06 -040088 private AccountSelectionButton mAccountSelectionButton;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040089 private AccountList mAccountList;
Adrien Béraudffd32412012-08-07 18:39:23 -040090
Alexandre Savarda1404652012-09-20 13:34:08 -040091 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
92 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
93 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
94 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Adrien Béraudffd32412012-08-07 18:39:23 -040095
Emeric Vigier1f1ced32012-11-02 16:56:32 -040096 @Override
97 public void onAttach(Activity activity) {
98 super.onAttach(activity);
99 sflphoneHome = (SFLPhoneHome) activity;
100 service = ((SFLphoneApplication) sflphoneHome.getApplication()).getSipService();
Emeric Vigiercc0785a2012-11-06 19:07:48 -0500101 mAccountList = ((SFLphoneApplication) sflphoneHome.getApplication()).getAccountList();
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400102 Log.i(TAG, "onAttach() service " + service);
103 }
104
Alexandre Savard60f0f1c2012-11-01 09:40:34 -0400105 public String getSelectedAccount() {
106 return mAccountSelectionButton.getText().toString();
107 }
108
Alexandre Savarda1404652012-09-20 13:34:08 -0400109 /**
110 * Runnable that fill information in a contact card asynchroniously.
111 */
112 public static class InfosLoader implements Runnable
113 {
114 private View view;
115 private long cid;
116 private ContentResolver cr;
Adrien Béraudffd32412012-08-07 18:39:23 -0400117
Alexandre Savarda1404652012-09-20 13:34:08 -0400118 public InfosLoader(Context context, View element, long contact_id)
Alexandre Savard38d6a152012-09-18 13:33:02 -0400119 {
Alexandre Savarda1404652012-09-20 13:34:08 -0400120 cid = contact_id;
121 cr = context.getContentResolver();
122 view = element;
Alexandre Savard38d6a152012-09-18 13:33:02 -0400123 }
Alexandre Savarda1404652012-09-20 13:34:08 -0400124
125 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
126 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
127 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
128 if (input == null) {
129 return null;
130 }
131 return BitmapFactory.decodeStream(input);
132 }
133
134 @Override
135 public void run()
136 {
137 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
138
139 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
140 CONTACTS_PHONES_PROJECTION,
141 CommonDataKinds.Phone.CONTACT_ID + " = ?",
142 new String[] { Long.toString(cid) }, null);
143
144 final List<String> numbers = new ArrayList<String>();
145 while (phones.moveToNext()) {
146 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
147 //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
148 numbers.add(number);
149 }
150 phones.close();
151 // TODO: same for SIP adresses.
152
153 final Bitmap bmp = photo_bmp;
154 view.post(new Runnable()
155 {
156 @Override
157 public void run()
158 {
159 /*
160 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
161 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
162
163 if (photo_bmp != null) {
164 photo_view.setImageBitmap(bmp);
165 photo_view.setVisibility(View.VISIBLE);
166 } else {
167 photo_view.setVisibility(View.GONE);
168 }
169
170 if (numbers.size() > 0) {
171 String phonestxt = numbers.get(0);
172 for (int i = 1, n = numbers.size(); i < n; i++)
173 phonestxt += "\n" + numbers.get(i);
174 phones_txt.setText(phonestxt);
175 phones_txt.setVisibility(View.VISIBLE);
176 } else
177 phones_txt.setVisibility(View.GONE);
178 */
179 }
180 });
181 }
182 }
183
184 /**
185 * A CursorAdapter that creates and update call elements using corresponding contact infos.
186 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
187 */
188 public static class CallElementAdapter extends ArrayAdapter
189 {
190 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
191 private Context mContext;
192 private final List mCallList;
193
194 public CallElementAdapter(Context context, List callList)
195 {
196 super(context, R.layout.call_element, callList);
197 mContext = context;
198 mCallList = callList;
199 }
200
201 @Override
202 public View getView(int position, View convertView, ViewGroup parent)
203 {
204 View rowView = convertView;
205 CallElementView entryView = null;
206
207 if(rowView == null)
208 {
209 // Get a new instance of the row layout view
210 LayoutInflater inflater = LayoutInflater.from(mContext);
211 rowView = inflater.inflate(R.layout.call_element, null);
212
213 // Hold the view objects in an object
214 // so they don't need to be re-fetched
215 entryView = new CallElementView();
216 entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
217 entryView.button = (Button) rowView.findViewById(R.id.button2);
218 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
219 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
220 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
Alexandre Savardd1976532012-10-26 15:32:45 -0400221 entryView.state = (TextView) rowView.findViewById(R.id.callstate);
Alexandre Savarda1404652012-09-20 13:34:08 -0400222
223 // Cache the view obects in the tag
224 // so they can be re-accessed later
225 rowView.setTag(entryView);
226 } else {
227 entryView = (CallElementView) rowView.getTag();
228 }
229
230 // Transfer the stock data from the data object
231 // to the view objects
232 SipCall call = (SipCall) mCallList.get(position);
Alexandre Savardd1976532012-10-26 15:32:45 -0400233 call.setAssociatedRowView(rowView);
234 entryView.displayName.setText(call.getDisplayName());
235 entryView.phones.setText(call.getPhone());
236 entryView.state.setText(CURRENT_STATE_LABEL + call.getCallStateString());
Alexandre Savarda1404652012-09-20 13:34:08 -0400237
238 return rowView;
239 }
240 };
241
242 public static class CallElementView
243 {
244 protected ImageButton toggleButton;
245 protected Button button;
246 protected ImageView photo;
247 protected TextView displayName;
Alexandre Savardd1976532012-10-26 15:32:45 -0400248 protected TextView phones;
249 protected TextView state;
Alexandre Savarda1404652012-09-20 13:34:08 -0400250 }
251
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400252 public CallElementList()
Alexandre Savard817dc502012-10-22 11:47:29 -0400253 {
254 super();
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400255 }
256
257 public void setAccountList(AccountList accountList) {
258 mAccountList = accountList;
Alexandre Savard817dc502012-10-22 11:47:29 -0400259 }
260
Alexandre Savarda1404652012-09-20 13:34:08 -0400261 public void addCall(SipCall c)
262 {
263 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
264 mAdapter.add(c);
265 }
266
Alexandre Savard9b742d52012-09-20 13:57:44 -0400267 public void removeCall(SipCall c)
268 {
269 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
270 mAdapter.remove(c);
271 }
272
273
274 @Override
275 public void onActivityCreated(Bundle savedInstanceState)
276 {
277 super.onActivityCreated(savedInstanceState);
278
279 // Give some text to display if there is no data. In a real
280 // application this would come from a resource.
281 //setEmptyText("No phone numbers");
282
283 // We have a menu item to show in action bar.
284 setHasOptionsMenu(true);
285
286
287 // Create an empty adapter we will use to display the loaded data.
Alexandre Savardd1976532012-10-26 15:32:45 -0400288 ArrayList calls = new ArrayList();
Alexandre Savard9b742d52012-09-20 13:57:44 -0400289 mAdapter = new CallElementAdapter(getActivity(), calls);
290 setListAdapter(mAdapter);
291
292 // Start out with a progress indicator.
293 //setListShown(false);
294
295 // Prepare the loader. Either re-connect with an existing one,
296 // or start a new one.
297 // getLoaderManager().initLoader(0, null, this)
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400298
299 final Context context = getActivity();
300 ListView lv = getListView();
301 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
302 @Override
303 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
304 Log.i(TAG, "On Long Click");
305 final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"};
306 final SipCall call = (SipCall) mAdapter.getItem(pos);
Emeric Vigier0038a612012-11-06 18:51:19 -0500307 // FIXME
308 service = sflphoneApplication.getSipService();
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400309 AlertDialog.Builder builder = new AlertDialog.Builder(context);
310 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
311 .setCancelable(true)
312 .setItems(items, new DialogInterface.OnClickListener() {
313 public void onClick(DialogInterface dialog, int item) {
314 Log.i(TAG, "Selected " + items[item]);
315 switch (item) {
316 case 0:
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400317 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400318 break;
319 case 1:
320 call.sendTextMessage();
321 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400322 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400323 break;
324 case 2:
325 call.addToConference();
326 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400327 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400328 break;
329 default:
330 break;
331 }
332 }
333 });
334 AlertDialog alert = builder.create();
335 alert.show();
336
337 return true;
338 }
339 });
Alexandre Savard9b742d52012-09-20 13:57:44 -0400340 }
341
342 @Override
343 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Savard9b742d52012-09-20 13:57:44 -0400344 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
Alexandre Savard817dc502012-10-22 11:47:29 -0400345
Alexandre Savard35d208a2012-10-31 15:15:06 -0400346 mAccountSelectionButton = (AccountSelectionButton) inflatedView.findViewById(R.id.account_selection_button);
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400347 mAccountList.addManagementUI(mAccountSelectionButton);
Alexandre Savardaf0f3c62012-11-01 18:35:56 -0400348 mAccountSelectionButton.setAccountList(mAccountList);
Alexandre Savard817dc502012-10-22 11:47:29 -0400349
Alexandre Savard9b742d52012-09-20 13:57:44 -0400350 return inflatedView;
351 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400352
Alexandre Savard9b742d52012-09-20 13:57:44 -0400353 public void onListItemClick(ListView l, View v, int position, long id)
354 {
355 // Insert desired behavior here.
Alexandre Savard6b7f4182012-09-20 14:10:42 -0400356 SipCall call = (SipCall) mAdapter.getItem(position);
Alexandre Savarddf544262012-10-25 14:24:08 -0400357 Log.i(TAG, "Call Clicked: " + call.getCallId());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400358
Alexandre Savarddf544262012-10-25 14:24:08 -0400359 call.launchCallActivity(getActivity());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400360
361 sflphoneHome.onSelectedCallAction(call);
Alexandre Savard9b742d52012-09-20 13:57:44 -0400362 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400363
364 @Override
365 public Loader<Cursor> onCreateLoader(int id, Bundle args)
366 {
367
368 //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
369
370 // This is called when a new Loader needs to be created. This
371 // sample only has one Loader, so we don't care about the ID.
372 // First, pick the base URI to use depending on whether we are
373 // currently filtering.
374 Uri baseUri;
375
376 if (mCurFilter != null) {
377 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
378 } else {
379 baseUri = Contacts.CONTENT_URI;
380 }
381
382 // Now create and return a CursorLoader that will take care of
383 // creating a Cursor for the data being displayed.
384 String select = "((" + Contacts.DISPLAY_NAME
385 + " NOTNULL) AND ("
386 + Contacts.HAS_PHONE_NUMBER
387 + "=1) AND ("
388 + Contacts.DISPLAY_NAME
389 + " != '' ))";
390 //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
391
392 return new CursorLoader(getActivity(),
393 baseUri,
394 CONTACTS_SUMMARY_PROJECTION,
395 select,
396 null,
397 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
398 }
399
400 @Override
401 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
402 {
403 // Swap the new cursor in. (The framework will take care of closing the
404 // old cursor once we return.)
Alexandre Savarda1404652012-09-20 13:34:08 -0400405 // mAdapter.swapCursor(data);
Adrien Béraudffd32412012-08-07 18:39:23 -0400406
407 // The list should now be shown.
408 /*
409 if (isResumed()) {
410 setListShown(true);
411 } else {
412 setListShownNoAnimation(true);
413 }*/
414 }
415
416 @Override
417 public void onLoaderReset(Loader<Cursor> loader)
418 {
419 // This is called when the last Cursor provided to onLoadFinished()
420 // above is about to be closed. We need to make sure we are no
421 // longer using it.
Alexandre Savarda1404652012-09-20 13:34:08 -0400422 // mAdapter.swapCursor(null);
Adrien Béraudffd32412012-08-07 18:39:23 -0400423 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400424}