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

23
23
 
24
24
package am.ed.importcontacts;
25
25
 
 
26
import java.util.HashMap;
26
27
import java.util.HashSet;
 
28
import java.util.Iterator;
27
29
 
28
30
import am.ed.importcontacts.ContactsCache.CacheIdentifier;
29
31
import am.ed.importcontacts.Importer.ContactData;
52
54
                // set of contact ids that we have not yet added
53
55
                HashSet< Long > unadded_ids = new HashSet< Long >();
54
56
 
 
57
                // notes
 
58
                HashMap< Long, String > notes = new HashMap< Long, String >();
 
59
 
55
60
                // get all contacts
56
61
                cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
57
62
                        new String[] {
58
63
                                Contacts.People._ID,
59
64
                                Contacts.People.NAME,
 
65
                                Contacts.People.NOTES,
60
66
                        }, null, null, null );
61
67
                while( cur.moveToNext() ) {
62
68
                        Long id = cur.getLong(
63
69
                                cur.getColumnIndex( Contacts.People._ID ) );
64
 
                        String name =
65
 
                                ContactsCache.normaliseName( cur.getString(
66
 
                                        cur.getColumnIndex( Contacts.People.NAME ) ) );
67
 
                        if( name != null )
 
70
                        String name = cur.getString(
 
71
                                        cur.getColumnIndex( Contacts.People.NAME ) );
 
72
                        String note = cur.getString(
 
73
                                        cur.getColumnIndex( Contacts.People.NOTES ) );
 
74
 
 
75
                        // if we can, add a lookup for the contact id by name
 
76
                        CacheIdentifier cache_identifier = CacheIdentifier.factory(
 
77
                                CacheIdentifier.Type.NAME, name );
 
78
                        if( cache_identifier != null ) {
 
79
                                cache.addLookup( cache_identifier, id );
 
80
 
 
81
                                // add any associated notes (this would get done at the end but,
 
82
                                // since it is most common that contacts are identified by name,
 
83
                                // it is worth doing a special case here
 
84
                                cache.addAssociatedNote( id, note );
 
85
                        }
 
86
                        else
68
87
                        {
69
 
                                // if we can, add a lookup for the contact id by name
70
 
                                if( name.length() > 0 ) {
71
 
                                        cache.addLookup( new CacheIdentifier(
72
 
                                                CacheIdentifier.Type.NAME, name ), id );
73
 
                                        continue;
74
 
                                }
 
88
                                // record that a lookup for this contact's id still needs to be
 
89
                                // added by some other means
 
90
                                unadded_ids.add( id );
 
91
 
 
92
                                // store this contact's notes, so that they can be added to the
 
93
                                // cache at the end, after this contact has been added (by
 
94
                                // whatever identifying means)
 
95
                                if( note != null && note.length() > 0 )
 
96
                                        notes.put( id, note );
75
97
                        }
76
 
 
77
 
                        // record that a lookup for this contact's id still needs to be
78
 
                        // added by some other means
79
 
                        unadded_ids.add( id );
80
98
                }
81
99
 
82
100
                // get contact organisations, primary ones first
