blob: ce193301495beff3110bc2b70a1c06a1e591052a [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 Savard23240c12012-09-19 18:23:44 -040048import android.provider.*;
Alexandre Savard6668cf82012-09-19 10:59:21 -040049import android.provider.ContactsContract.CommonDataKinds;
50import android.provider.ContactsContract.CommonDataKinds.Phone;
51import android.provider.ContactsContract.CommonDataKinds.SipAddress;
52import android.provider.ContactsContract.Contacts;
Alexandre Savard6668cf82012-09-19 10:59:21 -040053import android.text.TextUtils;
Alexandre Savarda04c5202012-09-18 17:19:53 -040054import android.view.LayoutInflater;
Alexandre Savard6668cf82012-09-19 10:59:21 -040055import android.view.Menu;
56import android.view.MenuItem;
57import android.view.MenuInflater;
Alexandre Savarda04c5202012-09-18 17:19:53 -040058import android.view.View;
59import android.view.ViewGroup;
Alexandre Savard23240c12012-09-19 18:23:44 -040060import android.widget.CursorAdapter;
61import android.widget.ImageView;
Alexandre Savarda04c5202012-09-18 17:19:53 -040062import android.widget.ListView;
Alexandre Savard23240c12012-09-19 18:23:44 -040063import android.widget.TextView;
Alexandre Savardaef9d802012-09-20 17:31:32 -040064import android.widget.Toast;
Alexandre Savard6668cf82012-09-19 10:59:21 -040065import android.widget.SearchView;
Alexandre Savard31433992012-09-20 15:19:49 -040066import android.widget.AdapterView;
67import android.widget.AdapterView.OnItemLongClickListener;
68import android.widget.AdapterView.OnItemSelectedListener;
Alexandre Savard6668cf82012-09-19 10:59:21 -040069import android.widget.SearchView.OnQueryTextListener;
Alexandre Savarda04c5202012-09-18 17:19:53 -040070import android.util.Log;
Alexandre Savard23240c12012-09-19 18:23:44 -040071import java.io.InputStream;
72import java.util.concurrent.ExecutorService;
73import java.util.concurrent.Executors;
74import java.util.List;
75import java.util.ArrayList;
Alexandre Savarda04c5202012-09-18 17:19:53 -040076
77import com.savoirfairelinux.sflphone.R;
78
Alexandre Savard6668cf82012-09-19 10:59:21 -040079public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
Alexandre Savarda04c5202012-09-18 17:19:53 -040080{
Alexandre Savard7a46f542012-09-20 11:23:33 -040081 final String TAG = "ConatctListFragment";
Alexandre Savard23240c12012-09-19 18:23:44 -040082 ContactElementAdapter mAdapter;
Alexandre Savardaef9d802012-09-20 17:31:32 -040083 Context mContext;
Alexandre Savard6668cf82012-09-19 10:59:21 -040084 String mCurFilter;
85
86 // These are the Contacts rows that we will retrieve.
87 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
88 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
89 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
90 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Alexandre Savarda04c5202012-09-18 17:19:53 -040091
Alexandre Savard23240c12012-09-19 18:23:44 -040092 public static class InfosLoader implements Runnable
93 {
94 private View view;
95 private long cid;
96 private ContentResolver cr;
97
98 public InfosLoader(Context context, View element, long contact_id)
99 {
100 cid = contact_id;
101 cr = context.getContentResolver();
102 view = element;
103 }
104
105 public static Bitmap loadContactPhoto(ContentResolver cr, long id)
106 {
107 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
108 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
109 if (input == null) {
110 return null;
111 }
112 return BitmapFactory.decodeStream(input);
113 }
114
115 @Override
116 public void run()
117 {
118 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
119
120 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
121 CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?",
122 new String[] { Long.toString(cid) },
123 null);
124
125 final List<String> numbers = new ArrayList<String>();
126 while (phones.moveToNext()) {
127 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
128 // int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
129 numbers.add(number);
130 }
131 phones.close();
132
133 final Bitmap bmp = photo_bmp;
134 view.post(new Runnable()
135 {
136 @Override
137 public void run()
138 {
139 }
140 });
141 }
142 }
143
144 public static class ContactElementAdapter extends CursorAdapter
145 {
146 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
147
148 public ContactElementAdapter(Context context, Cursor c)
149 {
150 super(context, c, 0);
151 }
152
153 @Override
154 public View newView(Context context, Cursor cursor, ViewGroup parent)
155 {
156 LayoutInflater inflater = LayoutInflater.from(context);
157 View v = inflater.inflate(R.layout.call_element, parent, false);
158 bindView(v, context, cursor);
159 return v;
160 }
161
162 @Override
163 public void bindView(final View view, Context context, Cursor cursor)
164 {
165 final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
166 final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
167 // final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
168 // final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
169
170 TextView display_name_txt = (TextView) view.findViewById(R.id.display_name);
171 display_name_txt.setText(display_name);
172
173 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
174 photo_view.setVisibility(View.GONE);
175
176 infos_fetcher.execute(new InfosLoader(context, view, contact_id));
177 }
178 };
179
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400180 public ContactListFragment()
Alexandre Savard23240c12012-09-19 18:23:44 -0400181 {
182 super();
Alexandre Savardaef9d802012-09-20 17:31:32 -0400183 mContext = getActivity();
Alexandre Savard23240c12012-09-19 18:23:44 -0400184 }
185
Alexandre Savarda04c5202012-09-18 17:19:53 -0400186 @Override
187 public void onActivityCreated(Bundle savedInstanceState)
188 {
189 super.onActivityCreated(savedInstanceState);
190
Alexandre Savard6668cf82012-09-19 10:59:21 -0400191 // In order to onCreateOptionsMenu be called
Alexandre Savarda04c5202012-09-18 17:19:53 -0400192 setHasOptionsMenu(true);
193
Alexandre Savard23240c12012-09-19 18:23:44 -0400194 mAdapter = new ContactElementAdapter(getActivity(), null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400195 setListAdapter(mAdapter);
Alexandre Savard23240c12012-09-19 18:23:44 -0400196
197 getLoaderManager().initLoader(0, null, this);
Alexandre Savard31433992012-09-20 15:19:49 -0400198
Alexandre Savardaef9d802012-09-20 17:31:32 -0400199 final Context context = getActivity();
Alexandre Savard31433992012-09-20 15:19:49 -0400200 ListView lv = getListView();
201 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
202 @Override
203 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
204 Log.i(TAG, "On Long Click");
Alexandre Savardaef9d802012-09-20 17:31:32 -0400205 final CharSequence[] items = {"Make Call", "Send Message", "Add to Conference"};
206 final SipCall.CallInfo info = new SipCall.CallInfo();
207 info.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText();
208 info.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText();
209 final SipCall call = SipCall.getCallInstance(info);
210 AlertDialog.Builder builder = new AlertDialog.Builder(context);
211 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
212 .setCancelable(true)
213 .setItems(items, new DialogInterface.OnClickListener() {
214 public void onClick(DialogInterface dialog, int item) {
215 Log.i(TAG, "Selected " + items[item]);
216 switch (item) {
217 case 0:
218 call.placeCall();
219 break;
220 case 1:
221 call.sendTextMessage();
222 // Need to hangup this call immediately since no way to do it after this action
223 call.hangup();
224 break;
225 case 2:
226 call.addToConference();
227 // Need to hangup this call immediately since no way to do it after this action
228 call.hangup();
229 break;
230 default:
231 break;
232 }
233 }
234 });
235 AlertDialog alert = builder.create();
236 alert.show();
237
Alexandre Savard31433992012-09-20 15:19:49 -0400238 return true;
239 }
240 });
Alexandre Savarda04c5202012-09-18 17:19:53 -0400241 }
242
243 @Override
244 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
245 {
246 return inflater.inflate(R.layout.call_element_list, container, false);
247 }
248
249 @Override
Alexandre Savard6668cf82012-09-19 10:59:21 -0400250 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
251 {
252 // Place an action bar item for searching
253 MenuItem item = menu.add("Search");
254 item.setIcon(R.drawable.ic_menu_search);
255 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
256 SearchView sv = new SearchView(getActivity());
257 sv.setOnQueryTextListener(this);
258 item.setActionView(sv);
259 }
260
261 @Override
Alexandre Savarda04c5202012-09-18 17:19:53 -0400262 public void onListItemClick(ListView l, View v, int position, long id)
263 {
264 // Insert desired behavior here.
Alexandre Savard7a46f542012-09-20 11:23:33 -0400265 SipCall.CallInfo callInfo = new SipCall.CallInfo();
266 callInfo.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText();
267 callInfo.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText();
268 Log.i(TAG, "Contact clicked: " + callInfo.mDisplayName + ", Phone number: " + callInfo.mPhone);
Alexandre Savarda1404652012-09-20 13:34:08 -0400269
270 int nbCallBefore = SipCall.getNbCalls();
Alexandre Savard7a46f542012-09-20 11:23:33 -0400271 SipCall call = SipCall.getCallInstance(callInfo);
272 Log.i(TAG, "Number of calls " + SipCall.getNbCalls());
Alexandre Savarda1404652012-09-20 13:34:08 -0400273 int nbCallAfter = SipCall.getNbCalls();
Alexandre Savard23240c12012-09-19 18:23:44 -0400274
Alexandre Savarda1404652012-09-20 13:34:08 -0400275 if(nbCallAfter > nbCallBefore)
276 call.placeCall();
Alexandre Savard6668cf82012-09-19 10:59:21 -0400277 }
278
279 @Override
280 public boolean onQueryTextChange(String newText)
281 {
282 // Called when the action bar search text has changed. Update
283 // the search filter, and restart the loader to do a new query
284 // with this filter.
285 String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
286 // Don't do anything if the filter hasn't actually changed.
287 // Prefents restarting the loader when restoring state.
288 if (mCurFilter == null && newFilter == null) { return true; }
289 if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; }
290 mCurFilter = newFilter;
291 getLoaderManager().restartLoader(0, null, this);
292 return true;
293 }
294
295 @Override
296 public boolean onQueryTextSubmit(String query)
297 {
298 // Return false to let the SearchView perform the default action
299 return false;
300 }
301
302 @Override
303 public Loader<Cursor> onCreateLoader(int id, Bundle args)
304 {
305 Uri baseUri;
306
307 if(mCurFilter != null) {
308 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
309 } else {
310 baseUri = Contacts.CONTENT_URI;
311 }
312
313 // Now create and return a CursorLoader that will take care of
314 // creating a Cursor for the data being displayed.
315 String select = "((" + Contacts.DISPLAY_NAME
316 + " NOTNULL) AND ("
317 + Contacts.HAS_PHONE_NUMBER
318 + "=1) AND ("
319 + Contacts.DISPLAY_NAME
320 + " != '' ))";
321
322 return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION,
323 select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
324 }
325
326 @Override
327 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
328 {
329 // Swap the new cursor in.
Alexandre Savard23240c12012-09-19 18:23:44 -0400330 mAdapter.swapCursor(data);
Alexandre Savard6668cf82012-09-19 10:59:21 -0400331 }
332
333 @Override
334 public void onLoaderReset(Loader<Cursor> loader)
335 {
336 // Thi is called when the last Cursor provided to onLoadFinished
Alexandre Savard23240c12012-09-19 18:23:44 -0400337 mAdapter.swapCursor(null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400338 }
339}