/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-19 17:51:35 UTC
  • Revision ID: tim@ed.am-20121219175135-1cpuafp76jg1ib1p
added preliminary (buggy) ContactsContract backend

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsBackend.java
3
3
 *
4
 
 * Copyright (C) 2012 to 2013 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2012 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;
27
26
import java.util.HashSet;
28
 
import java.util.Iterator;
29
27
 
30
28
import am.ed.importcontacts.ContactsCache.CacheIdentifier;
31
29
import am.ed.importcontacts.Importer.ContactData;
54
52
                // set of contact ids that we have not yet added
55
53
                HashSet< Long > unadded_ids = new HashSet< Long >();
56
54
 
57
 
                // notes
58
 
                HashMap< Long, String > notes = new HashMap< Long, String >();
59
 
 
60
55
                // get all contacts
61
 
                cur = _activity.getContentResolver().query(
62
 
                        Contacts.People.CONTENT_URI,
 
56
                cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
63
57
                        new String[] {
64
58
                                Contacts.People._ID,
65
59
                                Contacts.People.NAME,
66
 
                                Contacts.People.NOTES,
67
60
                        }, null, null, null );
68
61
                while( cur.moveToNext() ) {
69
62
                        Long id = cur.getLong(
70
63
                                cur.getColumnIndex( Contacts.People._ID ) );
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
 
64
                        String name =
 
65
                                ContactsCache.normaliseName( cur.getString(
 
66
                                        cur.getColumnIndex( Contacts.People.NAME ) ) );
 
67
                        if( name != null )
88
68
                        {
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 );
 
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
                                }
 
75
                        }
92
76
 
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 );
98
 
                        }
 
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 );
99
80
                }
100
 
                cur.close();
101
81
 
102
82
                // get contact organisations, primary ones first
103
 
                cur = _activity.getContentResolver().query(
104
 
                        Contacts.Organizations.CONTENT_URI,
 
83
                cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
105
84
                        new String[] {
106
85
                                Contacts.Phones.PERSON_ID,
107
86
                                Contacts.Organizations.COMPANY,
109
88
                while( cur.moveToNext() ) {
110
89
                        Long id = cur.getLong( cur.getColumnIndex(
111
90
                                Contacts.Organizations.PERSON_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 );
 
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 );
123
102
                                        unadded_ids.remove( id );
124
103
                                }
 
104
 
 
105
                                // add associated data
 
106
                                cache.addAssociatedOrganisation( id, organisation );
125
107
                        }
126
 
 
127
 
                        // add associated data
128
 
                        cache.addAssociatedOrganisation( id, organisation );
129
108
                }
130
 
                cur.close();
131
109
 
132
110
                // get all phone numbers, primary ones first
133
 
                cur = _activity.getContentResolver().query(
134
 
                        Contacts.Phones.CONTENT_URI,
 
111
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
135
112
                        new String[] {
136
113
                                Contacts.Phones.PERSON_ID,
137
114
                                Contacts.Phones.NUMBER,
139
116
                while( cur.moveToNext() ) {
140
117
                        Long id = cur.getLong(
141
118
                                cur.getColumnIndex( Contacts.Phones.PERSON_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 );
 
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 );
153
130
                                        unadded_ids.remove( id );
154
131
                                }
 
132
 
 
133
                                // add associated data
 
134
                                cache.addAssociatedNumber( id, number );
155
135
                        }
156
 
 
157
 
                        // add associated data
158
 
                        cache.addAssociatedNumber( id, number );
159
136
                }
160
 
                cur.close();
161
137
 
162
138
                // now get all email addresses, primary ones first, and postal addresses
163
 
                cur = _activity.getContentResolver().query(
164
 
                        Contacts.ContactMethods.CONTENT_URI,
 
139
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
165
140
                        new String[] {
166
141
                                Contacts.ContactMethods.PERSON_ID,
167
142
                                Contacts.ContactMethods.DATA,
178
153
                                cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
179
154
                        if( kind == Contacts.KIND_EMAIL )
180
155
                        {
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 );
 
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 );
192
167
                                                unadded_ids.remove( id );
193
168
                                        }
 
169
 
 
170
                                        // add associated data
 
171
                                        cache.addAssociatedEmail( id, email );
194
172
                                }
195
 
 
196
 
                                // add associated data
197
 
                                cache.addAssociatedEmail( id, email );
198
173
                        }
199
174
                        else if( kind == Contacts.KIND_POSTAL )
200
175
                        {
201
 
                                String address = cur.getString(
202
 
                                        cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
203
 
 
204
 
                                // add associated data
205
 
                                cache.addAssociatedAddress( id, address );
 
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
                                }
206
184
                        }
207
185
                }
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
 
                }
218
186
        }
219
187
 
220
188
        @Override
257
225
                        switch( type )
258
226
                        {
259
227
                        case ContactData.TYPE_HOME:
260
 
                                return Contacts.PhonesColumns.TYPE_HOME;
 
228
                                return Contacts.Phones.TYPE_HOME;
261
229
                        case ContactData.TYPE_WORK:
262
 
                                return Contacts.PhonesColumns.TYPE_WORK;
 
230
                                return Contacts.Phones.TYPE_WORK;
263
231
                        case ContactData.TYPE_MOBILE:
264
 
                                return Contacts.PhonesColumns.TYPE_MOBILE;
 
232
                                return Contacts.Phones.TYPE_MOBILE;
265
233
                        case ContactData.TYPE_FAX_HOME:
266
 
                                return Contacts.PhonesColumns.TYPE_FAX_HOME;
 
234
                                return Contacts.Phones.TYPE_FAX_HOME;
267
235
                        case ContactData.TYPE_FAX_WORK:
268
 
                                return Contacts.PhonesColumns.TYPE_FAX_WORK;
 
236
                                return Contacts.Phones.TYPE_FAX_WORK;
269
237
                        case ContactData.TYPE_PAGER:
270
 
                                return Contacts.PhonesColumns.TYPE_PAGER;
 
238
                                return Contacts.Phones.TYPE_PAGER;
271
239
                        }
272
240
                }
273
241
                else if( cls == Contacts.ContactMethods.class )
275
243
                        switch( type )
276
244
                        {
277
245
                        case ContactData.TYPE_HOME:
278
 
                                return Contacts.ContactMethodsColumns.TYPE_HOME;
 
246
                                return Contacts.ContactMethods.TYPE_HOME;
279
247
                        case ContactData.TYPE_WORK:
280
 
                                return Contacts.ContactMethodsColumns.TYPE_WORK;
 
248
                                return Contacts.ContactMethods.TYPE_WORK;
281
249
                        }
282
250
                }
283
251
 
360
328
                        Contacts.Organizations.CONTENT_URI, values );
361
329
        }
362
330
 
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
 
        }
373
331
 
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
 
        }
380
332
}