/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 11:58:46 UTC
  • Revision ID: tim@ed.am-20121218115846-xdwnw3zzcedbb3rp
bump version to 1.3 (we're skipping v1.2, for f-droid)

Show diffs side-by-side

added added

removed removed

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