alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.loaders; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | |
| 5 | import android.content.AsyncTaskLoader; |
| 6 | import android.content.Context; |
| 7 | import android.database.Cursor; |
| 8 | import android.net.Uri; |
| 9 | import android.os.Bundle; |
| 10 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 11 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 12 | import android.provider.ContactsContract.Contacts; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 13 | |
| 14 | import com.savoirfairelinux.sflphone.model.CallContact; |
| 15 | |
| 16 | public class ContactsLoader extends AsyncTaskLoader<Bundle> { |
| 17 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 18 | // private static final String TAG = ContactsLoader.class.getSimpleName(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 19 | |
| 20 | // These are the Contacts rows that we will retrieve. |
| 21 | static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, Contacts.STARRED }; |
| 22 | static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE }; |
| 23 | static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE }; |
| 24 | |
| 25 | String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))"; |
| 26 | Uri baseUri; |
| 27 | |
| 28 | public ContactsLoader(Context context, Uri u) { |
| 29 | super(context); |
| 30 | baseUri = u; |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public Bundle loadInBackground() { |
| 35 | ArrayList<CallContact> contacts = new ArrayList<CallContact>(); |
| 36 | ArrayList<CallContact> starred = new ArrayList<CallContact>(); |
| 37 | |
| 38 | Cursor result = getContext().getContentResolver().query(baseUri, CONTACTS_SUMMARY_PROJECTION, select, null, |
| 39 | Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); |
| 40 | int iID = result.getColumnIndex(Contacts._ID); |
| 41 | int iName = result.getColumnIndex(Contacts.DISPLAY_NAME); |
| 42 | int iPhoto = result.getColumnIndex(Contacts.PHOTO_ID); |
| 43 | int iStarred = result.getColumnIndex(Contacts.STARRED); |
| 44 | CallContact.ContactBuilder builder = CallContact.ContactBuilder.getInstance(); |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 45 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 46 | while (result.moveToNext()) { |
| 47 | builder.startNewContact(result.getLong(iID), result.getString(iName), result.getLong(iPhoto)); |
| 48 | |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 49 | // Cursor cPhones = getContext().getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, |
| 50 | // Phone.CONTACT_ID + " =" + result.getLong(iID), null, null); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 51 | |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 52 | // while (cPhones.moveToNext()) { |
| 53 | // builder.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE))); |
| 54 | //// Log.i(TAG,"Phone:"+cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER))); |
| 55 | // } |
| 56 | // cPhones.close(); |
| 57 | // |
| 58 | // Cursor cSip = getContext().getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION, |
| 59 | // Phone.CONTACT_ID + "=" + result.getLong(iID), null, null); |
| 60 | // |
| 61 | // while (cSip.moveToNext()) { |
| 62 | // builder.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE))); |
| 63 | //// Log.i(TAG,"Phone:"+cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS))); |
| 64 | // } |
| 65 | // cSip.close(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 66 | |
| 67 | contacts.add(builder.build()); |
| 68 | if (result.getInt(iStarred) == 1) { |
| 69 | starred.add(builder.build()); |
| 70 | } |
| 71 | |
| 72 | } |
| 73 | |
| 74 | result.close(); |
| 75 | Bundle toReturn = new Bundle(); |
| 76 | |
| 77 | toReturn.putParcelableArrayList("Contacts", contacts); |
| 78 | toReturn.putParcelableArrayList("Starred", starred); |
| 79 | |
| 80 | return toReturn; |
| 81 | } |
| 82 | } |