blob: 61ebcc3ac6231b4ee7597ea8ed4b9ceb600d59d1 [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;
13import android.util.Log;
14
15import com.savoirfairelinux.sflphone.model.CallContact;
16
17public 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
alisiond8c83882013-05-17 17:00:42 -040050 Cursor cPhones = getContext().getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION,
51 Phone.CONTACT_ID + " =" + result.getLong(iID), null, null);
alisiond295ec22013-05-17 10:12:13 -040052
alisiond8c83882013-05-17 17:00:42 -040053 while (cPhones.moveToNext()) {
54 builder.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
alision11e8e162013-05-28 10:33:14 -040055// Log.i(TAG,"Phone:"+cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)));
alisiond8c83882013-05-17 17:00:42 -040056 }
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)));
alision11e8e162013-05-28 10:33:14 -040064// Log.i(TAG,"Phone:"+cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)));
alisiond8c83882013-05-17 17:00:42 -040065 }
66 cSip.close();
alisiond295ec22013-05-17 10:12:13 -040067
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}