/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-20 16:49:39 UTC
  • Revision ID: tim@ed.am-20121220164939-j9mg98v0uofws7kw
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters

Show diffs side-by-side

added added

removed removed

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
 
                        String name = ContactsCache.normaliseName(
101
 
                                cur.getString( cur.getColumnIndex(
102
 
                                        CommonDataKinds.StructuredName.DISPLAY_NAME ) ) );
103
 
                        if( name != null && id != null )
 
100
                        if( id != null )
104
101
                        {
 
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
 
                                        cache.addLookup( new CacheIdentifier(
109
 
                                                CacheIdentifier.Type.NAME, name ), id );
110
 
                                        unadded_ids.remove( 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
                                        }
111
114
                                }
112
115
                        }
113
116
                }
125
128
                        Long raw_id = cur.getLong( cur.getColumnIndex(
126
129
                                ContactsContract.Data.RAW_CONTACT_ID ) );
127
130
                        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 )
 
131
                        if( id != null )
132
132
                        {
 
133
                                String organisation = cur.getString( cur.getColumnIndex(
 
134
                                        CommonDataKinds.Organization.COMPANY ) );
 
135
 
133
136
                                // if this is an organisation name for a contact for whom we
134
137
                                // have not added a lookup, add a lookup for the contact id
135
138
                                // by organisation
136
139
                                if( unadded_ids.contains( id ) ) {
137
 
                                        cache.addLookup( new CacheIdentifier(
138
 
                                                CacheIdentifier.Type.ORGANISATION, organisation ), id );
139
 
                                        unadded_ids.remove( 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
                                        }
140
146
                                }
141
147
 
142
148
                                // add associated data
157
163
                        Long raw_id = cur.getLong( cur.getColumnIndex(
158
164
                                ContactsContract.Data.RAW_CONTACT_ID ) );
159
165
                        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 )
 
166
                        if( id != null )
164
167
                        {
 
168
                                String number = cur.getString( cur.getColumnIndex(
 
169
                                        CommonDataKinds.Phone.NUMBER ) );
 
170
 
165
171
                                // if this is a number for a contact for whom we have not
166
172
                                // added a lookup, add a lookup for the contact id by phone
167
173
                                // number
168
174
                                if( unadded_ids.contains( id ) ) {
169
 
                                        cache.addLookup( new CacheIdentifier(
170
 
                                                CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
171
 
                                        unadded_ids.remove( 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
                                        }
172
181
                                }
173
182
 
174
183
                                // add associated data
189
198
                        Long raw_id = cur.getLong( cur.getColumnIndex(
190
199
                                ContactsContract.Data.RAW_CONTACT_ID ) );
191
200
                        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 )
 
201
                        if( id != null )
196
202
                        {
 
203
                                String email = cur.getString( cur.getColumnIndex(
 
204
                                        CommonDataKinds.Email.DATA ) );
 
205
 
197
206
                                // if this is an email address for a contact for whom we have
198
207
                                // not added a lookup, add a lookup for the contact id by email
199
208
                                // address
200
209
                                if( unadded_ids.contains( id ) ) {
201
 
                                        cache.addLookup( new CacheIdentifier(
202
 
                                                CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
203
 
                                        unadded_ids.remove( 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
                                        }
204
216
                                }
205
217
 
206
218
                                // add associated data
215
227
                                CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
216
228
                        },
217
229
                        ContactsContract.Data.MIMETYPE + " = '" +
218
 
                                CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'",
 
230
                                CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'",
219
231
                        null, ContactsContract.Data.IS_PRIMARY + " DESC" );
220
232
                while( cur.moveToNext() ) {
221
233
                        Long raw_id = cur.getLong( cur.getColumnIndex(
222
234
                                ContactsContract.Data.RAW_CONTACT_ID ) );
223
235
                        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 )
 
236
                        if( id != null )
228
237
                        {
 
238
                                String address = cur.getString( cur.getColumnIndex(
 
239
                                        CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) );
 
240
 
229
241
                                // add associated data
230
242
                                cache.addAssociatedAddress( id, address );
231
243
                        }
232
244
                }
 
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
                }
233
268
        }
234
269
 
235
270
        @Override
459
494
                _activity.getContentResolver().insert(
460
495
                        ContactsContract.Data.CONTENT_URI, values );
461
496
        }
 
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
 
462
514
}