/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-21 15:50:09 UTC
  • Revision ID: tim@ed.am-20121221155009-exbirzrfx4p95ipj
fixed some column valuesa dn a broken check to contatinate name parts with spaces

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;
34
36
import android.net.Uri;
35
37
import android.provider.Contacts;
36
38
 
 
39
@SuppressWarnings( "deprecation" )
37
40
public class ContactsBackend implements Backend
38
41
{
39
42
        private Activity _activity = null;
51
54
                // set of contact ids that we have not yet added
52
55
                HashSet< Long > unadded_ids = new HashSet< Long >();
53
56
 
 
57
                // notes
 
58
                HashMap< Long, String > notes = new HashMap< Long, String >();
 
59
 
54
60
                // get all contacts
55
61
                cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
56
62
                        new String[] {
57
63
                                Contacts.People._ID,
58
64
                                Contacts.People.NAME,
 
65
                                Contacts.People.NOTES,
59
66
                        }, null, null, null );
60
67
                while( cur.moveToNext() ) {
61
68
                        Long id = cur.getLong(
62
69
                                cur.getColumnIndex( Contacts.People._ID ) );
63
 
                        String name =
64
 
                                ContactsCache.normaliseName( cur.getString(
65
 
                                        cur.getColumnIndex( Contacts.People.NAME ) ) );
66
 
                        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
67
87
                        {
68
 
                                // if we can, add a lookup for the contact id by name
69
 
                                if( name.length() > 0 ) {
70
 
                                        cache.addLookup( new CacheIdentifier(
71
 
                                                CacheIdentifier.Type.NAME, name ), id );
72
 
                                        continue;
73
 
                                }
 
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 );
74
97
                        }
75
 
 
76
 
                        // record that a lookup for this contact's id still needs to be
77
 
                        // added by some other means
78
 
                        unadded_ids.add( id );
79
98
                }
80
99
 
81
100
                // get contact organisations, primary ones first