88
106
                while( cur.moveToNext() ) {
89
107
                        Long id = cur.getLong( cur.getColumnIndex(
90
108
                                Contacts.Organizations.PERSON_ID ) );
91
 
                        String organisation =
92
 
                                ContactsCache.normaliseOrganisation( cur.getString(
93
 
                                        cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
94
 
                        if( organisation != null )
95
 
                        {
96
 
                                // if this is an organisation name for a contact for whom we
97
 
                                // have not added a lookup, add a lookup for the contact id
98
 
                                // by organisation
99
 
                                if( unadded_ids.contains( id ) ) {
100
 
                                        cache.addLookup( new CacheIdentifier(
101
 
                                                CacheIdentifier.Type.ORGANISATION, organisation ), id );
 
109
                        String organisation = cur.getString(
 
110
                                cur.getColumnIndex( Contacts.Organizations.COMPANY ) );
 
111
 
 
112
                        // if this is an organisation name for a contact for whom we have
 
113
                        // not added a lookup, add a lookup for the contact id by
 
114
                        // organisation
 
115
                        if( unadded_ids.contains( id ) ) {
 
116
                                CacheIdentifier cache_identifier = CacheIdentifier.factory(
 
117
                                        CacheIdentifier.Type.ORGANISATION, organisation );
 
118
                                if( cache_identifier != null ) {
 
119
                                        cache.addLookup( cache_identifier, id );
102
120
                                        unadded_ids.remove( id );
103
121
                                }
104
 
 
105
 
                                // add associated data
106
 
                                cache.addAssociatedOrganisation( id, organisation );
107
122
                        }
 
123
 
 
124
                        // add associated data
 
125
                        cache.addAssociatedOrganisation( id, organisation );
108
126
                }
109
127
 
110
128
                // get all phone numbers, primary ones first
116
134
                while( cur.moveToNext() ) {
117
135
                        Long id = cur.getLong(
118
136
                                cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
119
 
                        String number =
120
 
                                ContactsCache.normalisePhoneNumber( cur.getString(
121
 
                                        cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
122
 
                        if( number != null )
123
 
                        {
124
 
                                // if this is a number for a contact for whom we have not
125
 
                                // added a lookup, add a lookup for the contact id by phone
126
 
                                // number
127
 
                                if( unadded_ids.contains( id ) ) {
128
 
                                        cache.addLookup( new CacheIdentifier(
129
 
                                                CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
 
137
                        String number = cur.getString(
 
138
                                cur.getColumnIndex( Contacts.Phones.NUMBER ) );
 
139
 
 
140
                        // if this is a number for a contact for whom we have not
 
141
                        // added a lookup, add a lookup for the contact id by phone
 
142
                        // number
 
143
                        if( unadded_ids.contains( id ) ) {
 
144
                                CacheIdentifier cache_identifier = CacheIdentifier.factory(
 
145
                                        CacheIdentifier.Type.PRIMARY_NUMBER, number );
 
146
                                if( cache_identifier != null ) {
 
147
                                        cache.addLookup( cache_identifier, id );
130
148
                                        unadded_ids.remove( id );
131
149
                                }
132
 
 
133
 
                                // add associated data
134
 
                                cache.addAssociatedNumber( id, number );
135
150
                        }
 
151
 
 
152
                        // add associated data
 
153
                        cache.addAssociatedNumber( id, number );
136
154
                }
137
155
 
138
156
                // now get all email addresses, primary ones first, and postal addresses
153
171
                                cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
154
172
                        if( kind == Contacts.KIND_EMAIL )
155
173
                        {
156
 
                                String email =
157
 
                                        ContactsCache.normaliseEmailAddress( cur.getString(
158
 
                                                cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
159
 
                                if( email != null )
160
 
                                {
161
 
                                        // if this is an email address for a contact for whom we
162
 
                                        // have not added a lookup, add a lookup for the contact
163
 
                                        // id by email address
164
 
                                        if( unadded_ids.contains( id ) ) {
165
 
                                                cache.addLookup( new CacheIdentifier(
166
 
                                                        CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
 
174
                                String email = cur.getString(
 
175
                                        cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
 
176
 
 
177
                                // if this is an email address for a contact for whom we have
 
178
                                // not added a lookup, add a lookup for the contact id by email
 
179
                                // address
 
180
                                if( unadded_ids.contains( id ) ) {
 
181
                                        CacheIdentifier cache_identifier = CacheIdentifier.factory(
 
182
                                                CacheIdentifier.Type.PRIMARY_EMAIL, email );
 
183
                                        if( cache_identifier != null ) {
 
184
                                                cache.addLookup( cache_identifier, id );
167
185
                                                unadded_ids.remove( id );
168
186
                                        }
 
187
                                }
169
188
 
170
 
                                        // add associated data
171
 
                                        cache.addAssociatedEmail( id, email );
172
 
                                }
 
189
                                // add associated data
 
190
                                cache.addAssociatedEmail( id, email );
173
191
                        }
174
192
                        else if( kind == Contacts.KIND_POSTAL )
175
193
                        {
176
 
                                String address =
177
 
                                        ContactsCache.normaliseAddress( cur.getString(
178
 
                                                cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
179
 
                                if( address != null )
180
 
                                {
181
 
                                        // add associated data
182
 
                                        cache.addAssociatedAddress( id, address );
183
 
                                }
 
194
                                String address = cur.getString(
 
195
                                        cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
 
196
 
 
197
                                // add associated data
 
198
                                cache.addAssociatedAddress( id, address );
184
199
                        }
185
200
                }
 
201
 
 
202
                // finally, add the notes that we stored earlier (we have to add these
 
203
                // at the end because we can't be sure which piece of contact data will
 
204
                // cause the contact to be added to the cache
 
205
                Iterator< Long > i = notes.keySet().iterator();
 
206
                while( i.hasNext() ) {
 
207
                        Long id = i.next();
 
208
                        cache.addAssociatedNote( id, notes.get( id ) );
 
209
                }
186
210
        }
187
211
 
188
212
        @Override
328
352
                        Contacts.Organizations.CONTENT_URI, values );
329
353
        }
330
354
 
 
355
        @Override
 
356
        public void addContactNote( Long id, String note )
 
357
                throws ContactCreationException
 
358
        {
 
359
                ContentValues values = new ContentValues();
 
360
                values.put( Contacts.People.NOTES, note );
 
361
                _activity.getContentResolver().update(
 
362
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
 
363
                        values, null, null );
 
364
        }
331
365
 
332
366
}