blob: 713a938ad91d340d65f79fd4e06cfa1fdebcb687 [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
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
Alexandre Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.loaders;
alisiond295ec22013-05-17 10:12:13 -040033
34import java.util.ArrayList;
35
Alexandre Lision064e1e02013-10-01 16:18:42 -040036import org.sflphone.model.CallContact;
37
alisiond295ec22013-05-17 10:12:13 -040038import android.content.AsyncTaskLoader;
39import android.content.Context;
40import android.database.Cursor;
41import android.net.Uri;
42import android.os.Bundle;
43import android.provider.ContactsContract.CommonDataKinds.Phone;
44import android.provider.ContactsContract.CommonDataKinds.SipAddress;
45import android.provider.ContactsContract.Contacts;
alisiond295ec22013-05-17 10:12:13 -040046
alisiond295ec22013-05-17 10:12:13 -040047public class ContactsLoader extends AsyncTaskLoader<Bundle> {
48
alision2ec64f92013-06-17 17:28:58 -040049// private static final String TAG = ContactsLoader.class.getSimpleName();
alisiond295ec22013-05-17 10:12:13 -040050
51 // These are the Contacts rows that we will retrieve.
52 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, Contacts.STARRED };
53 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
54 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
55
56 String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
57 Uri baseUri;
58
59 public ContactsLoader(Context context, Uri u) {
60 super(context);
61 baseUri = u;
62 }
63
64 @Override
65 public Bundle loadInBackground() {
66 ArrayList<CallContact> contacts = new ArrayList<CallContact>();
67 ArrayList<CallContact> starred = new ArrayList<CallContact>();
68
69 Cursor result = getContext().getContentResolver().query(baseUri, CONTACTS_SUMMARY_PROJECTION, select, null,
70 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
71 int iID = result.getColumnIndex(Contacts._ID);
72 int iName = result.getColumnIndex(Contacts.DISPLAY_NAME);
73 int iPhoto = result.getColumnIndex(Contacts.PHOTO_ID);
74 int iStarred = result.getColumnIndex(Contacts.STARRED);
75 CallContact.ContactBuilder builder = CallContact.ContactBuilder.getInstance();
alision55c36cb2013-06-14 14:57:38 -040076
alisiond295ec22013-05-17 10:12:13 -040077 while (result.moveToNext()) {
78 builder.startNewContact(result.getLong(iID), result.getString(iName), result.getLong(iPhoto));
79
alision55c36cb2013-06-14 14:57:38 -040080// Cursor cPhones = getContext().getContentResolver().query(Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION,
81// Phone.CONTACT_ID + " =" + result.getLong(iID), null, null);
alisiond295ec22013-05-17 10:12:13 -040082
alision55c36cb2013-06-14 14:57:38 -040083// while (cPhones.moveToNext()) {
84// builder.addPhoneNumber(cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)), cPhones.getInt(cPhones.getColumnIndex(Phone.TYPE)));
85//// Log.i(TAG,"Phone:"+cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)));
86// }
87// cPhones.close();
88//
89// Cursor cSip = getContext().getContentResolver().query(Phone.CONTENT_URI, CONTACTS_SIP_PROJECTION,
90// Phone.CONTACT_ID + "=" + result.getLong(iID), null, null);
91//
92// while (cSip.moveToNext()) {
93// builder.addSipNumber(cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)), cSip.getInt(cSip.getColumnIndex(SipAddress.TYPE)));
94//// Log.i(TAG,"Phone:"+cSip.getString(cSip.getColumnIndex(SipAddress.SIP_ADDRESS)));
95// }
96// cSip.close();
alisiond295ec22013-05-17 10:12:13 -040097
98 contacts.add(builder.build());
99 if (result.getInt(iStarred) == 1) {
100 starred.add(builder.build());
101 }
102
103 }
104
105 result.close();
106 Bundle toReturn = new Bundle();
107
108 toReturn.putParcelableArrayList("Contacts", contacts);
109 toReturn.putParcelableArrayList("Starred", starred);
110
111 return toReturn;
112 }
113}