/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: 2013-06-22 17:29:31 UTC
  • Revision ID: tim@ed.am-20130622172931-ujydoni23t3a543b
minor style tweaks

Show diffs side-by-side

added added

removed removed

4
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.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();
268
284
        }
269
285
 
270
286
        @Override
300
316
                        ContentUris.withAppendedId(
301
317
                                ContactsContract.RawContacts.CONTENT_URI, raw_id ),
302
318
                        ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY );
303
 
                Cursor cur = _activity.managedQuery( contact_uri,
 
319
                Cursor cur = _activity.getContentResolver().query( contact_uri,
304
320
                        new String[] {
305
321
                                ContactsContract.RawContacts.CONTACT_ID,
306
322
                        }, null, null, null );
308
324
                if( cur.moveToNext() )
309
325
                        id = cur.getLong(
310
326
                                cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) );
 
327
                cur.close();
311
328
                if( id == null || id == 0 )
312
329
                {
313
330
                        // we didn't find an aggregate contact id, so try to clean up (by
326
343
         * Obtain the raw contact id for the phone-only raw contact that is
327
344
         * associated with the aggregate contact id.  One will be created if
328
345
         * necessary.
 
346
         *
329
347
         * @param id the aggregate contact id
330
348
         * @return the raw contact id
331
349
         * @throws ContactCreationException
337
355
                if( raw_id != null ) return raw_id;
338
356
 
339
357
                // find a corresponding raw contact that has no account name/type
340
 
                Cursor cur = _activity.managedQuery(
 
358
                Cursor cur = _activity.getContentResolver().query(
341
359
                        ContactsContract.RawContacts.CONTENT_URI,
342
360
                        new String[] {
343
361
                                ContactsContract.RawContacts._ID,
355
373
                if( cur.moveToNext() )
356
374
                        raw_id = cur.getLong(
357
375
                                cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
 
376
                cur.close();
358
377
 
359
378
                // if one wasn't found, we'll need to create one
360
379
                if( raw_id == null ) {