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