blob: a0d9e25da62adc5e4a216ea78d135dd8f95d7df0 [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;
37import android.util.Log;
38import java.util.ArrayList;
39
40public class ContactManager
41{
42 int mCount;
43 Context mContext;
44 ArrayList<CallContact> contactList;
45
46 public ContactManager(Context context)
47 {
48 mCount = 0;
49 mContext = context;
50 contactList = new ArrayList<CallContact>();
51
52 loadContactList();
53 }
54
55 public void loadContactList()
56 {
57 ContentResolver resolver = mContext.getContentResolver();
58
59 Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
60 while(cursor.moveToNext()) {
61 mCount++;
62 String displayName = "";
63 String phoneNumber = "";
64 String emailAddress = "";
65
66 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
67
68 displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
69
70 /*
71 Cursor structuredName = resolver.query(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, null,
72 ContactsContract.Contacts._ID +" = "+ contactId, null, null);
73 if(structureName.getCount() > 0)
74 Log.i("loadContactList", "Got a given name");
75 */
76
77 /*
78 Cursor sipAddress = resolver.query(ContactsContract.Contacts.SIP_ADDRESS, null,
79 ContactsContract.Contacts._ID +" = "+ contactId, null, null);
80 if(sipAddress.getCount() > 0)
81 Log.i("loadContactList", "Got a sip address");
82 */
83
84 String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
85 if(hasPhone.compareTo("1") == 0) {
86 // You know it has a number so now query it like this
87 Cursor phones = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
88 ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
89 while(phones.moveToNext()) {
90 phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
91 }
92 phones.close();
93 }
94
95 Cursor emails = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
96 ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
97 while (emails.moveToNext()) {
98 // This would allow you get several email addresses
99 emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
100 }
101 emails.close();
102
103 contactList.add(new CallContact(displayName, phoneNumber, emailAddress));
104
105 }
106 cursor.close();
107 }
108
109 public int getNbContacts()
110 {
111 return mCount;
112 }
113
114 public CallContact getContact(int position)
115 {
116 return contactList.get(position);
117 }
Alexandre Savard38d6a152012-09-18 13:33:02 -0400118
119 public ArrayList<CallContact> getContactList()
120 {
121 return contactList;
122 }
Alexandre Savard166dbb62012-09-18 09:37:27 -0400123}
124