/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/org/waxworlds/edam/importcontacts/ContactsCache.java

  • Committer: edam
  • Date: 2011-04-22 15:36:06 UTC
  • Revision ID: edam@waxworlds.org-20110422153606-9x9l0nbmvx6oxfxu
- pulled contacts cache out in to seperate class

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsCache.java
3
3
 *
4
 
 * Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
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
35
35
 
36
36
public class ContactsCache
37
37
{
38
 
        /**
39
 
         * Information that can be used to identify a contact within the cache
40
 
         */
41
 
        static public class CacheIdentifier
42
 
        {
43
 
                public enum Type {
44
 
                        NONE, NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
45
 
 
46
 
                private Type _type;
47
 
                private String _detail;
48
 
 
49
 
                protected CacheIdentifier()
50
 
                {
51
 
                        _type = Type.NONE;
52
 
                }
53
 
 
54
 
                protected CacheIdentifier( Type type, String detail )
55
 
                {
56
 
                        _type = type;
57
 
                        _detail = detail;
58
 
                }
59
 
 
60
 
                public Type getType()
61
 
                {
62
 
                        return _type;
63
 
                }
64
 
 
65
 
                public String getDetail()
66
 
                {
67
 
                        return _detail;
68
 
                }
69
 
        }
70
 
 
71
 
        // 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;
76
 
 
77
 
        // mapping of contact ids to sets of associated data
 
38
        private HashMap< String, Long > _contacts;
 
39
 
78
40
        private HashMap< Long, HashSet< String > > _contactNumbers;
79
41
        private HashMap< Long, HashSet< String > > _contactEmails;
80
42
        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
 
        }
117
 
 
118
 
        public boolean canLookup( CacheIdentifier identifier )
119
 
        {
120
 
                return lookup( identifier ) != null;
121
 
        }
122
 
 
123
 
        public Long lookup( CacheIdentifier identifier )
124
 
        {
125
 
                switch( identifier.getType() )
126
 
                {
127
 
                case NAME:
128
 
                        return _contactsByName.get( identifier.getDetail() );
129
 
                case ORGANISATION:
130
 
                        return _contactsByOrg.get( identifier.getDetail() );
131
 
                case PRIMARY_NUMBER:
132
 
                        return _contactsByNumber.get( identifier.getDetail() );
133
 
                case PRIMARY_EMAIL:
134
 
                        return _contactsByEmail.get( identifier.getDetail() );
135
 
                }
136
 
                return null;
137
 
        }
138
 
 
139
 
        public Long removeLookup( CacheIdentifier identifier )
140
 
        {
141
 
                switch( identifier.getType() )
142
 
                {
143
 
                case NAME:
144
 
                        return _contactsByName.remove( identifier.getDetail() );
145
 
                case ORGANISATION:
146
 
                        return _contactsByOrg.remove( identifier.getDetail() );
147
 
                case PRIMARY_NUMBER:
148
 
                        return _contactsByNumber.remove( identifier.getDetail() );
149
 
                case PRIMARY_EMAIL:
150
 
                        return _contactsByEmail.remove( identifier.getDetail() );
151
 
                }
152
 
                return null;
153
 
        }
154
 
 
155
 
        public void addLookup( CacheIdentifier identifier, Long id )
156
 
        {
157
 
                switch( identifier.getType() )
158
 
                {
159
 
                case NAME:
160
 
                        _contactsByName.put( identifier.getDetail(), id );
161
 
                        break;
162
 
                case ORGANISATION:
163
 
                        _contactsByOrg.put( identifier.getDetail(), id );
164
 
                        break;
165
 
                case PRIMARY_NUMBER:
166
 
                        _contactsByNumber.put( identifier.getDetail(), id );
167
 
                        break;
168
 
                case PRIMARY_EMAIL:
169
 
                        _contactsByEmail.put( identifier.getDetail(), id );
170
 
                        break;
171
 
                }
172
 
        }
173
 
 
174
 
        public void removeAssociatedData( Long id )
175
 
        {
176
 
                _contactNumbers.remove( id );
177
 
                _contactEmails.remove( id );
178
 
                _contactAddresses.remove( id );
179
 
                _contactOrganisations.remove( id );
180
 
        }
181
 
 
182
 
        public boolean hasAssociatedNumber( Long id, String number )
183
 
        {
184
 
                number = normalisePhoneNumber( number );
185
 
                if( number == null ) return false;
186
 
 
187
 
                HashSet< String > set = _contactNumbers.get( id );
188
 
                return set != null && set.contains( number );
189
 
        }
190
 
 
191
 
        public void addAssociatedNumber( Long id, String number )
192
 
        {
193
 
                number = normalisePhoneNumber( number );
194
 
                if( number == null ) return;
195
 
 
196
 
                HashSet< String > set = _contactNumbers.get( id );
197
 
                if( set == null ) {
198
 
                        set = new HashSet< String >();
199
 
                        _contactNumbers.put( id, set );
200
 
                }
201
 
                set.add( normalisePhoneNumber( number ) );
202
 
        }
203
 
 
204
 
        public boolean hasAssociatedEmail( Long id, String email )
205
 
        {
206
 
                email = normaliseEmailAddress( email );
207
 
                if( email == null ) return false;
208
 
 
209
 
                HashSet< String > set = _contactEmails.get( id );
210
 
                return set != null && set.contains( normaliseEmailAddress( email ) );
211
 
        }
212
 
 
213
 
        public void addAssociatedEmail( Long id, String email )
214
 
        {
215
 
                email = normaliseEmailAddress( email );
216
 
                if( email == null ) return;
217
 
 
218
 
                HashSet< String > set = _contactEmails.get( id );
219
 
                if( set == null ) {
220
 
                        set = new HashSet< String >();
221
 
                        _contactEmails.put( id, set );
222
 
                }
223
 
                set.add( normaliseEmailAddress( email ) );
224
 
        }
225
 
 
226
 
        public boolean hasAssociatedAddress( Long id, String address )
227
 
        {
228
 
                address = normaliseAddress( address );
229
 
                if( address == null ) return false;
230
 
 
231
 
                HashSet< String > set = _contactAddresses.get( id );
232
 
                return set != null && set.contains( normaliseAddress( address ) );
233
 
        }
234
 
 
235
 
        public void addAssociatedAddress( Long id, String address )
236
 
        {
237
 
                address = normaliseAddress( address );
238
 
                if( address == null ) return;
239
 
 
240
 
                HashSet< String > set = _contactAddresses.get( id );
241
 
                if( set == null ) {
242
 
                        set = new HashSet< String >();
243
 
                        _contactAddresses.put( id, set );
244
 
                }
245
 
                set.add( normaliseAddress( address ) );
246
 
        }
247
 
 
248
 
        public boolean hasAssociatedOrganisation( Long id, String organisation )
249
 
        {
250
 
                organisation = normaliseOrganisation( organisation );
251
 
                if( organisation == null ) return false;
252
 
 
253
 
                HashSet< String > set = _contactOrganisations.get( id );
254
 
                return set != null && set.contains(
255
 
                        normaliseOrganisation( organisation ) );
256
 
        }
257
 
 
258
 
        public void addAssociatedOrganisation( Long id, String organisation )
259
 
        {
260
 
                organisation = normaliseOrganisation( organisation );
261
 
                if( organisation == null ) return;
262
 
 
263
 
                HashSet< String > set = _contactOrganisations.get( id );
264
 
                if( set == null ) {
265
 
                        set = new HashSet< String >();
266
 
                        _contactOrganisations.put( id, set );
267
 
                }
268
 
                set.add( normaliseOrganisation( organisation ) );
 
43
 
 
44
        public boolean exists( String name )
 
45
        {
 
46
                return _contacts.containsKey( name );
 
47
        }
 
48
 
 
49
        public Long getId( String name )
 
50
        {
 
51
                return _contacts.get( name );
 
52
        }
 
53
 
 
54
        public void remove( String name )
 
55
        {
 
56
                _contacts.remove( name );
 
57
        }
 
58
 
 
59
        public void put( Long id, String name )
 
60
        {
 
61
                _contacts.put( name, id );
 
62
        }
 
63
 
 
64
        public boolean hasNumber( Long id, String number )
 
65
        {
 
66
                HashSet< String > cache = _contactNumbers.get( id );
 
67
                return cache != null && cache.contains( number );
 
68
        }
 
69
 
 
70
        public void addNumber( Long id, String number )
 
71
        {
 
72
                HashSet< String > cache = _contactNumbers.get( id );
 
73
                if( cache == null ) {
 
74
                        cache = new HashSet< String >();
 
75
                        _contactNumbers.put( id, cache );
 
76
                }
 
77
                cache.add( number );
 
78
        }
 
79
 
 
80
        public boolean hasEmail( Long id, String email )
 
81
        {
 
82
                HashSet< String > cache = _contactEmails.get( id );
 
83
                return cache != null && cache.contains( email );
 
84
        }
 
85
 
 
86
        public void addEmail( Long id, String email )
 
87
        {
 
88
                HashSet< String > cache = _contactEmails.get( id );
 
89
                if( cache == null ) {
 
90
                        cache = new HashSet< String >();
 
91
                        _contactEmails.put( id, cache );
 
92
                }
 
93
                cache.add( email );
 
94
        }
 
95
 
 
96
        public boolean hasAddress( Long id, String address )
 
97
        {
 
98
                HashSet< String > cache = _contactAddresses.get( id );
 
99
                return cache != null && cache.contains( address );
 
100
        }
 
101
 
 
102
        public void addAddress( Long id, String address )
 
103
        {
 
104
                HashSet< String > cache = _contactAddresses.get( id );
 
105
                if( cache == null ) {
 
106
                        cache = new HashSet< String >();
 
107
                        _contactAddresses.put( id, cache );
 
108
                }
 
109
                cache.add( address );
269
110
        }
270
111
 
271
112
        public void buildCache( Activity activity )
272
113
                throws AbortImportException
273
114
        {
 
115
                String[] cols;
274
116
                Cursor cur;
275
117
 
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
 
118
                // init contacts caches
 
119
                _contacts = new HashMap< String, Long >();
283
120
                _contactNumbers = new HashMap< Long, HashSet< String > >();
284
121
                _contactEmails = new HashMap< Long, HashSet< String > >();
285
122
                _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
 
123
 
 
124
                // query and store map of contact names to ids
 
125
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
292
126
                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
 
127
                        cols, null, null, null);
 
128
                if( cur.moveToFirst() ) {
 
129
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
 
130
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
 
131
                        do {
 
132
                                _contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
 
133
                        } while( cur.moveToNext() );
 
134
                }
 
135
 
 
136
                // query and store map of contact ids to sets of phone numbers
 
137
                cols = new String[] { Contacts.Phones.PERSON_ID,
 
138
                                Contacts.Phones.NUMBER };
345
139
                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 )
419
 
        {
420
 
                if( name == null ) return null;
421
 
                name = name.trim();
422
 
                return name.length() > 0? name : null;
423
 
        }
424
 
 
425
 
        static private String normalisePhoneNumber( String number )
426
 
        {
427
 
                if( number == null ) return null;
428
 
                number = number.trim().replaceAll( "[-\\(\\) ]", "" );
429
 
                return number.length() > 0? number : null;
430
 
        }
431
 
 
432
 
        static private String normaliseEmailAddress( String email )
433
 
        {
434
 
                if( email == null ) return null;
435
 
                email = email.trim().toLowerCase();
436
 
                return email.length() > 0? email : null;
437
 
        }
438
 
 
439
 
        static private String normaliseOrganisation( String organisation )
440
 
        {
441
 
                if( organisation == null ) return null;
442
 
                organisation = organisation.trim();
443
 
                return organisation.length() > 0? organisation : null;
444
 
        }
445
 
 
446
 
        static private String normaliseAddress( String address )
447
 
        {
448
 
                if( address == null ) return null;
449
 
                address = address.trim();
450
 
                return address.length() > 0? address : null;
 
140
                        cols, null, null, null);
 
141
                if( cur.moveToFirst() ) {
 
142
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
 
143
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
 
144
                        do {
 
145
                                Long id = cur.getLong( personIdCol );
 
146
                                String number = Importer.sanitisePhoneNumber(
 
147
                                                cur.getString( numberCol ) );
 
148
                                if( number != null ) {
 
149
                                        HashSet< String > numbers = _contactNumbers.get( id );
 
150
                                        if( numbers == null ) {
 
151
                                                numbers = new HashSet< String >();
 
152
                                                _contactNumbers.put( id, numbers );
 
153
                                        }
 
154
                                        numbers.add( number );
 
155
                                }
 
156
                        } while( cur.moveToNext() );
 
157
                }
 
158
 
 
159
                // query and store map of contact ids to sets of email addresses
 
160
                cols = new String[] { Contacts.ContactMethods.PERSON_ID,
 
161
                                Contacts.ContactMethods.DATA };
 
162
                cur = activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
163
                                cols, Contacts.ContactMethods.KIND + " = ?",
 
164
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
 
165
                if( cur.moveToFirst() ) {
 
166
                        int personIdCol = cur.getColumnIndex(
 
167
                                Contacts.ContactMethods.PERSON_ID );
 
168
                        int addressCol = cur.getColumnIndex(
 
169
                                Contacts.ContactMethods.DATA );
 
170
                        do {
 
171
                                Long id = cur.getLong( personIdCol );
 
172
                                String address = Importer.sanitiseEmailAddress(
 
173
                                        cur.getString( addressCol ) );
 
174
                                if( address != null ) {
 
175
                                        HashSet< String > addresses = _contactEmails.get( id );
 
176
                                        if( addresses == null ) {
 
177
                                                addresses = new HashSet< String >();
 
178
                                                _contactEmails.put( id, addresses );
 
179
                                        }
 
180
                                        addresses.add( address );
 
181
                                }
 
182
                        } while( cur.moveToNext() );
 
183
                }
 
184
 
 
185
                // query and store map of contact ids to sets of postal addresses
 
186
                cols = new String[] { Contacts.ContactMethods.PERSON_ID,
 
187
                        Contacts.ContactMethods.DATA };
 
188
                cur = activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
189
                        cols, Contacts.ContactMethods.KIND + " = ?",
 
190
                        new String[] { "" + Contacts.KIND_POSTAL }, null );
 
191
                if( cur.moveToFirst() ) {
 
192
                        int personIdCol = cur.getColumnIndex(
 
193
                                Contacts.ContactMethods.PERSON_ID );
 
194
                        int addressCol = cur.getColumnIndex(
 
195
                                Contacts.ContactMethods.DATA );
 
196
                        do {
 
197
                                Long id = cur.getLong( personIdCol );
 
198
                                String address = cur.getString( addressCol );
 
199
                                if( address != null ) {
 
200
                                        HashSet< String > addresses = _contactAddresses.get( id );
 
201
                                        if( addresses == null ) {
 
202
                                                addresses = new HashSet< String >();
 
203
                                                _contactAddresses.put( id, addresses );
 
204
                                        }
 
205
                                        addresses.add( address );
 
206
                                }
 
207
                        } while( cur.moveToNext() );
 
208
                }
451
209
        }
452
210
}