blob: 6e83c37df9856befd4077dcf8385a83dea446482 [file] [log] [blame]
Alexandre Savarda04c5202012-09-18 17:19:53 -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 android.app.ListFragment;
Alexandre Savard6668cf82012-09-19 10:59:21 -040034import android.app.LoaderManager;
35import android.content.CursorLoader;
36import android.content.Loader;
37import android.database.Cursor;
38import android.net.Uri;
Alexandre Savarda04c5202012-09-18 17:19:53 -040039import android.os.Bundle;
Alexandre Savard6668cf82012-09-19 10:59:21 -040040import android.provider.ContactsContract.CommonDataKinds;
41import android.provider.ContactsContract.CommonDataKinds.Phone;
42import android.provider.ContactsContract.CommonDataKinds.SipAddress;
43import android.provider.ContactsContract.Contacts;
44import android.provider.ContactsContract.Profile;
45import android.text.TextUtils;
46import android.text.TextUtils;
Alexandre Savarda04c5202012-09-18 17:19:53 -040047import android.view.LayoutInflater;
Alexandre Savard6668cf82012-09-19 10:59:21 -040048import android.view.Menu;
49import android.view.MenuItem;
50import android.view.MenuInflater;
Alexandre Savarda04c5202012-09-18 17:19:53 -040051import android.view.View;
52import android.view.ViewGroup;
53import android.widget.ListView;
Alexandre Savard6668cf82012-09-19 10:59:21 -040054import android.widget.SearchView;
55import android.widget.SearchView.OnQueryTextListener;
Alexandre Savarda04c5202012-09-18 17:19:53 -040056import android.util.Log;
57
58import com.savoirfairelinux.sflphone.R;
59
Alexandre Savard6668cf82012-09-19 10:59:21 -040060public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>
Alexandre Savarda04c5202012-09-18 17:19:53 -040061{
62 ContactManager mContactManager;
63 CallElementList.CallElementAdapter mAdapter;
Alexandre Savard6668cf82012-09-19 10:59:21 -040064 String mCurFilter;
65
66 // These are the Contacts rows that we will retrieve.
67 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
68 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
69 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
70 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
Alexandre Savarda04c5202012-09-18 17:19:53 -040071
72 @Override
73 public void onActivityCreated(Bundle savedInstanceState)
74 {
75 super.onActivityCreated(savedInstanceState);
76
Alexandre Savard6668cf82012-09-19 10:59:21 -040077 // In order to onCreateOptionsMenu be called
Alexandre Savarda04c5202012-09-18 17:19:53 -040078 setHasOptionsMenu(true);
79
80 mContactManager = new ContactManager(getActivity());
81
82 mAdapter = new CallElementList.CallElementAdapter(getActivity(), mContactManager);
83 setListAdapter(mAdapter);
84 }
85
86 @Override
87 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
88 {
89 return inflater.inflate(R.layout.call_element_list, container, false);
90 }
91
92 @Override
Alexandre Savard6668cf82012-09-19 10:59:21 -040093 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
94 {
95 // Place an action bar item for searching
96 MenuItem item = menu.add("Search");
97 item.setIcon(R.drawable.ic_menu_search);
98 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
99 SearchView sv = new SearchView(getActivity());
100 sv.setOnQueryTextListener(this);
101 item.setActionView(sv);
102 }
103
104 @Override
Alexandre Savarda04c5202012-09-18 17:19:53 -0400105 public void onListItemClick(ListView l, View v, int position, long id)
106 {
107 // Insert desired behavior here.
Alexandre Savard6668cf82012-09-19 10:59:21 -0400108 CallContact contact = mContactManager.getContact(position);
109 Log.i("ContactListFragment", "Contact clicked: " + contact.getDisplayName());
Alexandre Savard800faa62012-09-19 13:18:57 -0400110
111 SipCall call = SipCall.getCallInstance(contact);
112 Log.i("ConatctListFragment", "OK");
113 Log.i("ContactListFragment", "Number of calls " + SipCall.getNbCalls());
Alexandre Savard6668cf82012-09-19 10:59:21 -0400114 }
115
116 @Override
117 public boolean onQueryTextChange(String newText)
118 {
119 // Called when the action bar search text has changed. Update
120 // the search filter, and restart the loader to do a new query
121 // with this filter.
122 String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
123 // Don't do anything if the filter hasn't actually changed.
124 // Prefents restarting the loader when restoring state.
125 if (mCurFilter == null && newFilter == null) { return true; }
126 if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; }
127 mCurFilter = newFilter;
128 getLoaderManager().restartLoader(0, null, this);
129 return true;
130 }
131
132 @Override
133 public boolean onQueryTextSubmit(String query)
134 {
135 // Return false to let the SearchView perform the default action
136 return false;
137 }
138
139 @Override
140 public Loader<Cursor> onCreateLoader(int id, Bundle args)
141 {
142 Uri baseUri;
143
144 if(mCurFilter != null) {
145 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
146 } else {
147 baseUri = Contacts.CONTENT_URI;
148 }
149
150 // Now create and return a CursorLoader that will take care of
151 // creating a Cursor for the data being displayed.
152 String select = "((" + Contacts.DISPLAY_NAME
153 + " NOTNULL) AND ("
154 + Contacts.HAS_PHONE_NUMBER
155 + "=1) AND ("
156 + Contacts.DISPLAY_NAME
157 + " != '' ))";
158
159 return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION,
160 select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
161 }
162
163 @Override
164 public void onLoadFinished(Loader<Cursor> loader, Cursor data)
165 {
166 // Swap the new cursor in.
167 }
168
169 @Override
170 public void onLoaderReset(Loader<Cursor> loader)
171 {
172 // Thi is called when the last Cursor provided to onLoadFinished
Alexandre Savarda04c5202012-09-18 17:19:53 -0400173 }
174}