blob: 0fd1c327a79b4d1e02867ec69c0515f49483ba8a [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;
54import android.provider.*;
55import android.provider.ContactsContract.CommonDataKinds;
56import android.provider.ContactsContract.CommonDataKinds.Phone;
57import android.provider.ContactsContract.CommonDataKinds.SipAddress;
58import android.provider.ContactsContract.Contacts;
Adrien Béraudffd32412012-08-07 18:39:23 -040059import android.util.Log;
60import android.view.*;
Alexandre Savardf1e32cf2012-09-20 17:45:16 -040061import android.widget.AdapterView.OnItemLongClickListener;
62import android.widget.AdapterView.OnItemSelectedListener;
Adrien Béraudffd32412012-08-07 18:39:23 -040063import android.widget.*;
Alexandre Savard38d6a152012-09-18 13:33:02 -040064import java.util.List;
65import java.util.ArrayList;
Adrien Béraudffd32412012-08-07 18:39:23 -040066
67import com.savoirfairelinux.sflphone.R;
68
69/**
70 * Main list of Call Elements.
71 * We don't manage contacts ourself so they are
72 */
Alexandre Savard9b742d52012-09-20 13:57:44 -040073public class CallElementList extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
Adrien Béraudffd32412012-08-07 18:39:23 -040074{
Alexandre Savarda1404652012-09-20 13:34:08 -040075 final String TAG = "CallElementList";
76 ContactManager mContactManager;
77 ArrayAdapter mAdapter;
78 String mCurFilter;
Adrien Béraudffd32412012-08-07 18:39:23 -040079
Alexandre Savarda1404652012-09-20 13:34:08 -040080 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
81 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
82 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
83 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Adrien Béraudffd32412012-08-07 18:39:23 -040084
Alexandre Savarda1404652012-09-20 13:34:08 -040085 /**
86 * Runnable that fill information in a contact card asynchroniously.
87 */
88 public static class InfosLoader implements Runnable
89 {
90 private View view;
91 private long cid;
92 private ContentResolver cr;
Adrien Béraudffd32412012-08-07 18:39:23 -040093
Alexandre Savarda1404652012-09-20 13:34:08 -040094 public InfosLoader(Context context, View element, long contact_id)
Alexandre Savard38d6a152012-09-18 13:33:02 -040095 {
Alexandre Savarda1404652012-09-20 13:34:08 -040096 cid = contact_id;
97 cr = context.getContentResolver();
98 view = element;
Alexandre Savard38d6a152012-09-18 13:33:02 -040099 }
Alexandre Savarda1404652012-09-20 13:34:08 -0400100
101 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
102 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
103 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
104 if (input == null) {
105 return null;
106 }
107 return BitmapFactory.decodeStream(input);
108 }
109
110 @Override
111 public void run()
112 {
113 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
114
115 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
116 CONTACTS_PHONES_PROJECTION,
117 CommonDataKinds.Phone.CONTACT_ID + " = ?",
118 new String[] { Long.toString(cid) }, null);
119
120 final List<String> numbers = new ArrayList<String>();
121 while (phones.moveToNext()) {
122 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
123 //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
124 numbers.add(number);
125 }
126 phones.close();
127 // TODO: same for SIP adresses.
128
129 final Bitmap bmp = photo_bmp;
130 view.post(new Runnable()
131 {
132 @Override
133 public void run()
134 {
135 /*
136 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
137 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
138
139 if (photo_bmp != null) {
140 photo_view.setImageBitmap(bmp);
141 photo_view.setVisibility(View.VISIBLE);
142 } else {
143 photo_view.setVisibility(View.GONE);
144 }
145
146 if (numbers.size() > 0) {
147 String phonestxt = numbers.get(0);
148 for (int i = 1, n = numbers.size(); i < n; i++)
149 phonestxt += "\n" + numbers.get(i);
150 phones_txt.setText(phonestxt);
151 phones_txt.setVisibility(View.VISIBLE);
152 } else
153 phones_txt.setVisibility(View.GONE);
154 */
155 }
156 });
157 }
158 }
159
160 /**
161 * A CursorAdapter that creates and update call elements using corresponding contact infos.
162 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
163 */
164 public static class CallElementAdapter extends ArrayAdapter
165 {
166 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
167 private Context mContext;
168 private final List mCallList;
169
170 public CallElementAdapter(Context context, List callList)
171 {
172 super(context, R.layout.call_element, callList);
173 mContext = context;
174 mCallList = callList;
175 }
176
177 @Override
178 public View getView(int position, View convertView, ViewGroup parent)
179 {
180 View rowView = convertView;
181 CallElementView entryView = null;
182
183 if(rowView == null)
184 {
185 // Get a new instance of the row layout view
186 LayoutInflater inflater = LayoutInflater.from(mContext);
187 rowView = inflater.inflate(R.layout.call_element, null);
188
189 // Hold the view objects in an object
190 // so they don't need to be re-fetched
191 entryView = new CallElementView();
192 entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
193 entryView.button = (Button) rowView.findViewById(R.id.button2);
194 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
195 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
196 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
197
198 // Cache the view obects in the tag
199 // so they can be re-accessed later
200 rowView.setTag(entryView);
201 } else {
202 entryView = (CallElementView) rowView.getTag();
203 }
204
205 // Transfer the stock data from the data object
206 // to the view objects
207 SipCall call = (SipCall) mCallList.get(position);
208 entryView.displayName.setText(call.mCallInfo.mDisplayName);
209 entryView.phones.setText(call.mCallInfo.mPhone);
210
211 return rowView;
212 }
213 };
214
215 public static class CallElementView
216 {
217 protected ImageButton toggleButton;
218 protected Button button;
219 protected ImageView photo;
220 protected TextView displayName;
221 protected TextView phones;
222 }
223
224 public void addCall(SipCall c)
225 {
226 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
227 mAdapter.add(c);
228 }
229
Alexandre Savard9b742d52012-09-20 13:57:44 -0400230 public void removeCall(SipCall c)
231 {
232 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
233 mAdapter.remove(c);
234 }
235
236
237 @Override
238 public void onActivityCreated(Bundle savedInstanceState)
239 {
240 super.onActivityCreated(savedInstanceState);
241
242 // Give some text to display if there is no data. In a real
243 // application this would come from a resource.
244 //setEmptyText("No phone numbers");
245
246 // We have a menu item to show in action bar.
247 setHasOptionsMenu(true);
248
249
250 // Create an empty adapter we will use to display the loaded data.
251 List calls = new ArrayList();
252 mAdapter = new CallElementAdapter(getActivity(), calls);
253 setListAdapter(mAdapter);
254
255 // Start out with a progress indicator.
256 //setListShown(false);
257
258 // Prepare the loader. Either re-connect with an existing one,
259 // or start a new one.
260 // getLoaderManager().initLoader(0, null, this)
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400261
262 final Context context = getActivity();
263 ListView lv = getListView();
264 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
265 @Override
266 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
267 Log.i(TAG, "On Long Click");
268 final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"};
269 final SipCall call = (SipCall) mAdapter.getItem(pos);
270 AlertDialog.Builder builder = new AlertDialog.Builder(context);
271 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
272 .setCancelable(true)
273 .setItems(items, new DialogInterface.OnClickListener() {
274 public void onClick(DialogInterface dialog, int item) {
275 Log.i(TAG, "Selected " + items[item]);
276 switch (item) {
277 case 0:
278 call.hangup();
279 break;
280 case 1:
281 call.sendTextMessage();
282 // Need to hangup this call immediately since no way to do it after this action
283 call.hangup();
284 break;
285 case 2:
286 call.addToConference();
287 // Need to hangup this call immediately since no way to do it after this action
288 call.hangup();
289 break;
290 default:
291 break;
292 }
293 }
294 });
295 AlertDialog alert = builder.create();
296 alert.show();
297
298 return true;
299 }
300 });
Alexandre Savard9b742d52012-09-20 13:57:44 -0400301 }
302
303 @Override
304 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
305 //LayoutInflater newInflater = inflater.cloneInContext(new ContextThemeWrapper(getActivity(), R.style.));
306 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
307 return inflatedView;
308 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400309
Alexandre Savard9b742d52012-09-20 13:57:44 -0400310 public void onListItemClick(ListView l, View v, int position, long id)
311 {
312 // Insert desired behavior here.
313 Log.i("CallElementList", "Item clicked: " + id);
Alexandre Savard6b7f4182012-09-20 14:10:42 -0400314 SipCall call = (SipCall) mAdapter.getItem(position);
315 call.hangup();
Alexandre Savard9b742d52012-09-20 13:57:44 -0400316 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400317
318 @Override
319 public Loader<Cursor> onCreateLoader(int id, Bundle args)
320 {
321
322 //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
323
324 // This is called when a new Loader needs to be created. This
325 // sample only has one Loader, so we don't care about the ID.
326 // First, pick the base URI to use depending on whether we are
327 // currently filtering.
328 Uri baseUri;
329
330 if (mCurFilter != null) {
331 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
332 } else {
333 baseUri = Contacts.CONTENT_URI;
334 }
335
336 // Now create and return a CursorLoader that will take care of
337 // creating a Cursor for the data being displayed.
338 String select = "((" + Contacts.DISPLAY_NAME
339 + " NOTNULL) AND ("
340 + Contacts.HAS_PHONE_NUMBER
341 + "=1) AND ("
342 + Contacts.DISPLAY_NAME
343 + " != '' ))";
344 //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
345
346 return new CursorLoader(getActivity(),
347 baseUri,
348 CONTACTS_SUMMARY_PROJECTION,
349 select,
350 null,
351 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
352 }
353
354 @Override
355 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
356 {
357 // Swap the new cursor in. (The framework will take care of closing the
358 // old cursor once we return.)
Alexandre Savarda1404652012-09-20 13:34:08 -0400359 // mAdapter.swapCursor(data);
Adrien Béraudffd32412012-08-07 18:39:23 -0400360
361 // The list should now be shown.
362 /*
363 if (isResumed()) {
364 setListShown(true);
365 } else {
366 setListShownNoAnimation(true);
367 }*/
368 }
369
370 @Override
371 public void onLoaderReset(Loader<Cursor> loader)
372 {
373 // This is called when the last Cursor provided to onLoadFinished()
374 // above is about to be closed. We need to make sure we are no
375 // longer using it.
Alexandre Savarda1404652012-09-20 13:34:08 -0400376 // mAdapter.swapCursor(null);
Adrien Béraudffd32412012-08-07 18:39:23 -0400377 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400378}