/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: Tim Marston
  • Date: 2013-10-20 17:51:32 UTC
  • Revision ID: tim@ed.am-20131020175132-lvqrbal1ztz5jepl
updated .bzrignore

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 to 2013 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
 
7
 * to as "this program").  For more information, see
8
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
36
36
         */
37
37
        public static class CacheIdentifier
38
38
        {
39
 
                public enum Type {
40
 
                        NONE, NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
 
39
                public enum Type { NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
41
40
 
42
41
                private Type _type;
43
42
                private String _detail;
44
43
 
45
 
                protected CacheIdentifier()
46
 
                {
47
 
                        _type = Type.NONE;
48
 
                }
49
 
 
50
44
                /**
51
45
                 * Obtain a cache identifier.  This routine is designed to be as robust
52
46
                 * as possible (in terms of bad or null detail values), and to return
53
47
                 * null when a cache identifier can not be created.
 
48
                 *
54
49
                 * @param type the detail type
55
50
                 * @param detail the detail
56
51
                 * @return the cache identifier, or null
73
68
                 * Obtain a cache identifier from contact data.  This routine is
74
69
                 * designed to be as robust as possible and may return null when a cache
75
70
                 * identifier can not be created.
 
71
                 *
76
72
                 * @param contact the contact data
77
73
                 * @return the cache identifier, or null
78
74
                 */
79
75
                public static CacheIdentifier factory( Importer.ContactData contact )
80
76
                {
81
 
                        CacheIdentifier ret = null;
 
77
                        CacheIdentifier identifier = null;
82
78
 
83
79
                        if( contact.hasName() )
84
 
                                ret = factory( CacheIdentifier.Type.NAME,
 
80
                                identifier = factory( CacheIdentifier.Type.NAME,
85
81
                                        contact.getName() );
86
 
                        if( ret == null && contact.hasPrimaryOrganisation() )
87
 
                                ret = factory( CacheIdentifier.Type.ORGANISATION,
 
82
                        if( identifier != null ) return identifier;
 
83
 
 
84
                        if( contact.hasPrimaryOrganisation() )
 
85
                                identifier = factory( CacheIdentifier.Type.ORGANISATION,
88
86
                                        contact.getPrimaryOrganisation() );
89
 
                        if( ret == null && contact.hasPrimaryNumber() )
90
 
                                ret = factory( CacheIdentifier.Type.PRIMARY_NUMBER,
 
87
                        if( identifier != null ) return identifier;
 
88
 
 
89
                        if( contact.hasPrimaryNumber() )
 
90
                                identifier = factory( CacheIdentifier.Type.PRIMARY_NUMBER,
91
91
                                        contact.getPrimaryNumber() );
92
 
                        if( ret == null && contact.hasPrimaryEmail() )
93
 
                                ret = factory( CacheIdentifier.Type.PRIMARY_EMAIL,
 
92
                        if( identifier != null ) return identifier;
 
93
 
 
94
                        if( contact.hasPrimaryEmail() )
 
95
                                identifier = factory( CacheIdentifier.Type.PRIMARY_EMAIL,
94
96
                                        contact.getPrimaryEmail() );
 
97
                        if( identifier != null ) return identifier;
95
98
 
96
 
                        return ret;
 
99
                        return null;
97
100
                }
98
101
 
99
102
                protected CacheIdentifier( Type type, String detail )
134
137
                = new HashMap< Long, HashSet< String > >();
135
138
        private HashMap< Long, HashSet< String > > _contactNotes
136
139
                = new HashMap< Long, HashSet< String > >();
 
140
        private HashMap< Long, String > _contactBirthdays
 
141
                = new HashMap< Long, String >();
137
142
 
138
143
        public boolean canLookup( CacheIdentifier identifier )
139
144
        {
143
148
        /**
144
149
         * Retrieve the contact id of a contact identified by the specified cache
145
150
         * identifier, if it exists.
 
151
         *
146
152
         * @param identifier the cache identifier
147
153
         * @return a contact id, or null
148
154
         */
164
170
 
165
171
        /**
166
172
         * Remove any cache entry that is identified by the cache identifier.
 
173
         *
167
174
         * @param identifier the cache identifier
168
175
         * @return the contact id of the contact that was removed, or null
169
176
         */
185
192
 
186
193
        /**
187
194
         * Add a lookup from a contact identifier to a contact id to the cache.
 
195
         *
188
196
         * @param identifier the cache identifier
189
197
         * @param id teh contact id
190
198
         */
209
217
 
210
218
        /**
211
219
         * Remove any data that is associated with an contact id.
 
220
         *
212
221
         * @param id
213
222
         */
214
223
        public void removeAssociatedData( Long id )
330
339
                set.add( note );
331
340
        }
332
341
 
 
342
        public boolean hasAssociatedBirthday( Long id, String birthday )
 
343
        {
 
344
                birthday = normaliseBirthday( birthday );
 
345
                if( birthday == null ) return false;
 
346
 
 
347
                String found = _contactBirthdays.get( id );
 
348
                return found != null && found.equalsIgnoreCase( birthday );
 
349
        }
 
350
 
 
351
        public void addAssociatedBirthday( Long id, String birthday )
 
352
        {
 
353
                birthday = normaliseBirthday( birthday );
 
354
                if( birthday == null ) return;
 
355
 
 
356
                _contactBirthdays.put( id, birthday );
 
357
        }
 
358
 
333
359
        static public String normaliseName( String name )
334
360
        {
335
361
                if( name == null ) return null;
371
397
                note = note.trim();
372
398
                return note.length() > 0? note : null;
373
399
        }
 
400
 
 
401
        static public String normaliseBirthday( String birthday )
 
402
        {
 
403
                if( birthday == null ) return null;
 
404
                birthday = birthday.trim();
 
405
                return birthday.length() > 0? birthday : null;
 
406
        }
374
407
}