/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-05-08 15:09:13 UTC
  • Revision ID: tim@ed.am-20120508150913-t9qzbbs1kld691dy
abstracted the android contacts API in to an interface, ready to be switched to
another depending on the platform version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import java.util.HashMap;
27
27
import java.util.HashSet;
28
28
 
29
 
import am.ed.importcontacts.Importer.AbortImportException;
30
 
 
31
29
import android.app.Activity;
32
 
import android.database.Cursor;
33
 
import android.provider.Contacts;
34
30
 
35
31
 
36
32
public class ContactsCache
69
65
        }
70
66
 
71
67
        // mappings of contact names, organisations and primary numbers to ids
72
 
        private HashMap< String, Long > _contactsByName;
73
 
        private HashMap< String, Long > _contactsByOrg;
74
 
        private HashMap< String, Long > _contactsByNumber;
75
 
        private HashMap< String, Long > _contactsByEmail;
 
68
        private HashMap< String, Long > _contactsByName
 
69
                = new HashMap< String, Long >();
 
70
        private HashMap< String, Long > _contactsByOrg
 
71
                = new HashMap< String, Long >();
 
72
        private HashMap< String, Long > _contactsByNumber
 
73
                = new HashMap< String, Long >();
 
74
        private HashMap< String, Long > _contactsByEmail
 
75
                = new HashMap< String, Long >();
76
76
 
77
77
        // mapping of contact ids to sets of associated data
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;
 
78
        private HashMap< Long, HashSet< String > > _contactNumbers
 
79
                = new HashMap< Long, HashSet< String > >();
 
80
        private HashMap< Long, HashSet< String > > _contactEmails
 
81
                = new HashMap< Long, HashSet< String > >();
 
82
        private HashMap< Long, HashSet< String > > _contactAddresses
 
83
                = new HashMap< Long, HashSet< String > >();
 
84
        private HashMap< Long, HashSet< String > > _contactOrganisations
 
85
                = new HashMap< Long, HashSet< String > >();
 
86
 
 
87
        ContactsCache()
 
88
        {
 
89
                // init id lookups
 
90
//              _contactsByName = new HashMap< String, Long >();
 
91
//              _contactsByOrg = new HashMap< String, Long >();
 
92
//              _contactsByNumber = new HashMap< String, Long >();
 
93
//              _contactsByEmail = new HashMap< String, Long >();
 
94
 
 
95
                // init associated data cache
 
96
//              _contactNumbers = new HashMap< Long, HashSet< String > >();
 
97
//              _contactEmails = new HashMap< Long, HashSet< String > >();
 
98
//              _contactAddresses = new HashMap< Long, HashSet< String > >();
 
99
//              _contactOrganisations = new HashMap< Long, HashSet< String > >();
 
100
        }
82
101
 
83
102
        public static CacheIdentifier createIdentifier(
84
103
                Importer.ContactData contact )
268
287
                set.add( normaliseOrganisation( organisation ) );
269
288
        }
270
289
 
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 )
 
290
        static public String normaliseName( String name )
419
291
        {
420
292
                if( name == null ) return null;
421
293
                name = name.trim();
422
294
                return name.length() > 0? name : null;
423
295
        }
424
296
 
425
 
        static private String normalisePhoneNumber( String number )
 
297
        static public String normalisePhoneNumber( String number )
426
298
        {
427
299
                if( number == null ) return null;
428
300
                number = number.trim().replaceAll( "[-\\(\\) ]", "" );
429
301
                return number.length() > 0? number : null;
430
302
        }
431
303
 
432
 
        static private String normaliseEmailAddress( String email )
 
304
        static public String normaliseEmailAddress( String email )
433
305
        {
434
306
                if( email == null ) return null;
435
307
                email = email.trim().toLowerCase();
436
308
                return email.length() > 0? email : null;
437
309
        }
438
310
 
439
 
        static private String normaliseOrganisation( String organisation )
 
311
        static public String normaliseOrganisation( String organisation )
440
312
        {
441
313
                if( organisation == null ) return null;
442
314
                organisation = organisation.trim();
443
315
                return organisation.length() > 0? organisation : null;
444
316
        }
445
317
 
446
 
        static private String normaliseAddress( String address )
 
318
        static public String normaliseAddress( String address )
447
319
        {
448
320
                if( address == null ) return null;
449
321
                address = address.trim();