blob: 2e870b969f7a332d131cf6a99371b451f177184f [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
33import android.app.ListFragment;
Alexandre Savard6668cf82012-09-19 10:59:21 -040034import android.app.LoaderManager;
Alexandre Savardaef9d802012-09-20 17:31:32 -040035import android.app.AlertDialog;
36import android.app.AlertDialog.Builder;
Alexandre Savard23240c12012-09-19 18:23:44 -040037import android.content.ContentResolver;
38import android.content.ContentUris;
39import android.content.Context;
Alexandre Savard6668cf82012-09-19 10:59:21 -040040import android.content.CursorLoader;
Alexandre Savardaef9d802012-09-20 17:31:32 -040041import android.content.DialogInterface;
Alexandre Savard6668cf82012-09-19 10:59:21 -040042import android.content.Loader;
43import android.database.Cursor;
Alexandre Savard23240c12012-09-19 18:23:44 -040044import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
Alexandre Savard6668cf82012-09-19 10:59:21 -040046import android.net.Uri;
Alexandre Savarda04c5202012-09-18 17:19:53 -040047import android.os.Bundle;
Alexandre Savard817dc502012-10-22 11:47:29 -040048import android.os.RemoteException;
Alexandre Savard23240c12012-09-19 18:23:44 -040049import android.provider.*;
Alexandre Savard6668cf82012-09-19 10:59:21 -040050import android.provider.ContactsContract.CommonDataKinds;
51import android.provider.ContactsContract.CommonDataKinds.Phone;
52import android.provider.ContactsContract.CommonDataKinds.SipAddress;
53import android.provider.ContactsContract.Contacts;
Alexandre Savard6668cf82012-09-19 10:59:21 -040054import android.text.TextUtils;
Alexandre Savarda04c5202012-09-18 17:19:53 -040055import android.view.LayoutInflater;
Alexandre Savard6668cf82012-09-19 10:59:21 -040056import android.view.Menu;
57import android.view.MenuItem;
58import android.view.MenuInflater;
Alexandre Savarda04c5202012-09-18 17:19:53 -040059import android.view.View;
60import android.view.ViewGroup;
Alexandre Savard817dc502012-10-22 11:47:29 -040061import android.widget.Button;
Alexandre Savard23240c12012-09-19 18:23:44 -040062import android.widget.CursorAdapter;
63import android.widget.ImageView;
Alexandre Savarda04c5202012-09-18 17:19:53 -040064import android.widget.ListView;
Alexandre Savard23240c12012-09-19 18:23:44 -040065import android.widget.TextView;
Alexandre Savardaef9d802012-09-20 17:31:32 -040066import android.widget.Toast;
Alexandre Savard6668cf82012-09-19 10:59:21 -040067import android.widget.SearchView;
Alexandre Savard31433992012-09-20 15:19:49 -040068import android.widget.AdapterView;
69import android.widget.AdapterView.OnItemLongClickListener;
70import android.widget.AdapterView.OnItemSelectedListener;
Alexandre Savard6668cf82012-09-19 10:59:21 -040071import android.widget.SearchView.OnQueryTextListener;
Alexandre Savarda04c5202012-09-18 17:19:53 -040072import android.util.Log;
Alexandre Savard23240c12012-09-19 18:23:44 -040073import java.io.InputStream;
74import java.util.concurrent.ExecutorService;
75import java.util.concurrent.Executors;
76import java.util.List;
77import java.util.ArrayList;
Alexandre Savarda04c5202012-09-18 17:19:53 -040078
79import com.savoirfairelinux.sflphone.R;
Alexandre Savard817dc502012-10-22 11:47:29 -040080import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040081import com.savoirfairelinux.sflphone.utils.AccountList;
82import com.savoirfairelinux.sflphone.utils.AccountSelectionButton;
Alexandre Savarda04c5202012-09-18 17:19:53 -040083
Alexandre Savard6668cf82012-09-19 10:59:21 -040084public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
Alexandre Savarda04c5202012-09-18 17:19:53 -040085{
Alexandre Savard7a46f542012-09-20 11:23:33 -040086 final String TAG = "ConatctListFragment";
Alexandre Savard23240c12012-09-19 18:23:44 -040087 ContactElementAdapter mAdapter;
Alexandre Savardaef9d802012-09-20 17:31:32 -040088 Context mContext;
Alexandre Savard6668cf82012-09-19 10:59:21 -040089 String mCurFilter;
Alexandre Savard817dc502012-10-22 11:47:29 -040090 private ISipService service;
Alexandre Savardbb6f1872012-11-01 17:25:55 -040091 private AccountSelectionButton mAccountSelectionButton;
92 private AccountList mAccountList;
Alexandre Savard6668cf82012-09-19 10:59:21 -040093
94 // These are the Contacts rows that we will retrieve.
95 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
96 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
97 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
98 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Alexandre Savarda04c5202012-09-18 17:19:53 -040099
Alexandre Savard23240c12012-09-19 18:23:44 -0400100 public static class InfosLoader implements Runnable
101 {
102 private View view;
103 private long cid;
104 private ContentResolver cr;
105
106 public InfosLoader(Context context, View element, long contact_id)
107 {
108 cid = contact_id;
109 cr = context.getContentResolver();
110 view = element;
111 }
112
113 public static Bitmap loadContactPhoto(ContentResolver cr, long id)
114 {
115 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
116 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
117 if (input == null) {
118 return null;
119 }
120 return BitmapFactory.decodeStream(input);
121 }
122
123 @Override
124 public void run()
125 {
126 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
127
128 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
129 CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?",
130 new String[] { Long.toString(cid) },
131 null);
132
133 final List<String> numbers = new ArrayList<String>();
134 while (phones.moveToNext()) {
135 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
136 // int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
137 numbers.add(number);
138 }
139 phones.close();
140
141 final Bitmap bmp = photo_bmp;
142 view.post(new Runnable()
143 {
144 @Override
145 public void run()
146 {
147 }
148 });
149 }
150 }
151
152 public static class ContactElementAdapter extends CursorAdapter
153 {
154 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
155
156 public ContactElementAdapter(Context context, Cursor c)
157 {
158 super(context, c, 0);
159 }
160
161 @Override
162 public View newView(Context context, Cursor cursor, ViewGroup parent)
163 {
164 LayoutInflater inflater = LayoutInflater.from(context);
165 View v = inflater.inflate(R.layout.call_element, parent, false);
166 bindView(v, context, cursor);
167 return v;
168 }
169
170 @Override
171 public void bindView(final View view, Context context, Cursor cursor)
172 {
173 final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
174 final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
175 // final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
176 // final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
177
178 TextView display_name_txt = (TextView) view.findViewById(R.id.display_name);
179 display_name_txt.setText(display_name);
180
181 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
182 photo_view.setVisibility(View.GONE);
183
184 infos_fetcher.execute(new InfosLoader(context, view, contact_id));
185 }
186 };
187
Alexandre Savard817dc502012-10-22 11:47:29 -0400188 public ContactListFragment(ISipService s)
Alexandre Savard23240c12012-09-19 18:23:44 -0400189 {
190 super();
Alexandre Savard817dc502012-10-22 11:47:29 -0400191 service = s;
192 }
193
194 public void setService(ISipService s)
195 {
196 service = s;
Alexandre Savard23240c12012-09-19 18:23:44 -0400197 }
198
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400199 public void setAccountList(AccountList accountList) {
200 mAccountList = accountList;
201 }
202
Alexandre Savarda04c5202012-09-18 17:19:53 -0400203 @Override
204 public void onActivityCreated(Bundle savedInstanceState)
205 {
206 super.onActivityCreated(savedInstanceState);
Alexandre Savard817dc502012-10-22 11:47:29 -0400207 mContext = getActivity();
Alexandre Savarda04c5202012-09-18 17:19:53 -0400208
Alexandre Savard6668cf82012-09-19 10:59:21 -0400209 // In order to onCreateOptionsMenu be called
Alexandre Savarda04c5202012-09-18 17:19:53 -0400210 setHasOptionsMenu(true);
211
Alexandre Savard23240c12012-09-19 18:23:44 -0400212 mAdapter = new ContactElementAdapter(getActivity(), null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400213 setListAdapter(mAdapter);
Alexandre Savard23240c12012-09-19 18:23:44 -0400214
215 getLoaderManager().initLoader(0, null, this);
Alexandre Savard31433992012-09-20 15:19:49 -0400216
Alexandre Savardaef9d802012-09-20 17:31:32 -0400217 final Context context = getActivity();
Alexandre Savard31433992012-09-20 15:19:49 -0400218 ListView lv = getListView();
219 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
220 @Override
221 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
222 Log.i(TAG, "On Long Click");
Alexandre Savardaef9d802012-09-20 17:31:32 -0400223 final CharSequence[] items = {"Make Call", "Send Message", "Add to Conference"};
224 final SipCall.CallInfo info = new SipCall.CallInfo();
225 info.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText();
226 info.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText();
Alexandre Savard85936b92012-10-24 11:23:53 -0400227 // TODO getCallInstnace should be implemented in SipCallList
228 // final SipCall call = SipCall.getCallInstance(info);
229 final SipCall call = new SipCall(info);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400230 AlertDialog.Builder builder = new AlertDialog.Builder(context);
231 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
232 .setCancelable(true)
233 .setItems(items, new DialogInterface.OnClickListener() {
234 public void onClick(DialogInterface dialog, int item) {
235 Log.i(TAG, "Selected " + items[item]);
236 switch (item) {
237 case 0:
Alexandre Savarda949eec2012-10-25 17:30:49 -0400238 call.placeCallUpdateUi();
Alexandre Savardaef9d802012-09-20 17:31:32 -0400239 break;
240 case 1:
241 call.sendTextMessage();
242 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400243 call.notifyServiceHangup(service);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400244 break;
245 case 2:
246 call.addToConference();
247 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400248 call.notifyServiceHangup(service);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400249 break;
250 default:
251 break;
252 }
253 }
254 });
255 AlertDialog alert = builder.create();
256 alert.show();
257
Alexandre Savard31433992012-09-20 15:19:49 -0400258 return true;
259 }
260 });
Alexandre Savard0014d232012-09-24 10:31:23 -0400261
262 lv.setOnItemSelectedListener(new OnItemSelectedListener() {
263 @Override
264 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
265 Log.i(TAG, "On Item Selected");
266 }
267
268 @Override
269 public void onNothingSelected(AdapterView<?> parent) {
270 Log.i(TAG, "On Nothing Selected");
271 }
272 });
Alexandre Savarda04c5202012-09-18 17:19:53 -0400273 }
274
275 @Override
276 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
277 {
Alexandre Savard817dc502012-10-22 11:47:29 -0400278 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
279
Alexandre Savard5ea84ae2012-10-31 16:41:44 -0400280 // Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button);
Alexandre Savardbb6f1872012-11-01 17:25:55 -0400281 mAccountSelectionButton = (AccountSelectionButton) inflatedView.findViewById(R.id.account_selection_button);
282 mAccountList.addManagementUI(mAccountSelectionButton);
Alexandre Savard817dc502012-10-22 11:47:29 -0400283
284 return inflatedView;
Alexandre Savarda04c5202012-09-18 17:19:53 -0400285 }
286
287 @Override
Alexandre Savard6668cf82012-09-19 10:59:21 -0400288 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
289 {
290 // Place an action bar item for searching
291 MenuItem item = menu.add("Search");
292 item.setIcon(R.drawable.ic_menu_search);
293 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
294 SearchView sv = new SearchView(getActivity());
295 sv.setOnQueryTextListener(this);
296 item.setActionView(sv);
297 }
298
299 @Override
Alexandre Savarda04c5202012-09-18 17:19:53 -0400300 public void onListItemClick(ListView l, View v, int position, long id)
301 {
302 // Insert desired behavior here.
Alexandre Savard7a46f542012-09-20 11:23:33 -0400303 SipCall.CallInfo callInfo = new SipCall.CallInfo();
304 callInfo.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText();
305 callInfo.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText();
306 Log.i(TAG, "Contact clicked: " + callInfo.mDisplayName + ", Phone number: " + callInfo.mPhone);
Alexandre Savarda1404652012-09-20 13:34:08 -0400307
Alexandre Savard85936b92012-10-24 11:23:53 -0400308 // TODO This should be implemented in CallList
309 // int nbCallBefore = SipCall.getNbCalls();
310 // SipCall call = SipCall.getCallInstance(callInfo);
311 // Log.i(TAG, "Number of calls " + SipCall.getNbCalls());
312 // int nbCallAfter = SipCall.getNbCalls();
313 SipCall call = new SipCall(callInfo);
Alexandre Savard23240c12012-09-19 18:23:44 -0400314
Alexandre Savard85936b92012-10-24 11:23:53 -0400315 // if(nbCallAfter > nbCallBefore)
316 // call.placeCall();
Alexandre Savard6668cf82012-09-19 10:59:21 -0400317 }
318
319 @Override
320 public boolean onQueryTextChange(String newText)
321 {
322 // Called when the action bar search text has changed. Update
323 // the search filter, and restart the loader to do a new query
324 // with this filter.
325 String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
326 // Don't do anything if the filter hasn't actually changed.
327 // Prefents restarting the loader when restoring state.
328 if (mCurFilter == null && newFilter == null) { return true; }
329 if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; }
330 mCurFilter = newFilter;
331 getLoaderManager().restartLoader(0, null, this);
332 return true;
333 }
334
335 @Override
336 public boolean onQueryTextSubmit(String query)
337 {
338 // Return false to let the SearchView perform the default action
339 return false;
340 }
341
342 @Override
343 public Loader<Cursor> onCreateLoader(int id, Bundle args)
344 {
345 Uri baseUri;
346
347 if(mCurFilter != null) {
348 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
349 } else {
350 baseUri = Contacts.CONTENT_URI;
351 }
352
353 // Now create and return a CursorLoader that will take care of
354 // creating a Cursor for the data being displayed.
355 String select = "((" + Contacts.DISPLAY_NAME
356 + " NOTNULL) AND ("
357 + Contacts.HAS_PHONE_NUMBER
358 + "=1) AND ("
359 + Contacts.DISPLAY_NAME
360 + " != '' ))";
361
362 return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION,
363 select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
364 }
365
366 @Override
367 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
368 {
369 // Swap the new cursor in.
Alexandre Savard23240c12012-09-19 18:23:44 -0400370 mAdapter.swapCursor(data);
Alexandre Savard6668cf82012-09-19 10:59:21 -0400371 }
372
373 @Override
374 public void onLoaderReset(Loader<Cursor> loader)
375 {
376 // Thi is called when the last Cursor provided to onLoadFinished
Alexandre Savard23240c12012-09-19 18:23:44 -0400377 mAdapter.swapCursor(null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400378 }
379}