2
2
* ContactsBackend.java
4
* Copyright (C) 2012 to 2013 Tim Marston <tim@ed.am>
4
* Copyright (C) 2012 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
54
52
// set of contact ids that we have not yet added
55
53
HashSet< Long > unadded_ids = new HashSet< Long >();
58
HashMap< Long, String > notes = new HashMap< Long, String >();
60
55
// get all contacts
61
cur = _activity.getContentResolver().query(
62
Contacts.People.CONTENT_URI,
56
cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
64
58
Contacts.People._ID,
65
59
Contacts.People.NAME,
66
Contacts.People.NOTES,
67
60
}, null, null, null );
68
61
while( cur.moveToNext() ) {
69
62
Long id = cur.getLong(
70
63
cur.getColumnIndex( Contacts.People._ID ) );
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 );
65
ContactsCache.normaliseName( cur.getString(
66
cur.getColumnIndex( Contacts.People.NAME ) ) );
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 );
69
// if we can, add a lookup for the contact id by name
70
if( name.length() > 0 ) {
71
cache.addLookup( new CacheIdentifier(
72
CacheIdentifier.Type.NAME, name ), 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 );
77
// record that a lookup for this contact's id still needs to be
78
// added by some other means
79
unadded_ids.add( id );
102
82
// get contact organisations, primary ones first
103
cur = _activity.getContentResolver().query(
104
Contacts.Organizations.CONTENT_URI,
83
cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
106
85
Contacts.Phones.PERSON_ID,
107
86
Contacts.Organizations.COMPANY,
109
88
while( cur.moveToNext() ) {
110
89
Long id = cur.getLong( cur.getColumnIndex(
111
90
Contacts.Organizations.PERSON_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 );
92
ContactsCache.normaliseOrganisation( cur.getString(
93
cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
94
if( organisation != null )
96
// if this is an organisation name for a contact for whom we
97
// have not added a lookup, add a lookup for the contact id
99
if( unadded_ids.contains( id ) ) {
100
cache.addLookup( new CacheIdentifier(
101
CacheIdentifier.Type.ORGANISATION, organisation ), id );
123
102
unadded_ids.remove( id );
105
// add associated data
106
cache.addAssociatedOrganisation( id, organisation );
127
// add associated data
128
cache.addAssociatedOrganisation( id, organisation );
132
110
// get all phone numbers, primary ones first
133
cur = _activity.getContentResolver().query(
134
Contacts.Phones.CONTENT_URI,
111
cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
136
113
Contacts.Phones.PERSON_ID,
137
114
Contacts.Phones.NUMBER,
139
116
while( cur.moveToNext() ) {
140
117
Long id = cur.getLong(
141
118
cur.getColumnIndex( Contacts.Phones.PERSON_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 );
120
ContactsCache.normalisePhoneNumber( cur.getString(
121
cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
124
// if this is a number for a contact for whom we have not
125
// added a lookup, add a lookup for the contact id by phone
127
if( unadded_ids.contains( id ) ) {
128
cache.addLookup( new CacheIdentifier(
129
CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
153
130
unadded_ids.remove( id );
133
// add associated data
134
cache.addAssociatedNumber( id, number );
157
// add associated data
158
cache.addAssociatedNumber( id, number );
162
138
// now get all email addresses, primary ones first, and postal addresses
163
cur = _activity.getContentResolver().query(
164
Contacts.ContactMethods.CONTENT_URI,
139
cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
166
141
Contacts.ContactMethods.PERSON_ID,
167
142
Contacts.ContactMethods.DATA,
178
153
cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
179
154
if( kind == Contacts.KIND_EMAIL )
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 );
157
ContactsCache.normaliseEmailAddress( cur.getString(
158
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
161
// if this is an email address for a contact for whom we
162
// have not added a lookup, add a lookup for the contact
163
// id by email address
164
if( unadded_ids.contains( id ) ) {
165
cache.addLookup( new CacheIdentifier(
166
CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
192
167
unadded_ids.remove( id );
170
// add associated data
171
cache.addAssociatedEmail( id, email );
196
// add associated data
197
cache.addAssociatedEmail( id, email );
199
174
else if( kind == Contacts.KIND_POSTAL )
201
String address = cur.getString(
202
cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
204
// add associated data
205
cache.addAssociatedAddress( id, address );
177
ContactsCache.normaliseAddress( cur.getString(
178
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
179
if( address != null )
181
// add associated data
182
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 ) );
259
227
case ContactData.TYPE_HOME:
260
return Contacts.PhonesColumns.TYPE_HOME;
228
return Contacts.Phones.TYPE_HOME;
261
229
case ContactData.TYPE_WORK:
262
return Contacts.PhonesColumns.TYPE_WORK;
230
return Contacts.Phones.TYPE_WORK;
263
231
case ContactData.TYPE_MOBILE:
264
return Contacts.PhonesColumns.TYPE_MOBILE;
232
return Contacts.Phones.TYPE_MOBILE;
265
233
case ContactData.TYPE_FAX_HOME:
266
return Contacts.PhonesColumns.TYPE_FAX_HOME;
234
return Contacts.Phones.TYPE_FAX_HOME;
267
235
case ContactData.TYPE_FAX_WORK:
268
return Contacts.PhonesColumns.TYPE_FAX_WORK;
236
return Contacts.Phones.TYPE_FAX_WORK;
269
237
case ContactData.TYPE_PAGER:
270
return Contacts.PhonesColumns.TYPE_PAGER;
238
return Contacts.Phones.TYPE_PAGER;
273
241
else if( cls == Contacts.ContactMethods.class )
360
328
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