2
* ContactsContactAccessor.java
4
* Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
6
* This file is part of the Export Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/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 am.ed.exportcontacts;
26
import am.ed.exportcontacts.Exporter.ContactData;
27
import android.app.Activity;
28
import android.database.Cursor;
29
import android.provider.Contacts;
31
@SuppressWarnings( "deprecation" )
32
public class ContactsBackend implements Backend
34
Activity _activity = null;
35
Exporter _exporter = null;
38
public ContactsBackend( Activity activity, Exporter exporter )
45
public int getNumContacts()
47
Cursor cursor = _activity.managedQuery(
48
Contacts.People.CONTENT_URI,
51
}, null, null, null );
52
return cursor.getCount();
55
private int convertBackendTypeToType( Class< ? > cls, int type )
57
if( cls == Contacts.Phones.class )
61
case Contacts.Phones.TYPE_MOBILE:
62
return ContactData.TYPE_MOBILE;
63
case Contacts.Phones.TYPE_FAX_HOME:
64
return ContactData.TYPE_FAX_HOME;
65
case Contacts.Phones.TYPE_FAX_WORK:
66
return ContactData.TYPE_FAX_WORK;
67
case Contacts.Phones.TYPE_PAGER:
68
return ContactData.TYPE_PAGER;
69
case Contacts.Phones.TYPE_WORK:
70
return ContactData.TYPE_WORK;
72
return ContactData.TYPE_HOME;
75
else if( cls == Contacts.ContactMethods.class )
79
case Contacts.Phones.TYPE_WORK:
80
return ContactData.TYPE_WORK;
82
return ContactData.TYPE_HOME;
86
return ContactData.TYPE_HOME;
90
public boolean getNextContact( Exporter.ContactData contact )
96
_cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
100
Contacts.People.NOTES,
101
}, null, null, null );
104
// if there are no more contacts, abort
105
if( _cur == null || !_cur.moveToNext() ) {
110
// get this contact's id
111
Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
115
_cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
119
_cur.getString( _cur.getColumnIndex( Contacts.People.NOTES ) ) );
121
// add the organisations
122
Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
124
Contacts.Organizations.COMPANY,
125
Contacts.Organizations.TITLE,
126
}, Contacts.Organizations.PERSON_ID + " = ?",
127
new String[] { id.toString() },
128
Contacts.Organizations.ISPRIMARY + " DESC, " +
129
Contacts.Organizations.PERSON_ID + " ASC" );
130
while( cur.moveToNext() )
131
contact.addOrganisation( contact.new OrganisationDetail(
132
cur.getString( cur.getColumnIndex(
133
Contacts.Organizations.COMPANY ) ),
134
cur.getString( cur.getColumnIndex(
135
Contacts.Organizations.TITLE ) ) ) );
138
// add the phone numbers
139
cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
141
Contacts.Phones.NUMBER,
142
Contacts.Phones.TYPE,
143
}, Contacts.Phones.PERSON_ID + " = ?",
144
new String[] { id.toString() },
145
Contacts.Phones.ISPRIMARY + " DESC," +
146
Contacts.Phones.PERSON_ID + " ASC" );
147
while( cur.moveToNext() )
148
contact.addNumber( contact.new NumberDetail(
149
convertBackendTypeToType( Contacts.Phones.class,
150
cur.getInt( cur.getColumnIndex( Contacts.Phones.TYPE ) ) ),
151
cur.getString( cur.getColumnIndex(
152
Contacts.Phones.NUMBER ) ) ) );
155
// add the email and postal addresses
156
cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
158
Contacts.ContactMethods.KIND,
159
Contacts.ContactMethods.TYPE,
160
Contacts.ContactMethods.DATA,
162
Contacts.ContactMethods.PERSON_ID + " = ? AND " +
163
Contacts.ContactMethods.KIND + " IN( ?, ? )",
166
"" + Contacts.KIND_EMAIL,
167
"" + Contacts.KIND_POSTAL,
169
Contacts.ContactMethods.ISPRIMARY + " DESC," +
170
Contacts.ContactMethods.PERSON_ID + " ASC" );
171
while( cur.moveToNext() ) {
172
int kind = cur.getInt( cur.getColumnIndex(
173
Contacts.ContactMethods.KIND ) );
174
if( kind == Contacts.KIND_EMAIL )
175
contact.addEmail( contact.new EmailDetail(
176
convertBackendTypeToType( Contacts.ContactMethods.class,
177
cur.getInt( cur.getColumnIndex(
178
Contacts.ContactMethods.TYPE ) ) ),
179
cur.getString( cur.getColumnIndex(
180
Contacts.ContactMethods.DATA ) ) ) );
182
contact.addAddress( contact.new AddressDetail(
183
convertBackendTypeToType( Contacts.ContactMethods.class,
184
cur.getInt( cur.getColumnIndex(
185
Contacts.ContactMethods.TYPE ) ) ),
186
cur.getString( cur.getColumnIndex(
187
Contacts.ContactMethods.DATA ) ) ) );