/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: 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
 * ContactsBackend.java
3
3
 *
4
 
 * Copyright (C) 2012 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2012 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
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
 
                cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
 
61
                cur = _activity.getContentResolver().query(
 
62
                        Contacts.People.CONTENT_URI,
57
63
                        new String[] {
58
64
                                Contacts.People._ID,
59
65
                                Contacts.People.NAME,
 
66
                                Contacts.People.NOTES,
60
67
                        }, null, null, null );
61
68
                while( cur.moveToNext() ) {
62
69
                        Long id = cur.getLong(
63
70
                                cur.getColumnIndex( Contacts.People._ID ) );
64
 
                        String name =
65
 
                                ContactsCache.normaliseName( cur.getString(
66
 
                                        cur.getColumnIndex( Contacts.People.NAME ) ) );
67
 
                        if( name != null )
 
71
                        String name = cur.getString(
 
72
                                        cur.getColumnIndex( Contacts.People.NAME ) );
 
73
                        String note = cur.getString(
 
74
                                        cur.getColumnIndex( Contacts.People.NOTES ) );
 
75
 
 
76
                        // if we can, add a lookup for the contact id by name
 
77
                        CacheIdentifier cache_identifier = CacheIdentifier.factory(
 
78
                                CacheIdentifier.Type.NAME, name );
 
79
                        if( cache_identifier != null ) {
 
80
                                cache.addLookup( cache_identifier, id );
 
81
 
 
82
                                // add any associated notes (this would get done at the end but,
 
83
                                // since it is most common that contacts are identified by name,
 
84
                                // it is worth doing a special case here
 
85
                                cache.addAssociatedNote( id, note );
 
86
                        }
 
87
                        else
68
88
                        {
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
 
                                }
 
89
                                // record that a lookup for this contact's id still needs to be
 
90
                                // added by some other means
 
91
                                unadded_ids.add( id );
 
92
 
 
93
                                // store this contact's notes, so that they can be added to the
 
94
                                // cache at the end, after this contact has been added (by
 
95
                                // whatever identifying means)
 
96
                                if( note != null && note.length() > 0 )
 
97
                                        notes.put( id, note );
75
98
                        }
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
99
                }
 
100
                cur.close();
81
101
 
82
102
                // get contact organisations, primary ones first
83
 
                cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
 
