/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-05-08 15:09:13 UTC
  • Revision ID: tim@ed.am-20120508150913-t9qzbbs1kld691dy
abstracted the android contacts API in to an interface, ready to be switched to
another depending on the platform version.

Show diffs side-by-side

added added

removed removed

34
34
import android.net.Uri;
35
35
import android.provider.Contacts;
36
36
 
37
 
@SuppressWarnings( "deprecation" )
 
37
 
38
38
public class ContactsBackend implements Backend
39
39
{
40
40
        private Activity _activity = null;
50
50
                Cursor cur;
51
51
 
52
52
                // set of contact ids that we have not yet added
53
 
                HashSet< Long > unadded_ids = new HashSet< Long >();
 
53
                HashSet< Long > unadded = new HashSet< Long >();
54
54
 
55
55
                // get all contacts
56
56
                cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
76
76
 
77
77
                        // record that a lookup for this contact's id still needs to be
78
78
                        // added by some other means
79
 
                        unadded_ids.add( id );
 
79
                        unadded.add( id );
80
80
                }
81
81
 
82
82
                // get contact organisations, primary ones first
96
96
                                // if this is an organisation name for a contact for whom we
97
97
                                // have not added a lookup, add a lookup for the contact id
98
98
                                // by organisation
99
 
                                if( unadded_ids.contains( id ) ) {
 
99
                                if( unadded.contains( id ) ) {
100
100
                                        cache.addLookup( new CacheIdentifier(
101
101
                                                CacheIdentifier.Type.ORGANISATION, organisation ), id );
102
 
                                        unadded_ids.remove( id );
 
102
                                        unadded.remove( id );
103
103
                                }
104
104
 
105
105
                                // add associated data
124
124
                                // if this is a number for a contact for whom we have not
125
125
                                // added a lookup, add a lookup for the contact id by phone
126
126
                                // number
127
 
                                if( unadded_ids.contains( id ) ) {
 
127
                                if( unadded.contains( id ) ) {
128
128
                                        cache.addLookup( new CacheIdentifier(
129
129
                                                CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
130
 
                                        unadded_ids.remove( id );
 
130
                                        unadded.remove( id );
131
131
                                }
132
132
 
133
133
                                // add associated data
141
141
                                Contacts.ContactMethods.PERSON_ID,
142
142
                                Contacts.ContactMethods.DATA,
143
143
                                Contacts.ContactMethods.KIND,
144
 
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )",
145
 
                        new String[] {
 
144
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
146
145
                                "" + Contacts.KIND_EMAIL,
147
146
                                "" + Contacts.KIND_POSTAL,
148
147
                        }, Contacts.ContactMethods.ISPRIMARY + " DESC" );
161
160
                                        // if this is an email address for a contact for whom we
162
161
                                        // have not added a lookup, add a lookup for the contact
163
162
                                        // id by email address
164
 
                                        if( unadded_ids.contains( id ) ) {
 
163
                                        if( unadded.contains( id ) ) {
165
164
                                                cache.addLookup( new CacheIdentifier(
166
165
                                                        CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
167
 
                                                unadded_ids.remove( id );
 
166
                                                unadded.remove( id );
168
167
                                        }
169
168
 
170
169
                                        // add associated data
194
193
        }
195
194
 
196
195
        @Override
197
 
        public Long addContact( String name ) throws ContactCreationException
 
196
        public Long addContact( String name )
198
197
        {
199
198
                ContentValues values = new ContentValues();
200
 
                if( name != null )
201
 
                        values.put( Contacts.People.NAME, name );
 
199
                values.put( Contacts.People.NAME, name );
202
200
                Uri contact_uri = _activity.getContentResolver().insert(
203
201
                        Contacts.People.CONTENT_URI, values );
204
202
                Long id = ContentUris.parseId( contact_uri );
205
 
                if( id == 0 )
206
 
                        throw new ContactCreationException();
207
203
 
208
204
                // try to add them to the "My Contacts" group
209
 
                try {
210
 
                        Contacts.People.addToMyContactsGroup(
211
 
                                _activity.getContentResolver(), id );
212
 
                }
213
 
                catch( IllegalStateException e ) {
214
 
                        // ignore any failure
215
 
                }
216
 
 
217
 
                return id;
218
 
        }
219
 
 
220
 
        private int convertTypeToBackendType( Class< ? > cls, int type )
221
 
                throws ContactCreationException
222
 
        {
223
 
                if( cls == Contacts.Phones.class )
224
 
                {
225
 
                        switch( type )
226
 
                        {
227
 
                        case ContactData.TYPE_HOME:
228
 
                                return Contacts.Phones.TYPE_HOME;
229
 
                        case ContactData.TYPE_WORK:
230
 
                                return Contacts.Phones.TYPE_WORK;
231
 
                        case ContactData.TYPE_MOBILE:
232
 
                                return Contacts.Phones.TYPE_MOBILE;
233
 
                        case ContactData.TYPE_FAX_HOME:
234
 
                                return Contacts.Phones.TYPE_FAX_HOME;
235
 
                        case ContactData.TYPE_FAX_WORK:
236
 
                                return Contacts.Phones.TYPE_FAX_WORK;
237
 
                        case ContactData.TYPE_PAGER:
238
 
                                return Contacts.Phones.TYPE_PAGER;
239
 
                        }
240
 
                }
241
 
                else if( cls == Contacts.ContactMethods.class )
242
 
                {
243
 
                        switch( type )
244
 
                        {
245
 
                        case ContactData.TYPE_HOME:
246
 
                                return Contacts.ContactMethods.TYPE_HOME;
247
 
                        case ContactData.TYPE_WORK:
248
 
                                return Contacts.ContactMethods.TYPE_WORK;
249
 
                        }
250
 
                }
251
 
 
252
 
                // still here?
253
 
                throw new ContactCreationException();
 
205
                if( id != null && id > 0 ) {
 
206
                        try {
 
207
                                Contacts.People.addToMyContactsGroup(
 
208
                                        _activity.getContentResolver(), id );
 
209
                        }
 
210
                        catch( IllegalStateException e ) {
 
211
                                // ignore any failure
 
212
                        }
 
213
                }
 
214
 
 
215
                return id == null? 0 : id;
254
216
        }
255
217
 
256
218
        @Override
257
219
        public void addContactPhone( Long id, String number,
258
 
                ContactData.PreferredDetail data ) throws ContactCreationException
 
220
                ContactData.PreferredDetail data )
259
221
        {
260
222
                Uri contact_phones_uri = Uri.withAppendedPath(
261
223
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
262
224
                        Contacts.People.Phones.CONTENT_DIRECTORY );
263
225
 
264
226
                ContentValues values = new ContentValues();
265
 
                values.put( Contacts.Phones.TYPE,
266
 
                        convertTypeToBackendType( Contacts.Phones.class,
267
 
                                data.getType() ) );
 
227
                values.put( Contacts.Phones.TYPE, data.getType() );
268
228
                values.put( Contacts.Phones.NUMBER, number );
269
229
                if( data.isPreferred() )
270
230
                        values.put( Contacts.Phones.ISPRIMARY, 1 );
274
234
 
275
235
        @Override
276
236
        public void addContactEmail( Long id, String email,
277
 
                ContactData.PreferredDetail data ) throws ContactCreationException
 
237
                ContactData.PreferredDetail data )
278
238
        {
279
239
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
280
240
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
283
243
                ContentValues values = new ContentValues();
284
244
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
285
245
                values.put( Contacts.ContactMethods.DATA, email );
286
 
                values.put( Contacts.ContactMethods.TYPE,
287
 
                        convertTypeToBackendType( Contacts.ContactMethods.class,
288
 
                                data.getType() ) );
 
246
                values.put( Contacts.ContactMethods.TYPE, data.getType() );
289
247
                if( data.isPreferred() )
290
248
                        values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
291
249
 
295
253
 
296
254
        @Override
297
255
        public void addContactAddresses( Long id, String address,
298
 
                ContactData.TypeDetail data ) throws ContactCreationException
 
256
                ContactData.TypeDetail data )
299
257
        {
300
258
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
301
259
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
304
262
                ContentValues values = new ContentValues();
305
263
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
306
264
                values.put( Contacts.ContactMethods.DATA, address );
307
 
                values.put( Contacts.ContactMethods.TYPE,
308
 
                        convertTypeToBackendType( Contacts.ContactMethods.class,
309
 
                                data.getType() ) );
 
265
                values.put( Contacts.ContactMethods.TYPE, data.getType() );
310
266
 
311
267
                _activity.getContentResolver().insert( contact_contact_methods_uri,
312
268
                        values );
314
270
 
315
271
        @Override
316
272
        public void addContactOrganisation( Long id, String organisation,
317
 
                ContactData.ExtraDetail data ) throws ContactCreationException
 
273
                ContactData.ExtraDetail data )
318
274
        {
319
275
                ContentValues values = new ContentValues();
320
276
                values.put( Contacts.Organizations.PERSON_ID, id );