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 cursor = _activity.managedQuery(
52
ContactsContract.Contacts.CONTENT_URI,
54
ContactsContract.Contacts._ID,
55
}, null, null, null );
56
return cursor.getCount();
59
private int convertBackendTypeToType( Class< ? > cls, int type )
61
if( cls == CommonDataKinds.Phone.class )
65
case CommonDataKinds.Phone.TYPE_MOBILE:
66
return ContactData.TYPE_MOBILE;
67
case CommonDataKinds.Phone.TYPE_FAX_HOME:
68
return ContactData.TYPE_FAX_HOME;
69
case CommonDataKinds.Phone.TYPE_FAX_WORK:
70
return ContactData.TYPE_FAX_WORK;
71
case CommonDataKinds.Phone.TYPE_PAGER:
72
return ContactData.TYPE_PAGER;
73
case CommonDataKinds.Phone.TYPE_WORK:
74
return ContactData.TYPE_WORK;
76
return ContactData.TYPE_HOME;
79
else if( cls == CommonDataKinds.Email.class )
83
case CommonDataKinds.Email.TYPE_WORK:
84
return ContactData.TYPE_WORK;
86
return ContactData.TYPE_HOME;
89
else if( cls == CommonDataKinds.StructuredPostal.class )
93
case CommonDataKinds.StructuredPostal.TYPE_WORK:
94
return ContactData.TYPE_WORK;
96
return ContactData.TYPE_HOME;
100
return ContactData.TYPE_HOME;
104
public boolean getNextContact( Exporter.ContactData contact )
109
// get all aggregate contacts
110
_cur = _activity.managedQuery(
111
ContactsContract.Contacts.CONTENT_URI,
113
ContactsContract.Contacts._ID,
114
ContactsContract.Contacts.DISPLAY_NAME,
115
}, null, null, null );
118
// if there are no more aggregate contacts, abort
119
if( _cur == null || !_cur.moveToNext() ) {
124
// get this aggregate contact's id
125
Long id = _cur.getLong( _cur.getColumnIndex(
126
ContactsContract.Contacts._ID ) );
129
contact.setName( _cur.getString( _cur.getColumnIndex(
130
ContactsContract.Contacts.DISPLAY_NAME ) ) );
132
// get all contact data pertaining to the aggregate contact
133
Cursor cur = _activity.getContentResolver().query(
134
ContactsContract.Data.CONTENT_URI,
136
ContactsContract.Data.MIMETYPE,
137
ContactsContract.Data.IS_PRIMARY,
138
ContactsContract.Data.DATA1,
139
ContactsContract.Data.DATA2,
140
ContactsContract.Data.DATA4,
142
ContactsContract.Data.CONTACT_ID + " = ? AND " +
143
// column DELTED not found!?
144
// ContactsContract.Data.DELETED + " = 0 AND " +
145
ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ? )",
147
String.valueOf( id ),
148
CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
149
CommonDataKinds.Email.CONTENT_ITEM_TYPE,
150
CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
151
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,
152
CommonDataKinds.Note.CONTENT_ITEM_TYPE,
154
ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
155
ContactsContract.Data.RAW_CONTACT_ID + ", " +
156
ContactsContract.Data.IS_PRIMARY + " DESC" );
157
while( cur.moveToNext() )
159
String type = cur.getString( cur.getColumnIndex(
160
ContactsContract.Data.MIMETYPE ) );
163
if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) )
164
contact.addNumber( contact.new NumberDetail(
165
convertBackendTypeToType( CommonDataKinds.Phone.class,
166
cur.getInt( cur.getColumnIndex(
167
CommonDataKinds.Phone.TYPE ) ) ),
168
cur.getString( cur.getColumnIndex(
169
CommonDataKinds.Phone.NUMBER ) ) ) );
171
// add email addresses
172
else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
173
contact.addEmail( contact.new EmailDetail(
174
convertBackendTypeToType( CommonDataKinds.Email.class,
175
cur.getInt( cur.getColumnIndex(
176
CommonDataKinds.Email.TYPE ) ) ),
177
cur.getString( cur.getColumnIndex(
178
CommonDataKinds.Email.DATA ) ) ) );
180
// add postal addresses
181
else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
182
contact.addAddress( contact.new AddressDetail(
183
convertBackendTypeToType( CommonDataKinds.StructuredPostal.class,
184
cur.getInt( cur.getColumnIndex(
185
CommonDataKinds.StructuredPostal.TYPE ) ) ),
186
cur.getString( cur.getColumnIndex(
187
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) );
189
// add organisations/titles
190
else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) )
191
contact.addOrganisation( contact.new OrganisationDetail(
192
cur.getString( cur.getColumnIndex(
193
CommonDataKinds.Organization.COMPANY ) ),
194
cur.getString( cur.getColumnIndex(
195
CommonDataKinds.Organization.TITLE ) ) ) );
198
else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
199
contact.addNote( cur.getString( cur.getColumnIndex(
200
CommonDataKinds.Note.NOTE ) ) );