Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 31 | package com.savoirfairelinux.sflphone.client; |
| 32 | |
| 33 | import android.app.ListFragment; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 34 | import android.app.LoaderManager; |
| 35 | import android.content.CursorLoader; |
| 36 | import android.content.Loader; |
| 37 | import android.database.Cursor; |
| 38 | import android.net.Uri; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 39 | import android.os.Bundle; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 40 | import android.provider.ContactsContract.CommonDataKinds; |
| 41 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 42 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 43 | import android.provider.ContactsContract.Contacts; |
| 44 | import android.provider.ContactsContract.Profile; |
| 45 | import android.text.TextUtils; |
| 46 | import android.text.TextUtils; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 47 | import android.view.LayoutInflater; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 48 | import android.view.Menu; |
| 49 | import android.view.MenuItem; |
| 50 | import android.view.MenuInflater; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 51 | import android.view.View; |
| 52 | import android.view.ViewGroup; |
| 53 | import android.widget.ListView; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 54 | import android.widget.SearchView; |
| 55 | import android.widget.SearchView.OnQueryTextListener; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 56 | import android.util.Log; |
| 57 | |
| 58 | import com.savoirfairelinux.sflphone.R; |
| 59 | |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 60 | public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 61 | { |
| 62 | ContactManager mContactManager; |
| 63 | CallElementList.CallElementAdapter mAdapter; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 64 | 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 Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public void onActivityCreated(Bundle savedInstanceState) |
| 74 | { |
| 75 | super.onActivityCreated(savedInstanceState); |
| 76 | |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 77 | // In order to onCreateOptionsMenu be called |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 78 | 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 Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 93 | 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 Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 105 | public void onListItemClick(ListView l, View v, int position, long id) |
| 106 | { |
| 107 | // Insert desired behavior here. |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 108 | CallContact contact = mContactManager.getContact(position); |
| 109 | Log.i("ContactListFragment", "Contact clicked: " + contact.getDisplayName()); |
Alexandre Savard | 800faa6 | 2012-09-19 13:18:57 -0400 | [diff] [blame] | 110 | |
| 111 | SipCall call = SipCall.getCallInstance(contact); |
| 112 | Log.i("ConatctListFragment", "OK"); |
| 113 | Log.i("ContactListFragment", "Number of calls " + SipCall.getNbCalls()); |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 114 | } |
| 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 Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 173 | } |
| 174 | } |