/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/ContactsCache.java

  • Committer: edam
  • Date: 2012-04-24 09:56:17 UTC
  • Revision ID: tim@ed.am-20120424095617-lsfkkpkrlqk9xthn
updated all URLs, email addresses and package names to ed.am

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsCache.java
3
3
 *
4
 
 * Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
25
25
 
26
26
import java.util.HashMap;
27
27
import java.util.HashSet;
28
 
import java.util.Locale;
 
28
 
 
29
import am.ed.importcontacts.Importer.AbortImportException;
 
30
 
 
31
import android.app.Activity;
 
32
import android.database.Cursor;
 
33
import android.provider.Contacts;
 
34
 
29
35
 
30
36
public class ContactsCache
31
37
{
32
38
        /**
33
 
         * A thing that can be used to identify (or lookup) a contact within the
34
 
         * contacts cache.  It is not a reference to a cache entry and may not
35
 
         * identify an existing contact in the cache.
 
39
         * Information that can be used to identify a contact within the cache
36
40
         */
37
 
        public static class CacheIdentifier
 
41
        static public class CacheIdentifier
38
42
        {
39
 
                public enum Type { NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
 
43
                public enum Type {
 
44
                        NONE, NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
40
45
 
41
46
                private Type _type;
42
47
                private String _detail;
43
48
 
44
 
                /**
45
 
                 * Obtain a cache identifier.  This routine is designed to be as robust
46
 
                 * as possible (in terms of bad or null detail values), and to return
47
 
                 * null when a cache identifier can not be created.
48
 
                 * @param type the detail type
49
 
                 * @param detail the detail
50
 
                 * @return the cache identifier, or null
51
 
                 */
52
 
                public static CacheIdentifier factory( Type type, String detail )
53
 
                {
54
 
                        switch( type )
55
 
                        {
56
 
                        case NAME: detail = normaliseName( detail ); break;
57
 
                        case ORGANISATION: detail = normaliseOrganisation( detail ); break;
58
 
                        case PRIMARY_NUMBER: detail = normalisePhoneNumber( detail ); break;
59
 
                        case PRIMARY_EMAIL: detail = normaliseEmailAddress( detail ); break;
60
 
                        default: return null;
61
 
                        }
62
 
                        if( detail == null ) return null;
63
 
                        return new CacheIdentifier( type, detail );
64
 
                }
65
 
 
66
 
                /**
67
 
                 * Obtain a cache identifier from contact data.  This routine is
68
 
                 * designed to be as robust as possible and may return null when a cache
69
 
                 * identifier can not be created.
70
 
                 * @param contact the contact data
71
 
                 * @return the cache identifier, or null
72
 
                 */
73
 
                public static CacheIdentifier factory( Importer.ContactData contact )
74
 
                {
75
 
                        CacheIdentifier identifier = null;
76
 
 
77
 
                        if( contact.hasName() )
78
 
                                identifier = factory( CacheIdentifier.Type.NAME,
79
 
                                        contact.getName() );
80
 
                        if( identifier != null ) return identifier;
81
 
 
82
 
                        if( contact.hasPrimaryOrganisation() )
83
 
                                identifier = factory( CacheIdentifier.Type.ORGANISATION,
84
 
                                        contact.getPrimaryOrganisation() );
85
 
                        if( identifier != null ) return identifier;
86
 
 
87
 
                        if( contact.hasPrimaryNumber() )
88
 
                                identifier = factory( CacheIdentifier.Type.PRIMARY_NUMBER,
89
 
                                        contact.getPrimaryNumber() );
90
 
                        if( identifier != null ) return identifier;
91
 
 
92
 
                        if( contact.hasPrimaryEmail() )
93
 
                                identifier = factory( CacheIdentifier.Type.PRIMARY_EMAIL,
94
 
                                        contact.getPrimaryEmail() );
95
 
                        if( identifier != null ) return identifier;
96
 
 
97
 
                        return null;
 
49
                protected CacheIdentifier()
 
50
                {
 
51
                        _type = Type.NONE;
98
52
                }
99
53
 
100
54
                protected CacheIdentifier( Type type, String detail )
115
69
        }
116
70
 
117
71
        // mappings of contact names, organisations and primary numbers to ids
118
 
        private HashMap< String, Long > _contactsByName
119
 
                = new HashMap< String, Long >();
120
 
        private HashMap< String, Long > _contactsByOrg
121
 
                = new HashMap< String, Long >();
122
 
        private HashMap< String, Long > _contactsByNumber
123
 
                = new HashMap< String, Long >();
124
 
        private HashMap< String, Long > _contactsByEmail
125
 
                = new HashMap< String, Long >();
 
72
        private HashMap< String, Long > _contactsByName;
 
73
        private HashMap< String, Long > _contactsByOrg;
 
74
        private HashMap< String, Long > _contactsByNumber;
 
75
        private HashMap< String, Long > _contactsByEmail;
126
76
 
127
77
        // mapping of contact ids to sets of associated data
128
 
        private HashMap< Long, HashSet< String > > _contactNumbers
129
 
                = new HashMap< Long, HashSet< String > >();
130
 
        private HashMap< Long, HashSet< String > > _contactEmails
131
 
                = new HashMap< Long, HashSet< String > >();
132
 
        private HashMap< Long, HashSet< String > > _contactAddresses
133
 
                = new HashMap< Long, HashSet< String > >();
134
 
        private HashMap< Long, HashSet< String > > _contactOrganisations
135
 
                = new HashMap< Long, HashSet< String > >();
136
 
        private HashMap< Long, HashSet< String > > _contactNotes
137
 
                = new HashMap< Long, HashSet< String > >();
 
78
        private HashMap< Long, HashSet< String > > _contactNumbers;
 
79
        private HashMap< Long, HashSet< String > > _contactEmails;
 
80
        private HashMap< Long, HashSet< String > > _contactAddresses;
 
81
        private HashMap< Long, HashSet< String > > _contactOrganisations;
 
82
 
 
83
        public static CacheIdentifier createIdentifier(
 
84
                Importer.ContactData contact )
 
85
        {
 
86
                if( contact.hasName() ) {
 
87
                        String name = normaliseName( contact.getName() );
 
88
                        if( name != null )
 
89
                                return new CacheIdentifier(
 
90
                                        CacheIdentifier.Type.NAME, name );
 
91
                }
 
92
 
 
93
                if( contact.hasPrimaryOrganisation() ) {
 
94
                        String organisation = normaliseOrganisation(
 
95
                                contact.getPrimaryOrganisation() );
 
96
                        if( organisation != null )
 
97
                                return new CacheIdentifier(
 
98
                                        CacheIdentifier.Type.ORGANISATION, organisation );
 
99
                }
 
100
 
 
101
                if( contact.hasPrimaryNumber() ) {
 
102
                        String number = normalisePhoneNumber( contact.getPrimaryNumber() );
 
103
                        if( number != null )
 
104
                        return new CacheIdentifier(
 
105
                                CacheIdentifier.Type.PRIMARY_NUMBER, number );
 
106
                }
 
107
 
 
108
                if( contact.hasPrimaryEmail() ) {
 
109
                        String email = normaliseEmailAddress( contact.getPrimaryEmail() );
 
110
                        if( email != null )
 
111
                        return new CacheIdentifier(
 
112
                                CacheIdentifier.Type.PRIMARY_EMAIL, email );
 
113
                }
 
114
 
 
115
                return null;
 
116
        }
138
117
 
139
118
        public boolean canLookup( CacheIdentifier identifier )
140
119
        {
141
120
                return lookup( identifier ) != null;
142
121
        }
143
122
 
144
 
        /**
145
 
         * Retrieve the contact id of a contact identified by the specified cache
146
 
         * identifier, if it exists.
147
 
         * @param identifier the cache identifier
148
 
         * @return a contact id, or null
149
 
         */
150
123
        public Long lookup( CacheIdentifier identifier )
151
124
        {
152
125
                switch( identifier.getType() )
163
136
                return null;
164
137
        }
165
138
 
166
 
        /**
167
 
         * Remove any cache entry that is identified by the cache identifier.
168
 
         * @param identifier the cache identifier
169
 
         * @return the contact id of the contact that was removed, or null
170
 
         */
171
139
        public Long removeLookup( CacheIdentifier identifier )
172
140
        {
173
141
                switch( identifier.getType() )
184
152
                return null;
185
153
        }
186
154
 
187
 
        /**
188
 
         * Add a lookup from a contact identifier to a contact id to the cache.
189
 
         * @param identifier the cache identifier
190
 
         * @param id teh contact id
191
 
         */
192
155
        public void addLookup( CacheIdentifier identifier, Long id )
193
156
        {
194
157
                switch( identifier.getType() )
208
171
                }
209
172
        }
210
173
 
211
 
        /**
212
 
         * Remove any data that is associated with an contact id.
213
 
         * @param id
214
 
         */
215
174
        public void removeAssociatedData( Long id )
216
175
        {
217
176
                _contactNumbers.remove( id );
218
177
                _contactEmails.remove( id );
219
178
                _contactAddresses.remove( id );
220
179
                _contactOrganisations.remove( id );
221
 
                _contactNotes.remove( id );
222
180
        }
223
181
 
224
182
        public boolean hasAssociatedNumber( Long id, String number )
240
198
                        set = new HashSet< String >();
241
199
                        _contactNumbers.put( id, set );
242
200
                }
243
 
                set.add( number );
 
201
                set.add( normalisePhoneNumber( number ) );
244
202
        }
245
203
 
246
204
        public boolean hasAssociatedEmail( Long id, String email )
249
207
                if( email == null ) return false;
250
208
 
251
209
                HashSet< String > set = _contactEmails.get( id );
252
 
                return set != null && set.contains( email );
 
210
                return set != null && set.contains( normaliseEmailAddress( email ) );
253
211
        }
