blob: 961a0270f4d745d583aebd34e7845fcc172f395d [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;
Alexandre Savard817dc502012-10-22 11:47:29 -040086 private ISipService service;
Alexandre Savard35d208a2012-10-31 15:15:06 -040087 private AccountSelectionButton mAccountSelectionButton;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040088 private AccountList mAccountList;
Adrien Béraudffd32412012-08-07 18:39:23 -040089
Alexandre Savarda1404652012-09-20 13:34:08 -040090 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
91 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
92 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
93 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Adrien Béraudffd32412012-08-07 18:39:23 -040094
Emeric Vigier1f1ced32012-11-02 16:56:32 -040095 @Override
96 public void onAttach(Activity activity) {
97 super.onAttach(activity);
98 sflphoneHome = (SFLPhoneHome) activity;
99 service = ((SFLphoneApplication) sflphoneHome.getApplication()).getSipService();
Emeric Vigiercc0785a2012-11-06 19:07:48 -0500100 mAccountList = ((SFLphoneApplication) sflphoneHome.getApplication()).getAccountList();
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400101 Log.i(TAG, "onAttach() service " + service);
102 }
103
Alexandre Savard60f0f1c2012-11-01 09:40:34 -0400104 public String getSelectedAccount() {
105 return mAccountSelectionButton.getText().toString();
106 }
107
Alexandre Savarda1404652012-09-20 13:34:08 -0400108 /**
109 * Runnable that fill information in a contact card asynchroniously.
110 */
111 public static class InfosLoader implements Runnable
112 {
113 private View view;
114 private long cid;
115 private ContentResolver cr;
Adrien Béraudffd32412012-08-07 18:39:23 -0400116
Alexandre Savarda1404652012-09-20 13:34:08 -0400117 public InfosLoader(Context context, View element, long contact_id)
Alexandre Savard38d6a152012-09-18 13:33:02 -0400118 {
Alexandre Savarda1404652012-09-20 13:34:08 -0400119 cid = contact_id;
120 cr = context.getContentResolver();
121 view = element;
Alexandre Savard38d6a152012-09-18 13:33:02 -0400122 }
Alexandre Savarda1404652012-09-20 13:34:08 -0400123
124 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
125 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
126 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
127 if (input == null) {
128 return null;
129 }
130 return BitmapFactory.decodeStream(input);
131 }
132
133 @Override
134 public void run()
135 {
136 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
137
138 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
139 CONTACTS_PHONES_PROJECTION,
140 CommonDataKinds.Phone.CONTACT_ID + " = ?",
141 new String[] { Long.toString(cid) }, null);
142
143 final List<String> numbers = new ArrayList<String>();
144 while (phones.moveToNext()) {
145 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
146 //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
147 numbers.add(number);
148 }
149 phones.close();
150 // TODO: same for SIP adresses.
151
152 final Bitmap bmp = photo_bmp;
153 view.post(new Runnable()
154 {
155 @Override
156 public void run()
157 {
158 /*
159 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
160 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
161
162 if (photo_bmp != null) {
163 photo_view.setImageBitmap(bmp);
164 photo_view.setVisibility(View.VISIBLE);
165 } else {
166 photo_view.setVisibility(View.GONE);
167 }
168
169 if (numbers.size() > 0) {
170 String phonestxt = numbers.get(0);
171 for (int i = 1, n = numbers.size(); i < n; i++)
172 phonestxt += "\n" + numbers.get(i);
173 phones_txt.setText(phonestxt);
174 phones_txt.setVisibility(View.VISIBLE);
175 } else
176 phones_txt.setVisibility(View.GONE);
177 */
178 }
179 });
180 }
181 }
182
183 /**
184 * A CursorAdapter that creates and update call elements using corresponding contact infos.
185 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
186 */
187 public static class CallElementAdapter extends ArrayAdapter
188 {
189 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
190 private Context mContext;
191 private final List mCallList;
192
193 public CallElementAdapter(Context context, List callList)
194 {
195 super(context, R.layout.call_element, callList);
196 mContext = context;
197 mCallList = callList;
198 }
199
200 @Override
201 public View getView(int position, View convertView, ViewGroup parent)
202 {
203 View rowView = convertView;
204 CallElementView entryView = null;
205
206 if(rowView == null)
207 {
208 // Get a new instance of the row layout view
209 LayoutInflater inflater = LayoutInflater.from(mContext);
210 rowView = inflater.inflate(R.layout.call_element, null);
211
212 // Hold the view objects in an object
213 // so they don't need to be re-fetched
214 entryView = new CallElementView();
215 entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
216 entryView.button = (Button) rowView.findViewById(R.id.button2);
217 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
218 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
219 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
Alexandre Savardd1976532012-10-26 15:32:45 -0400220 entryView.state = (TextView) rowView.findViewById(R.id.callstate);
Alexandre Savarda1404652012-09-20 13:34:08 -0400221
222 // Cache the view obects in the tag
223 // so they can be re-accessed later
224 rowView.setTag(entryView);
225 } else {
226 entryView = (CallElementView) rowView.getTag();
227 }
228
229 // Transfer the stock data from the data object
230 // to the view objects
231 SipCall call = (SipCall) mCallList.get(position);
Alexandre Savardd1976532012-10-26 15:32:45 -0400232 call.setAssociatedRowView(rowView);
233 entryView.displayName.setText(call.getDisplayName());
234 entryView.phones.setText(call.getPhone());
235 entryView.state.setText(CURRENT_STATE_LABEL + call.getCallStateString());
Alexandre Savarda1404652012-09-20 13:34:08 -0400236
237 return rowView;
238 }
239 };
240
241 public static class CallElementView
242 {
243 protected ImageButton toggleButton;
244 protected Button button;
245 protected ImageView photo;
246 protected TextView displayName;
Alexandre Savardd1976532012-10-26 15:32:45 -0400247 protected TextView phones;
248 protected TextView state;
Alexandre Savarda1404652012-09-20 13:34:08 -0400249 }
250
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400251 public CallElementList()
Alexandre Savard817dc502012-10-22 11:47:29 -0400252 {
253 super();
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400254 }
255
256 public void setAccountList(AccountList accountList) {
257 mAccountList = accountList;
Alexandre Savard817dc502012-10-22 11:47:29 -0400258 }
259
Alexandre Savarda1404652012-09-20 13:34:08 -0400260 public void addCall(SipCall c)
261 {
262 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
263 mAdapter.add(c);
264 }
265
Alexandre Savard9b742d52012-09-20 13:57:44 -0400266 public void removeCall(SipCall c)
267 {
268 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
269 mAdapter.remove(c);
270 }
271
272
273 @Override
274 public void onActivityCreated(Bundle savedInstanceState)
275 {
276 super.onActivityCreated(savedInstanceState);
277
278 // Give some text to display if there is no data. In a real
279 // application this would come from a resource.
280 //setEmptyText("No phone numbers");
281
282 // We have a menu item to show in action bar.
283 setHasOptionsMenu(true);
284
285
286 // Create an empty adapter we will use to display the loaded data.
Alexandre Savardd1976532012-10-26 15:32:45 -0400287 ArrayList calls = new ArrayList();
Alexandre Savard9b742d52012-09-20 13:57:44 -0400288 mAdapter = new CallElementAdapter(getActivity(), calls);
289 setListAdapter(mAdapter);
290
291 // Start out with a progress indicator.
292 //setListShown(false);
293
294 // Prepare the loader. Either re-connect with an existing one,
295 // or start a new one.
296 // getLoaderManager().initLoader(0, null, this)
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400297
298 final Context context = getActivity();
299 ListView lv = getListView();
300 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
301 @Override
302 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
303 Log.i(TAG, "On Long Click");
304 final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"};
305 final SipCall call = (SipCall) mAdapter.getItem(pos);
306 AlertDialog.Builder builder = new AlertDialog.Builder(context);
307 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
308 .setCancelable(true)
309 .setItems(items, new DialogInterface.OnClickListener() {
310 public void onClick(DialogInterface dialog, int item) {
311 Log.i(TAG, "Selected " + items[item]);
312 switch (item) {
313 case 0:
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400314 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400315 break;
316 case 1:
317 call.sendTextMessage();
318 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400319 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400320 break;
321 case 2:
322 call.addToConference();
323 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400324 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400325 break;
326 default:
327 break;
328 }
329 }
330 });
331 AlertDialog alert = builder.create();
332 alert.show();
333
334 return true;
335 }
336 });
Alexandre Savard9b742d52012-09-20 13:57:44 -0400337 }
338
339 @Override
340 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Savard9b742d52012-09-20 13:57:44 -0400341 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
Alexandre Savard817dc502012-10-22 11:47:29 -0400342
Alexandre Savard35d208a2012-10-31 15:15:06 -0400343 mAccountSelectionButton = (AccountSelectionButton) inflatedView.findViewById(R.id.account_selection_button);
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400344 mAccountList.addManagementUI(mAccountSelectionButton);
Alexandre Savardaf0f3c62012-11-01 18:35:56 -0400345 mAccountSelectionButton.setAccountList(mAccountList);
Alexandre Savard817dc502012-10-22 11:47:29 -0400346
Alexandre Savard9b742d52012-09-20 13:57:44 -0400347 return inflatedView;
348 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400349
Alexandre Savard9b742d52012-09-20 13:57:44 -0400350 public void onListItemClick(ListView l, View v, int position, long id)
351 {
352 // Insert desired behavior here.
Alexandre Savard6b7f4182012-09-20 14:10:42 -0400353 SipCall call = (SipCall) mAdapter.getItem(position);
Alexandre Savarddf544262012-10-25 14:24:08 -0400354 Log.i(TAG, "Call Clicked: " + call.getCallId());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400355
Alexandre Savarddf544262012-10-25 14:24:08 -0400356 call.launchCallActivity(getActivity());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400357
358 sflphoneHome.onSelectedCallAction(call);
Alexandre Savard9b742d52012-09-20 13:57:44 -0400359 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400360
361 @Override
362 public Loader<Cursor> onCreateLoader(int id, Bundle args)
363 {
364
365 //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
366
367 // This is called when a new Loader needs to be created. This
368 // sample only has one Loader, so we don't care about the ID.
369 // First, pick the base URI to use depending on whether we are
370 // currently filtering.
371 Uri baseUri;
372
373 if (mCurFilter != null) {
374 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
375 } else {
376 baseUri = Contacts.CONTENT_URI;
377 }
378
379 // Now create and return a CursorLoader that will take care of
380 // creating a Cursor for the data being displayed.
381 String select = "((" + Contacts.DISPLAY_NAME
382 + " NOTNULL) AND ("
383 + Contacts.HAS_PHONE_NUMBER
384 + "=1) AND ("
385 + Contacts.DISPLAY_NAME
386 + " != '' ))";
387 //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
388
389 return new CursorLoader(getActivity(),
390 baseUri,
391 CONTACTS_SUMMARY_PROJECTION,
392 select,
393 null,
394 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
395 }
396
397 @Override
398 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
399 {
400 // Swap the new cursor in. (The framework will take care of closing the
401 // old cursor once we return.)
Alexandre Savarda1404652012-09-20 13:34:08 -0400402 // mAdapter.swapCursor(data);
Adrien Béraudffd32412012-08-07 18:39:23 -0400403
404 // The list should now be shown.
405 /*
406 if (isResumed()) {
407 setListShown(true);
408 } else {
409 setListShownNoAnimation(true);
410 }*/
411 }
412
413 @Override
414 public void onLoaderReset(Loader<Cursor> loader)
415 {
416 // This is called when the last Cursor provided to onLoadFinished()
417 // above is about to be closed. We need to make sure we are no
418 // longer using it.
Alexandre Savarda1404652012-09-20 13:34:08 -0400419 // mAdapter.swapCursor(null);
Adrien Béraudffd32412012-08-07 18:39:23 -0400420 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400421}