2
* ContactsContractContactAccessor.java
4
* Copyright (C) 2011 to 2013 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.annotation.TargetApi;
28
import android.app.Activity;
29
import android.database.Cursor;
30
import android.provider.ContactsContract;
31
import android.provider.ContactsContract.CommonDataKinds;
34
public class ContactsContractBackend implements Backend
36
Activity _activity = null;
37
Exporter _exporter = null;
40
public ContactsContractBackend( Activity activity,
48
public int getNumContacts()
50
// get number of aggregate contacts
51
Cursor cur = _activity.getContentResolver().query(
52
ContactsContract.Contacts.CONTENT_URI,
54
ContactsContract.Contacts._ID,
55
}, null, null, null );
56
int ret = cur.getCount();
61
private int convertBackendTypeToType( Class< ? > cls, int type )
63
if( cls == CommonDataKinds.Phone.class )
67
case CommonDataKinds.Phone.TYPE_MOBILE:
68
return ContactData.TYPE_MOBILE;
69
case CommonDataKinds.Phone.TYPE_FAX_HOME:
70
return ContactData.TYPE_FAX_HOME;
71
case CommonDataKinds.Phone.TYPE_FAX_WORK:
72
return ContactData.TYPE_FAX_WORK;
73
case CommonDataKinds.Phone.TYPE_PAGER:
74
return ContactData.TYPE_PAGER;
75
case CommonDataKinds.Phone.TYPE_WORK:
76
return ContactData.TYPE_WORK;
78
return ContactData.TYPE_HOME;
81
else if( cls == CommonDataKinds.Email.class )
85
case CommonDataKinds.Email.TYPE_WORK:
86
return ContactData.TYPE_WORK;
88
return ContactData.TYPE_HOME;
91
else if( cls == CommonDataKinds.StructuredPostal.class )
95
case CommonDataKinds.StructuredPostal.TYPE_WORK:
96
return ContactData.TYPE_WORK;
98
return ContactData.TYPE_HOME;
102
return ContactData.TYPE_HOME;
106
public boolean getNextContact( Exporter.ContactData contact )
111
// get all aggregate contacts
112
_cur = _activity.getContentResolver().query(
113
ContactsContract.Contacts.CONTENT_URI,
115
ContactsContract.Contacts._ID,
116
ContactsContract.Contacts.DISPLAY_NAME,
117
}, null, null, null );
120
// if there are no more aggregate contacts, abort
121
if( _cur == null ) return false;
122
if( !_cur.moveToNext() ) {
128
// get this aggregate contact's id
129
Long id = _cur.getLong( _cur.getColumnIndex(
130
ContactsContract.Contacts._ID ) );
133
contact.setName( _cur.getString( _cur.getColumnIndex(
134
ContactsContract.Contacts.DISPLAY_NAME ) ) );
136
// get all contact data pertaining to the aggregate contact
137
Cursor cur = _activity.getContentResolver().query(
138
ContactsContract.Data.CONTENT_URI,
140
ContactsContract.Data.MIMETYPE,
141
ContactsContract.Data.IS_PRIMARY,
142
ContactsContract.Data.DATA1,
143
ContactsContract.Data.DATA2,
144
ContactsContract.Data.DATA4,
146
ContactsContract.Data.CONTACT_ID + " = ? AND " +
147
// column DELETED not found!?
148
// ContactsContract.Data.DELETED + " = 0 AND " +
149
ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ?, ? ) ",
151
String.valueOf( id ),
152
CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
153
CommonDataKinds.Email.CONTENT_ITEM_TYPE,
154
CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
155
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,
156
CommonDataKinds.Note.CONTENT_ITEM_TYPE,
157
CommonDataKinds.Event.CONTENT_ITEM_TYPE,
159
ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
160
ContactsContract.Data.RAW_CONTACT_ID + ", " +
161
ContactsContract.Data.IS_PRIMARY + " DESC" );
162
while( cur.moveToNext() )
164
String type = cur.getString( cur.getColumnIndex(
165
ContactsContract.Data.MIMETYPE ) );
168
if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) )
169
contact.addNumber( contact.new NumberDetail(
170
convertBackendTypeToType( CommonDataKinds.Phone.class,
171
cur.getInt( cur.getColumnIndex(
172
CommonDataKinds.Phone.TYPE ) ) ),
173
cur.getString( cur.getColumnIndex(
174
CommonDataKinds.Phone.NUMBER ) ) ) );
176
// add email addresses
177
else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
178
contact.addEmail( contact.new EmailDetail(
179
convertBackendTypeToType( CommonDataKinds.Email.class,
180
cur.getInt( cur.getColumnIndex(
181
CommonDataKinds.Email.TYPE ) ) ),
182
cur.getString( cur.getColumnIndex(
183
CommonDataKinds.Email.DATA ) ) ) );
185
// add postal addresses
186
else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
187
contact.addAddress( contact.new AddressDetail(
188
convertBackendTypeToType( CommonDataKinds.StructuredPostal.class,
189
cur.getInt( cur.getColumnIndex(
190
CommonDataKinds.StructuredPostal.TYPE ) ) ),
191
cur.getString( cur.getColumnIndex(
192
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) );
194
// add organisations/titles
195
else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) )
196
contact.addOrganisation( contact.new OrganisationDetail(
197
cur.getString( cur.getColumnIndex(
198
CommonDataKinds.Organization.COMPANY ) ),
199
cur.getString( cur.getColumnIndex(
200
CommonDataKinds.Organization.TITLE ) ) ) );
203
else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
204
contact.addNote( cur.getString( cur.getColumnIndex(
205
CommonDataKinds.Note.NOTE ) ) );
208
else if( type.equals( CommonDataKinds.Event.CONTENT_ITEM_TYPE ) ) {
209
int event = cur.getInt( cur.getColumnIndex(
210
CommonDataKinds.Event.TYPE ) );
211
if( event == CommonDataKinds.Event.TYPE_BIRTHDAY )
212
contact.setBirthday( cur.getString( cur.getColumnIndex(
213
CommonDataKinds.Event.START_DATE ) ) );