/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:50:33 UTC
  • Revision ID: tim@ed.am-20121219175033-61acsxzjulqpnian
made contacts backend throw exceptions when it can't create contacts on the device; specified Locale in lower/upper case conversions; stripped old Contacts types from Importer (and replaced with our own types, which are now converted in the backend; switched using Integer() constructors to Integer.valueOf(); rewrote the main importer routine a bit

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
import android.net.Uri;
35
35
import android.provider.Contacts;
36
36
 
 
37
@SuppressWarnings( "deprecation" )
37
38
public class ContactsBackend implements Backend
38
39
{
39
40
        private Activity _activity = null;
140
141
                                Contacts.ContactMethods.PERSON_ID,
141
142
                                Contacts.ContactMethods.DATA,
142
143
                                Contacts.ContactMethods.KIND,
143
 
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
 
144
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )",
 
145
                        new String[] {
144
146
                                "" + Contacts.KIND_EMAIL,
145
147
                                "" + Contacts.KIND_POSTAL,
146
148
                        }, Contacts.ContactMethods.ISPRIMARY + " DESC" );
192
194
        }
193
195
 
194
196
        @Override
195
 
        public Long addContact( String name )
 
197
        public Long addContact( String name ) throws ContactCreationException
196
198
        {
197
199
                ContentValues values = new ContentValues();
198
 
                values.put( Contacts.People.NAME, name );
 
200
                if( name != null )
 
201
                        values.put( Contacts.People.NAME, name );
199
202
                Uri contact_uri = _activity.getContentResolver().insert(
200
203
                        Contacts.People.CONTENT_URI, values );
201
204
                Long id = ContentUris.parseId( contact_uri );
202
 
                if( id == 0 ) id = null;
 
205
                if( id == 0 )
 
206
                        throw new ContactCreationException();
203
207
 
204
208
                // 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
 
                        }
 
209
                try {
 
210
                        Contacts.People.addToMyContactsGroup(
 
211
                                _activity.getContentResolver(), id );
 
212
                }
 
213
                catch( IllegalStateException e ) {
 
214
                        // ignore any failure
213
215
                }
214
216
 
215
217
                return id;
216
218
        }
217
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();
 
254
        }
 
255
 
218
256
        @Override
219
257
        public void addContactPhone( Long id, String number,
220
 
                ContactData.PreferredDetail data )
 
258
                ContactData.PreferredDetail data ) throws ContactCreationException
221
259
        {
222
260
                Uri contact_phones_uri = Uri.withAppendedPath(
223
261
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
224
262
                        Contacts.People.Phones.CONTENT_DIRECTORY );
225
263
 
226
264
                ContentValues values = new ContentValues();
227
 
                values.put( Contacts.Phones.TYPE, data.getType() );
 
265
                values.put( Contacts.Phones.TYPE,
 
266
                        convertTypeToBackendType( Contacts.Phones.class,
 
267
                                data.getType() ) );
228
268
                values.put( Contacts.Phones.NUMBER, number );
229
269
                if( data.isPreferred() )
230
270
                        values.put( Contacts.Phones.ISPRIMARY, 1 );
234
274
 
235
275
        @Override
236
276
        public void addContactEmail( Long id, String email,
237
 
                ContactData.PreferredDetail data )
 
277
                ContactData.PreferredDetail data ) throws ContactCreationException
238
278
        {
239
279
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
240
280
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
243
283
                ContentValues values = new ContentValues();
244
284
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
245
285
                values.put( Contacts.ContactMethods.DATA, email );
246
 
                values.put( Contacts.ContactMethods.TYPE, data.getType() );
 
286
                values.put( Contacts.ContactMethods.TYPE,
 
287
                        convertTypeToBackendType( Contacts.ContactMethods.class,
 
288
                                data.getType() ) );
247
289
                if( data.isPreferred() )
248
290
                        values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
249
291
 
253
295
 
254
296
        @Override
255
297
        public void addContactAddresses( Long id, String address,
256
 
                ContactData.TypeDetail data )
 
298
                ContactData.TypeDetail data ) throws ContactCreationException
257
299
        {
258
300
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
259
301
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
262
304
                ContentValues values = new ContentValues();
263
305
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
264
306
                values.put( Contacts.ContactMethods.DATA, address );
265
 
                values.put( Contacts.ContactMethods.TYPE, data.getType() );
 
307
                values.put( Contacts.ContactMethods.TYPE,
 
308
                        convertTypeToBackendType( Contacts.ContactMethods.class,
 
309
                                data.getType() ) );
266
310
 
267
311
                _activity.getContentResolver().insert( contact_contact_methods_uri,
268
312
                        values );
270
314
 
271
315
        @Override
272
316
        public void addContactOrganisation( Long id, String organisation,
273
 
                ContactData.ExtraDetail data )
 
317
                ContactData.ExtraDetail data ) throws ContactCreationException
274
318
        {
275
319
                ContentValues values = new ContentValues();
276
320
                values.put( Contacts.Organizations.PERSON_ID, id );