blob: de15e6ba73bb0ee99c0809a403dcfbdfe6b911d2 [file] [log] [blame]
alisiond295ec22013-05-17 10:12:13 -04001package com.savoirfairelinux.sflphone.loaders;
2
3import java.util.ArrayList;
4
5import android.content.AsyncTaskLoader;
6import android.content.Context;
7import android.database.Cursor;
8import android.net.Uri;
9import android.os.Bundle;
10import android.provider.ContactsContract.CommonDataKinds.Phone;
11import android.provider.ContactsContract.CommonDataKinds.SipAddress;
12import android.provider.ContactsContract.Contacts;
alisiond295ec22013-05-17 10:12:13 -040013
14import com.savoirfairelinux.sflphone.model.CallContact;
15
16public class ContactsLoader extends AsyncTaskLoader<Bundle> {
17
alision2ec64f92013-06-17 17:28:58 -040018// private static final String TAG = ContactsLoader.class.getSimpleName();
alisiond295ec22013-05-17 10:12:13 -040019
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();
alision55c36cb2013-06-14 14:57:38 -040045
alisiond295ec22013-05-17 10:12:13 -040046 while (result.moveToNext()) {
47 builder.startNewContact(result.getLong(iID), result.getString(iName), result.getLong(iPhoto));
48
alision55c36cb2013-06-14 14:57:38 -040049// Cursor cPhones = getContext().getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION,
50// Phone.CONTACT_ID + " =" + result.getLong(iID), null, null);
alisiond295ec22013-05-17 10:12:13 -040051
alision55c36cb2013-06-14 14:57:38 -040052// 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();
alisiond295ec22013-05-17 10:12:13 -040066
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}