blob: d492bba9b56544fc66b7645bfd4cfd3d34d9cdf8 [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
39import android.app.ListFragment;
40import android.app.LoaderManager;
41import android.content.ContentResolver;
42import android.content.ContentUris;
43import android.content.Context;
44import android.content.CursorLoader;
45import android.content.Loader;
46import android.database.Cursor;
47import android.graphics.Bitmap;
48import android.graphics.BitmapFactory;
49import android.net.Uri;
50import android.os.Bundle;
51import android.provider.*;
52import android.provider.ContactsContract.CommonDataKinds;
53import android.provider.ContactsContract.CommonDataKinds.Phone;
54import android.provider.ContactsContract.CommonDataKinds.SipAddress;
55import android.provider.ContactsContract.Contacts;
Alexandre Savard4d88fee2012-09-17 15:11:38 -040056import android.provider.ContactsContract.Profile;
Adrien Béraudffd32412012-08-07 18:39:23 -040057import android.text.TextUtils;
58import android.util.Log;
59import android.view.*;
60import android.widget.*;
61import android.widget.SearchView.OnQueryTextListener;
62
63import com.savoirfairelinux.sflphone.R;
64
65/**
66 * Main list of Call Elements.
67 * We don't manage contacts ourself so they are
68 */
69public class CallElementList extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
70{
Alexandre Savard166dbb62012-09-18 09:37:27 -040071 ContactManager mContactManager;
Alexandre Savard4d88fee2012-09-17 15:11:38 -040072 CallElementAdapter mAdapter;
Adrien Béraudffd32412012-08-07 18:39:23 -040073 String mCurFilter;
74
75 // These are the Contacts rows that we will retrieve.
76 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
Alexandre Savard166dbb62012-09-18 09:37:27 -040077 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
Adrien Béraudffd32412012-08-07 18:39:23 -040078 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
79 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
80
81 /**
82 * Runnable that fill information in a contact card asynchroniously.
83 */
84 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 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
99 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
100 if (input == null) {
101 return null;
102 }
103 return BitmapFactory.decodeStream(input);
104 }
105
106 @Override
107 public void run()
108 {
109 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
110
111 Cursor phones = cr.query( CommonDataKinds.Phone.CONTENT_URI,
112 CONTACTS_PHONES_PROJECTION,
113 CommonDataKinds.Phone.CONTACT_ID + " = ?",
114 new String[] { Long.toString(cid) },
115 null);
116 final List<String> numbers = new ArrayList<String>();
117 while (phones.moveToNext()) {
118 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
119 //int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
120 numbers.add(number);
121 }
122 phones.close();
123 // TODO: same for SIP adresses.
124
125 final Bitmap bmp = photo_bmp;
126 view.post(new Runnable()
127 {
128 @Override
129 public void run()
130 {
131 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
132 TextView phones_txt = (TextView) view.findViewById(R.id.phones);
133
134 if (photo_bmp != null) {
135 photo_view.setImageBitmap(bmp);
136 photo_view.setVisibility(View.VISIBLE);
137 } else {
138 photo_view.setVisibility(View.GONE);
139 }
140
141 if (numbers.size() > 0) {
142 String phonestxt = numbers.get(0);
143 for (int i = 1, n = numbers.size(); i < n; i++)
144 phonestxt += "\n" + numbers.get(i);
145 phones_txt.setText(phonestxt);
146 phones_txt.setVisibility(View.VISIBLE);
147 } else
148 phones_txt.setVisibility(View.GONE);
149 }
150 });
151
152 }
153
154 }
155
156 /**
157 * A CursorAdapter that creates and update call elements using corresponding contact infos.
158 * TODO: handle contact list separatly to allow showing synchronized contacts on Call cards with multiple contacts etc.
159 */
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400160 class CallElementAdapter extends ArrayAdapter
Adrien Béraudffd32412012-08-07 18:39:23 -0400161 {
162 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
163
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400164 public CallElementAdapter(Context context)
Adrien Béraudffd32412012-08-07 18:39:23 -0400165 {
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400166 super(context, 0);
Adrien Béraudffd32412012-08-07 18:39:23 -0400167 }
168
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400169 @Override
170 public View getView(int position, View convertView, ViewGroup parent)
171 {
172 LayoutInflater inflater = LayoutInflater.from(getActivity());
173 final long contact_id = 0;
174 View v = inflater.inflate(R.layout.call_element, parent, false);
175 infos_fetcher.execute(new InfosLoader(getActivity(), v, contact_id));
176
177 return v;
178 }
179
180/*
Adrien Béraudffd32412012-08-07 18:39:23 -0400181 @Override
182 public View newView(Context context, Cursor cursor, ViewGroup parent)
183 {
184 LayoutInflater inflater = LayoutInflater.from(context);
185 View v = inflater.inflate(R.layout.call_element, parent, false);
186 bindView(v, context, cursor);
187 return v;
188 }
189
190 @Override
191 public void bindView(final View view, Context context, Cursor cursor)
192 {
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400193 final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
Adrien Béraudffd32412012-08-07 18:39:23 -0400194 final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
195 //final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
196 //final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
197
198 TextView display_name_txt = (TextView) view.findViewById(R.id.display_name);
199 display_name_txt.setText(display_name);
200
201 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
202 photo_view.setVisibility(View.GONE);
203
204 infos_fetcher.execute(new InfosLoader(getActivity(), view, contact_id));
205 }
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400206*/
Adrien Béraudffd32412012-08-07 18:39:23 -0400207
208 };
209
210 @Override
211 public void onActivityCreated(Bundle savedInstanceState)
212 {
213 super.onActivityCreated(savedInstanceState);
214
215 // Give some text to display if there is no data. In a real
216 // application this would come from a resource.
217 //setEmptyText("No phone numbers");
218
219 // We have a menu item to show in action bar.
220 setHasOptionsMenu(true);
221
222 // Create an empty adapter we will use to display the loaded data.
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400223 mAdapter = new CallElementAdapter(getActivity());
Adrien Béraudffd32412012-08-07 18:39:23 -0400224 setListAdapter(mAdapter);
225
226 // Start out with a progress indicator.
227 //setListShown(false);
228
Alexandre Savard166dbb62012-09-18 09:37:27 -0400229 mContactManager = new ContactManager(getActivity());
230
Adrien Béraudffd32412012-08-07 18:39:23 -0400231 // Prepare the loader. Either re-connect with an existing one,
232 // or start a new one.
233 getLoaderManager().initLoader(0, null, this);
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400234
Adrien Béraudffd32412012-08-07 18:39:23 -0400235 }
236
237 @Override
238 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
239 //LayoutInflater newInflater = inflater.cloneInContext(new ContextThemeWrapper(getActivity(), R.style.));
240 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
241 return inflatedView;
242 }
243
244 @Override
245 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
246 {
247 // Place an action bar item for searching.
248 MenuItem item = menu.add("Search");
249 //item.setIcon(android.R.drawable.ic_menu_search);
250 item.setIcon(R.drawable.ic_menu_search);
251 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
252 SearchView sv = new SearchView(getActivity());
253 sv.setOnQueryTextListener(this);
254 item.setActionView(sv);
255 }
256
257 @Override
258 public boolean onQueryTextChange(String newText)
259 {
260 // Called when the action bar search text has changed. Update
261 // the search filter, and restart the loader to do a new query
262 // with this filter.
263 String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
264 // Don't do anything if the filter hasn't actually changed.
265 // Prevents restarting the loader when restoring state.
266 if (mCurFilter == null && newFilter == null) { return true; }
267 if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; }
268 mCurFilter = newFilter;
269 getLoaderManager().restartLoader(0, null, this);
270 return true;
271 }
272
273 @Override
274 public boolean onQueryTextSubmit(String query)
275 {
276 // Don't care about this.
277 return true;
278 }
279
280 @Override
281 public void onListItemClick(ListView l, View v, int position, long id)
282 {
283 // Insert desired behavior here.
284 Log.i("FragmentComplexList", "Item clicked: " + id);
285 }
286
287 @Override
288 public Loader<Cursor> onCreateLoader(int id, Bundle args)
289 {
290
291 //return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
292
293 // This is called when a new Loader needs to be created. This
294 // sample only has one Loader, so we don't care about the ID.
295 // First, pick the base URI to use depending on whether we are
296 // currently filtering.
297 Uri baseUri;
298
299 if (mCurFilter != null) {
300 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
301 } else {
302 baseUri = Contacts.CONTENT_URI;
303 }
304
305 // Now create and return a CursorLoader that will take care of
306 // creating a Cursor for the data being displayed.
307 String select = "((" + Contacts.DISPLAY_NAME
308 + " NOTNULL) AND ("
309 + Contacts.HAS_PHONE_NUMBER
310 + "=1) AND ("
311 + Contacts.DISPLAY_NAME
312 + " != '' ))";
313 //String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
314
315 return new CursorLoader(getActivity(),
316 baseUri,
317 CONTACTS_SUMMARY_PROJECTION,
318 select,
319 null,
320 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
321 }
322
323 @Override
324 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
325 {
326 // Swap the new cursor in. (The framework will take care of closing the
327 // old cursor once we return.)
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400328 // mAdapter.swapCursor(data);
Adrien Béraudffd32412012-08-07 18:39:23 -0400329
330 // The list should now be shown.
331 /*
332 if (isResumed()) {
333 setListShown(true);
334 } else {
335 setListShownNoAnimation(true);
336 }*/
337 }
338
339 @Override
340 public void onLoaderReset(Loader<Cursor> loader)
341 {
342 // This is called when the last Cursor provided to onLoadFinished()
343 // above is about to be closed. We need to make sure we are no
344 // longer using it.
Alexandre Savard4d88fee2012-09-17 15:11:38 -0400345 // mAdapter.swapCursor(null);
Adrien Béraudffd32412012-08-07 18:39:23 -0400346 }
Adrien Béraudffd32412012-08-07 18:39:23 -0400347}