87
106
                while( cur.moveToNext() ) {
88
107
                        Long id = cur.getLong( cur.getColumnIndex(
89
108
                                Contacts.Organizations.PERSON_ID ) );
90
 
                        String organisation =
91
 
                                ContactsCache.normaliseOrganisation( cur.getString(
92
 
                                        cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
93
 
                        if( organisation != null )
94
 
                        {
95
 
                                // if this is an organisation name for a contact for whom we
96
 
                                // have not added a lookup, add a lookup for the contact id
97
 
                                // by organisation
98
 
                                if( unadded_ids.contains( id ) ) {
99
 
                                        cache.addLookup( new CacheIdentifier(
100
 
                                                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 );
101
120
                                        unadded_ids.remove( id );
102
121
                                }
103
 
 
104
 
                                // add associated data
105
 
                                cache.addAssociatedOrganisation( id, organisation );
106
122
                        }
 
123
 
 
124
                        // add associated data
 
125
                        cache.addAssociatedOrganisation( id, organisation );
107
126
                }
108
127
 
109
128
                // get all phone numbers, primary ones first
115
134
                while( cur.moveToNext() ) {
116
135
                        Long id = cur.getLong(
117
136
                                cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
118
 
                        String number =
119
 
                                ContactsCache.normalisePhoneNumber( cur.getString(
120
 
                                        cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
121
 
                        if( number != null )
122
 
                        {
123
 
                                // if this is a number for a contact for whom we have not
124
 
                                // added a lookup, add a lookup for the contact id by phone
125
 
                                // number
126
 
                                if( unadded_ids.contains( id ) ) {
127
 
                                        cache.addLookup( new CacheIdentifier(
128
 
                                                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 );
129
148
                                        unadded_ids.remove( id );
130
149
                                }
131
 
 
132
 
                                // add associated data
133
 
                                cache.addAssociatedNumber( id, number );
134
150
                        }
 
151
 
 
152
                        // add associated data
 
153
                        cache.addAssociatedNumber( id, number );
135
154
                }
136
155
 
137
156
                // now get all email addresses, primary ones first, and postal addresses
140
159
                                Contacts.ContactMethods.PERSON_ID,
141
160
                                Contacts.ContactMethods.DATA,
142
161
                                Contacts.ContactMethods.KIND,
143
 
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
 
162
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )",
 
163
                        new String[] {
144
164
                                "" + Contacts.KIND_EMAIL,
145
165
                                "" + Contacts.KIND_POSTAL,
146
166
                        }, Contacts.ContactMethods.ISPRIMARY + " DESC" );
151
171
                                cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
152
172
                        if( kind == Contacts.KIND_EMAIL )
153
173
                        {
154
 
                                String email =
155
 
                                        ContactsCache.normaliseEmailAddress( cur.getString(
156
 
                                                cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
157
 
                                if( email != null )
158
 
                                {
159
 
                                        // if this is an email address for a contact for whom we
160
 
                                        // have not added a lookup, add a lookup for the contact
161
 
                                        // id by email address
162
 
                                        if( unadded_ids.contains( id ) ) {
163
 
                                                cache.addLookup( new CacheIdentifier(
164
 
                                                        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 );
165
185
                                                unadded_ids.remove( id );
166
186
                                        }
 
187
                                }
167
188
 
168
 
                                        // add associated data
169
 
                                        cache.addAssociatedEmail( id, email );
170
 
                                }
 
189
                                // add associated data
 
190
                                cache.addAssociatedEmail( id, email );
171
191
                        }
172
192
                        else if( kind == Contacts.KIND_POSTAL )
173
193
                        {
174
 
                                String address =
175
 
                                        ContactsCache.normaliseAddress( cur.getString(
176
 
                                                cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
177
 
                                if( address != null )
178
 
                                {
179
 
                                        // add associated data
180
 
                                        cache.addAssociatedAddress( id, address );
181
 
                                }
 
194
                                String address = cur.getString(
 
195
                                        cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
 
196
 
 
197
                                // add associated data
 
198
                                cache.addAssociatedAddress( id, address );
182
199
                        }
183
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
                }
184
210
        }
185
211
 
186
212
        @Override
192
218
        }
193
219
 
194
220
        @Override
195
 
        public Long addContact( String name )
 
221
        public Long addContact( String name ) throws ContactCreationException
196
222
        {
197
223
                ContentValues values = new ContentValues();
198
 
                values.put( Contacts.People.NAME, name );
 
224
                if( name != null )
 
225
                        values.put( Contacts.People.NAME, name );
199
226
                Uri contact_uri = _activity.getContentResolver().insert(
200
227
                        Contacts.People.CONTENT_URI, values );
201
228
                Long id = ContentUris.parseId( contact_uri );
202
 
                if( id == 0 ) id = null;
 
229
                if( id == 0 )
 
230
                        throw new ContactCreationException();
203
231
 
204
232
                // try to add them to the "My Contacts" group
205
 
                if( id != null ) {
206
 
                        try {
207
 
                                Contacts.People.addToMyContactsGroup(
208
 
                                        _activity.getContentResolver(), id );
209
 
                        }
210
 
                        catch( IllegalStateException e ) {
211
 
                                // ignore any failure
212
 
                        }
 
233
                try {
 
234
                        Contacts.People.addToMyContactsGroup(
 
235
                                _activity.getContentResolver(), id );
 
236
                }
 
237
                catch( IllegalStateException e ) {
 
238
                        // ignore any failure
213
239
                }
214
240
 
215
241
                return id;
216
242
        }
217
243
 
 
244
        private int convertTypeToBackendType( Class< ? > cls, int type )
 
245
                throws ContactCreationException
 
246
        {
 
247
                if( cls == Contacts.Phones.class )
 
248
                {
 
249
                        switch( type )
 
250
                        {
 
251
                        case ContactData.TYPE_HOME:
 
252
                                return Contacts.PhonesColumns.TYPE_HOME;
 
253
                        case ContactData.TYPE_WORK:
 
254
                                return Contacts.PhonesColumns.TYPE_WORK;
 
255
                        case ContactData.TYPE_MOBILE:
 
256
                                return Contacts.PhonesColumns.TYPE_MOBILE;
 
257
                        case ContactData.TYPE_FAX_HOME:
 
258
                                return Contacts.PhonesColumns.TYPE_FAX_HOME;
 
259
                        case ContactData.TYPE_FAX_WORK:
 
260
                                return Contacts.PhonesColumns.TYPE_FAX_WORK;
 
261
                        case ContactData.TYPE_PAGER:
 
262
                                return Contacts.PhonesColumns.TYPE_PAGER;
 
263
                        }
 
264
                }
 
265
                else if( cls == Contacts.ContactMethods.class )
 
266
                {
 
267
                        switch( type )
 
268
                        {
 
269
                        case ContactData.TYPE_HOME:
 
270
                                return Contacts.ContactMethodsColumns.TYPE_HOME;
 
271
                        case ContactData.TYPE_WORK:
 
272
                                return Contacts.ContactMethodsColumns.TYPE_WORK;
 
273
                        }
 
274
                }
 
275
 
 
276
                // still here?
 
277
                throw new ContactCreationException();
 
278
        }
 
279
 
218
280
        @Override
219
281
        public void addContactPhone( Long id, String number,
220
 
                ContactData.PreferredDetail data )
 
282
                ContactData.PreferredDetail data ) throws ContactCreationException
221
283
        {
222
284
                Uri contact_phones_uri = Uri.withAppendedPath(
223
285
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
224
286
                        Contacts.People.Phones.CONTENT_DIRECTORY );
225
287
 
226
288
                ContentValues values = new ContentValues();
227
 
                values.put( Contacts.Phones.TYPE, data.getType() );
 
289
                values.put( Contacts.Phones.TYPE,
 
290
                        convertTypeToBackendType( Contacts.Phones.class,
 
291
                                data.getType() ) );
228
292
                values.put( Contacts.Phones.NUMBER, number );
229
293
                if( data.isPreferred() )
230
294
                        values.put( Contacts.Phones.ISPRIMARY, 1 );
234
298
 
235
299
        @Override
236
300
        public void addContactEmail( Long id, String email,
237
 
                ContactData.PreferredDetail data )
 
301
                ContactData.PreferredDetail data ) throws ContactCreationException
238
302
        {
239
303
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
240
304
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
243
307
                ContentValues values = new ContentValues();
244
308
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
245
309
                values.put( Contacts.ContactMethods.DATA, email );
246
 
                values.put( Contacts.ContactMethods.TYPE, data.getType() );
 
310
                values.put( Contacts.ContactMethods.TYPE,
 
311
                        convertTypeToBackendType( Contacts.ContactMethods.class,
 
312
                                data.getType() ) );
247
313
                if( data.isPreferred() )
248
314
                        values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
249
315
 
253
319
 
254
320
        @Override
255
321
        public void addContactAddresses( Long id, String address,
256
 
                ContactData.TypeDetail data )
 
322
                ContactData.TypeDetail data ) throws ContactCreationException
257
323
        {
258
324
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
259
325
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
262
328
                ContentValues values = new ContentValues();
263
329
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
264
330
                values.put( Contacts.ContactMethods.DATA, address );
265
 
                values.put( Contacts.ContactMethods.TYPE, data.getType() );
 
331
                values.put( Contacts.ContactMethods.TYPE,
 
332
                        convertTypeToBackendType( Contacts.ContactMethods.class,
 
333
                                data.getType() ) );
266
334
 
267
335
                _activity.getContentResolver().insert( contact_contact_methods_uri,
268
336
                        values );
270
338
 
271
339
        @Override
272
340
        public void addContactOrganisation( Long id, String organisation,
273
 
                ContactData.ExtraDetail data )
 
341
                ContactData.ExtraDetail data ) throws ContactCreationException
274
342
        {
275
343
                ContentValues values = new ContentValues();
276
344
                values.put( Contacts.Organizations.PERSON_ID, id );
284
352
                        Contacts.Organizations.CONTENT_URI, values );
285
353
        }
286
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
        }
287
365
 
288
366
}