254
212
 
255
213
        public void addAssociatedEmail( Long id, String email )
262
220
                        set = new HashSet< String >();
263
221
                        _contactEmails.put( id, set );
264
222
                }
265
 
                set.add( email );
 
223
                set.add( normaliseEmailAddress( email ) );
266
224
        }
267
225
 
268
226
        public boolean hasAssociatedAddress( Long id, String address )
271
229
                if( address == null ) return false;
272
230
 
273
231
                HashSet< String > set = _contactAddresses.get( id );
274
 
                return set != null && set.contains( address );
 
232
                return set != null && set.contains( normaliseAddress( address ) );
275
233
        }
276
234
 
277
235
        public void addAssociatedAddress( Long id, String address )
284
242
                        set = new HashSet< String >();
285
243
                        _contactAddresses.put( id, set );
286
244
                }
287
 
                set.add( address );
 
245
                set.add( normaliseAddress( address ) );
288
246
        }
289
247
 
290
248
        public boolean hasAssociatedOrganisation( Long id, String organisation )
293
251
                if( organisation == null ) return false;
294
252
 
295
253
                HashSet< String > set = _contactOrganisations.get( id );
296
 
                return set != null && set.contains( organisation );
 
254
                return set != null && set.contains(
 
255
                        normaliseOrganisation( organisation ) );
