Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 3 | * |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 4 | * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 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 | |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 33 | import android.app.Activity; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 34 | import android.app.ListFragment; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 35 | import android.app.LoaderManager; |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 36 | import android.app.AlertDialog; |
| 37 | import android.app.AlertDialog.Builder; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 38 | import android.content.ContentResolver; |
| 39 | import android.content.ContentUris; |
| 40 | import android.content.Context; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 41 | import android.content.CursorLoader; |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 42 | import android.content.DialogInterface; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 43 | import android.content.Loader; |
| 44 | import android.database.Cursor; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 45 | import android.graphics.Bitmap; |
| 46 | import android.graphics.BitmapFactory; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 47 | import android.net.Uri; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 48 | import android.os.Bundle; |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 49 | import android.os.RemoteException; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 50 | import android.provider.*; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 51 | import android.provider.ContactsContract.CommonDataKinds; |
| 52 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 53 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 54 | import android.provider.ContactsContract.Contacts; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 55 | import android.text.TextUtils; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 56 | import android.view.LayoutInflater; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 57 | import android.view.Menu; |
| 58 | import android.view.MenuItem; |
| 59 | import android.view.MenuInflater; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 60 | import android.view.View; |
| 61 | import android.view.ViewGroup; |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 62 | import android.widget.Button; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 63 | import android.widget.CursorAdapter; |
| 64 | import android.widget.ImageView; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 65 | import android.widget.ListView; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 66 | import android.widget.TextView; |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 67 | import android.widget.Toast; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 68 | import android.widget.SearchView; |
Alexandre Savard | 3143399 | 2012-09-20 15:19:49 -0400 | [diff] [blame] | 69 | import android.widget.AdapterView; |
| 70 | import android.widget.AdapterView.OnItemLongClickListener; |
| 71 | import android.widget.AdapterView.OnItemSelectedListener; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 72 | import android.widget.SearchView.OnQueryTextListener; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 73 | import android.util.Log; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 74 | import java.io.InputStream; |
| 75 | import java.util.concurrent.ExecutorService; |
| 76 | import java.util.concurrent.Executors; |
| 77 | import java.util.List; |
| 78 | import java.util.ArrayList; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 79 | |
| 80 | import com.savoirfairelinux.sflphone.R; |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 81 | import com.savoirfairelinux.sflphone.service.ISipService; |
Alexandre Savard | bb6f187 | 2012-11-01 17:25:55 -0400 | [diff] [blame] | 82 | import com.savoirfairelinux.sflphone.utils.AccountList; |
| 83 | import com.savoirfairelinux.sflphone.utils.AccountSelectionButton; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 84 | |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 85 | public class ContactListFragment extends ListFragment implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 86 | { |
Emeric Vigier | 3050b06 | 2012-11-02 14:19:00 -0400 | [diff] [blame] | 87 | final String TAG = "ContactListFragment"; |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 88 | ContactElementAdapter mAdapter; |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 89 | Context mContext; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 90 | String mCurFilter; |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 91 | private SFLPhoneHome sflphoneHome; |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 92 | private ISipService service; |
Alexandre Savard | bb6f187 | 2012-11-01 17:25:55 -0400 | [diff] [blame] | 93 | private AccountSelectionButton mAccountSelectionButton; |
| 94 | private AccountList mAccountList; |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 95 | |
| 96 | // These are the Contacts rows that we will retrieve. |
| 97 | static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, |
| 98 | Contacts.PHOTO_ID, Contacts.LOOKUP_KEY }; |
| 99 | static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE }; |
| 100 | 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] | 101 | |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 102 | @Override |
| 103 | public void onAttach(Activity activity) { |
| 104 | super.onAttach(activity); |
| 105 | sflphoneHome = (SFLPhoneHome) activity; |
| 106 | service = ((SFLphoneApplication) sflphoneHome.getApplication()).getSipService(); |
| 107 | Log.i(TAG, "onAttach() service " + service); |
| 108 | } |
| 109 | |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 110 | public static class InfosLoader implements Runnable |
| 111 | { |
| 112 | private View view; |
| 113 | private long cid; |
| 114 | private ContentResolver cr; |
| 115 | |
| 116 | public InfosLoader(Context context, View element, long contact_id) |
| 117 | { |
| 118 | cid = contact_id; |
| 119 | cr = context.getContentResolver(); |
| 120 | view = element; |
| 121 | } |
| 122 | |
| 123 | public static Bitmap loadContactPhoto(ContentResolver cr, long id) |
| 124 | { |
| 125 | Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); |
| 126 | InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); |
| 127 | if (input == null) { |
| 128 | return null; |
| 129 | } |
| 130 | return BitmapFactory.decodeStream(input); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void run() |
| 135 | { |
| 136 | final Bitmap photo_bmp = loadContactPhoto(cr, cid); |
| 137 | |
| 138 | Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI, |
| 139 | CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?", |
| 140 | new String[] { Long.toString(cid) }, |
| 141 | null); |
| 142 | |
| 143 | final List<String> numbers = new ArrayList<String>(); |
| 144 | while (phones.moveToNext()) { |
| 145 | String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER)); |
| 146 | // int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE)); |
| 147 | numbers.add(number); |
| 148 | } |
| 149 | phones.close(); |
| 150 | |
| 151 | final Bitmap bmp = photo_bmp; |
| 152 | view.post(new Runnable() |
| 153 | { |
| 154 | @Override |
| 155 | public void run() |
| 156 | { |
| 157 | } |
| 158 | }); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public static class ContactElementAdapter extends CursorAdapter |
| 163 | { |
| 164 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
| 165 | |
| 166 | public ContactElementAdapter(Context context, Cursor c) |
| 167 | { |
| 168 | super(context, c, 0); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public View newView(Context context, Cursor cursor, ViewGroup parent) |
| 173 | { |
| 174 | LayoutInflater inflater = LayoutInflater.from(context); |
| 175 | View v = inflater.inflate(R.layout.call_element, parent, false); |
| 176 | bindView(v, context, cursor); |
| 177 | return v; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void bindView(final View view, Context context, Cursor cursor) |
| 182 | { |
| 183 | final long contact_id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); |
| 184 | final String display_name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)); |
| 185 | // final long photo_uri_string = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID)); |
| 186 | // final String photo_uri_string = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI)); |
| 187 | |
| 188 | TextView display_name_txt = (TextView) view.findViewById(R.id.display_name); |
| 189 | display_name_txt.setText(display_name); |
| 190 | |
| 191 | ImageView photo_view = (ImageView) view.findViewById(R.id.photo); |
| 192 | photo_view.setVisibility(View.GONE); |
| 193 | |
| 194 | infos_fetcher.execute(new InfosLoader(context, view, contact_id)); |
| 195 | } |
| 196 | }; |
| 197 | |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 198 | public ContactListFragment() |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 199 | { |
| 200 | super(); |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Alexandre Savard | bb6f187 | 2012-11-01 17:25:55 -0400 | [diff] [blame] | 203 | public void setAccountList(AccountList accountList) { |
| 204 | mAccountList = accountList; |
| 205 | } |
| 206 | |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 207 | @Override |
| 208 | public void onActivityCreated(Bundle savedInstanceState) |
| 209 | { |
| 210 | super.onActivityCreated(savedInstanceState); |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 211 | mContext = getActivity(); |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 212 | |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 213 | // In order to onCreateOptionsMenu be called |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 214 | setHasOptionsMenu(true); |
| 215 | |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 216 | mAdapter = new ContactElementAdapter(getActivity(), null); |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 217 | setListAdapter(mAdapter); |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 218 | |
| 219 | getLoaderManager().initLoader(0, null, this); |
Alexandre Savard | 3143399 | 2012-09-20 15:19:49 -0400 | [diff] [blame] | 220 | |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 221 | final Context context = getActivity(); |
Alexandre Savard | 3143399 | 2012-09-20 15:19:49 -0400 | [diff] [blame] | 222 | ListView lv = getListView(); |
| 223 | lv.setOnItemLongClickListener(new OnItemLongClickListener() { |
| 224 | @Override |
| 225 | public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) { |
| 226 | Log.i(TAG, "On Long Click"); |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 227 | final CharSequence[] items = {"Make Call", "Send Message", "Add to Conference"}; |
| 228 | final SipCall.CallInfo info = new SipCall.CallInfo(); |
| 229 | info.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText(); |
| 230 | info.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText(); |
Alexandre Savard | 85936b9 | 2012-10-24 11:23:53 -0400 | [diff] [blame] | 231 | // TODO getCallInstnace should be implemented in SipCallList |
| 232 | // final SipCall call = SipCall.getCallInstance(info); |
| 233 | final SipCall call = new SipCall(info); |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 234 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 235 | builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName) |
| 236 | .setCancelable(true) |
| 237 | .setItems(items, new DialogInterface.OnClickListener() { |
| 238 | public void onClick(DialogInterface dialog, int item) { |
| 239 | Log.i(TAG, "Selected " + items[item]); |
| 240 | switch (item) { |
| 241 | case 0: |
Alexandre Savard | a949eec | 2012-10-25 17:30:49 -0400 | [diff] [blame] | 242 | call.placeCallUpdateUi(); |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 243 | break; |
| 244 | case 1: |
| 245 | call.sendTextMessage(); |
| 246 | // Need to hangup this call immediately since no way to do it after this action |
Alexandre Savard | 4f42ade | 2012-10-24 18:03:31 -0400 | [diff] [blame] | 247 | call.notifyServiceHangup(service); |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 248 | break; |
| 249 | case 2: |
| 250 | call.addToConference(); |
| 251 | // Need to hangup this call immediately since no way to do it after this action |
Alexandre Savard | 4f42ade | 2012-10-24 18:03:31 -0400 | [diff] [blame] | 252 | call.notifyServiceHangup(service); |
Alexandre Savard | aef9d80 | 2012-09-20 17:31:32 -0400 | [diff] [blame] | 253 | break; |
| 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | }); |
| 259 | AlertDialog alert = builder.create(); |
| 260 | alert.show(); |
| 261 | |
Alexandre Savard | 3143399 | 2012-09-20 15:19:49 -0400 | [diff] [blame] | 262 | return true; |
| 263 | } |
| 264 | }); |
Alexandre Savard | 0014d23 | 2012-09-24 10:31:23 -0400 | [diff] [blame] | 265 | |
| 266 | lv.setOnItemSelectedListener(new OnItemSelectedListener() { |
| 267 | @Override |
| 268 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 269 | Log.i(TAG, "On Item Selected"); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void onNothingSelected(AdapterView<?> parent) { |
| 274 | Log.i(TAG, "On Nothing Selected"); |
| 275 | } |
| 276 | }); |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) |
| 281 | { |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 282 | View inflatedView = inflater.inflate(R.layout.call_element_list, container, false); |
| 283 | |
Alexandre Savard | 5ea84ae | 2012-10-31 16:41:44 -0400 | [diff] [blame] | 284 | // Button accountSelectionButton = (Button) inflatedView.findViewById(R.id.account_selection_button); |
Alexandre Savard | bb6f187 | 2012-11-01 17:25:55 -0400 | [diff] [blame] | 285 | mAccountSelectionButton = (AccountSelectionButton) inflatedView.findViewById(R.id.account_selection_button); |
| 286 | mAccountList.addManagementUI(mAccountSelectionButton); |
Alexandre Savard | af0f3c6 | 2012-11-01 18:35:56 -0400 | [diff] [blame] | 287 | mAccountSelectionButton.setAccountList(mAccountList); |
Alexandre Savard | 817dc50 | 2012-10-22 11:47:29 -0400 | [diff] [blame] | 288 | |
| 289 | return inflatedView; |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 293 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) |
| 294 | { |
| 295 | // Place an action bar item for searching |
| 296 | MenuItem item = menu.add("Search"); |
| 297 | item.setIcon(R.drawable.ic_menu_search); |
| 298 | item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); |
| 299 | SearchView sv = new SearchView(getActivity()); |
| 300 | sv.setOnQueryTextListener(this); |
| 301 | item.setActionView(sv); |
| 302 | } |
| 303 | |
| 304 | @Override |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 305 | public void onListItemClick(ListView l, View v, int position, long id) |
| 306 | { |
| 307 | // Insert desired behavior here. |
Alexandre Savard | 7a46f54 | 2012-09-20 11:23:33 -0400 | [diff] [blame] | 308 | SipCall.CallInfo callInfo = new SipCall.CallInfo(); |
| 309 | callInfo.mDisplayName = (String) ((TextView) v.findViewById(R.id.display_name)).getText(); |
| 310 | callInfo.mPhone = (String) ((TextView) v.findViewById(R.id.phones)).getText(); |
| 311 | Log.i(TAG, "Contact clicked: " + callInfo.mDisplayName + ", Phone number: " + callInfo.mPhone); |
Alexandre Savard | a140465 | 2012-09-20 13:34:08 -0400 | [diff] [blame] | 312 | |
Alexandre Savard | 85936b9 | 2012-10-24 11:23:53 -0400 | [diff] [blame] | 313 | // TODO This should be implemented in CallList |
| 314 | // int nbCallBefore = SipCall.getNbCalls(); |
| 315 | // SipCall call = SipCall.getCallInstance(callInfo); |
| 316 | // Log.i(TAG, "Number of calls " + SipCall.getNbCalls()); |
| 317 | // int nbCallAfter = SipCall.getNbCalls(); |
| 318 | SipCall call = new SipCall(callInfo); |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 319 | |
Alexandre Savard | 85936b9 | 2012-10-24 11:23:53 -0400 | [diff] [blame] | 320 | // if(nbCallAfter > nbCallBefore) |
| 321 | // call.placeCall(); |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public boolean onQueryTextChange(String newText) |
| 326 | { |
| 327 | // Called when the action bar search text has changed. Update |
| 328 | // the search filter, and restart the loader to do a new query |
| 329 | // with this filter. |
| 330 | String newFilter = !TextUtils.isEmpty(newText) ? newText : null; |
| 331 | // Don't do anything if the filter hasn't actually changed. |
| 332 | // Prefents restarting the loader when restoring state. |
| 333 | if (mCurFilter == null && newFilter == null) { return true; } |
| 334 | if (mCurFilter != null && mCurFilter.equals(newFilter)) { return true; } |
| 335 | mCurFilter = newFilter; |
| 336 | getLoaderManager().restartLoader(0, null, this); |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public boolean onQueryTextSubmit(String query) |
| 342 | { |
| 343 | // Return false to let the SearchView perform the default action |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public Loader<Cursor> onCreateLoader(int id, Bundle args) |
| 349 | { |
| 350 | Uri baseUri; |
| 351 | |
| 352 | if(mCurFilter != null) { |
| 353 | baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter)); |
| 354 | } else { |
| 355 | baseUri = Contacts.CONTENT_URI; |
| 356 | } |
| 357 | |
| 358 | // Now create and return a CursorLoader that will take care of |
| 359 | // creating a Cursor for the data being displayed. |
| 360 | String select = "((" + Contacts.DISPLAY_NAME |
| 361 | + " NOTNULL) AND (" |
| 362 | + Contacts.HAS_PHONE_NUMBER |
| 363 | + "=1) AND (" |
| 364 | + Contacts.DISPLAY_NAME |
| 365 | + " != '' ))"; |
| 366 | |
| 367 | return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION, |
| 368 | select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) |
| 373 | { |
| 374 | // Swap the new cursor in. |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 375 | mAdapter.swapCursor(data); |
Alexandre Savard | 6668cf8 | 2012-09-19 10:59:21 -0400 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public void onLoaderReset(Loader<Cursor> loader) |
| 380 | { |
| 381 | // Thi is called when the last Cursor provided to onLoadFinished |
Alexandre Savard | 23240c1 | 2012-09-19 18:23:44 -0400 | [diff] [blame] | 382 | mAdapter.swapCursor(null); |
Alexandre Savard | a04c520 | 2012-09-18 17:19:53 -0400 | [diff] [blame] | 383 | } |
| 384 | } |