/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: edam
  • Date: 2012-12-21 14:51:26 UTC
  • Revision ID: tim@ed.am-20121221145126-l30l0yii8xkqs1m2
added a new class to VcardImporter, a ContentLine, which represents the data returned by the ContentLineIterator (including whether the next line looks folded, and the ability to generate/cache a US-ASCII String of the line)

Show diffs side-by-side

added added

removed removed

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