103
                cur = _activity.getContentResolver().query(
 
104
                        Contacts.Organizations.CONTENT_URI,
84
105
                        new String[] {
85
106
                                Contacts.Phones.PERSON_ID,
86
107
                                Contacts.Organizations.COMPANY,
88
109
                while( cur.moveToNext() ) {
89
110
                        Long id = cur.getLong( cur.getColumnIndex(
90
111
                                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 );
 
112
                        String organisation = cur.getString(
 
113
                                cur.getColumnIndex( Contacts.Organizations.COMPANY ) );
 
114
 
 
115
                        // if this is an organisation name for a contact for whom we have
 
116
                        // not added a lookup, add a lookup for the contact id by
 
117
                        // organisation
 
118
                        if( unadded_ids.contains( id ) ) {
 
119
                                CacheIdentifier cache_identifier = CacheIdentifier.factory(
 
120
                                        CacheIdentifier.Type.ORGANISATION, organisation );
 
121
                                if( cache_identifier != null ) {
 
122
                                        cache.addLookup( cache_identifier, id );
102
123
                                        unadded_ids.remove( id );
103
124
                                }
104
 
 
105
 
                                // add associated data
106
 
                                cache.addAssociatedOrganisation( id, organisation );
107
125
                        }
 
126
 
 
127
                        // add associated data
 
128
                        cache.addAssociatedOrganisation( id, organisation );
108
129
                }
 
130
                cur.close();
109
131
 
110
132
                // get all phone numbers, primary ones first
111
 
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
 
133
                cur = _activity.getContentResolver().query(
 
134
                        Contacts.Phones.CONTENT_URI,
112
135
                        new String[] {
113
136
                                Contacts.Phones.PERSON_ID,
114
137
                                Contacts.Phones.NUMBER,
116
139
                while( cur.moveToNext() ) {
117
140
                        Long id = cur.getLong(
118
141
                                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 );
 
142
                        String number = cur.getString(
 
143
                                cur.getColumnIndex( Contacts.Phones.NUMBER ) );
 
144
 
 
145
                        // if this is a number for a contact for whom we have not
 
146
                        // added a lookup, add a lookup for the contact id by phone
 
147
                        // number
 
148
                        if( unadded_ids.contains( id ) ) {
 
149
                                CacheIdentifier cache_identifier = CacheIdentifier.factory(
 
150
                                        CacheIdentifier.Type.PRIMARY_NUMBER, number );
 
151
                                if( cache_identifier != null ) {
 
152
                                        cache.addLookup( cache_identifier, id );
130
153
                                        unadded_ids.remove( id );
131
154
                                }
132
 
 
133
 
                                // add associated data
134
 
                                cache.addAssociatedNumber( id, number );
135
155
                        }
 
156
 
 
157
                        // add associated data
 
158
                        cache.addAssociatedNumber( id, number );
136
159
                }
 
160
                cur.close();
137
161
 
138
162
                // now get all email addresses, primary ones first, and postal addresses
139
 
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
163
                cur = _activity.getContentResolver().query(
 
164
                        Contacts.ContactMethods.CONTENT_URI,
140
165
                        new String[] {
141
166
                                Contacts.ContactMethods.PERSON_ID,
142
167
                                Contacts.ContactMethods.DATA,
153
178
                                cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
154
179
                        if( kind == Contacts.KIND_EMAIL )
155
180
                        {
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 );
 
181
                                String email = cur.getString(
 
182
                                        cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
 
183
 
 
184
                                // if this is an email address for a contact for whom we have
 
185
                                // not added a lookup, add a lookup for the contact id by email
 
186
                                // address
 
187
                                if( unadded_ids.contains( id ) ) {
 
188
                                        CacheIdentifier cache_identifier = CacheIdentifier.factory(
 
189
                                                CacheIdentifier.Type.PRIMARY_EMAIL, email );
 
190
                                        if( cache_identifier != null ) {
 
191
                                                cache.addLookup( cache_identifier, id );
167
192
                                                unadded_ids.remove( id );
168
193
                                        }
 
194
                                }
169
195
 
170
 
                                        // add associated data
171
 
                                        cache.addAssociatedEmail( id, email );
172
 
                                }
 
196
                                // add associated data
 
197
                                cache.addAssociatedEmail( id, email );
173
198
                        }
174
199
                        else if( kind == Contacts.KIND_POSTAL )
175
200
                        {
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
 
                                }
 
201
                                String address = cur.getString(
 
202
                                        cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
 
203
 
 
204
                                // add associated data
 
205
                                cache.addAssociatedAddress( id, address );
184
206
                        }
185
207
                }
 
208
                cur.close();
 
209
 
 
210
                // finally, add the notes that we stored earlier (we have to add these
 
211
                // at the end because we can't be sure which piece of contact data will
 
212
                // cause the contact to be added to the cache
 
213
                Iterator< Long > i = notes.keySet().iterator();
 
214
                while( i.hasNext() ) {
 
215
                        Long id = i.next();
 
216
                        cache.addAssociatedNote( id, notes.get( id ) );
 
217
                }
186
218
        }
187
219
 
188
220
        @Override
225
257
                        switch( type )
226
258
                        {
227
259
                        case ContactData.TYPE_HOME:
228
 
                                return Contacts.Phones.TYPE_HOME;
 
260
                                return Contacts.PhonesColumns.TYPE_HOME;
229
261
                        case ContactData.TYPE_WORK:
230
 
                                return Contacts.Phones.TYPE_WORK;
 
262
                                return Contacts.PhonesColumns.TYPE_WORK;
231
263
                        case ContactData.TYPE_MOBILE:
232
 
                                return Contacts.Phones.TYPE_MOBILE;
 
264
                                return Contacts.PhonesColumns.TYPE_MOBILE;
233
265
                        case ContactData.TYPE_FAX_HOME:
234
 
                                return Contacts.Phones.TYPE_FAX_HOME;
 
266
                                return Contacts.PhonesColumns.TYPE_FAX_HOME;
235
267
                        case ContactData.TYPE_FAX_WORK:
236
 
                                return Contacts.Phones.TYPE_FAX_WORK;
 
268
                                return Contacts.PhonesColumns.TYPE_FAX_WORK;
237
269
                        case ContactData.TYPE_PAGER:
238
 
                                return Contacts.Phones.TYPE_PAGER;
 
270
                                return Contacts.PhonesColumns.TYPE_PAGER;
239
271
                        }
240
272
                }
241
273
                else if( cls == Contacts.ContactMethods.class )
243
275
                        switch( type )
244
276
                        {
245
277
                        case ContactData.TYPE_HOME:
246
 
                                return Contacts.ContactMethods.TYPE_HOME;
 
278
                                return Contacts.ContactMethodsColumns.TYPE_HOME;
247
279
                        case ContactData.TYPE_WORK:
248
 
                                return Contacts.ContactMethods.TYPE_WORK;
 
280
                                return Contacts.ContactMethodsColumns.TYPE_WORK;
249
281
                        }
250
282
                }
251
283
 
328
360
                        Contacts.Organizations.CONTENT_URI, values );
329
361
        }
330
362
 
 
363
        @Override
 
364
        public void addContactNote( Long id, String note )
 
365
                throws ContactCreationException
 
366
        {
 
367
                ContentValues values = new ContentValues();
 
368
                values.put( Contacts.People.NOTES, note );
 
369
                _activity.getContentResolver().update(
 
370
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
 
371
                        values, null, null );
 
372
        }
331
373
 
 
374
        @Override
 
375
        public void addContactBirthday( Long id, String birthday )
 
376
                throws ContactCreationException
 
377
        {
 
378
                // this contacts API doesn't support birthdays, so just ignore them
 
379
        }
332
380
}