/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:41:04 UTC
  • Revision ID: tim@ed.am-20121219174104-ly9xyjxdhqt0tu9b
ignore temporary files in eclipse project

Show diffs side-by-side

added added

removed removed

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