blob: 0cf95b49afc2299dd8c1677b3b3635c731bf90db [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 Savarda04c5202012-09-18 17:19:53 -040081
Alexandre Savard6668cf82012-09-19 10:59:21 -040082public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
Alexandre Savarda04c5202012-09-18 17:19:53 -040083{
Alexandre Savard7a46f542012-09-20 11:23:33 -040084 final String TAG = "ConatctListFragment";
Alexandre Savard23240c12012-09-19 18:23:44 -040085 ContactElementAdapter mAdapter;
Alexandre Savardaef9d802012-09-20 17:31:32 -040086 Context mContext;
Alexandre Savard6668cf82012-09-19 10:59:21 -040087 String mCurFilter;
Alexandre Savard817dc502012-10-22 11:47:29 -040088 private ISipService service;
Alexandre Savard6668cf82012-09-19 10:59:21 -040089
90 // These are the Contacts rows that we will retrieve.
91 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
92 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
93 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
94 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Alexandre Savarda04c5202012-09-18 17:19:53 -040095
Alexandre Savard23240c12012-09-19 18:23:44 -040096 public static class InfosLoader implements Runnable
97 {
98 private View view;
99 private long cid;
100 private ContentResolver cr;
101
102 public InfosLoader(Context context, View element, long contact_id)
103 {
104 cid = contact_id;
105 cr = context.getContentResolver();
106 view = element;
107 }
108
109 public static Bitmap loadContactPhoto(ContentResolver cr, long id)
110 {
111 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
112 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
113 if (input == null) {
114 return null;
115 }
116 return BitmapFactory.decodeStream(input);
117 }
118
119 @Override
120 public void run()
121 {
122 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
123
124 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI,
125 CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?",
126 new String[] { Long.toString(cid) },
127 null);
128
129 final List<String> numbers = new ArrayList<String>();
130 while (phones.moveToNext()) {
131 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
132 // int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
133 numbers.add(number);
134 }
135 phones.close();
136
137 final Bitmap bmp = photo_bmp;
138 view.post(new Runnable()
139 {
140 @Override
141 public void run()
142 {
143 }
144 });
145 }
146 }
147
148 public static class ContactElementAdapter extends CursorAdapter
149 {
150 private ExecutorService infos_fetcher = Executors.newCachedThreadPool();
151
152 public ContactElementAdapter(Context context, Cursor c)
153 {
154 super(context, c, 0);
155 }
156
157 @Override
158 public View newView(Context context, Cursor cursor, ViewGroup parent)
159 {
160 LayoutInflater inflater = LayoutInflater.from(context);
161 View v = inflater.inflate(R.layout.call_element, parent, false);
162 bindView(v, context, cursor);
163 return v;
164 }
165
166 @Override
167 public void bindView(final View view, Context context, Cursor cursor)
168 {
169 final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
170 final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
171 // final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));
172 // final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
173
174 TextView display_name_txt = (TextView) view.findViewById(R.id.display_name);
175 display_name_txt.setText(display_name);
176
177 ImageView photo_view = (ImageView) view.findViewById(R.id.photo);
178 photo_view.setVisibility(View.GONE);
179
180 infos_fetcher.execute(new InfosLoader(context, view, contact_id));
181 }
182 };
183
Alexandre Savard817dc502012-10-22 11:47:29 -0400184 public ContactListFragment(ISipService s)
Alexandre Savard23240c12012-09-19 18:23:44 -0400185 {
186 super();
Alexandre Savard817dc502012-10-22 11:47:29 -0400187 service = s;
188 }
189
190 public void setService(ISipService s)
191 {
192 service = s;
Alexandre Savard23240c12012-09-19 18:23:44 -0400193 }
194
Alexandre Savarda04c5202012-09-18 17:19:53 -0400195 @Override
196 public void onActivityCreated(Bundle savedInstanceState)
197 {
198 super.onActivityCreated(savedInstanceState);
Alexandre Savard817dc502012-10-22 11:47:29 -0400199 mContext = getActivity();
Alexandre Savarda04c5202012-09-18 17:19:53 -0400200
Alexandre Savard6668cf82012-09-19 10:59:21 -0400201 // In order to onCreateOptionsMenu be called
Alexandre Savarda04c5202012-09-18 17:19:53 -0400202 setHasOptionsMenu(true);
203
Alexandre Savard23240c12012-09-19 18:23:44 -0400204 mAdapter = new ContactElementAdapter(getActivity(), null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400205 setListAdapter(mAdapter);
Alexandre Savard23240c12012-09-19 18:23:44 -0400206
207 getLoaderManager().initLoader(0, null, this);
Alexandre Savard31433992012-09-20 15:19:49 -0400208
Alexandre Savardaef9d802012-09-20 17:31:32 -0400209 final Context context = getActivity();
Alexandre Savard31433992012-09-20 15:19:49 -0400210 ListView lv = getListView();
211 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
212 @Override
213 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
214 Log.i(TAG, "On Long Click");
Alexandre Savardaef9d802012-09-20 17:31:32 -0400215 final CharSequence[] items = {"Make Call", "Send Message", "Add to Conference"};
216 final SipCall.CallInfo info = new SipCall.CallInfo();
217 info.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText();
218 info.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText();
Alexandre Savard85936b92012-10-24 11:23:53 -0400219 // TODO getCallInstnace should be implemented in SipCallList
220 // final SipCall call = SipCall.getCallInstance(info);
221 final SipCall call = new SipCall(info);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400222 AlertDialog.Builder builder = new AlertDialog.Builder(context);
223 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName)
224 .setCancelable(true)
225 .setItems(items, new DialogInterface.OnClickListener() {
226 public void onClick(DialogInterface dialog, int item) {
227 Log.i(TAG, "Selected " + items[item]);
228 switch (item) {
229 case 0:
230 call.placeCall();
231 break;
232 case 1:
233 call.sendTextMessage();
234 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400235 call.hangup(service);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400236 break;
237 case 2:
238 call.addToConference();
239 // Need to hangup this call immediately since no way to do it after this action
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400240 call.hangup(service);
Alexandre Savardaef9d802012-09-20 17:31:32 -0400241 break;
242 default:
243 break;
244 }
245 }
246 });
247 AlertDialog alert = builder.create();
248 alert.show();
249
Alexandre Savard31433992012-09-20 15:19:49 -0400250 return true;
251 }
252 });
Alexandre Savard0014d232012-09-24 10:31:23 -0400253
254 lv.setOnItemSelectedListener(new OnItemSelectedListener() {
255 @Override
256 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
257 Log.i(TAG, "On Item Selected");
258 }
259
260 @Override
261 public void onNothingSelected(AdapterView<?> parent) {
262 Log.i(TAG, "On Nothing Selected");
263 }
264 });
Alexandre Savarda04c5202012-09-18 17:19:53 -0400265 }
266
267 @Override
268 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
269 {
Alexandre Savard817dc502012-10-22 11:47:29 -0400270 View inflatedView = inflater.inflate(R.layout.call_element_list, container, false);
271
272 Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button);
273 accountSelectionButton.setOnClickListener(new View.OnClickListener() {
274 public void onClick(View v) {
275 try {
276 ArrayList<String> list = (ArrayList<String>)service.getAccountList();
277 AccountSelectionDialog accountSelectionDialog = new AccountSelectionDialog(getActivity(), list);
278 accountSelectionDialog.show();
279 }
280 catch (RemoteException e) {
281 Log.e(TAG, "Remote exception", e);
282 }
283 }
284 });
285
286 return inflatedView;
Alexandre Savarda04c5202012-09-18 17:19:53 -0400287 }
288
289 @Override
Alexandre Savard6668cf82012-09-19 10:59:21 -0400290 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
291 {
292 // Place an action bar item for searching
293 MenuItem item = menu.add("Search");
294 item.setIcon(R.drawable.ic_menu_search);
295 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
296 SearchView sv = new SearchView(getActivity());
297 sv.setOnQueryTextListener(this);
298 item.setActionView(sv);
299 }
300
301 @Override
Alexandre Savarda04c5202012-09-18 17:19:53 -0400302 public void onListItemClick(ListView l, View v, int position, long id)
303 {
304 // Insert desired behavior here.
Alexandre Savard7a46f542012-09-20 11:23:33 -0400305 SipCall.CallInfo callInfo = new SipCall.CallInfo();
306 callInfo.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText();
307 callInfo.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText();
308 Log.i(TAG, "Contact clicked: " + callInfo.mDisplayName + ", Phone number: " + callInfo.mPhone);
Alexandre Savarda1404652012-09-20 13:34:08 -0400309
Alexandre Savard85936b92012-10-24 11:23:53 -0400310 // TODO This should be implemented in CallList
311 // int nbCallBefore = SipCall.getNbCalls();
312 // SipCall call = SipCall.getCallInstance(callInfo);
313 // Log.i(TAG, "Number of calls " + SipCall.getNbCalls());
314 // int nbCallAfter = SipCall.getNbCalls();
315 SipCall call = new SipCall(callInfo);
Alexandre Savard23240c12012-09-19 18:23:44 -0400316
Alexandre Savard85936b92012-10-24 11:23:53 -0400317 // if(nbCallAfter > nbCallBefore)
318 // call.placeCall();
Alexandre Savard6668cf82012-09-19 10:59:21 -0400319 }
320
321 @Override
322 public boolean onQueryTextChange(String newText)
323 {
324 // Called when the action bar search text has changed. Update
325 // the search filter, and restart the loader to do a new query
326 // with this filter.
327 String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
328 // Don't do anything if the filter hasn't actually changed.
329 // Prefents restarting the loader when restoring state.
330 if (mCurFilter == null && newFilter == null) { return true; }
331 if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; }
332 mCurFilter = newFilter;
333 getLoaderManager().restartLoader(0, null, this);
334 return true;
335 }
336
337 @Override
338 public boolean onQueryTextSubmit(String query)
339 {
340 // Return false to let the SearchView perform the default action
341 return false;
342 }
343
344 @Override
345 public Loader<Cursor> onCreateLoader(int id, Bundle args)
346 {
347 Uri baseUri;
348
349 if(mCurFilter != null) {
350 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
351 } else {
352 baseUri = Contacts.CONTENT_URI;
353 }
354
355 // Now create and return a CursorLoader that will take care of
356 // creating a Cursor for the data being displayed.
357 String select = "((" + Contacts.DISPLAY_NAME
358 + " NOTNULL) AND ("
359 + Contacts.HAS_PHONE_NUMBER
360 + "=1) AND ("
361 + Contacts.DISPLAY_NAME
362 + " != '' ))";
363
364 return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION,
365 select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
366 }
367
368 @Override
369 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
370 {
371 // Swap the new cursor in.
Alexandre Savard23240c12012-09-19 18:23:44 -0400372 mAdapter.swapCursor(data);
Alexandre Savard6668cf82012-09-19 10:59:21 -0400373 }
374
375 @Override
376 public void onLoaderReset(Loader<Cursor> loader)
377 {
378 // Thi is called when the last Cursor provided to onLoadFinished
Alexandre Savard23240c12012-09-19 18:23:44 -0400379 mAdapter.swapCursor(null);
Alexandre Savarda04c5202012-09-18 17:19:53 -0400380 }
381}