/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-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

25
25
 
26
26
import java.util.HashMap;
27
27
import java.util.HashSet;
 
28
import java.util.Locale;
28
29
 
29
30
public class ContactsCache
30
31
{
31
32
        /**
32
 
         * Information that can be used to identify a contact within the cache
 
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.
33
36
         */
34
 
        static public class CacheIdentifier
 
37
        public static class CacheIdentifier
35
38
        {
36
39
                public enum Type {
37
40
                        NONE, NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
44
47
                        _type = Type.NONE;
45
48
                }
46
49
 
 
50
                /**
 
51
                 * Obtain a cache identifier.  This routine is designed to be as robust
 
52
                 * as possible (in terms of bad or null detail values), and to return
 
53
                 * null when a cache identifier can not be created.
 
54
                 * @param type the detail type
 
55
                 * @param detail the detail
 
56
                 * @return the cache identifier, or null
 
57
                 */
 
58
                public static CacheIdentifier factory( Type type, String detail )
 
59
                {
 
60
                        switch( type )
 
61
                        {
 
62
                        case NAME: detail = normaliseName( detail ); break;
 
63
                        case ORGANISATION: detail = normaliseOrganisation( detail ); break;
 
64
                        case PRIMARY_NUMBER: detail = normalisePhoneNumber( detail ); break;
 
65
                        case PRIMARY_EMAIL: detail = normaliseEmailAddress( detail ); break;
 
66
                        default: return null;
 
67
                        }
 
68
                        if( detail == null ) return null;
 
69
                        return new CacheIdentifier( type, detail );
 
70
                }
 
71
 
 
72
                /**
 
73
                 * Obtain a cache identifier from contact data.  This routine is
 
74
                 * designed to be as robust as possible and may return null when a cache
 
75
                 * identifier can not be created.
 
76
                 * @param contact the contact data
 
77
                 * @return the cache identifier, or null
 
78
                 */
 
79
                public static CacheIdentifier factory( Importer.ContactData contact )
 
80
                {
 
81
                        CacheIdentifier ret = null;
 
82
 
 
83
                        if( contact.hasName() )
 
84
                                ret = factory( CacheIdentifier.Type.NAME,
 
85
                                        contact.getName() );
 
86
                        if( ret == null && contact.hasPrimaryOrganisation() )
 
87
                                ret = factory( CacheIdentifier.Type.ORGANISATION,
 
88
                                        contact.getPrimaryOrganisation() );
 
89
                        if( ret == null && contact.hasPrimaryNumber() )
 
90
                                ret = factory( CacheIdentifier.Type.PRIMARY_NUMBER,
 
91
                                        contact.getPrimaryNumber() );
 
92
                        if( ret == null && contact.hasPrimaryEmail() )
 
93
                                ret = factory( CacheIdentifier.Type.PRIMARY_EMAIL,
 
94
                                        contact.getPrimaryEmail() );
 
95
 
 
96
                        return ret;
 
97
                }
 
98
 
47
99
                protected CacheIdentifier( Type type, String detail )
48
100
                {
49
101
                        _type = type;
80
132
                = new HashMap< Long, HashSet< String > >();
81
133
        private HashMap< Long, HashSet< String > > _contactOrganisations
82
134
                = new HashMap< Long, HashSet< String > >();
83
 
 
84
 
        public static CacheIdentifier createIdentifier(
85
 
                Importer.ContactData contact )
86
 
        {
87
 
                if( contact.hasName() ) {
88
 
                        String name = normaliseName( contact.getName() );
89
 
                        if( name != null )
90
 
                                return new CacheIdentifier(
91
 
                                        CacheIdentifier.Type.NAME, name );
92
 
                }
93
 
 
94
 
                if( contact.hasPrimaryOrganisation() ) {
95
 
                        String organisation = normaliseOrganisation(
96
 
                                contact.getPrimaryOrganisation() );
97
 
                        if( organisation != null )
98
 
                                return new CacheIdentifier(
99
 
                                        CacheIdentifier.Type.ORGANISATION, organisation );
100
 
                }
101
 
 
102
 
                if( contact.hasPrimaryNumber() ) {
103
 
                        String number = normalisePhoneNumber( contact.getPrimaryNumber() );
104
 
                        if( number != null )
105
 
                        return new CacheIdentifier(
106
 
                                CacheIdentifier.Type.PRIMARY_NUMBER, number );
107
 
                }
108
 
 
109
 
                if( contact.hasPrimaryEmail() ) {
110
 
                        String email = normaliseEmailAddress( contact.getPrimaryEmail() );
111
 
                        if( email != null )
112
 
                        return new CacheIdentifier(
113
 
                                CacheIdentifier.Type.PRIMARY_EMAIL, email );
114
 
                }
115
 
 
116
 
                return null;
117
 
        }
 
135
        private HashMap< Long, HashSet< String > > _contactNotes
 
136
                = new HashMap< Long, HashSet< String > >();
118
137
 
119
138
        public boolean canLookup( CacheIdentifier identifier )
120
139
        {
121
140
                return lookup( identifier ) != null;
122
141
        }
123
142
 
 
143
        /**
 
144
         * Retrieve the contact id of a contact identified by the specified cache
 
145
         * identifier, if it exists.
 
146
         * @param identifier the cache identifier
 
147
         * @return a contact id, or null
 
148
         */
124
149
        public Long lookup( CacheIdentifier identifier )
125
150
        {
126
151
                switch( identifier.getType() )
137
162
                return null;
138
163
        }
139
164
 
 
165
        /**
 
166
         * Remove any cache entry that is identified by the cache identifier.
 
167
         * @param identifier the cache identifier
 
168
         * @return the contact id of the contact that was removed, or null
 
169
         */
140
170
        public Long removeLookup( CacheIdentifier identifier )
141
171
        {
142
172
                switch( identifier.getType() )
153
183
                return null;
154
184
        }
155
185
 
 
186
        /**
 
187
         * Add a lookup from a contact identifier to a contact id to the cache.
 
188
         * @param identifier the cache identifier
 
189
         * @param id teh contact id
 
190
         */
156
191
        public void addLookup( CacheIdentifier identifier, Long id )
157
192
        {
158
193
                switch( identifier.getType() )
172
207
                }
173
208
        }
174
209
 
 
210
        /**
 
211
         * Remove any data that is associated with an contact id.
 
212
         * @param id
 
213
         */
175
214
        public void removeAssociatedData( Long id )
176
215
        {
177
216
                _contactNumbers.remove( id );
178
217
                _contactEmails.remove( id );
179
218
                _contactAddresses.remove( id );
180
219
                _contactOrganisations.remove( id );
 
220
                _contactNotes.remove( id );
181
221
        }
182
222
 
183
223
        public boolean hasAssociatedNumber( Long id, String number )
199
239
                        set = new HashSet< String >();
200
240
                        _contactNumbers.put( id, set );
201
241
                }
202
 
                set.add( normalisePhoneNumber( number ) );
 
242
                set.add( number );
203
243
        }
