66
67
cur.getColumnIndex( ContactsContract.Contacts._ID ) );
67
68
unadded_ids.add( id );
70
72
// build a mapping of the ids of raw contacts to the ids of their
71
73
// aggregate contacts
72
74
HashMap< Long, Long > raw_to_aggregate_ids =
73
75
new HashMap< Long, Long >();
74
cur = _activity.managedQuery( ContactsContract.RawContacts.CONTENT_URI,
76
cur = _activity.getContentResolver().query(
77
ContactsContract.RawContacts.CONTENT_URI,
76
79
ContactsContract.RawContacts._ID,
77
80
ContactsContract.RawContacts.CONTACT_ID,
78
}, ContactsContract.RawContacts.DELETED + " != 0", null, null );
81
}, ContactsContract.RawContacts.DELETED + " = 0", null, null );
79
82
while( cur.moveToNext() ) {
80
83
Long raw_id = cur.getLong(
81
84
cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
83
86
cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) );
84
87
raw_to_aggregate_ids.put( raw_id, id );
87
91
// get structured names, primary ones first
88
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
92
cur = _activity.getContentResolver().query(
93
ContactsContract.Data.CONTENT_URI,
90
95
ContactsContract.Data.RAW_CONTACT_ID,
91
96
CommonDataKinds.StructuredName.DISPLAY_NAME,
97
102
Long raw_id = cur.getLong( cur.getColumnIndex(
98
103
ContactsContract.Data.RAW_CONTACT_ID ) );
99
104
Long id = raw_to_aggregate_ids.get( raw_id );
100
String name = ContactsCache.normaliseName(
101
cur.getString( cur.getColumnIndex(
102
CommonDataKinds.StructuredName.DISPLAY_NAME ) ) );
103
if( name != null && id != null )
107
String name = cur.getString( cur.getColumnIndex(
108
CommonDataKinds.StructuredName.DISPLAY_NAME ) );
105
110
// if this is a name for a contact for whom we have not added a
106
111
// lookup, add a lookup for the contact id by name
107
112
if( unadded_ids.contains( id ) ) {
108
cache.addLookup( new CacheIdentifier(
109
CacheIdentifier.Type.NAME, name ), id );
110
unadded_ids.remove( id );
113
CacheIdentifier cache_identifier = CacheIdentifier.factory(
114
CacheIdentifier.Type.NAME, name );
115
if( cache_identifier != null ) {
116
cache.addLookup( cache_identifier, id );
117
unadded_ids.remove( id );
115
124
// get contact organisations, primary ones first
116
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
125
cur = _activity.getContentResolver().query(
126
ContactsContract.Data.CONTENT_URI,
118
128
ContactsContract.Data.RAW_CONTACT_ID,
119
129
CommonDataKinds.Organization.COMPANY,
125
135
Long raw_id = cur.getLong( cur.getColumnIndex(
126
136
ContactsContract.Data.RAW_CONTACT_ID ) );
127
137
Long id = raw_to_aggregate_ids.get( raw_id );
128
String organisation = ContactsCache.normaliseOrganisation(
129
cur.getString( cur.getColumnIndex(
130
CommonDataKinds.Organization.COMPANY ) ) );
131
if( organisation != null && id != null )
140
String organisation = cur.getString( cur.getColumnIndex(
141
CommonDataKinds.Organization.COMPANY ) );
133
143
// if this is an organisation name for a contact for whom we
134
144
// have not added a lookup, add a lookup for the contact id
135
145
// by organisation
136
146
if( unadded_ids.contains( id ) ) {
137
cache.addLookup( new CacheIdentifier(
138
CacheIdentifier.Type.ORGANISATION, organisation ), id );
139
unadded_ids.remove( id );
147
CacheIdentifier cache_identifier = CacheIdentifier.factory(
148
CacheIdentifier.Type.ORGANISATION, organisation );
149
if( cache_identifier != null ) {
150
cache.addLookup( cache_identifier, id );
151
unadded_ids.remove( id );
142
155
// add associated data
143
156
cache.addAssociatedOrganisation( id, organisation );
147
161
// get all phone numbers, primary ones first
148
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
162
cur = _activity.getContentResolver().query(
163
ContactsContract.Data.CONTENT_URI,
150
165
ContactsContract.Data.RAW_CONTACT_ID,
151
166
CommonDataKinds.Phone.NUMBER,
157
172
Long raw_id = cur.getLong( cur.getColumnIndex(
158
173
ContactsContract.Data.RAW_CONTACT_ID ) );
159
174
Long id = raw_to_aggregate_ids.get( raw_id );
160
String number = ContactsCache.normalisePhoneNumber(
161
cur.getString( cur.getColumnIndex(
162
CommonDataKinds.Phone.NUMBER ) ) );
163
if( number != null && id != null )
177
String number = cur.getString( cur.getColumnIndex(
178
CommonDataKinds.Phone.NUMBER ) );
165
180
// if this is a number for a contact for whom we have not
166
181
// added a lookup, add a lookup for the contact id by phone
168
183
if( unadded_ids.contains( id ) ) {
169
cache.addLookup( new CacheIdentifier(
170
CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
171
unadded_ids.remove( id );
184
CacheIdentifier cache_identifier = CacheIdentifier.factory(
185
CacheIdentifier.Type.PRIMARY_NUMBER, number );
186
if( cache_identifier != null ) {
187
cache.addLookup( cache_identifier, id );
188
unadded_ids.remove( id );
174
192
// add associated data
175
193
cache.addAssociatedNumber( id, number );
179
198
// get all email addresses, primary ones first
180
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
199
cur = _activity.getContentResolver().query(
200
ContactsContract.Data.CONTENT_URI,
182
202
ContactsContract.Data.RAW_CONTACT_ID,
183
203
CommonDataKinds.Email.DATA,
189
209
Long raw_id = cur.getLong( cur.getColumnIndex(
190
210
ContactsContract.Data.RAW_CONTACT_ID ) );
191
211
Long id = raw_to_aggregate_ids.get( raw_id );
192
String email = ContactsCache.normaliseEmailAddress(
193
cur.getString( cur.getColumnIndex(
194
CommonDataKinds.Email.DATA ) ) );
195
if( email != null && id != null )
214
String email = cur.getString( cur.getColumnIndex(
215
CommonDataKinds.Email.DATA ) );
197
217
// if this is an email address for a contact for whom we have
198
218
// not added a lookup, add a lookup for the contact id by email
200
220
if( unadded_ids.contains( id ) ) {
201
cache.addLookup( new CacheIdentifier(
202
CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
203
unadded_ids.remove( id );
221
CacheIdentifier cache_identifier = CacheIdentifier.factory(
222
CacheIdentifier.Type.PRIMARY_EMAIL, email );
223
if( cache_identifier != null ) {
224
cache.addLookup( cache_identifier, id );
225
unadded_ids.remove( id );
206
229
// add associated data
207
230
cache.addAssociatedEmail( id, email );
211
235
// get all postal addresses, primary ones first
212
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
236
cur = _activity.getContentResolver().query(
237
ContactsContract.Data.CONTENT_URI,
214
239
ContactsContract.Data.RAW_CONTACT_ID,
215
240
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
217
242
ContactsContract.Data.MIMETYPE + " = '" +
218
CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'",
243
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'",
219
244
null, ContactsContract.Data.IS_PRIMARY + " DESC" );
220
245
while( cur.moveToNext() ) {
221
246
Long raw_id = cur.getLong( cur.getColumnIndex(
222
247
ContactsContract.Data.RAW_CONTACT_ID ) );
223
248
Long id = raw_to_aggregate_ids.get( raw_id );
224
String address = ContactsCache.normaliseAddress(
225
cur.getString( cur.getColumnIndex(
226
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) );
227
if( address != null && id != null )
251
String address = cur.getString( cur.getColumnIndex(
252
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) );
229
254
// add associated data
230
255
cache.addAssociatedAddress( id, address );
261
cur = _activity.getContentResolver().query(
262
ContactsContract.Data.CONTENT_URI,
264
ContactsContract.Data.RAW_CONTACT_ID,
265
CommonDataKinds.Note.NOTE,
267
ContactsContract.Data.MIMETYPE + " = '" +
268
CommonDataKinds.Note.CONTENT_ITEM_TYPE + "'",
270
while( cur.moveToNext() ) {
271
Long raw_id = cur.getLong( cur.getColumnIndex(
272
ContactsContract.Data.RAW_CONTACT_ID ) );
273
Long id = raw_to_aggregate_ids.get( raw_id );
276
String note = cur.getString( cur.getColumnIndex(
277
CommonDataKinds.Note.NOTE ) );
279
// add associated data
280
cache.addAssociatedNote( id, note );
302
355
if( raw_id != null ) return raw_id;
304
357
// find a corresponding raw contact that has no account name/type
305
Cursor cur = _activity.managedQuery(
358
Cursor cur = _activity.getContentResolver().query(
306
359
ContactsContract.RawContacts.CONTENT_URI,
308
361
ContactsContract.RawContacts._ID,
362
ContactsContract.RawContacts.ACCOUNT_NAME,
310
ContactsContract.RawContacts.DELETED + " != 0 AND " +
364
ContactsContract.RawContacts.DELETED + " = 0 AND " +
365
ContactsContract.RawContacts.CONTACT_ID + " = ? AND " +
311
366
"IFNULL( " + ContactsContract.RawContacts.ACCOUNT_NAME +
312
367
", '' ) = '' AND " +
313
368
"IFNULL( " + ContactsContract.RawContacts.ACCOUNT_TYPE +
371
String.valueOf( id ),
316
373
if( cur.moveToNext() )
317
374
raw_id = cur.getLong(
318
375
cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
320
378
// if one wasn't found, we'll need to create one
321
379
if( raw_id == null ) {
455
513
_activity.getContentResolver().insert(
456
514
ContactsContract.Data.CONTENT_URI, values );
518
public void addContactNote( Long id, String note )
519
throws ContactCreationException
521
ContentValues values = new ContentValues();
522
values.put( ContactsContract.Data.RAW_CONTACT_ID,
523
obtainRawContact( id ) );
524
values.put( ContactsContract.Data.MIMETYPE,
525
CommonDataKinds.Note.CONTENT_ITEM_TYPE );
527
CommonDataKinds.Note.NOTE, note );
529
_activity.getContentResolver().insert(
530
ContactsContract.Data.CONTENT_URI, values );