blob: 63e13537875561681adb849f02fa813843da2bc3 [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 Savard23240c12012-09-19 18:23:44 -040035import android.content.ContentResolver;
36import android.content.ContentUris;
37import android.content.Context;
Alexandre Savard6668cf82012-09-19 10:59:21 -040038import android.content.CursorLoader;
39import android.content.Loader;
40import android.database.Cursor;
Alexandre Savard23240c12012-09-19 18:23:44 -040041import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
Alexandre Savard6668cf82012-09-19 10:59:21 -040043import android.net.Uri;
Alexandre Savarda04c5202012-09-18 17:19:53 -040044import android.os.Bundle;
Alexandre Savard23240c12012-09-19 18:23:44 -040045import android.provider.*;
Alexandre Savard6668cf82012-09-19 10:59:21 -040046import android.provider.ContactsContract.CommonDataKinds;
47import android.provider.ContactsContract.CommonDataKinds.Phone;
48import android.provider.ContactsContract.CommonDataKinds.SipAddress;
49import android.provider.ContactsContract.Contacts;
50import android.provider.ContactsContract.Profile;
51import android.text.TextUtils;
Alexandre Savarda04c5202012-09-18 17:19:53 -040052import android.view.LayoutInflater;
Alexandre Savard6668cf82012-09-19 10:59:21 -040053import android.view.Menu;
54import android.view.MenuItem;
55import android.view.MenuInflater;
Alexandre Savarda04c5202012-09-18 17:19:53 -040056import android.view.View;
57import android.view.ViewGroup;
Alexandre Savard23240c12012-09-19 18:23:44 -040058import android.widget.CursorAdapter;
59import android.widget.ImageView;
Alexandre Savarda04c5202012-09-18 17:19:53 -040060import android.widget.ListView;
Alexandre Savard23240c12012-09-19 18:23:44 -040061import android.widget.TextView;
Alexandre Savard6668cf82012-09-19 10:59:21 -040062import android.widget.SearchView;
63import android.widget.SearchView.OnQueryTextListener;
Alexandre Savarda04c5202012-09-18 17:19:53 -040064import android.util.Log;
Alexandre Savard23240c12012-09-19 18:23:44 -040065import java.io.InputStream;
66import java.util.concurrent.ExecutorService;
67import java.util.concurrent.Executors;
68import java.util.List;
69import java.util.ArrayList;
Alexandre Savarda04c5202012-09-18 17:19:53 -040070
71import com.savoirfairelinux.sflphone.R;
72
Alexandre Savard6668cf82012-09-19 10:59:21 -040073public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
Alexandre Savarda04c5202012-09-18 17:19:53 -040074{
Alexandre Savard23240c12012-09-19 18:23:44 -040075 ContactElementAdapter mAdapter;
Alexandre Savard6668cf82012-09-19 10:59:21 -040076 String mCurFilter;
77
78 // These are the Contacts rows that we will retrieve.
79 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
80 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
81 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
82 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Alexandre Savarda04c5202012-09-18 17:19:53 -040083
Alexandre Savard23240c12012-09-19 18:23:44 -040084 public static class InfosLoader implements Runnable
85 {
86 private View view;
87 private long cid;
88 private ContentResolver cr;
89
90 public InfosLoader(Context context, View element, long contact_id)
91 {
92 cid = contact_id;
93 cr = context.getContentResolver();
94 view = element;
95 }
96
97 public static Bitmap loadContactPhoto(ContentResolver cr, long id)
98 {
99 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
100 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
101 if (input == null) {
102 return null;
103 }
104 return BitmapFactory.decodeStream(input);
105 }
106
107 @Override
108 public void run()
109 {
110 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
111
112 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
113 CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?",
114 new String[] { Long.toString(cid) },
115 null);
116
117 final List<String> numbers = new ArrayList<String>();
118 while (phones.moveToNext()) {
119 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
120 // int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
121 numbers.add(number);
122 }
123 phones.close();
124
125 final Bitmap bmp = photo_bmp;
126 view.post(new Runnable()
127 {
128 @Override
129 public void run()
130 {
131 }
132 });
133 }
134 }
135
136 public static class ContactElementAdapter extends CursorAdapter
137 {
138 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
139
140 public ContactElementAdapter(Context context, Cursor c)
141 {
142 super(context, c, 0);
143 }
144
145 @Override
146 public View newView(Context context, Cursor cursor, ViewGroup parent)
147 {
148 LayoutInflater inflater = LayoutInflater.from(context);
149 View v = inflater.inflate(R.layout.call_element, parent, false);
150 bindView(v, context, cursor);
151 return v;
152 }
153
154 @Override
155 public void bindView(final View view, Context context, Cursor cursor)
156 {
157 final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
158 final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
159 // final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
160 // final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
161
162 TextView display_name_txt = (TextView) view.findViewById(R.id.display_name);
163 display_name_txt.setText(display_name);
164
165 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
166 photo_view.setVisibility(View.GONE);
167
168 infos_fetcher.execute(new InfosLoader(context, view, contact_id));
169 }
170 };
171
Alexandre Savard6c0584e2012-09-20 09:42:59 -0400172 public ContactListFragment()
Alexandre Savard23240c12012-09-19 18:23:44 -0400173 {
174 super();
Alexandre Savard23240c12012-09-19 18:23:44 -0400175 }
176
Alexandre Savarda04c5202012-09-18 17:19:53 -0400177 @Override
178 public void onActivityCreated(Bundle savedInstanceState)
179 {
180 super.onActivityCreated(savedInstanceState);
181
Alexandre Savard6668cf82012-09-19 10:59:21 -0400182 // In order to onCreateOptionsMenu be called
Alexandre Savarda04c5202012-09-18 17:19:53 -0400183 setHasOptionsMenu(true);
184
Alexandre Savard23240c12012-09-19 18:23:44 -0400185 mAdapter = new ContactElementAdapter(getActivity(), null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400186 setListAdapter(mAdapter);
Alexandre Savard23240c12012-09-19 18:23:44 -0400187
188 getLoaderManager().initLoader(0, null, this);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400189 }
190
191 @Override
192 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
193 {
194 return inflater.inflate(R.layout.call_element_list, container, false);
195 }
196
197 @Override
Alexandre Savard6668cf82012-09-19 10:59:21 -0400198 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
199 {
200 // Place an action bar item for searching
201 MenuItem item = menu.add("Search");
202 item.setIcon(R.drawable.ic_menu_search);
203 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
204 SearchView sv = new SearchView(getActivity());
205 sv.setOnQueryTextListener(this);
206 item.setActionView(sv);
207 }
208
209 @Override
Alexandre Savarda04c5202012-09-18 17:19:53 -0400210 public void onListItemClick(ListView l, View v, int position, long id)
211 {
212 // Insert desired behavior here.
Alexandre Savard23240c12012-09-19 18:23:44 -0400213/*
Alexandre Savard6668cf82012-09-19 10:59:21 -0400214 Log.i("ContactListFragment", "Contact clicked: " + contact.getDisplayName());
Alexandre Savard800faa62012-09-19 13:18:57 -0400215
216 SipCall call = SipCall.getCallInstance(contact);
217 Log.i("ConatctListFragment", "OK");
218 Log.i("ContactListFragment", "Number of calls " + SipCall.getNbCalls());
Alexandre Savard23240c12012-09-19 18:23:44 -0400219
220 mManager.callmanagerJNI.placeCall("IP2IP", "CALL1234", "192.168.40.35");
221*/
Alexandre Savard6668cf82012-09-19 10:59:21 -0400222 }
223
224 @Override
225 public boolean onQueryTextChange(String newText)
226 {
227 // Called when the action bar search text has changed. Update
228 // the search filter, and restart the loader to do a new query
229 // with this filter.
230 String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
231 // Don't do anything if the filter hasn't actually changed.
232 // Prefents restarting the loader when restoring state.
233 if (mCurFilter == null && newFilter == null) { return true; }
234 if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; }
235 mCurFilter = newFilter;
236 getLoaderManager().restartLoader(0, null, this);
237 return true;
238 }
239
240 @Override
241 public boolean onQueryTextSubmit(String query)
242 {
243 // Return false to let the SearchView perform the default action
244 return false;
245 }
246
247 @Override
248 public Loader<Cursor> onCreateLoader(int id, Bundle args)
249 {
250 Uri baseUri;
251
252 if(mCurFilter != null) {
253 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
254 } else {
255 baseUri = Contacts.CONTENT_URI;
256 }
257
258 // Now create and return a CursorLoader that will take care of
259 // creating a Cursor for the data being displayed.
260 String select = "((" + Contacts.DISPLAY_NAME
261 + " NOTNULL) AND ("
262 + Contacts.HAS_PHONE_NUMBER
263 + "=1) AND ("
264 + Contacts.DISPLAY_NAME
265 + " != '' ))";
266
267 return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION,
268 select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
269 }
270
271 @Override
272 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
273 {
274 // Swap the new cursor in.
Alexandre Savard23240c12012-09-19 18:23:44 -0400275 mAdapter.swapCursor(data);
Alexandre Savard6668cf82012-09-19 10:59:21 -0400276 }
277
278 @Override
279 public void onLoaderReset(Loader<Cursor> loader)
280 {
281 // Thi is called when the last Cursor provided to onLoadFinished
Alexandre Savard23240c12012-09-19 18:23:44 -0400282 mAdapter.swapCursor(null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400283 }
284}