204
244
 
205
245
        public boolean hasAssociatedEmail( Long id, String email )
208
248
                if( email == null ) return false;
209
249
 
210
250
                HashSet< String > set = _contactEmails.get( id );
211
 
                return set != null && set.contains( normaliseEmailAddress( email ) );
 
251
                return set != null && set.contains( email );
212
252
        }
213
253
 
214
254
        public void addAssociatedEmail( Long id, String email )
221
261
                        set = new HashSet< String >();
222
262
                        _contactEmails.put( id, set );
223
263
                }
224
 
                set.add( normaliseEmailAddress( email ) );
 
264
                set.add( email );
225
265
        }
226
266
 
227
267
        public boolean hasAssociatedAddress( Long id, String address )
230
270
                if( address == null ) return false;
231
271
 
232
272
                HashSet< String > set = _contactAddresses.get( id );
233
 
                return set != null && set.contains( normaliseAddress( address ) );
 
273
                return set != null && set.contains( address );
234
274
        }
235
275
 
236
276
        public void addAssociatedAddress( Long id, String address )
243
283
                        set = new HashSet< String >();
244
284
                        _contactAddresses.put( id, set );
245
285
                }
246
 
                set.add( normaliseAddress( address ) );
 
286
                set.add( address );
247
287
        }
248
288
 
249
289
        public boolean hasAssociatedOrganisation( Long id, String organisation )
252
292
                if( organisation == null ) return false;
253
293
 
254
294
                HashSet< String > set = _contactOrganisations.get( id );
255
 
                return set != null && set.contains(
256
 
                        normaliseOrganisation( organisation ) );
 
295
                return set != null && set.contains( organisation );
257
296
        }
258
297
 
259
298
        public void addAssociatedOrganisation( Long id, String organisation )
266
305
                        set = new HashSet< String >();
267
306
                        _contactOrganisations.put( id, set );
268
307
                }
269
 
                set.add( normaliseOrganisation( organisation ) );
 
308
                set.add( organisation );
 
309
        }
 
310
 
 
311
        public boolean hasAssociatedNote( Long id, String note )
 
312
        {
 
313
                note = normaliseNote( note );
 
314
                if( note == null ) return false;
 
315
 
 
316
                HashSet< String > set = _contactNotes.get( id );
 
317
                return set != null && set.contains( note );
 
318
        }
 
319
 
 
320
        public void addAssociatedNote( Long id, String note )
 
321
        {
 
322
                note = normaliseNote( note );
 
323
                if( note == null ) return;
 
324
 
 
325
                HashSet< String > set = _contactNotes.get( id );
 
326
                if( set == null ) {
 
327
                        set = new HashSet< String >();
 
328
                        _contactNotes.put( id, set );
 
329
                }
 
330
                set.add( note );
270
331
        }
271
332
 
272
333
        static public String normaliseName( String name )
286
347
        static public String normaliseEmailAddress( String email )
287
348
        {
288
349
                if( email == null ) return null;
289
 
                email = email.trim().toLowerCase();
 
350
                email = email.trim().toLowerCase( Locale.US );
290
351
                return email.length() > 0? email : null;
291
352
        }
292
353
 
303
364
                address = address.trim();
304
365
                return address.length() > 0? address : null;
305
366
        }
 
367
 
 
368
        static public String normaliseNote( String note )
 
369
        {
 
370
                if( note == null ) return null;
 
371
                note = note.trim();
 
372
                return note.length() > 0? note : null;
 
373
        }
306
374
}