2
2
* ContactsBackend.java
4
* Copyright (C) 2012 Tim Marston <tim@ed.am>
4
* Copyright (C) 2012 to 2013 Tim Marston <tim@ed.am>
6
6
* This file is part of the Import Contacts program (hereafter referred
7
* to as "this program"). For more information, see
7
* to as "this program"). For more information, see
8
8
* http://ed.am/dev/android/import-contacts
10
10
* This program is free software: you can redistribute it and/or modify
51
54
// set of contact ids that we have not yet added
52
55
HashSet< Long > unadded_ids = new HashSet< Long >();
58
HashMap< Long, String > notes = new HashMap< Long, String >();
54
60
// get all contacts
55
cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
61
cur = _activity.getContentResolver().query(
62
Contacts.People.CONTENT_URI,
57
64
Contacts.People._ID,
58
65
Contacts.People.NAME,
66
Contacts.People.NOTES,
59
67
}, null, null, null );
60
68
while( cur.moveToNext() ) {
61
69
Long id = cur.getLong(
62
70
cur.getColumnIndex( Contacts.People._ID ) );
64
ContactsCache.normaliseName( cur.getString(
65
cur.getColumnIndex( Contacts.People.NAME ) ) );
71
String name = cur.getString(
72
cur.getColumnIndex( Contacts.People.NAME ) );
73
String note = cur.getString(
74
cur.getColumnIndex( Contacts.People.NOTES ) );
76
// if we can, add a lookup for the contact id by name
77
CacheIdentifier cache_identifier = CacheIdentifier.factory(
78
CacheIdentifier.Type.NAME, name );
79
if( cache_identifier != null ) {
80
cache.addLookup( cache_identifier, id );
82
// add any associated notes (this would get done at the end but,
83
// since it is most common that contacts are identified by name,
84
// it is worth doing a special case here
85
cache.addAssociatedNote( id, note );
68
// if we can, add a lookup for the contact id by name
69
if( name.length() > 0 ) {
70
cache.addLookup( new CacheIdentifier(
71
CacheIdentifier.Type.NAME, name ), id );
89
// record that a lookup for this contact's id still needs to be
90
// added by some other means
91
unadded_ids.add( id );
93
// store this contact's notes, so that they can be added to the
94
// cache at the end, after this contact has been added (by
95
// whatever identifying means)
96
if( note != null && note.length() > 0 )
97
notes.put( id, note );
76
// record that a lookup for this contact's id still needs to be
77
// added by some other means
78
unadded_ids.add( id );
81
102
// get contact organisations, primary ones first
82
cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
103
cur = _activity.getContentResolver().query(
104
Contacts.Organizations.CONTENT_URI,
84
106
Contacts.Phones.PERSON_ID,
85
107
Contacts.Organizations.COMPANY,
87
109
while( cur.moveToNext() ) {
88
110
Long id = cur.getLong( cur.getColumnIndex(
89
111
Contacts.Organizations.PERSON_ID ) );
91
ContactsCache.normaliseOrganisation( cur.getString(
92
cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
93
if( organisation != null )
95
// if this is an organisation name for a contact for whom we
96
// have not added a lookup, add a lookup for the contact id
98
if( unadded_ids.contains( id ) ) {
99
cache.addLookup( new CacheIdentifier(
100
CacheIdentifier.Type.ORGANISATION, organisation ), id );
112
String organisation = cur.getString(
113
cur.getColumnIndex( Contacts.Organizations.COMPANY ) );
115
// if this is an organisation name for a contact for whom we have
116
// not added a lookup, add a lookup for the contact id by
118
if( unadded_ids.contains( id ) ) {
119
CacheIdentifier cache_identifier = CacheIdentifier.factory(
120
CacheIdentifier.Type.ORGANISATION, organisation );
121
if( cache_identifier != null ) {
122
cache.addLookup( cache_identifier, id );
101
123
unadded_ids.remove( id );
104
// add associated data
105
cache.addAssociatedOrganisation( id, organisation );
127
// add associated data
128
cache.addAssociatedOrganisation( id, organisation );
109
132
// get all phone numbers, primary ones first
110
cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
133
cur = _activity.getContentResolver().query(
134
Contacts.Phones.CONTENT_URI,
112
136
Contacts.Phones.PERSON_ID,
113
137
Contacts.Phones.NUMBER,
115
139
while( cur.moveToNext() ) {
116
140
Long id = cur.getLong(
117
141
cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
119
ContactsCache.normalisePhoneNumber( cur.getString(
120
cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
123
// if this is a number for a contact for whom we have not
124
// added a lookup, add a lookup for the contact id by phone
126
if( unadded_ids.contains( id ) ) {
127
cache.addLookup( new CacheIdentifier(
128
CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
142
String number = cur.getString(
143
cur.getColumnIndex( Contacts.Phones.NUMBER ) );
145
// if this is a number for a contact for whom we have not
146
// added a lookup, add a lookup for the contact id by phone
148
if( unadded_ids.contains( id ) ) {
149
CacheIdentifier cache_identifier = CacheIdentifier.factory(
150
CacheIdentifier.Type.PRIMARY_NUMBER, number );
151
if( cache_identifier != null ) {
152
cache.addLookup( cache_identifier, id );
129
153
unadded_ids.remove( id );
132
// add associated data
133
cache.addAssociatedNumber( id, number );
157
// add associated data
158
cache.addAssociatedNumber( id, number );
137
162
// now get all email addresses, primary ones first, and postal addresses
138
cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
163
cur = _activity.getContentResolver().query(
164
Contacts.ContactMethods.CONTENT_URI,
140
166
Contacts.ContactMethods.PERSON_ID,
141
167
Contacts.ContactMethods.DATA,
142
168
Contacts.ContactMethods.KIND,
143
}, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
169
}, Contacts.ContactMethods.KIND + " IN( ?, ? )",
144
171
"" + Contacts.KIND_EMAIL,
145
172
"" + Contacts.KIND_POSTAL,
146
173
}, Contacts.ContactMethods.ISPRIMARY + " DESC" );
151
178
cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
152
179
if( kind == Contacts.KIND_EMAIL )
155
ContactsCache.normaliseEmailAddress( cur.getString(
156
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
159
// if this is an email address for a contact for whom we
160
// have not added a lookup, add a lookup for the contact
161
// id by email address
162
if( unadded_ids.contains( id ) ) {
163
cache.addLookup( new CacheIdentifier(
164
CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
181
String email = cur.getString(
182
cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
184
// if this is an email address for a contact for whom we have
185
// not added a lookup, add a lookup for the contact id by email
187
if( unadded_ids.contains( id ) ) {
188
CacheIdentifier cache_identifier = CacheIdentifier.factory(
189
CacheIdentifier.Type.PRIMARY_EMAIL, email );
190
if( cache_identifier != null ) {
191
cache.addLookup( cache_identifier, id );
165
192
unadded_ids.remove( id );
168
// add associated data
169
cache.addAssociatedEmail( id, email );
196
// add associated data
197
cache.addAssociatedEmail( id, email );
172
199
else if( kind == Contacts.KIND_POSTAL )
175
ContactsCache.normaliseAddress( cur.getString(
176
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
177
if( address != null )
179
// add associated data
180
cache.addAssociatedAddress( id, address );
201
String address = cur.getString(
202
cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
204
// add associated data
205
cache.addAssociatedAddress( id, address );
210
// finally, add the notes that we stored earlier (we have to add these
211
// at the end because we can't be sure which piece of contact data will
212
// cause the contact to be added to the cache
213
Iterator< Long > i = notes.keySet().iterator();
214
while( i.hasNext() ) {
216
cache.addAssociatedNote( id, notes.get( id ) );
195
public Long addContact( String name )
229
public Long addContact( String name ) throws ContactCreationException
197
231
ContentValues values = new ContentValues();
198
values.put( Contacts.People.NAME, name );
233
values.put( Contacts.People.NAME, name );
199
234
Uri contact_uri = _activity.getContentResolver().insert(
200
235
Contacts.People.CONTENT_URI, values );
201
236
Long id = ContentUris.parseId( contact_uri );
202
if( id == 0 ) id = null;
238
throw new ContactCreationException();
204
240
// try to add them to the "My Contacts" group
207
Contacts.People.addToMyContactsGroup(
208
_activity.getContentResolver(), id );
210
catch( IllegalStateException e ) {
211
// ignore any failure
242
Contacts.People.addToMyContactsGroup(
243
_activity.getContentResolver(), id );
245
catch( IllegalStateException e ) {
246
// ignore any failure
252
private int convertTypeToBackendType( Class< ? > cls, int type )
253
throws ContactCreationException
255
if( cls == Contacts.Phones.class )
259
case ContactData.TYPE_HOME:
260
return Contacts.PhonesColumns.TYPE_HOME;
261
case ContactData.TYPE_WORK:
262
return Contacts.PhonesColumns.TYPE_WORK;
263
case ContactData.TYPE_MOBILE:
264
return Contacts.PhonesColumns.TYPE_MOBILE;
265
case ContactData.TYPE_FAX_HOME:
266
return Contacts.PhonesColumns.TYPE_FAX_HOME;
267
case ContactData.TYPE_FAX_WORK:
268
return Contacts.PhonesColumns.TYPE_FAX_WORK;
269
case ContactData.TYPE_PAGER:
270
return Contacts.PhonesColumns.TYPE_PAGER;
273
else if( cls == Contacts.ContactMethods.class )
277
case ContactData.TYPE_HOME:
278
return Contacts.ContactMethodsColumns.TYPE_HOME;
279
case ContactData.TYPE_WORK:
280
return Contacts.ContactMethodsColumns.TYPE_WORK;
285
throw new ContactCreationException();
219
289
public void addContactPhone( Long id, String number,
220
ContactData.PreferredDetail data )
290
ContactData.PreferredDetail data ) throws ContactCreationException
222
292
Uri contact_phones_uri = Uri.withAppendedPath(
223
293
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
224
294
Contacts.People.Phones.CONTENT_DIRECTORY );
226
296
ContentValues values = new ContentValues();
227
values.put( Contacts.Phones.TYPE, data.getType() );
297
values.put( Contacts.Phones.TYPE,
298
convertTypeToBackendType( Contacts.Phones.class,
228
300
values.put( Contacts.Phones.NUMBER, number );
229
301
if( data.isPreferred() )
230
302
values.put( Contacts.Phones.ISPRIMARY, 1 );
243
315
ContentValues values = new ContentValues();
244
316
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
245
317
values.put( Contacts.ContactMethods.DATA, email );
246
values.put( Contacts.ContactMethods.TYPE, data.getType() );
318
values.put( Contacts.ContactMethods.TYPE,
319
convertTypeToBackendType( Contacts.ContactMethods.class,
247
321
if( data.isPreferred() )
248
322
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
262
336
ContentValues values = new ContentValues();
263
337
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
264
338
values.put( Contacts.ContactMethods.DATA, address );
265
values.put( Contacts.ContactMethods.TYPE, data.getType() );
339
values.put( Contacts.ContactMethods.TYPE,
340
convertTypeToBackendType( Contacts.ContactMethods.class,
267
343
_activity.getContentResolver().insert( contact_contact_methods_uri,
284
360
Contacts.Organizations.CONTENT_URI, values );
364
public void addContactNote( Long id, String note )
365
throws ContactCreationException
367
ContentValues values = new ContentValues();
368
values.put( Contacts.People.NOTES, note );
369
_activity.getContentResolver().update(
370
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
371
values, null, null );
375
public void addContactBirthday( Long id, String birthday )
376
throws ContactCreationException
378
// this contacts API doesn't support birthdays, so just ignore them