/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-19 17:41:29 UTC
  • Revision ID: tim@ed.am-20121219174129-41i7vrz0jviideqi
fix intro strings

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;
29
28
 
30
29
public class ContactsCache
31
30
{
32
31
        /**
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.
 
32
         * Information that can be used to identify a contact within the cache
36
33
         */
37
 
        public static class CacheIdentifier
 
34
        static public class CacheIdentifier
38
35
        {
39
36
                public enum Type {
40
37
                        NONE, NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
47
44
                        _type = Type.NONE;
48
45
                }
49
46
 
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
 
 
99
47
                protected CacheIdentifier( Type type, String detail )
100
48
                {
101
49
                        _type = type;
132
80
                = new HashMap< Long, HashSet< String > >();
133
81
        private HashMap< Long, HashSet< String > > _contactOrganisations
134
82
                = new HashMap< Long, HashSet< String > >();
135
 
        private HashMap< Long, HashSet< String > > _contactNotes
136
 
                = 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
        }
137
118
 
138
119
        public boolean canLookup( CacheIdentifier identifier )
139
120
        {
140
121
                return lookup( identifier ) != null;
141
122
        }
142
123
 
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
 
         */
149
124
        public Long lookup( CacheIdentifier identifier )
150
125
        {
151
126
                switch( identifier.getType() )
162
137
                return null;
163
138
        }
164
139
 
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
 
         */
170
140
        public Long removeLookup( CacheIdentifier identifier )
171
141
        {
172
142
                switch( identifier.getType() )
183
153
                return null;
184
154
        }
185
155
 
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
 
         */
191
156
        public void addLookup( CacheIdentifier identifier, Long id )
192
157
        {
193
158
                switch( identifier.getType() )
207
172
                }
208
173
        }
209
174
 
210
 
        /**
211
 
         * Remove any data that is associated with an contact id.
212
 
         * @param id
213
 
         */
214
175
        public void removeAssociatedData( Long id )
215
176
        {
216
177
                _contactNumbers.remove( id );
217
178
                _contactEmails.remove( id );
218
179
                _contactAddresses.remove( id );
219
180
                _contactOrganisations.remove( id );
220
 
                _contactNotes.remove( id );
221
181
        }
222
182
 
223
183
        public boolean hasAssociatedNumber( Long id, String number )
239
199
                        set = new HashSet< String >();
240
200
                        _contactNumbers.put( id, set );
241
201
                }
242
 
                set.add( number );
 
202
                set.add( normalisePhoneNumber( number ) );
243
203
        }
244
204
 
245
205
        public boolean hasAssociatedEmail( Long id, String email )
248
208
                if( email == null ) return false;
249
209
 
250
210
                HashSet< String > set = _contactEmails.get( id );
251
 
                return set != null && set.contains( email );
 
211
                return set != null && set.contains( normaliseEmailAddress( email ) );
252
212
        }
253
213
 
254
214
        public void addAssociatedEmail( Long id, String email )
261
221
                        set = new HashSet< String >();
262
222
                        _contactEmails.put( id, set );
263
223
                }
264
 
                set.add( email );
 
224
                set.add( normaliseEmailAddress( email ) );
265
225
        }
266
226
 
267
227
        public boolean hasAssociatedAddress( Long id, String address )
270
230
                if( address == null ) return false;
271
231
 
272
232
                HashSet< String > set = _contactAddresses.get( id );
273
 
                return set != null && set.contains( address );
 
233
                return set != null && set.contains( normaliseAddress( address ) );
274
234
        }
275
235
 
276
236
        public void addAssociatedAddress( Long id, String address )
283
243
                        set = new HashSet< String >();
284
244
                        _contactAddresses.put( id, set );
285
245
                }
286
 
                set.add( address );
 
246
                set.add( normaliseAddress( address ) );
287
247
        }
288
248
 
289
249
        public boolean hasAssociatedOrganisation( Long id, String organisation )
292
252
                if( organisation == null ) return false;
293
253
 
294
254
                HashSet< String > set = _contactOrganisations.get( id );
295
 
                return set != null && set.contains( organisation );
 
255
                return set != null && set.contains(
 
256
                        normaliseOrganisation( organisation ) );
296
257
        }
297
258
 
298
259
        public void addAssociatedOrganisation( Long id, String organisation )
305
266
                        set = new HashSet< String >();
306
267
                        _contactOrganisations.put( id, set );
307
268
                }
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 );
 
269
                set.add( normaliseOrganisation( organisation ) );
331
270
        }
332
271
 
333
272
        static public String normaliseName( String name )
347
286
        static public String normaliseEmailAddress( String email )
348
287
        {
349
288
                if( email == null ) return null;
350
 
                email = email.trim().toLowerCase( Locale.US );
 
289
                email = email.trim().toLowerCase();
351
290
                return email.length() > 0? email : null;
352
291
        }
353
292
 
364
303
                address = address.trim();
365
304
                return address.length() > 0? address : null;
366
305
        }
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
 
        }
374
306
}