2
* ContactsContactReader.java
4
* Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
6
* This file is part of the Export Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/android/export-contacts
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package org.waxworlds.edam.exportcontacts;
26
import android.app.Activity;
27
import android.database.Cursor;
28
import android.provider.Contacts;
30
public class ContactsContactReader implements ContactReader
32
Activity _activity = null;
33
Exporter _exporter = null;
36
public ContactsContactReader( Activity activity, Exporter exporter )
43
public int getNumContacts()
45
Cursor cursor = _activity.managedQuery(
46
Contacts.People.CONTENT_URI,
49
}, null, null, null );
50
return cursor.getCount();
54
public boolean getNextContact( Exporter.ContactData contact )
60
_cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
64
}, null, null, null );
67
// if there are no more contacts, abort
68
if( !_cur.moveToNext() ) {
73
// get this contact's id
74
Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
78
_cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
80
// get the organisations
81
Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
83
Contacts.Organizations.COMPANY,
84
Contacts.Organizations.TITLE,
85
}, Contacts.Organizations.PERSON_ID + " = ?",
86
new String[] { id.toString() },
87
Contacts.Organizations.ISPRIMARY + " DESC, " +
88
Contacts.Organizations.PERSON_ID + " ASC" );
89
while( cur.moveToNext() )
90
contact.addOrganisation( contact.new OrganisationDetail(
91
cur.getString( cur.getColumnIndex(
92
Contacts.Organizations.COMPANY ) ),
93
cur.getString( cur.getColumnIndex(
94
Contacts.Organizations.TITLE ) ) ) );
97
// get the phone numbers
98
cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
100
Contacts.Phones.NUMBER,
101
Contacts.Phones.TYPE,
102
}, Contacts.Phones.PERSON_ID + " = ?",
103
new String[] { id.toString() },
104
Contacts.Phones.ISPRIMARY + " DESC," +
105
Contacts.Phones.PERSON_ID + " ASC" );
106
while( cur.moveToNext() )
107
contact.addNumber( contact.new NumberDetail(
108
cur.getInt( cur.getColumnIndex(
109
Contacts.Phones.TYPE ) ),
110
cur.getString( cur.getColumnIndex(
111
Contacts.Phones.NUMBER ) ) ) );
114
// get the email and postal addresses
115
cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
117
Contacts.ContactMethods.KIND,
118
Contacts.ContactMethods.TYPE,
119
Contacts.ContactMethods.DATA,
121
Contacts.ContactMethods.PERSON_ID + " = ? AND " +
122
Contacts.ContactMethods.KIND + " IN( ?, ? )",
125
"" + Contacts.KIND_EMAIL,
126
"" + Contacts.KIND_POSTAL,
128
Contacts.ContactMethods.ISPRIMARY + " DESC," +
129
Contacts.ContactMethods.PERSON_ID + " ASC" );
130
while( cur.moveToNext() ) {
131
int kind = cur.getInt( cur.getColumnIndex(
132
Contacts.ContactMethods.KIND ) );
133
if( kind == Contacts.KIND_EMAIL )
134
contact.addEmail( contact.new EmailDetail(
135
cur.getInt( cur.getColumnIndex(
136
Contacts.ContactMethods.TYPE ) ),
137
cur.getString( cur.getColumnIndex(
138
Contacts.ContactMethods.DATA ) ) ) );
140
contact.addAddress( contact.new AddressDetail(
141
cur.getInt( cur.getColumnIndex(
142
Contacts.ContactMethods.TYPE ) ),
143
cur.getString( cur.getColumnIndex(
144
Contacts.ContactMethods.DATA ) ) ) );