297
256
        }
298
257
 
299
258
        public void addAssociatedOrganisation( Long id, String organisation )
306
265
                        set = new HashSet< String >();
307
266
                        _contactOrganisations.put( id, set );
308
267
                }
309
 
                set.add( organisation );
310
 
        }
311
 
 
312
 
        public boolean hasAssociatedNote( Long id, String note )
313
 
        {
314
 
                note = normaliseNote( note );
315
 
                if( note == null ) return false;
316
 
 
317
 
                HashSet< String > set = _contactNotes.get( id );
318
 
                return set != null && set.contains( note );
319
 
        }
320
 
 
321
 
        public void addAssociatedNote( Long id, String note )
322
 
        {
323
 
                note = normaliseNote( note );
324
 
                if( note == null ) return;
325
 
 
326
 
                HashSet< String > set = _contactNotes.get( id );
327
 
                if( set == null ) {
328
 
                        set = new HashSet< String >();
329
 
                        _contactNotes.put( id, set );
330
 
                }
331
 
                set.add( note );
332
 
        }
333
 
 
334
 
        static public String normaliseName( String name )
 
268
                set.add( normaliseOrganisation( organisation ) );
 
269
        }
 
270
 
 
271
        public void buildCache( Activity activity )
 
272
                throws AbortImportException
 
