67
66
cur.getColumnIndex( ContactsContract.Contacts._ID ) );
68
67
unadded_ids.add( id );
72
70
// build a mapping of the ids of raw contacts to the ids of their
73
71
// aggregate contacts
74
72
HashMap< Long, Long > raw_to_aggregate_ids =
75
73
new HashMap< Long, Long >();
76
cur = _activity.getContentResolver().query(
77
ContactsContract.RawContacts.CONTENT_URI,
74
cur = _activity.managedQuery( ContactsContract.RawContacts.CONTENT_URI,
79
76
ContactsContract.RawContacts._ID,
80
77
ContactsContract.RawContacts.CONTACT_ID,
81
}, ContactsContract.RawContacts.DELETED + " = 0", null, null );
78
}, ContactsContract.RawContacts.DELETED + " != 0", null, null );
82
79
while( cur.moveToNext() ) {
83
80
Long raw_id = cur.getLong(
84
81
cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
102
97
Long raw_id = cur.getLong( cur.getColumnIndex(
103
98
ContactsContract.Data.RAW_CONTACT_ID ) );
104
99
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 ) );
110
105
// if this is a name for a contact for whom we have not added a
111
106
// lookup, add a lookup for the contact id by name
112
107
if( unadded_ids.contains( 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 );
108
cache.addLookup( new CacheIdentifier(
109
CacheIdentifier.Type.NAME, name ), id );
110
unadded_ids.remove( id );
124
115
// get contact organisations, primary ones first
125
cur = _activity.getContentResolver().query(
126
ContactsContract.Data.CONTENT_URI,
116
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
128
118
ContactsContract.Data.RAW_CONTACT_ID,
129
119
CommonDataKinds.Organization.COMPANY,
135
125
Long raw_id = cur.getLong( cur.getColumnIndex(
136
126
ContactsContract.Data.RAW_CONTACT_ID ) );
137
127
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 ) );
143
133
// if this is an organisation name for a contact for whom we
144
134
// have not added a lookup, add a lookup for the contact id
145
135
// by organisation
146
136
if( unadded_ids.contains( 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 );
137
cache.addLookup( new CacheIdentifier(
138
CacheIdentifier.Type.ORGANISATION, organisation ), id );
139
unadded_ids.remove( id );
155
142
// add associated data
156
143
cache.addAssociatedOrganisation( id, organisation );
161
147
// get all phone numbers, primary ones first
162
cur = _activity.getContentResolver().query(
163
ContactsContract.Data.CONTENT_URI,
148
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
165
150
ContactsContract.Data.RAW_CONTACT_ID,
166
151
CommonDataKinds.Phone.NUMBER,
172
157
Long raw_id = cur.getLong( cur.getColumnIndex(
173
158
ContactsContract.Data.RAW_CONTACT_ID ) );
174
159
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 ) );
180
165
// if this is a number for a contact for whom we have not
181
166
// added a lookup, add a lookup for the contact id by phone
183
168
if( unadded_ids.contains( 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 );
169
cache.addLookup( new CacheIdentifier(
170
CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
171
unadded_ids.remove( id );
192
174
// add associated data
193
175
cache.addAssociatedNumber( id, number );
198
179
// get all email addresses, primary ones first
199
cur = _activity.getContentResolver().query(
200
ContactsContract.Data.CONTENT_URI,
180
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
202
182
ContactsContract.Data.RAW_CONTACT_ID,
203
183
CommonDataKinds.Email.DATA,
209
189
Long raw_id = cur.getLong( cur.getColumnIndex(
210
190
ContactsContract.Data.RAW_CONTACT_ID ) );
211
191
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 ) );
217
197
// if this is an email address for a contact for whom we have
218
198
// not added a lookup, add a lookup for the contact id by email
220
200
if( unadded_ids.contains( 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 );
201
cache.addLookup( new CacheIdentifier(
202
CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
203
unadded_ids.remove( id );
229
206
// add associated data
230
207
cache.addAssociatedEmail( id, email );
235
211
// get all postal addresses, primary ones first
236
cur = _activity.getContentResolver().query(
237
ContactsContract.Data.CONTENT_URI,
212
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
239
214
ContactsContract.Data.RAW_CONTACT_ID,
240
215
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
242
217
ContactsContract.Data.MIMETYPE + " = '" +
243
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'",
218
CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'",
244
219
null, ContactsContract.Data.IS_PRIMARY + " DESC" );
245
220
while( cur.moveToNext() ) {
246
221
Long raw_id = cur.getLong( cur.getColumnIndex(
247
222
ContactsContract.Data.RAW_CONTACT_ID ) );
248
223
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 ) );
254
229
// add associated data
255
230
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 );
286
cur = _activity.getContentResolver().query(
287
ContactsContract.Data.CONTENT_URI,
289
ContactsContract.Data.RAW_CONTACT_ID,
290
CommonDataKinds.Event.START_DATE,
292
ContactsContract.Data.MIMETYPE + " = '" +
293
CommonDataKinds.Event.CONTENT_ITEM_TYPE + "' AND " +
294
CommonDataKinds.Event.TYPE + " = '" +
295
CommonDataKinds.Event.TYPE_BIRTHDAY + "'",
297
while( cur.moveToNext() ) {
298
Long raw_id = cur.getLong( cur.getColumnIndex(
299
ContactsContract.Data.RAW_CONTACT_ID ) );
300
Long id = raw_to_aggregate_ids.get( raw_id );
303
String birthday = cur.getString( cur.getColumnIndex(
304
CommonDataKinds.Event.START_DATE ) );
306
// add associated data
307
cache.addAssociatedBirthday( id, birthday );
382
302
if( raw_id != null ) return raw_id;
384
304
// find a corresponding raw contact that has no account name/type
385
Cursor cur = _activity.getContentResolver().query(
305
Cursor cur = _activity.managedQuery(
386
306
ContactsContract.RawContacts.CONTENT_URI,
388
308
ContactsContract.RawContacts._ID,
389
ContactsContract.RawContacts.ACCOUNT_NAME,
391
ContactsContract.RawContacts.DELETED + " = 0 AND " +
392
ContactsContract.RawContacts.CONTACT_ID + " = ? AND " +
310
ContactsContract.RawContacts.DELETED + " != 0 AND " +
393
311
"IFNULL( " + ContactsContract.RawContacts.ACCOUNT_NAME +
394
312
", '' ) = '' AND " +
395
313
"IFNULL( " + ContactsContract.RawContacts.ACCOUNT_TYPE +
398
String.valueOf( id ),
400
316
if( cur.moveToNext() )
401
317
raw_id = cur.getLong(
402
318
cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
405
320
// if one wasn't found, we'll need to create one
406
321
if( raw_id == null ) {
540
455
_activity.getContentResolver().insert(
541
456
ContactsContract.Data.CONTENT_URI, values );
545
public void addContactNote( Long id, String note )
546
throws ContactCreationException
548
ContentValues values = new ContentValues();
549
values.put( ContactsContract.Data.RAW_CONTACT_ID,
550
obtainRawContact( id ) );
551
values.put( ContactsContract.Data.MIMETYPE,
552
CommonDataKinds.Note.CONTENT_ITEM_TYPE );
554
CommonDataKinds.Note.NOTE, note );
556
_activity.getContentResolver().insert(
557
ContactsContract.Data.CONTENT_URI, values );
561
public void addContactBirthday( Long id, String birthday )
562
throws ContactCreationException
564
ContentValues values = new ContentValues();
565
values.put( ContactsContract.Data.RAW_CONTACT_ID,
566
obtainRawContact( id ) );
567
values.put( ContactsContract.Data.MIMETYPE,
568
CommonDataKinds.Event.CONTENT_ITEM_TYPE );
570
CommonDataKinds.Event.TYPE, CommonDataKinds.Event.TYPE_BIRTHDAY );
572
CommonDataKinds.Event.START_DATE, birthday );
573
_activity.getContentResolver().insert(
574
ContactsContract.Data.CONTENT_URI, values );