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