2
* ContactsContractContactAccessor.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.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 DELTED 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,
158
ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
159
ContactsContract.Data.RAW_CONTACT_ID + ", " +
160
ContactsContract.Data.IS_PRIMARY + " DESC" );
161
while( cur.moveToNext() )
163
String type = cur.getString( cur.getColumnIndex(
164
ContactsContract.Data.MIMETYPE ) );
167
if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) )
168
contact.addNumber( contact.new NumberDetail(
169
convertBackendTypeToType( CommonDataKinds.Phone.class,
170
cur.getInt( cur.getColumnIndex(
171
CommonDataKinds.Phone.TYPE ) ) ),
172
cur.getString( cur.getColumnIndex(
173
CommonDataKinds.Phone.NUMBER ) ) ) );
175
// add email addresses
176
else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
177
contact.addEmail( contact.new EmailDetail(
178
convertBackendTypeToType( CommonDataKinds.Email.class,
179
cur.getInt( cur.getColumnIndex(
180
CommonDataKinds.Email.TYPE ) ) ),
181
cur.getString( cur.getColumnIndex(
182
CommonDataKinds.Email.DATA ) ) ) );
184
// add postal addresses
185
else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
186
contact.addAddress( contact.new AddressDetail(
187
convertBackendTypeToType( CommonDataKinds.StructuredPostal.class,
188
cur.getInt( cur.getColumnIndex(
189
CommonDataKinds.StructuredPostal.TYPE ) ) ),
190
cur.getString( cur.getColumnIndex(
191
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) );
193
// add organisations/titles
194
else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) )
195
contact.addOrganisation( contact.new OrganisationDetail(
196
cur.getString( cur.getColumnIndex(
197
CommonDataKinds.Organization.COMPANY ) ),
198
cur.getString( cur.getColumnIndex(
199
CommonDataKinds.Organization.TITLE ) ) ) );
202
else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
203
contact.addNote( cur.getString( cur.getColumnIndex(
204
CommonDataKinds.Note.NOTE ) ) );