/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/importcontacts/ContactsContractBackend.java

  • Committer: Tim Marston
  • Date: 2016-03-28 18:13:50 UTC
  • Revision ID: tim@ed.am-20160328181350-ytxvox60fxj1tevm
fixed typo in strings

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsBackend.java
3
3
 *
4
 
 * Copyright (C) 2012 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2012 to 2013 Tim Marston <tim@ed.am>
5
5
 *
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
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
57
57
                // build a set of aggregate contact ids that haven't been added to the
58
58
                // cache yet
59
59
                HashSet< Long > unadded_ids = new HashSet< Long >();
60
 
                cur = _activity.managedQuery( ContactsContract.Contacts.CONTENT_URI,
 
60
                cur = _activity.getContentResolver().query(
 
61
                        ContactsContract.Contacts.CONTENT_URI,
61
62
                        new String[] {
62
63
                                ContactsContract.Contacts._ID,
63
64
                        }, null, null, null );
66
67
                                cur.getColumnIndex( ContactsContract.Contacts._ID ) );
67
68
                        unadded_ids.add( id );
68
69
                }
 
70
                cur.close();
69
71
 
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,
75
78
                        new String[] {
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 );
85
88
                }
 
89
                cur.close();
86
90
 
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,
89
94
                        new String[] {
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 )
 
105
                        if( id != null )
104
106
                        {
 
107
                                String name = cur.getString( cur.getColumnIndex(
 
108
                                        CommonDataKinds.StructuredName.DISPLAY_NAME ) );
 
109
 
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 );
 
118
                                        }
111
119
                                }
112
120
                        }
113
121
                }
 
122
                cur.close();
114
123
 
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,
117
127
                        new String[] {
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 )
 
138
                        if( id != null )
132
139
                        {
 
140
                                String organisation = cur.getString( cur.getColumnIndex(
 
141
                                        CommonDataKinds.Organization.COMPANY ) );
 
142
 
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 );
 
152
                                        }
140
153
                                }
141
154
 
142
155
                                // add associated data
143
156
                                cache.addAssociatedOrganisation( id, organisation );
144
157
                        }
145
158
                }
 
159
                cur.close();
146
160
 
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,
149
164
                        new String[] {
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 )
 
175
                        if( id != null )
164
176
                        {
 
177
                                String number = cur.getString( cur.getColumnIndex(
 
178
                                        CommonDataKinds.Phone.NUMBER ) );
 
179
 
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
167
182
                                // number
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 );
 
189
                                        }
172
190
                                }
173
191
 
174
192
                                // add associated data
175
193
                                cache.addAssociatedNumber( id, number );
176
194
                        }
177
195
                }
 
196
                cur.close();
178
197
 
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,
181
201
                        new String[] {
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 )
 
212
                        if( id != null )
196
213
                        {
 
214
                                String email = cur.getString( cur.getColumnIndex(
 
215
                                        CommonDataKinds.Email.DATA ) );
 
216
 
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
199
219
                                // address
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 );
 
226
                                        }
204
227
                                }
205
228
 
206
229
                                // add associated data
207
230
                                cache.addAssociatedEmail( id, email );
208
231
                        }
209
232
                }
 
233
                cur.close();
210
234
 
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,
213
238
                        new String[] {
214
239
                                ContactsContract.Data.RAW_CONTACT_ID,
215
240
                                CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
216
241
                        },
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 )
 
249
                        if( id != null )
228
250
                        {
 
251
                                String address = cur.getString( cur.getColumnIndex(
 
252
                                        CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) );
 
253
 
229
254
                                // add associated data
230
255
                                cache.addAssociatedAddress( id, address );
231
256
                        }
232
257
                }
 
258
                cur.close();
 
259
 
 
260
                // get all notes
 
261
                cur = _activity.getContentResolver().query(
 
262
                        ContactsContract.Data.CONTENT_URI,
 
263
                        new String[] {
 
264
                                ContactsContract.Data.RAW_CONTACT_ID,
 
265
                                CommonDataKinds.Note.NOTE,
 
266
                        },
 
267
                        ContactsContract.Data.MIMETYPE + " = '" +
 
268
                                CommonDataKinds.Note.CONTENT_ITEM_TYPE + "'",
 
269
                        null, null );
 
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 );
 
274
                        if( id != null )
 
275
                        {
 
276
                                String note = cur.getString( cur.getColumnIndex(
 
277
                                        CommonDataKinds.Note.NOTE ) );
 
278
 
 
279
                                // add associated data
 
280
                                cache.addAssociatedNote( id, note );
 
281
                        }
 
282
                }
 
283
                cur.close();
 
284
 
 
285
                // get all birthdays
 
