/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-18 13:00:43 UTC
  • Revision ID: tim@ed.am-20121218130043-4zhj1cdeaqko3c4b
cleanup; fixed some typos; updated TODO

Show diffs side-by-side

added added

removed removed

34
34
import android.net.Uri;
35
35
import android.provider.Contacts;
36
36
 
37
 
 
38
37
public class ContactsBackend implements Backend
39
38
{
40
39
        private Activity _activity = null;
50
49
                Cursor cur;
51
50
 
52
51
                // set of contact ids that we have not yet added
53
 
                HashSet< Long > unadded = new HashSet< Long >();
 
52
                HashSet< Long > unadded_ids = new HashSet< Long >();
54
53
 
55
54
                // get all contacts
56
55
                cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
76
75
 
77
76
                        // record that a lookup for this contact's id still needs to be
78
77
                        // added by some other means
79
 
                        unadded.add( id );
 
78
                        unadded_ids.add( id );
80
79
                }
81
80
 
82
81
                // get contact organisations, primary ones first
96
95
                                // if this is an organisation name for a contact for whom we
97
96
                                // have not added a lookup, add a lookup for the contact id
98
97
                                // by organisation
99
 
                                if( unadded.contains( id ) ) {
 
98
                                if( unadded_ids.contains( id ) ) {
100
99
                                        cache.addLookup( new CacheIdentifier(
101
100
                                                CacheIdentifier.Type.ORGANISATION, organisation ), id );
102
 
                                        unadded.remove( id );
 
101
                                        unadded_ids.remove( id );
103
102
                                }
104
103
 
105
104
                                // add associated data
124
123
                                // if this is a number for a contact for whom we have not
125
124
                                // added a lookup, add a lookup for the contact id by phone
126
125
                                // number
127
 
                                if( unadded.contains( id ) ) {
 
126
                                if( unadded_ids.contains( id ) ) {
128
127
                                        cache.addLookup( new CacheIdentifier(
129
128
                                                CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
130
 
                                        unadded.remove( id );
 
129
                                        unadded_ids.remove( id );
131
130
                                }
132
131
 
133
132
                                // add associated data
160
159
                                        // if this is an email address for a contact for whom we
161
160
                                        // have not added a lookup, add a lookup for the contact
162
161
                                        // id by email address
163
 
                                        if( unadded.contains( id ) ) {
 
162
                                        if( unadded_ids.contains( id ) ) {
164
163
                                                cache.addLookup( new CacheIdentifier(
165
164
                                                        CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
166
 
                                                unadded.remove( id );
 
165
                                                unadded_ids.remove( id );
167
166
                                        }
168
167
 
169
168
                                        // add associated data
200
199
                Uri contact_uri = _activity.getContentResolver().insert(
201
200
                        Contacts.People.CONTENT_URI, values );
202
201
                Long id = ContentUris.parseId( contact_uri );
 
202
                if( id == 0 ) id = null;
203
203
 
204
204
                // try to add them to the "My Contacts" group
205
 
                if( id != null && id > 0 ) {
 
205
                if( id != null ) {
206
206
                        try {
207
207
                                Contacts.People.addToMyContactsGroup(
208
208
                                        _activity.getContentResolver(), id );
212
212
                        }
213
213
                }
214
214
 
215
 
                return id == null? 0 : id;
 
215
                return id;
216
216
        }
217
217
 
218
218
        @Override