blob: 70e1c43d5ef564b86ffa24a99d7ab7d1d34fb221 [file] [log] [blame]
Alexandre Savarda04c5202012-09-18 17:19:53 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
Alexandre Savard23240c12012-09-19 18:23:44 -04004 * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Alexandre Savarda04c5202012-09-18 17:19:53 -04005 *
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
Emeric Vigier1f1ced32012-11-02 16:56:32 -040033import android.app.Activity;
Alexandre Savarda04c5202012-09-18 17:19:53 -040034import android.app.ListFragment;
Alexandre Savard6668cf82012-09-19 10:59:21 -040035import android.app.LoaderManager;
Alexandre Savardaef9d802012-09-20 17:31:32 -040036import android.app.AlertDialog;
37import android.app.AlertDialog.Builder;
Alexandre Savard23240c12012-09-19 18:23:44 -040038import android.content.ContentResolver;
39import android.content.ContentUris;
40import android.content.Context;
Alexandre Savard6668cf82012-09-19 10:59:21 -040041import android.content.CursorLoader;
Alexandre Savardaef9d802012-09-20 17:31:32 -040042import android.content.DialogInterface;
Alexandre Savard6668cf82012-09-19 10:59:21 -040043import android.content.Loader;
44import android.database.Cursor;
Alexandre Savard23240c12012-09-19 18:23:44 -040045import android.graphics.Bitmap;
46import android.graphics.BitmapFactory;
Alexandre Savard6668cf82012-09-19 10:59:21 -040047import android.net.Uri;
Alexandre Savarda04c5202012-09-18 17:19:53 -040048import android.os.Bundle;
Alexandre Savard817dc502012-10-22 11:47:29 -040049import android.os.RemoteException;
Alexandre Savard23240c12012-09-19 18:23:44 -040050import android.provider.*;
Alexandre Savard6668cf82012-09-19 10:59:21 -040051import android.provider.ContactsContract.CommonDataKinds;
52import android.provider.ContactsContract.CommonDataKinds.Phone;
53import android.provider.ContactsContract.CommonDataKinds.SipAddress;
54import android.provider.ContactsContract.Contacts;
Alexandre Savard6668cf82012-09-19 10:59:21 -040055import android.text.TextUtils;
Alexandre Savarda04c5202012-09-18 17:19:53 -040056import android.view.LayoutInflater;
Alexandre Savard6668cf82012-09-19 10:59:21 -040057import android.view.Menu;
58import android.view.MenuItem;
59import android.view.MenuInflater;
Alexandre Savarda04c5202012-09-18 17:19:53 -040060import android.view.View;
61import android.view.ViewGroup;
Alexandre Savard817dc502012-10-22 11:47:29 -040062import android.widget.Button;
Alexandre Savard23240c12012-09-19 18:23:44 -040063import android.widget.CursorAdapter;
64import android.widget.ImageView;
Alexandre Savarda04c5202012-09-18 17:19:53 -040065import android.widget.ListView;
Alexandre Savard23240c12012-09-19 18:23:44 -040066import android.widget.TextView;
Alexandre Savardaef9d802012-09-20 17:31:32 -040067import android.widget.Toast;
Alexandre Savard6668cf82012-09-19 10:59:21 -040068import android.widget.SearchView;
Alexandre Savard31433992012-09-20 15:19:49 -040069import android.widget.AdapterView;
70import android.widget.AdapterView.OnItemLongClickListener;
71import android.widget.AdapterView.OnItemSelectedListener;
Alexandre Savard6668cf82012-09-19 10:59:21 -040072import android.widget.SearchView.OnQueryTextListener;
Alexandre Savarda04c5202012-09-18 17:19:53 -040073import android.util.Log;
Alexandre Savard23240c12012-09-19 18:23:44 -040074import java.io.InputStream;
75import java.util.concurrent.ExecutorService;
76import java.util.concurrent.Executors;
77import java.util.List;
78import java.util.ArrayList;
Alexandre Savarda04c5202012-09-18 17:19:53 -040079
80import com.savoirfairelinux.sflphone.R;
Alexandre Savard817dc502012-10-22 11:47:29 -040081import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040082import com.savoirfairelinux.sflphone.utils.AccountList;
83import com.savoirfairelinux.sflphone.utils.AccountSelectionButton;
Alexandre Savarda04c5202012-09-18 17:19:53 -040084
Alexandre Savard6668cf82012-09-19 10:59:21 -040085public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
Alexandre Savarda04c5202012-09-18 17:19:53 -040086{
Alexandre Savard7a46f542012-09-20 11:23:33 -040087 final String TAG = "ConatctListFragment";
Alexandre Savard23240c12012-09-19 18:23:44 -040088 ContactElementAdapter mAdapter;
Alexandre Savardaef9d802012-09-20 17:31:32 -040089 Context mContext;
Alexandre Savard6668cf82012-09-19 10:59:21 -040090 String mCurFilter;
Emeric Vigier1f1ced32012-11-02 16:56:32 -040091 private SFLPhoneHome sflphoneHome;
Alexandre Savard817dc502012-10-22 11:47:29 -040092 private ISipService service;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040093 private AccountSelectionButton mAccountSelectionButton;
94 private AccountList mAccountList;
Alexandre Savard6668cf82012-09-19 10:59:21 -040095
96 // These are the Contacts rows that we will retrieve.
97 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
98 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
99 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
100 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Alexandre Savarda04c5202012-09-18 17:19:53 -0400101
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400102 @Override
103 public void onAttach(Activity activity) {
104 super.onAttach(activity);
105 sflphoneHome = (SFLPhoneHome) activity;
106 service = ((SFLphoneApplication) sflphoneHome.getApplication()).getSipService();
107 Log.i(TAG, "onAttach() service " + service);
108 }
109
Alexandre Savard23240c12012-09-19 18:23:44 -0400110 public static class InfosLoader implements Runnable
111 {
112 private View view;
113 private long cid;
114 private ContentResolver cr;
115
116 public InfosLoader(Context context, View element, long contact_id)
117 {
118 cid = contact_id;
119 cr = context.getContentResolver();
120 view = element;
121 }
122
123 public static Bitmap loadContactPhoto(ContentResolver cr, long id)
124 {
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, CommonDataKinds.Phone.CONTACT_ID + " = ?",
140 new String[] { Long.toString(cid) },
141 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
151 final Bitmap bmp = photo_bmp;
152 view.post(new Runnable()
153 {
154 @Override
155 public void run()
156 {
157 }
158 });
159 }
160 }
161
162 public static class ContactElementAdapter extends CursorAdapter
163 {
164 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
165
166 public ContactElementAdapter(Context context, Cursor c)
167 {
168 super(context, c, 0);
169 }
170
171 @Override
172 public View newView(Context context, Cursor cursor, ViewGroup parent)
173 {
174 LayoutInflater inflater = LayoutInflater.from(context);
175 View v = inflater.inflate(R.layout.call_element, parent, false);
176 bindView(v, context, cursor);
177 return v;
178 }
179
180 @Override
181 public void bindView(final View view, Context context, Cursor cursor)
182 {
183 final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
184 final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
185 // final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
186 // final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
187
188 TextView display_name_txt = (TextView) view.findViewById(R.id.display_name);
189 display_name_txt.setText(display_name);
190
191 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
192 photo_view.setVisibility(View.GONE);
193
194 infos_fetcher.execute(new InfosLoader(context, view, contact_id));
195 }
196 };
197
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400198 public ContactListFragment()
Alexandre Savard23240c12012-09-19 18:23:44 -0400199 {
200 super();
Alexandre Savard23240c12012-09-19 18:23:44 -0400201 }
202
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400203 public void setAccountList(AccountList accountList) {
204 mAccountList = accountList;
205 }
206
Alexandre Savarda04c5202012-09-18 17:19:53 -0400207 @Override
208 public void onActivityCreated(Bundle savedInstanceState)
209 {
210 super.onActivityCreated(savedInstanceState);
Alexandre Savard817dc502012-10-22 11:47:29 -0400211 mContext = getActivity();
Alexandre Savarda04c5202012-09-18 17:19:53 -0400212
Alexandre Savard6668cf82012-09-19 10:59:21 -0400213 // In order to onCreateOptionsMenu be called
Alexandre Savarda04c5202012-09-18 17:19:53 -0400214 setHasOptionsMenu(true);
215
Alexandre Savard23240c12012-09-19 18:23:44 -0400216 mAdapter = new ContactElementAdapter(getActivity(), null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400217 setListAdapter(mAdapter);
Alexandre Savard23240c12012-09-19 18:23:44 -0400218
219 getLoaderManager().initLoader(0, null, this);
Alexandre Savard31433992012-09-20 15:19:49 -0400220
Alexandre Savardaef9d802012-09-20 17:31:32 -0400221 final Context context = getActivity();
Alexandre Savard31433992012-09-20 15:19:49 -0400222 ListView lv = getListView();
223 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
224 @Override
225 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
226 Log.i(TAG, "On Long Click");
Alexandre Savardaef9d802012-09-20 17:31:32 -0400227 final CharSequence[] items = {"Make Call", "Send Message", "Add to Conference"};
228 final SipCall.CallInfo info = new SipCall.CallInfo();
229 info.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText();
230 info.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText();
Alexandre Savard85936b92012-10-24 11:23:53 -0400231 // TODO getCallInstnace should be implemented in SipCallList
232 // final SipCall call = SipCall.getCallInstance(info);
233 final SipCall call = new SipCall(info);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400234 AlertDialog.Builder builder = new AlertDialog.Builder(context);
235 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
236 .setCancelable(true)
237 .setItems(items, new DialogInterface.OnClickListener() {
238 public void onClick(DialogInterface dialog, int item) {
239 Log.i(TAG, "Selected " + items[item]);
240 switch (item) {
241 case 0:
Alexandre Savarda949eec2012-10-25 17:30:49 -0400242 call.placeCallUpdateUi();
Alexandre Savardaef9d802012-09-20 17:31:32 -0400243 break;
244 case 1:
245 call.sendTextMessage();
246 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400247 call.notifyServiceHangup(service);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400248 break;
249 case 2:
250 call.addToConference();
251 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400252 call.notifyServiceHangup(service);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400253 break;
254 default:
255 break;
256 }
257 }
258 });
259 AlertDialog alert = builder.create();
260 alert.show();
261
Alexandre Savard31433992012-09-20 15:19:49 -0400262 return true;
263 }
264 });
Alexandre Savard0014d232012-09-24 10:31:23 -0400265
266 lv.setOnItemSelectedListener(new OnItemSelectedListener() {
267 @Override
268 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
269 Log.i(TAG, "On Item Selected");
270 }
271
272 @Override
273 public void onNothingSelected(AdapterView<?> parent) {
274 Log.i(TAG, "On Nothing Selected");
275 }
276 });
Alexandre Savarda04c5202012-09-18 17:19:53 -0400277 }
278
279 @Override
280 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
281 {
Alexandre Savard817dc502012-10-22 11:47:29 -0400282 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
283
Alexandre Savard5ea84ae2012-10-31 16:41:44 -0400284 // Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button);
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400285 mAccountSelectionButton = (AccountSelectionButton) inflatedView.findViewById(R.id.account_selection_button);
286 mAccountList.addManagementUI(mAccountSelectionButton);
Alexandre Savardaf0f3c62012-11-01 18:35:56 -0400287 mAccountSelectionButton.setAccountList(mAccountList);
Alexandre Savard817dc502012-10-22 11:47:29 -0400288
289 return inflatedView;
Alexandre Savarda04c5202012-09-18 17:19:53 -0400290 }
291
292 @Override
Alexandre Savard6668cf82012-09-19 10:59:21 -0400293 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
294 {
295 // Place an action bar item for searching
296 MenuItem item = menu.add("Search");
297 item.setIcon(R.drawable.ic_menu_search);
298 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
299 SearchView sv = new SearchView(getActivity());
300 sv.setOnQueryTextListener(this);
301 item.setActionView(sv);
302 }
303
304 @Override
Alexandre Savarda04c5202012-09-18 17:19:53 -0400305 public void onListItemClick(ListView l, View v, int position, long id)
306 {
307 // Insert desired behavior here.
Alexandre Savard7a46f542012-09-20 11:23:33 -0400308 SipCall.CallInfo callInfo = new SipCall.CallInfo();
309 callInfo.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText();
310 callInfo.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText();
311 Log.i(TAG, "Contact clicked: " + callInfo.mDisplayName + ", Phone number: " + callInfo.mPhone);
Alexandre Savarda1404652012-09-20 13:34:08 -0400312
Alexandre Savard85936b92012-10-24 11:23:53 -0400313 // TODO This should be implemented in CallList
314 // int nbCallBefore = SipCall.getNbCalls();
315 // SipCall call = SipCall.getCallInstance(callInfo);
316 // Log.i(TAG, "Number of calls " + SipCall.getNbCalls());
317 // int nbCallAfter = SipCall.getNbCalls();
318 SipCall call = new SipCall(callInfo);
Alexandre Savard23240c12012-09-19 18:23:44 -0400319
Alexandre Savard85936b92012-10-24 11:23:53 -0400320 // if(nbCallAfter > nbCallBefore)
321 // call.placeCall();
Alexandre Savard6668cf82012-09-19 10:59:21 -0400322 }
323
324 @Override
325 public boolean onQueryTextChange(String newText)
326 {
327 // Called when the action bar search text has changed. Update
328 // the search filter, and restart the loader to do a new query
329 // with this filter.
330 String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
331 // Don't do anything if the filter hasn't actually changed.
332 // Prefents restarting the loader when restoring state.
333 if (mCurFilter == null && newFilter == null) { return true; }
334 if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; }
335 mCurFilter = newFilter;
336 getLoaderManager().restartLoader(0, null, this);
337 return true;
338 }
339
340 @Override
341 public boolean onQueryTextSubmit(String query)
342 {
343 // Return false to let the SearchView perform the default action
344 return false;
345 }
346
347 @Override
348 public Loader<Cursor> onCreateLoader(int id, Bundle args)
349 {
350 Uri baseUri;
351
352 if(mCurFilter != null) {
353 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
354 } else {
355 baseUri = Contacts.CONTENT_URI;
356 }
357
358 // Now create and return a CursorLoader that will take care of
359 // creating a Cursor for the data being displayed.
360 String select = "((" + Contacts.DISPLAY_NAME
361 + " NOTNULL) AND ("
362 + Contacts.HAS_PHONE_NUMBER
363 + "=1) AND ("
364 + Contacts.DISPLAY_NAME
365 + " != '' ))";
366
367 return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION,
368 select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
369 }
370
371 @Override
372 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
373 {
374 // Swap the new cursor in.
Alexandre Savard23240c12012-09-19 18:23:44 -0400375 mAdapter.swapCursor(data);
Alexandre Savard6668cf82012-09-19 10:59:21 -0400376 }
377
378 @Override
379 public void onLoaderReset(Loader<Cursor> loader)
380 {
381 // Thi is called when the last Cursor provided to onLoadFinished
Alexandre Savard23240c12012-09-19 18:23:44 -0400382 mAdapter.swapCursor(null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400383 }
384}