/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-19 17:51:35 UTC
  • Revision ID: tim@ed.am-20121219175135-1cpuafp76jg1ib1p
added preliminary (buggy) ContactsContract backend

Show diffs side-by-side

added added

removed removed

75
75
                        new String[] {
76
76
                                ContactsContract.RawContacts._ID,
77
77
                                ContactsContract.RawContacts.CONTACT_ID,
78
 
                        }, ContactsContract.RawContacts.DELETED + " = 0", null, null );
 
78
                        }, ContactsContract.RawContacts.DELETED + " != 0", null, null );
79
79
                while( cur.moveToNext() ) {
80
80
                        Long raw_id = cur.getLong(
81
81
                                cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
97
97
                        Long raw_id = cur.getLong( cur.getColumnIndex(
98
98
                                ContactsContract.Data.RAW_CONTACT_ID ) );
99
99
                        Long id = raw_to_aggregate_ids.get( raw_id );
100
 
                        if( id != null )
 
100
                        String name = ContactsCache.normaliseName(
 
101
                                cur.getString( cur.getColumnIndex(
 
102
                                        CommonDataKinds.StructuredName.DISPLAY_NAME ) ) );
 
103
                        if( name != null && id != null )
101
104
                        {
102
 
                                String name = cur.getString( cur.getColumnIndex(
103
 
                                        CommonDataKinds.StructuredName.DISPLAY_NAME ) );
104
 
 
105
105
                                // if this is a name for a contact for whom we have not added a
106
106
                                // lookup, add a lookup for the contact id by name
107
107
                                if( unadded_ids.contains( id ) ) {
108
 
                                        CacheIdentifier cache_identifier = CacheIdentifier.factory(
109
 
                                                CacheIdentifier.Type.NAME, name );
110
 
                                        if( cache_identifier != null ) {
111
 
                                                cache.addLookup( cache_identifier, id );
112
 
                                                unadded_ids.remove( id );
113
 
                                        }
 
108
                                        cache.addLookup( new CacheIdentifier(
 
109
                                                CacheIdentifier.Type.NAME, name ), id );
 
110
                                        unadded_ids.remove( id );
114
111
                                }
115
112
                        }
116
113
                }
128
125
                        Long raw_id = cur.getLong( cur.getColumnIndex(
129
126
                                ContactsContract.Data.RAW_CONTACT_ID ) );
130
127
                        Long id = raw_to_aggregate_ids.get( raw_id );
131
 
                        if( id != null )
 
128
                        String organisation = ContactsCache.normaliseOrganisation(
 
129
                                cur.getString( cur.getColumnIndex(
 
130
                                        CommonDataKinds.Organization.COMPANY ) ) );
 
131
                        if( organisation != null && id != null )
132
132
                        {
133
 
                                String organisation = cur.getString( cur.getColumnIndex(
134
 
                                        CommonDataKinds.Organization.COMPANY ) );
135
 
 
136
133
                                // if this is an organisation name for a contact for whom we
137
134
                                // have not added a lookup, add a lookup for the contact id
138
135
                                // by organisation
139
136
                                if( unadded_ids.contains( id ) ) {
140
 
                                        CacheIdentifier cache_identifier = CacheIdentifier.factory(
141
 
                                                CacheIdentifier.Type.ORGANISATION, organisation );
142
 
                                        if( cache_identifier != null ) {
143
 
                                                cache.addLookup( cache_identifier, id );
144
 
                                                unadded_ids.remove( id );
145
 
                                        }
 
137
                                        cache.addLookup( new CacheIdentifier(
 
138
                                                CacheIdentifier.Type.ORGANISATION, organisation ), id );
 
139
                                        unadded_ids.remove( id );
146
140
                                }
147
141
 
148
142
                                // add associated data
163
157
                        Long raw_id = cur.getLong( cur.getColumnIndex(
164
158
                                ContactsContract.Data.RAW_CONTACT_ID ) );
165
159
                        Long id = raw_to_aggregate_ids.get( raw_id );
166
 
                        if( id != null )
 
160
                        String number = ContactsCache.normalisePhoneNumber(
 
161
                                cur.getString( cur.getColumnIndex(
 
162
                                        CommonDataKinds.Phone.NUMBER ) ) );
 
163
                        if( number != null && id != null )
167
164
                        {
168
 
                                String number = cur.getString( cur.getColumnIndex(
169
 
                                        CommonDataKinds.Phone.NUMBER ) );
170
 
 
171
165
                                // if this is a number for a contact for whom we have not
172
166
                                // added a lookup, add a lookup for the contact id by phone
173
167
                                // number
174
168
                                if( unadded_ids.contains( id ) ) {
175
 
                                        CacheIdentifier cache_identifier = CacheIdentifier.factory(
176
 
                                                CacheIdentifier.Type.PRIMARY_NUMBER, number );
177
 
                                        if( cache_identifier != null ) {
178
 
                                                cache.addLookup( cache_identifier, id );
179
 
                                                unadded_ids.remove( id );
180
 
                                        }
 
169
                                        cache.addLookup( new CacheIdentifier(
 
170
                                                CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
 
171
                                        unadded_ids.remove( id );
181
172
                                }
182
173
 
183
174
                                // add associated data
198
189
                        Long raw_id = cur.getLong( cur.getColumnIndex(
199
190
                                ContactsContract.Data.RAW_CONTACT_ID ) );
200
191
                        Long id = raw_to_aggregate_ids.get( raw_id );
201
 
                        if( id != null )
 
192
                        String email = ContactsCache.normaliseEmailAddress(
 
193
                                cur.getString( cur.getColumnIndex(
 
194
                                        CommonDataKinds.Email.DATA ) ) );
 
195
                        if( email != null && id != null )
202
196
                        {
203
 
                                String email = cur.getString( cur.getColumnIndex(
204
 
                                        CommonDataKinds.Email.DATA ) );
205
 
 
206
197
                                // if this is an email address for a contact for whom we have
207
198
                                // not added a lookup, add a lookup for the contact id by email
208
199
                                // address
209
200
                                if( unadded_ids.contains( id ) ) {
210
 
                                        CacheIdentifier cache_identifier = CacheIdentifier.factory(
211
 
                                                CacheIdentifier.Type.PRIMARY_EMAIL, email );
212
 
                                        if( cache_identifier != null ) {
213
 
                                                cache.addLookup( cache_identifier, id );
214
 
                                                unadded_ids.remove( id );
215
 
                                        }
 
201
                                        cache.addLookup( new CacheIdentifier(
 
202
                                                CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
 
203
                                        unadded_ids.remove( id );
216
204
                                }
217
205
 
218
206
                                // add associated data
227
215
                                CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
228
216
                        },
229
217
                        ContactsContract.Data.MIMETYPE + " = '" +
230
 
                                CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'",
 
218
                                CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'",
231
219
                        null, ContactsContract.Data.IS_PRIMARY + " DESC" );
232
220
                while( cur.moveToNext() ) {
233
221
                        Long raw_id = cur.getLong( cur.getColumnIndex(
234
222
                                ContactsContract.Data.RAW_CONTACT_ID ) );
235
223
                        Long id = raw_to_aggregate_ids.get( raw_id );
236
 
                        if( id != null )
 
224
                        String address = ContactsCache.normaliseAddress(
 
225
                                cur.getString( cur.getColumnIndex(
 
226
                                        CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) );
 
227
                        if( address != null && id != null )
237
228
                        {
238
 
                                String address = cur.getString( cur.getColumnIndex(
239
 
                                        CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) );
240
 
 
241
229
                                // add associated data
242
230
                                cache.addAssociatedAddress( id, address );
243
231
                        }
244
232
                }
245
 
 
246
 
                // get all notes
247
 
                cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
248
 
                        new String[] {
249
 
                                ContactsContract.Data.RAW_CONTACT_ID,
250
 
                                CommonDataKinds.Note.NOTE,
251
 
                        },
252
 
                        ContactsContract.Data.MIMETYPE + " = '" +
253
 
                                CommonDataKinds.Note.CONTENT_ITEM_TYPE + "'",
254
 
                        null, null );
255
 
                while( cur.moveToNext() ) {
256
 
                        Long raw_id = cur.getLong( cur.getColumnIndex(
257
 
                                ContactsContract.Data.RAW_CONTACT_ID ) );
258
 
                        Long id = raw_to_aggregate_ids.get( raw_id );
259
 
                        if( id != null )
260
 
                        {
261
 
                                String note = cur.getString( cur.getColumnIndex(
262
 
                                        CommonDataKinds.Note.NOTE ) );
263
 
 
264
 
                                // add associated data
265
 
                                cache.addAssociatedNote( id, note );
266
 
                        }
267
 
                }
268
233
        }
269
234
 
270
235
        @Override
341
306
                        ContactsContract.RawContacts.CONTENT_URI,
342
307
                        new String[] {
343
308
                                ContactsContract.RawContacts._ID,
344
 
                                ContactsContract.RawContacts.ACCOUNT_NAME,
345
309
                        },
346
 
                        ContactsContract.RawContacts.DELETED + " = 0 AND " +
347
 
                                ContactsContract.RawContacts.CONTACT_ID + " = ? AND " +
 
310
                        ContactsContract.RawContacts.DELETED + " != 0 AND " +
348
311
                                "IFNULL( " + ContactsContract.RawContacts.ACCOUNT_NAME +
349
312
                                        ", '' ) = '' AND " +
350
313
                                "IFNULL( " + ContactsContract.RawContacts.ACCOUNT_TYPE +
351
314
                                        ", '' ) = ''",
352
 
                        new String[] {
353
 
                                String.valueOf( id ),
354
 
                        }, null );
 
315
                        null, null );
355
316
                if( cur.moveToNext() )
356
317
                        raw_id = cur.getLong(
357
318
                                cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
494
455
                _activity.getContentResolver().insert(
495
456
                        ContactsContract.Data.CONTENT_URI, values );
496
457
        }
497
 
 
498
 
        @Override
499
 
        public void addContactNote( Long id, String note )
500
 
                throws ContactCreationException
501
 
        {
502
 
                ContentValues values = new ContentValues();
503
 
                values.put( ContactsContract.Data.RAW_CONTACT_ID,
504
 
                        obtainRawContact( id ) );
505
 
                values.put( ContactsContract.Data.MIMETYPE,
506
 
                        CommonDataKinds.Note.CONTENT_ITEM_TYPE );
507
 
                values.put(
508
 
                        CommonDataKinds.Note.NOTE, note );
509
 
 
510
 
                _activity.getContentResolver().insert(
511
 
                        ContactsContract.Data.CONTENT_URI, values );
512
 
        }
513
 
 
514
458
}