273
        {
 
274
                Cursor cur;
 
275
 
 
276
                // init id lookups
 
277
                _contactsByName = new HashMap< String, Long >();
 
278
                _contactsByOrg = new HashMap< String, Long >();
 
279
                _contactsByNumber = new HashMap< String, Long >();
 
280
                _contactsByEmail = new HashMap< String, Long >();
 
281
 
 
282
                // init associated data cache
 
283
                _contactNumbers = new HashMap< Long, HashSet< String > >();
 
284
                _contactEmails = new HashMap< Long, HashSet< String > >();
 
285
                _contactAddresses = new HashMap< Long, HashSet< String > >();
 
286
                _contactOrganisations = new HashMap< Long, HashSet< String > >();
 
287
 
 
288
                // set of contact ids that we have not yet added
 
289
                HashSet< Long > unadded = new HashSet< Long >();
 
290
 
 
291
                // get all contacts
 
292
                cur = activity.managedQuery( Contacts.People.CONTENT_URI,
 
293
                        new String[] {
 
294
                                Contacts.People._ID,
 
295
                                Contacts.People.NAME,
 
296
                        }, null, null, null );
 
297
                while( cur.moveToNext() ) {
 
298
                        Long id = cur.getLong(
 
299
                                cur.getColumnIndex( Contacts.People._ID ) );
 
300
                        String name = normaliseName( cur.getString(
 
301
                                cur.getColumnIndex( Contacts.People.NAME ) ) );
 
302
                        if( name != null )
 
303
                        {
 
304
                                // if we can, add a lookup for the contact id by name
 
305
                                if( name.length() > 0 ) {
 
306
                                        addLookup( new CacheIdentifier(
 
307
                                                CacheIdentifier.Type.NAME, name ), id );
 
308
                                        continue;
 
309
                                }
 
310
                        }
 
311
 
 
312
                        // record that a lookup for this contact's id still needs to be
 
313
                        // added by some other means
 
314
                        unadded.add( id );
 
315
                }
 
316
 
 
317
                // get contact organisations, primary ones first
 
318
                cur = activity.managedQuery( Contacts.Organizations.CONTENT_URI,
 
319
                        new String[] {
 
320
                                Contacts.Phones.PERSON_ID,
 
321
                                Contacts.Organizations.COMPANY,
 
322
                        }, null, null, Contacts.Organizations.ISPRIMARY + " DESC" );
 
323
                while( cur.moveToNext() ) {
 
324
                        Long id = cur.getLong( cur.getColumnIndex(
 
325
                                Contacts.Organizations.PERSON_ID ) );
 
326
                        String organisation = normaliseOrganisation( cur.getString(
 
327
                                cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
 
328
                        if( organisation != null )
 
329
                        {
 
330
                                // if this is an organisation name for a contact for whom we
 
331
                                // have not added a lookup, add a lookup for the contact id
 
332
                                // by organisation
 
333
                                if( unadded.contains( id ) ) {
 
334
                                        addLookup( new CacheIdentifier(
 
335
                                                CacheIdentifier.Type.ORGANISATION, organisation ), id );
 
336
                                        unadded.remove( id );
 
337
                                }
 
338
 
 
339
                                // add associated data
 
340
                                addAssociatedOrganisation( id, organisation );
 
341
                        }
 
342
                }
 
343
 
 
344
                // get all phone numbers, primary ones first
 
345
                cur = activity.managedQuery( Contacts.Phones.CONTENT_URI,
 
346
                        new String[] {
 
347
                                Contacts.Phones.PERSON_ID,
 
348
                                Contacts.Phones.NUMBER,
 
349
                        }, null, null, Contacts.Phones.ISPRIMARY + " DESC" );
 
350
                while( cur.moveToNext() ) {
 
351
                        Long id = cur.getLong(
 
352
                                cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
 
353
                        String number = normalisePhoneNumber( cur.getString(
 
354
                                cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
 
355
                        if( number != null )
 
356
                        {
 
357
                                // if this is a number for a contact for whom we have not
 
358
                                // added a lookup, add a lookup for the contact id by phone
 
359
                                // number
 
360
                                if( unadded.contains( id ) ) {
 
361
                                        addLookup( new CacheIdentifier(
 
362
                                                CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
 
363
                                        unadded.remove( id );
 
364
                                }
 
365
 
 
366
                                // add associated data
 
367
                                addAssociatedNumber( id, number );
 
368
                        }
 
369
                }
 
370
 
 
371
                // now get all email addresses, primary ones first, and postal addresses
 
372
                cur = activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
373
                        new String[] {
 
374
                                Contacts.ContactMethods.PERSON_ID,
 
375
                                Contacts.ContactMethods.DATA,
 
376
                                Contacts.ContactMethods.KIND,
 
377
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
 
378
                                "" + Contacts.KIND_EMAIL,
 
379
                                "" + Contacts.KIND_POSTAL,
 
380
                        }, Contacts.ContactMethods.ISPRIMARY + " DESC" );
 
381
                while( cur.moveToNext() ) {
 
382
                        Long id = cur.getLong(
 
383
                                cur.getColumnIndex( Contacts.ContactMethods.PERSON_ID ) );
 
384
                        int kind = cur.getInt(
 
385
                                cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
 
386
                        if( kind == Contacts.KIND_EMAIL )
 
387
                        {
 
388
                                String email = normaliseEmailAddress( cur.getString(
 
389
                                        cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
 
390
                                if( email != null )
 
391
                                {
 
392
                                        // if this is an email address for a contact for whom we
 
393
                                        // have not added a lookup, add a lookup for the contact
 
394
                                        // id by email address
 
395
                                        if( unadded.contains( id ) ) {
 
396
                                                addLookup( new CacheIdentifier(
 
397
                                                        CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
 
398
                                                unadded.remove( id );
 
399
                                        }
 
400
 
 
401
                                        // add associated data
 
402
                                        addAssociatedEmail( id, email );
 
403
                                }
 
404
                        }
 
405
                        else if( kind == Contacts.KIND_POSTAL )
 
406
                        {
 
407
                                String address = normaliseAddress( cur.getString(
 
408
                                        cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
 
409
                                if( address != null )
 
410
                                {
 
411
                                        // add associated data
 
412
                                        addAssociatedAddress( id, address );
 
413
                                }
 
414
                        }
 
415
                }
 
416
        }
 
417
 
 
418
        static private String normaliseName( String name )
335
419
        {
336
420
                if( name == null ) return null;
337
421
                name = name.trim();
338
422
                return name.length() > 0? name : null;
339
423
        }
340
424
 
341
 
        static public String normalisePhoneNumber( String number )
 
425
        static private String normalisePhoneNumber( String number )
342
426
        {
343
427
                if( number == null ) return null;
344
428
                number = number.trim().replaceAll( "[-\\(\\) ]", "" );
345
429
                return number.length() > 0? number : null;
346
430
        }
347
431
 
348
 
        static public String normaliseEmailAddress( String email )
 
432
        static private String normaliseEmailAddress( String email )
349
433
        {
350
434
                if( email == null ) return null;
351
 
                email = email.trim().toLowerCase( Locale.US );
 
435
                email = email.trim().toLowerCase();
352
436
                return email.length() > 0? email : null;
353
437
        }
354
438
 
355
 
        static public String normaliseOrganisation( String organisation )
 
439
        static private String normaliseOrganisation( String organisation )
356
440
        {
357
441
                if( organisation == null ) return null;
358
442
                organisation = organisation.trim();
359
443
                return organisation.length() > 0? organisation : null;
360
444
        }
361
445
 
362
 
        static public String normaliseAddress( String address )
 
446
        static private String normaliseAddress( String address )
363
447
        {
364
448
                if( address == null ) return null;
365
449
                address = address.trim();
366
450
                return address.length() > 0? address : null;
367
451
        }
368
 
 
369
 
        static public String normaliseNote( String note )
370
 
        {
371
 
                if( note == null ) return null;
372
 
                note = note.trim();
373
 
                return note.length() > 0? note : null;
374
 
        }
375
452
}