/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-05-10 12:16:15 UTC
  • Revision ID: tim@ed.am-20130510121615-g8qhsu0h3t0ymrt6
Tags: 1.3.2
fix spacing in NEWS

Show diffs side-by-side

added added

removed removed

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();
233
284
        }
234
285
 
235
286
        @Override
265
316
                        ContentUris.withAppendedId(
266
317
                                ContactsContract.RawContacts.CONTENT_URI, raw_id ),
267
318
                        ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY );
268
 
                Cursor cur = _activity.managedQuery( contact_uri,
 
319
                Cursor cur = _activity.getContentResolver().query( contact_uri,
269
320
                        new String[] {
270
321
                                ContactsContract.RawContacts.CONTACT_ID,
271
322
                        }, null, null, null );
273
324
                if( cur.moveToNext() )
274
325
                        id = cur.getLong(
275
326
                                cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) );
 
327
                cur.close();
276
328
                if( id == null || id == 0 )
277
329
                {
278
330
                        // we didn't find an aggregate contact id, so try to clean up (by
302
354
                if( raw_id != null ) return raw_id;
303
355
 
304
356
                // find a corresponding raw contact that has no account name/type
305
 
                Cursor cur = _activity.managedQuery(
 
357
                Cursor cur = _activity.getContentResolver().query(
306
358
                        ContactsContract.RawContacts.CONTENT_URI,
307
359
                        new String[] {
308
360
                                ContactsContract.RawContacts._ID,
 
361
                                ContactsContract.RawContacts.ACCOUNT_NAME,
309
362
                        },
310
 
                        ContactsContract.RawContacts.DELETED + " != 0 AND " +
 
363
                        ContactsContract.RawContacts.DELETED + " = 0 AND " +
 
364
                                ContactsContract.RawContacts.CONTACT_ID + " = ? AND " +
311
365
                                "IFNULL( " + ContactsContract.RawContacts.ACCOUNT_NAME +
312
366
                                        ", '' ) = '' AND " +
313
367
                                "IFNULL( " + ContactsContract.RawContacts.ACCOUNT_TYPE +
314
368
                                        ", '' ) = ''",
315
 
                        null, null );
 
369
                        new String[] {
 
370
                                String.valueOf( id ),
 
371
                        }, null );
316
372
                if( cur.moveToNext() )
317
373
                        raw_id = cur.getLong(
318
374
                                cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
 
375
                cur.close();
319
376
 
320
377
                // if one wasn't found, we'll need to create one
321
378
                if( raw_id == null ) {
455
512
                _activity.getContentResolver().insert(
456
513
                        ContactsContract.Data.CONTENT_URI, values );
457
514
        }
 
515
 
 
516
        @Override
 
517
        public void addContactNote( Long id, String note )
 
518
                throws ContactCreationException
 
519
        {
 
520
                ContentValues values = new ContentValues();
 
521
                values.put( ContactsContract.Data.RAW_CONTACT_ID,
 
522
                        obtainRawContact( id ) );
 
523
                values.put( ContactsContract.Data.MIMETYPE,
 
524
                        CommonDataKinds.Note.CONTENT_ITEM_TYPE );
 
525
                values.put(
 
526
                        CommonDataKinds.Note.NOTE, note );
 
527
 
 
528
                _activity.getContentResolver().insert(
 
529
                        ContactsContract.Data.CONTENT_URI, values );
 
530
        }
 
531
 
458
532
}