2
* ContactsContractContactAccessor.java
4
* Copyright (C) 2011 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
ContactsContract.Data.DELETED + " = 0 AND " +
144
ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ? )",
146
String.valueOf( id ),
147
CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
148
CommonDataKinds.Email.CONTENT_ITEM_TYPE,
149
CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
150
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,
151
CommonDataKinds.Note.CONTENT_ITEM_TYPE,
153
ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
154
ContactsContract.Data.RAW_CONTACT_ID + ", " +
155
ContactsContract.Data.IS_PRIMARY + " DESC" );
156
while( cur.moveToNext() )
158
String type = cur.getString( cur.getColumnIndex(
159
ContactsContract.Data.MIMETYPE ) );
162
if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) )
163
contact.addNumber( contact.new NumberDetail(
164
convertBackendTypeToType( CommonDataKinds.Phone.class,
165
cur.getInt( cur.getColumnIndex(
166
CommonDataKinds.Phone.TYPE ) ) ),
167
cur.getString( cur.getColumnIndex(
168
CommonDataKinds.Phone.NUMBER ) ) ) );
170
// add email addresses
171
else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
172
contact.addEmail( contact.new EmailDetail(
173
convertBackendTypeToType( CommonDataKinds.Phone.class,
174
cur.getInt( cur.getColumnIndex(
175
CommonDataKinds.Email.TYPE ) ) ),
176
cur.getString( cur.getColumnIndex(
177
CommonDataKinds.Email.DATA ) ) ) );
179
// add postal addresses
180
else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
181
contact.addAddress( contact.new AddressDetail(
182
convertBackendTypeToType( CommonDataKinds.Phone.class,
183
cur.getInt( cur.getColumnIndex(
184
CommonDataKinds.StructuredPostal.TYPE ) ) ),
185
cur.getString( cur.getColumnIndex(
186
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) );
188
// add organisations/titles
189
else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) )
190
contact.addOrganisation( contact.new OrganisationDetail(
191
cur.getString( cur.getColumnIndex(
192
CommonDataKinds.Organization.COMPANY ) ),
193
cur.getString( cur.getColumnIndex(
194
CommonDataKinds.Organization.TITLE ) ) ) );
197
else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
198
contact.addNote( cur.getString( cur.getColumnIndex(
199
CommonDataKinds.Note.NOTE ) ) );