blob: 13bfe188c18cd6fba7fa3286d77aa01d2325f1fc [file] [log] [blame]
Alexandre Savard166dbb62012-09-18 09:37:27 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Savard <alexandre.savard@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 */
31package com.savoirfairelinux.sflphone.client;
32
33import android.content.ContentResolver;
34import android.content.Context;
35import android.database.Cursor;
36import android.provider.ContactsContract;
Alexandre Savard23240c12012-09-19 18:23:44 -040037import android.provider.ContactsContract.Contacts;
Alexandre Savard166dbb62012-09-18 09:37:27 -040038import android.util.Log;
39import java.util.ArrayList;
40
41public class ContactManager
42{
43 int mCount;
44 Context mContext;
45 ArrayList<CallContact> contactList;
46
Alexandre Savard23240c12012-09-19 18:23:44 -040047 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,
48 Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
49
Alexandre Savard166dbb62012-09-18 09:37:27 -040050 public ContactManager(Context context)
51 {
52 mCount = 0;
53 mContext = context;
54 contactList = new ArrayList<CallContact>();
55
56 loadContactList();
57 }
58
59 public void loadContactList()
60 {
61 ContentResolver resolver = mContext.getContentResolver();
62
Alexandre Savard23240c12012-09-19 18:23:44 -040063 Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, null, null, null);
Alexandre Savard166dbb62012-09-18 09:37:27 -040064 while(cursor.moveToNext()) {
65 mCount++;
66 String displayName = "";
67 String phoneNumber = "";
68 String emailAddress = "";
69
70 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
71
72 displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
73
74 /*
75 Cursor structuredName = resolver.query(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, null,
76 ContactsContract.Contacts._ID +" = "+ contactId, null, null);
77 if(structureName.getCount() > 0)
78 Log.i("loadContactList", "Got a given name");
79 */
80
81 /*
82 Cursor sipAddress = resolver.query(ContactsContract.Contacts.SIP_ADDRESS, null,
83 ContactsContract.Contacts._ID +" = "+ contactId, null, null);
84 if(sipAddress.getCount() > 0)
85 Log.i("loadContactList", "Got a sip address");
86 */
87
88 String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
89 if(hasPhone.compareTo("1") == 0) {
90 // You know it has a number so now query it like this
91 Cursor phones = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
92 ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
93 while(phones.moveToNext()) {
94 phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
95 }
96 phones.close();
97 }
98
99 Cursor emails = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
100 ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
101 while (emails.moveToNext()) {
102 // This would allow you get several email addresses
103 emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
104 }
105 emails.close();
106
107 contactList.add(new CallContact(displayName, phoneNumber, emailAddress));
108
109 }
110 cursor.close();
111 }
112
113 public int getNbContacts()
114 {
115 return mCount;
116 }
117
118 public CallContact getContact(int position)
119 {
120 return contactList.get(position);
121 }
Alexandre Savard38d6a152012-09-18 13:33:02 -0400122
123 public ArrayList<CallContact> getContactList()
124 {
125 return contactList;
126 }
Alexandre Savard166dbb62012-09-18 09:37:27 -0400127}
128