blob: 336bd333578160b63b1cfff995df82f62c519e5f [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();
100 Log.i(TAG, "onAttach() service " + service);
101 }
102
Alexandre Savard60f0f1c2012-11-01 09:40:34 -0400103 public String getSelectedAccount() {
104 return mAccountSelectionButton.getText().toString();
105 }
106
Alexandre Savarda1404652012-09-20 13:34:08 -0400107 /**
108 * Runnable that fill information in a contact card asynchroniously.
109 */
110 public static class InfosLoader implements Runnable
111 {
112 private View view;
113 private long cid;
114 private ContentResolver cr;
Adrien Béraudffd32412012-08-07 18:39:23 -0400115
Alexandre Savarda1404652012-09-20 13:34:08 -0400116 public InfosLoader(Context context, View element, long contact_id)
Alexandre Savard38d6a152012-09-18 13:33:02 -0400117 {
Alexandre Savarda1404652012-09-20 13:34:08 -0400118 cid = contact_id;
119 cr = context.getContentResolver();
120 view = element;
Alexandre Savard38d6a152012-09-18 13:33:02 -0400121 }
Alexandre Savarda1404652012-09-20 13:34:08 -0400122
123 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
124 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
125 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
126 if (input == null) {
127 return null;
128 }
129 return BitmapFactory.decodeStream(input);
130 }
131
132 @Override
133 public void run()
134 {
135 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
136
137 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
138 CONTACTS_PHONES_PROJECTION,
139 CommonDataKinds.Phone.CONTACT_ID + " = ?",
140 new String[] { Long.toString(cid) }, null);
141
142 final List<String> numbers = new ArrayList<String>();
143 while (phones.moveToNext()) {
144 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
145 //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
146 numbers.add(number);
147 }
148 phones.close();
149 // TODO: same for SIP adresses.
150
151 final Bitmap bmp = photo_bmp;
152 view.post(new Runnable()
153 {
154 @Override
155 public void run()
156 {
157 /*
158 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
159 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
160
161 if (photo_bmp != null) {
162 photo_view.setImageBitmap(bmp);
163 photo_view.setVisibility(View.VISIBLE);
164 } else {
165 photo_view.setVisibility(View.GONE);
166 }
167
168 if (numbers.size() > 0) {
169 String phonestxt = numbers.get(0);
170 for (int i = 1, n = numbers.size(); i < n; i++)
171 phonestxt += "\n" + numbers.get(i);
172 phones_txt.setText(phonestxt);
173 phones_txt.setVisibility(View.VISIBLE);
174 } else
175 phones_txt.setVisibility(View.GONE);
176 */
177 }
178 });
179 }
180 }
181
182 /**
183 * A CursorAdapter that creates and update call elements using corresponding contact infos.
184 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
185 */
186 public static class CallElementAdapter extends ArrayAdapter
187 {
188 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
189 private Context mContext;
190 private final List mCallList;
191
192 public CallElementAdapter(Context context, List callList)
193 {
194 super(context, R.layout.call_element, callList);
195 mContext = context;
196 mCallList = callList;
197 }
198
199 @Override
200 public View getView(int position, View convertView, ViewGroup parent)
201 {
202 View rowView = convertView;
203 CallElementView entryView = null;
204
205 if(rowView == null)
206 {
207 // Get a new instance of the row layout view
208 LayoutInflater inflater = LayoutInflater.from(mContext);
209 rowView = inflater.inflate(R.layout.call_element, null);
210
211 // Hold the view objects in an object
212 // so they don't need to be re-fetched
213 entryView = new CallElementView();
214 entryView.toggleButton = (ImageButton) rowView.findViewById(R.id.toggleButton1);
215 entryView.button = (Button) rowView.findViewById(R.id.button2);
216 entryView.photo = (ImageView) rowView.findViewById(R.id.photo);
217 entryView.displayName = (TextView) rowView.findViewById(R.id.display_name);
218 entryView.phones = (TextView) rowView.findViewById(R.id.phones);
Alexandre Savardd1976532012-10-26 15:32:45 -0400219 entryView.state = (TextView) rowView.findViewById(R.id.callstate);
Alexandre Savarda1404652012-09-20 13:34:08 -0400220
221 // Cache the view obects in the tag
222 // so they can be re-accessed later
223 rowView.setTag(entryView);
224 } else {
225 entryView = (CallElementView) rowView.getTag();
226 }
227
228 // Transfer the stock data from the data object
229 // to the view objects
230 SipCall call = (SipCall) mCallList.get(position);
Alexandre Savardd1976532012-10-26 15:32:45 -0400231 call.setAssociatedRowView(rowView);
232 entryView.displayName.setText(call.getDisplayName());
233 entryView.phones.setText(call.getPhone());
234 entryView.state.setText(CURRENT_STATE_LABEL + call.getCallStateString());
Alexandre Savarda1404652012-09-20 13:34:08 -0400235
236 return rowView;
237 }
238 };
239
240 public static class CallElementView
241 {
242 protected ImageButton toggleButton;
243 protected Button button;
244 protected ImageView photo;
245 protected TextView displayName;
Alexandre Savardd1976532012-10-26 15:32:45 -0400246 protected TextView phones;
247 protected TextView state;
Alexandre Savarda1404652012-09-20 13:34:08 -0400248 }
249
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400250 public CallElementList()
Alexandre Savard817dc502012-10-22 11:47:29 -0400251 {
252 super();
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400253 }
254
255 public void setAccountList(AccountList accountList) {
256 mAccountList = accountList;
Alexandre Savard817dc502012-10-22 11:47:29 -0400257 }
258
Alexandre Savarda1404652012-09-20 13:34:08 -0400259 public void addCall(SipCall c)
260 {
261 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
262 mAdapter.add(c);
263 }
264
Alexandre Savard9b742d52012-09-20 13:57:44 -0400265 public void removeCall(SipCall c)
266 {
267 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
268 mAdapter.remove(c);
269 }
270
271
272 @Override
273 public void onActivityCreated(Bundle savedInstanceState)
274 {
275 super.onActivityCreated(savedInstanceState);
276
277 // Give some text to display if there is no data. In a real
278 // application this would come from a resource.
279 //setEmptyText("No phone numbers");
280
281 // We have a menu item to show in action bar.
282 setHasOptionsMenu(true);
283
284
285 // Create an empty adapter we will use to display the loaded data.
Alexandre Savardd1976532012-10-26 15:32:45 -0400286 ArrayList calls = new ArrayList();
Alexandre Savard9b742d52012-09-20 13:57:44 -0400287 mAdapter = new CallElementAdapter(getActivity(), calls);
288 setListAdapter(mAdapter);
289
290 // Start out with a progress indicator.
291 //setListShown(false);
292
293 // Prepare the loader. Either re-connect with an existing one,
294 // or start a new one.
295 // getLoaderManager().initLoader(0, null, this)
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400296
297 final Context context = getActivity();
298 ListView lv = getListView();
299 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
300 @Override
301 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
302 Log.i(TAG, "On Long Click");
303 final CharSequence[] items = {"Hang up Call", "Send Message", "Add to Conference"};
304 final SipCall call = (SipCall) mAdapter.getItem(pos);
305 AlertDialog.Builder builder = new AlertDialog.Builder(context);
306 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
307 .setCancelable(true)
308 .setItems(items, new DialogInterface.OnClickListener() {
309 public void onClick(DialogInterface dialog, int item) {
310 Log.i(TAG, "Selected " + items[item]);
311 switch (item) {
312 case 0:
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400313 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400314 break;
315 case 1:
316 call.sendTextMessage();
317 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400318 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400319 break;
320 case 2:
321 call.addToConference();
322 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400323 call.notifyServiceHangup(service);
Alexandre Savardf1e32cf2012-09-20 17:45:16 -0400324 break;
325 default:
326 break;
327 }
328 }
329 });
330 AlertDialog alert = builder.create();
331 alert.show();
332
333 return true;
334 }
335 });
Alexandre Savard9b742d52012-09-20 13:57:44 -0400336 }
337
338 @Override
339 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Savard9b742d52012-09-20 13:57:44 -0400340 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
Alexandre Savard817dc502012-10-22 11:47:29 -0400341
Alexandre Savard35d208a2012-10-31 15:15:06 -0400342 mAccountSelectionButton = (AccountSelectionButton) inflatedView.findViewById(R.id.account_selection_button);
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400343 mAccountList.addManagementUI(mAccountSelectionButton);
Alexandre Savardaf0f3c62012-11-01 18:35:56 -0400344 mAccountSelectionButton.setAccountList(mAccountList);
Alexandre Savard817dc502012-10-22 11:47:29 -0400345
Alexandre Savard9b742d52012-09-20 13:57:44 -0400346 return inflatedView;
347 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400348
Alexandre Savard9b742d52012-09-20 13:57:44 -0400349 public void onListItemClick(ListView l, View v, int position, long id)
350 {
351 // Insert desired behavior here.
Alexandre Savard6b7f4182012-09-20 14:10:42 -0400352 SipCall call = (SipCall) mAdapter.getItem(position);
Alexandre Savarddf544262012-10-25 14:24:08 -0400353 Log.i(TAG, "Call Clicked: " + call.getCallId());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400354
Alexandre Savarddf544262012-10-25 14:24:08 -0400355 call.launchCallActivity(getActivity());
Alexandre Savardf17e3172012-10-25 16:09:09 -0400356
357 sflphoneHome.onSelectedCallAction(call);
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}