2
* ContactsContactReader.java
4
* Copyright (C) 2010 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 ) ) ) );
96
// get the phone numbers
97
cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
99
Contacts.Phones.NUMBER,
100
Contacts.Phones.TYPE,
101
}, Contacts.Phones.PERSON_ID + " = ?",
102
new String[] { id.toString() },
103
Contacts.Phones.ISPRIMARY + " DESC," +
104
Contacts.Phones.PERSON_ID + " ASC" );
105
while( cur.moveToNext() )
106
contact.addNumber( contact.new NumberDetail(
107
cur.getInt( cur.getColumnIndex(
108
Contacts.Phones.TYPE ) ),
109
cur.getString( cur.getColumnIndex(
110
Contacts.Phones.NUMBER ) ) ) );
112
// get the email and postal addresses
113
cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
115
Contacts.ContactMethods.KIND,
116
Contacts.ContactMethods.TYPE,
117
Contacts.ContactMethods.DATA,
119
Contacts.ContactMethods.PERSON_ID + " = ? AND " +
120
Contacts.ContactMethods.KIND + " IN( ?, ? )",
123
"" + Contacts.KIND_EMAIL,
124
"" + Contacts.KIND_POSTAL,
126
Contacts.ContactMethods.ISPRIMARY + " DESC," +
127
Contacts.ContactMethods.PERSON_ID + " ASC" );
128
while( cur.moveToNext() ) {
129
int kind = cur.getInt( cur.getColumnIndex(
130
Contacts.ContactMethods.KIND ) );
131
if( kind == Contacts.KIND_EMAIL )
132
contact.addEmail( contact.new EmailDetail(
133
cur.getInt( cur.getColumnIndex(
134
Contacts.ContactMethods.TYPE ) ),
135
cur.getString( cur.getColumnIndex(
136
Contacts.ContactMethods.DATA ) ) ) );
138
contact.addAddress( contact.new AddressDetail(
139
cur.getInt( cur.getColumnIndex(
140
Contacts.ContactMethods.TYPE ) ),
141
cur.getString( cur.getColumnIndex(
142
Contacts.ContactMethods.DATA ) ) ) );