/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:18:11 UTC
  • Revision ID: tim@ed.am-20160328181811-as61lllmvnv8e5os
handle vcards with missing vesion lines (treat as v2.1)

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,
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,
114
119
                                }
115
120
                        }
116
121
                }
 
122
                cur.close();
117
123
 
118
124
                // get contact organisations, primary ones first
119
 
                cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
 
125
                cur = _activity.getContentResolver().query(
 
126
                        ContactsContract.Data.CONTENT_URI,
120
127
                        new String[] {
121
128
                                ContactsContract.Data.RAW_CONTACT_ID,
122
129
                                CommonDataKinds.Organization.COMPANY,
149
156
                                cache.addAssociatedOrganisation( id, organisation );
150
157
                        }
151
158
                }
 
159
                cur.close();
152
160
 
153
161
                // get all phone numbers, primary ones first
154
 
                cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
 
162
                cur = _activity.getContentResolver().query(
 
163
                        ContactsContract.Data.CONTENT_URI,
155
164
                        new String[] {
156
165
                                ContactsContract.Data.RAW_CONTACT_ID,
157
166
                                CommonDataKinds.Phone.NUMBER,
184
193
                                cache.addAssociatedNumber( id, number );
185
194
                        }
186
195
                }
 
196
                cur.close();
187
197
 
188
198
                // get all email addresses, primary ones first
189
 
                cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
 
199
                cur = _activity.getContentResolver().query(
 
200
                        ContactsContract.Data.CONTENT_URI,
190
201
                        new String[] {
191
202
                                ContactsContract.Data.RAW_CONTACT_ID,
192
203
                                CommonDataKinds.Email.DATA,
219
230
                                cache.addAssociatedEmail( id, email );
220
231
                        }
221
232
                }
 
233
                cur.close();
222
234
 
223
235
                // get all postal addresses, primary ones first
224
 
                cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
 
236
                cur = _activity.getContentResolver().query(
 
237
                        ContactsContract.Data.CONTENT_URI,
225
238
                        new String[] {
226
239
                                ContactsContract.Data.RAW_CONTACT_ID,
227
240
                                CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
242
255
                                cache.addAssociatedAddress( id, address );
243
256
                        }
244
257
                }
 
258
                cur.close();
245
259
 
246
260
                // get all notes
247
 
                cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
 
261
                cur = _activity.getContentResolver().query(
 
262
                        ContactsContract.Data.CONTENT_URI,
248
263
                        new String[] {
249
264
                                ContactsContract.Data.RAW_CONTACT_ID,
250
265
                                CommonDataKinds.Note.NOTE,
265
280
                                cache.addAssociatedNote( id, note );
266
281
                        }
267
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();
268
311
        }
269
312
 
270
313
        @Override
300
343
                        ContentUris.withAppendedId(
301
344
                                ContactsContract.RawContacts.CONTENT_URI, raw_id ),
302
345
                        ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY );
303
 
                Cursor cur = _activity.managedQuery( contact_uri,
 
346
                Cursor cur = _activity.getContentResolver().query( contact_uri,
304
347
                        new String[] {
305
348
                                ContactsContract.RawContacts.CONTACT_ID,
306
349
                        }, null, null, null );
308
351
                if( cur.moveToNext() )
309
352
                        id = cur.getLong(
310
353
                                cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) );
 
354
                cur.close();
311
355
                if( id == null || id == 0 )
312
356
                {
313
357
                        // we didn't find an aggregate contact id, so try to clean up (by
326
370
         * Obtain the raw contact id for the phone-only raw contact that is
327
371
         * associated with the aggregate contact id.  One will be created if
328
372
         * necessary.
 
373
         *
329
374
         * @param id the aggregate contact id
330
375
         * @return the raw contact id
331
376
         * @throws ContactCreationException
337
382
                if( raw_id != null ) return raw_id;
338
383
 
339
384
                // find a corresponding raw contact that has no account name/type
340
 
                Cursor cur = _activity.managedQuery(
 
385
                Cursor cur = _activity.getContentResolver().query(
341
386
                        ContactsContract.RawContacts.CONTENT_URI,
342
387
                        new String[] {
343
388
                                ContactsContract.RawContacts._ID,
355
400
                if( cur.moveToNext() )
356
401
                        raw_id = cur.getLong(
357
402
                                cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
 
403
                cur.close();
358
404
 
359
405
                // if one wasn't found, we'll need to create one
360
406
                if( raw_id == null ) {
511
557
                        ContactsContract.Data.CONTENT_URI, values );
512
558
        }
513
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
        }
514
576
}