286
                cur = _activity.getContentResolver().query(
 
287
                        ContactsContract.Data.CONTENT_URI,
 
288
                        new String[] {
 
289
                                ContactsContract.Data.RAW_CONTACT_ID,
 
290
                                CommonDataKinds.Event.START_DATE,
 
291
                        },
 
292
                        ContactsContract.Data.MIMETYPE + " = '" +
 
293
                                CommonDataKinds.Event.CONTENT_ITEM_TYPE + "' AND " +
 
294
                                CommonDataKinds.Event.TYPE + " = '" +
 
295
                                CommonDataKinds.Event.TYPE_BIRTHDAY + "'",
 
296
                        null, null );
 
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 );
 
301
                        if( id != null )
 
302
                        {
 
303
                                String birthday = cur.getString( cur.getColumnIndex(
 
304
                                        CommonDataKinds.Event.START_DATE ) );
 
305
 
 
306
                                // add associated data
 
307
                                cache.addAssociatedBirthday( id, birthday );
 
308
                        }
 
309
                }
 
310
                cur.close();
233
311
        }
234
312
 
235
313
        @Override
265
343
                        ContentUris.withAppendedId(
266
344
                                ContactsContract.RawContacts.CONTENT_URI, raw_id ),
267
345
                        ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY );
268
 
                Cursor cur = _activity.managedQuery( contact_uri,
 
346
                Cursor cur = _activity.getContentResolver().query( contact_uri,
269
347
                        new String[] {
270
348
                                ContactsContract.RawContacts.CONTACT_ID,
271
349
                        }, null, null, null );
273
351
                if( cur.moveToNext() )
274
352
                        id = cur.getLong(
275
353
                                cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) );
 
354
                cur.close();
276
355
                if( id == null || id == 0 )
277
356
                {
278
357
                        // we didn't find an aggregate contact id, so try to clean up (by
291
370
         * Obtain the raw contact id for the phone-only raw contact that is
292
371
         * associated with the aggregate contact id.  One will be created if
293
372
         * necessary.
 
373
         *
294
374
         * @param id the aggregate contact id
295
375
         * @return the raw contact id
296
376
         * @throws ContactCreationException
302
382
                if( raw_id != null ) return raw_id;
303
383
 
304
384
                // find a corresponding raw contact that has no account name/type
305
 
                Cursor cur = _activity.managedQuery(
 
385
                Cursor cur = _activity.getContentResolver().query(
306
386
                        ContactsContract.RawContacts.CONTENT_URI,
307
387
                        new String[] {
308
388
                                ContactsContract.RawContacts._ID,
 
389
                                ContactsContract.RawContacts.ACCOUNT_NAME,
309
390
                        },
310
 
                        ContactsContract.RawContacts.DELETED + " != 0 AND " +
 
391
                        ContactsContract.RawContacts.DELETED + " = 0 AND " +
 
392
                                ContactsContract.RawContacts.CONTACT_ID + " = ? AND " +
311
393
                                "IFNULL( " + ContactsContract.RawContacts.ACCOUNT_NAME +
312
394
                                        ", '' ) = '' AND " +
313
395
                                "IFNULL( " + ContactsContract.RawContacts.ACCOUNT_TYPE +
314
396
                                        ", '' ) = ''",
315
 
                        null, null );
 
397
                        new String[] {
 
398
                                String.valueOf( id ),
 
399
                        }, null );
316
400
                if( cur.moveToNext() )
317
401
                        raw_id = cur.getLong(
318
402
                                cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
 
403
                cur.close();
319
404
 
320
405
                // if one wasn't found, we'll need to create one
321
406
                if( raw_id == null ) {
455
540
                _activity.getContentResolver().insert(
456
541
                        ContactsContract.Data.CONTENT_URI, values );
457
542
        }
 
543
 
 
544
        @Override
 
545
        public void addContactNote( Long id, String note )
 
546
                throws ContactCreationException
 
547
        {
 
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 );
 
553
                values.put(
 
554
                        CommonDataKinds.Note.NOTE, note );
 
555
 
 
556
                _activity.getContentResolver().insert(
 
557
                        ContactsContract.Data.CONTENT_URI, values );
 
558
        }
 
559
 
 
560
        @Override
 
561
        public void addContactBirthday( Long id, String birthday )
 
562
                throws ContactCreationException
 
563
        {
 
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 );
 
569
                values.put(
 
570
                        CommonDataKinds.Event.TYPE, CommonDataKinds.Event.TYPE_BIRTHDAY );
 
571
                values.put(
 
572
                        CommonDataKinds.Event.START_DATE, birthday );
 
573
                _activity.getContentResolver().insert(
 
574
                        ContactsContract.Data.CONTENT_URI, values );
 
575
        }
458
576
}