blob: a855b431077bcce86806a2e19a4f9fcbbfb16998 [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 Savarda1404652012-09-20 13:34:08 -040077 final String TAG = "CallElementList";
78 ContactManager mContactManager;
79 ArrayAdapter mAdapter;
80 String mCurFilter;
Alexandre Savard817dc502012-10-22 11:47:29 -040081 private ISipService service;
Adrien Béraudffd32412012-08-07 18:39:23 -040082
Alexandre Savarda1404652012-09-20 13:34:08 -040083 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
84 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
85 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
86 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Adrien Béraudffd32412012-08-07 18:39:23 -040087
Alexandre Savarda1404652012-09-20 13:34:08 -040088 /**
89 * Runnable that fill information in a contact card asynchroniously.
90 */
91 public static class InfosLoader implements Runnable
92 {
93 private View view;
94 private long cid;
95 private ContentResolver cr;
Adrien Béraudffd32412012-08-07 18:39:23 -040096
Alexandre Savarda1404652012-09-20 13:34:08 -040097 public InfosLoader(Context context, View element, long contact_id)
Alexandre Savard38d6a152012-09-18 13:33:02 -040098 {
Alexandre Savarda1404652012-09-20 13:34:08 -040099 cid = contact_id;
100 cr = context.getContentResolver();
101 view = element;
Alexandre Savard38d6a152012-09-18 13:33:02 -0400102 }
Alexandre Savarda1404652012-09-20 13:34:08 -0400103
104 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
105 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
106 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
107 if (input == null) {
108 return null;
109 }
110 return BitmapFactory.decodeStream(input);
111 }
112
113 @Override
114 public void run()
115 {
116 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
117
118 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
119 CONTACTS_PHONES_PROJECTION,
120 CommonDataKinds.Phone.CONTACT_ID + " = ?",
121 new String[] { Long.toString(cid) }, null);
122
123 final List<String> numbers = new ArrayList<String>();
124 while (phones.moveToNext()) {
125 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
126 //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
127 numbers.add(number);
128 }
129 phones.close();
130 // TODO: same for SIP adresses.
131
132 final Bitmap bmp = photo_bmp;
133 view.post(new Runnable()
134 {
135 @Override
136 public void run()
137 {
138 /*
139 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
140 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
141
142 if (photo_bmp != null) {
143 photo_view.setImageBitmap(bmp);
144 photo_view.setVisibility(View.VISIBLE);
145 } else {
146 photo_view.setVisibility(View.GONE);
147 }
148
149 if (numbers.size() > 0) {
150 String phonestxt = numbers.get(0);
151 for (int i = 1, n = numbers.size(); i < n; i++)
152 phonestxt += "\n" + numbers.get(i);
153 phones_txt.setText(phonestxt);
154 phones_txt.setVisibility(View.VISIBLE);
155 } else
156 phones_txt.setVisibility(View.GONE);
157 */
158 }
159 });
160 }
161 }
162
163 /**
164 * A CursorAdapter that creates and update call elements using corresponding contact infos.
165 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
166 */
167 public static class CallElementAdapter extends ArrayAdapter
168 {
169 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
170 private Context mContext;
171 private final List mCallList;
172
173 public CallElementAdapter(Context context, List callList)
174 {
175 super(context, R.layout.call_element, callList);
176 mContext = context;
177 mCallList = callList;
178 }
179
180 @Override
181 public View getView(int position, View convertView, ViewGroup parent)
182 {
183 View rowView = convertView;
184 CallElementView entryView = null;
185
186 if(rowView == null)
187 {
188 // Get a new instance of the row layout view
189 LayoutInflater inflater = LayoutInflater.from(mContext);
190 rowView = inflater.inflate(R.layout.call_element, null);
191
192 // Hold the view objects in an object
193 // so they don't need to be re-fetched
194 entryView = new CallElementView();
195 entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
196 entryView.button = (Button) rowView.findViewById(R.id.button2);
197 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
198 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
199 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
200
201 // Cache the view obects in the tag
202 // so they can be re-accessed later
203 rowView.setTag(entryView);
204 } else {
205 entryView = (CallElementView) rowView.getTag();
206 }
207
208 // Transfer the stock data from the data object
209 // to the view objects
210 SipCall call = (SipCall) mCallList.get(position);
211 entryView.displayName.setText(call.mCallInfo.mDisplayName);
212 entryView.phones.setText(call.mCallInfo.mPhone);
213
214 return rowView;
215 }
216 };
217
218 public static class CallElementView
219 {
220 protected ImageButton toggleButton;
221 protected Button button;
222 protected ImageView photo;
223 protected TextView displayName;
224 protected TextView phones;
225 }
226
Alexandre Savard817dc502012-10-22 11:47:29 -0400227 public CallElementList(ISipService s)
228 {
229 super();
230 service = s;
231 }
232
233 public void setService(ISipService s)
234 {
235 service = s;
236 }
237
Alexandre Savarda1404652012-09-20 13:34:08 -0400238 public void addCall(SipCall c)
239 {
240 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
241 mAdapter.add(c);
242 }
243
Alexandre Savard9b742d52012-09-20 13:57:44 -0400244 public void removeCall(SipCall c)
245 {
246 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
247 mAdapter.remove(c);
248 }
249
250
251 @Override
252 public void onActivityCreated(Bundle savedInstanceState)
253 {
254 super.onActivityCreated(savedInstanceState);
255
256 // Give some text to display if there is no data. In a real
257 // application this would come from a resource.
258 //setEmptyText("No phone numbers");
259
260 // We have a menu item to show in action bar.
261 setHasOptionsMenu(true);
262
263
264 // Create an empty adapter we will use to display the loaded data.
265 List calls = new ArrayList();
266 mAdapter = new CallElementAdapter(getActivity(), calls);
267 setListAdapter(mAdapter);
268
269 // Start out with a progress indicator.
270 //setListShown(false);
271
272 // Prepare the loader. Either re-connect with an existing one,
273 // or start a new one.
274 // getLoaderManager().initLoader(0, null, this)
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400275
276 final Context context = getActivity();
277 ListView lv = getListView();
278 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
279 @Override
280 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
281 Log.i(TAG, "On Long Click");
282 final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"};
283 final SipCall call = (SipCall) mAdapter.getItem(pos);
284 AlertDialog.Builder builder = new AlertDialog.Builder(context);
285 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
286 .setCancelable(true)
287 .setItems(items, new DialogInterface.OnClickListener() {
288 public void onClick(DialogInterface dialog, int item) {
289 Log.i(TAG, "Selected " + items[item]);
290 switch (item) {
291 case 0:
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400292 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400293 break;
294 case 1:
295 call.sendTextMessage();
296 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400297 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400298 break;
299 case 2:
300 call.addToConference();
301 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400302 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400303 break;
304 default:
305 break;
306 }
307 }
308 });
309 AlertDialog alert = builder.create();
310 alert.show();
311
312 return true;
313 }
314 });
Alexandre Savard9b742d52012-09-20 13:57:44 -0400315 }
316
317 @Override
318 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
319 //LayoutInflater newInflater = inflater.cloneInContext(new ContextThemeWrapper(getActivity(), R.style.));
320 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
Alexandre Savard817dc502012-10-22 11:47:29 -0400321
322 Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button);
323 accountSelectionButton.setOnClickListener(new View.OnClickListener() {
324 public void onClick(View v) {
325 /*
326 ArrayList<String> list = new ArrayList<String>();
327 list.add("IP2IP");
328 list.add("SFL-181");
329 AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list);
330 accountSelectionDialog.show();
331 */
332 try {
333 if(service != null) {
334 ArrayList<String> list = (ArrayList<String>)service.getAccountList();
335 if(list != null) {
336 AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list);
337 accountSelectionDialog.show();
338 }
339 else { Log.i(TAG, "Could not get account list"); }
340 }
341 else { Log.i(TAG, "Could not get service"); }
342 }
343 catch (RemoteException e) {
344 Log.e(TAG, "Remote exception", e);
345 }
346 }
347 });
348
Alexandre Savard9b742d52012-09-20 13:57:44 -0400349 return inflatedView;
350 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400351
Alexandre Savard9b742d52012-09-20 13:57:44 -0400352 public void onListItemClick(ListView l, View v, int position, long id)
353 {
354 // Insert desired behavior here.
Alexandre Savard817dc502012-10-22 11:47:29 -0400355 Log.i(TAG, "Item clicked: " + id);
Alexandre Savard6b7f4182012-09-20 14:10:42 -0400356 SipCall call = (SipCall) mAdapter.getItem(position);
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400357 call.notifyServiceHangup(service);
Alexandre Savard9b742d52012-09-20 13:57:44 -0400358 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400359
360 @Override
361 public Loader<Cursor> onCreateLoader(int id, Bundle args)
362 {
363
364 //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
365
366 // This is called when a new Loader needs to be created. This
367 // sample only has one Loader, so we don't care about the ID.
368 // First, pick the base URI to use depending on whether we are
369 // currently filtering.
370 Uri baseUri;
371
372 if (mCurFilter != null) {
373 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
374 } else {
375 baseUri = Contacts.CONTENT_URI;
376 }
377
378 // Now create and return a CursorLoader that will take care of
379 // creating a Cursor for the data being displayed.
380 String select = "((" + Contacts.DISPLAY_NAME
381 + " NOTNULL) AND ("
382 + Contacts.HAS_PHONE_NUMBER
383 + "=1) AND ("
384 + Contacts.DISPLAY_NAME
385 + " != '' ))";
386 //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
387
388 return new CursorLoader(getActivity(),
389 baseUri,
390 CONTACTS_SUMMARY_PROJECTION,
391 select,
392 null,
393 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
394 }
395
396 @Override
397 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
398 {
399 // Swap the new cursor in. (The framework will take care of closing the
400 // old cursor once we return.)
Alexandre Savarda1404652012-09-20 13:34:08 -0400401 // mAdapter.swapCursor(data);
Adrien Béraudffd32412012-08-07 18:39:23 -0400402
403 // The list should now be shown.
404 /*
405 if (isResumed()) {
406 setListShown(true);
407 } else {
408 setListShownNoAnimation(true);
409 }*/
410 }
411
412 @Override
413 public void onLoaderReset(Loader<Cursor> loader)
414 {
415 // This is called when the last Cursor provided to onLoadFinished()
416 // above is about to be closed. We need to make sure we are no
417 // longer using it.
Alexandre Savarda1404652012-09-20 13:34:08 -0400418 // mAdapter.swapCursor(null);
Adrien Béraudffd32412012-08-07 18:39:23 -0400419 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400420}