/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 11:50:41 UTC
  • Revision ID: tim@ed.am-20121219115041-b1e2x8ac2oqs2l7j
use pre-generated cach identifier

Show diffs side-by-side

added added

removed removed

 
1
/*
 
2
 * ContactsBackend.java
 
3
 *
 
4
 * Copyright (C) 2012 Tim Marston <tim@ed.am>
 
5
 *
 
6
 * This file is part of the Import Contacts program (hereafter referred
 
7
 * to as "this program"). For more information, see
 
8
 * http://ed.am/dev/android/import-contacts
 
9
 *
 
10
 * This program is free software: you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 */
 
23
 
 
24
package am.ed.importcontacts;
 
25
 
 
26
import java.util.HashSet;
 
27
 
 
28
import am.ed.importcontacts.ContactsCache.CacheIdentifier;
 
29
import am.ed.importcontacts.Importer.ContactData;
 
30
import android.app.Activity;
 
31
import android.content.ContentUris;
 
32
import android.content.ContentValues;
 
33
import android.database.Cursor;
 
34
import android.net.Uri;
 
35
import android.provider.Contacts;
 
36
 
 
37
public class ContactsBackend implements Backend
 
38
{
 
39
        private Activity _activity = null;
 
40
 
 
41
        ContactsBackend( Activity activity )
 
42
        {
 
43
                _activity = activity;
 
44
        }
 
45
 
 
46
        @Override
 
47
        public void populateCache( ContactsCache cache )
 
48
        {
 
49
                Cursor cur;
 
50
 
 
51
                // set of contact ids that we have not yet added
 
52
                HashSet< Long > unadded_ids = new HashSet< Long >();
 
53
 
 
54
                // get all contacts
 
55
                cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
 
56
                        new String[] {
 
57
                                Contacts.People._ID,
 
58
                                Contacts.People.NAME,
 
59
                        }, null, null, null );
 
60
                while( cur.moveToNext() ) {
 
61
                        Long id = cur.getLong(
 
62
                                cur.getColumnIndex( Contacts.People._ID ) );
 
63
                        String name =
 
64
                                ContactsCache.normaliseName( cur.getString(
 
65
                                        cur.getColumnIndex( Contacts.People.NAME ) ) );
 
66
                        if( name != null )
 
67
                        {
 
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
                        }
 
75
 
 
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 );
 
79
                }
 
80
 
 
81
                // get contact organisations, primary ones first
 
82
                cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
 
83
                        new String[] {
 
84
                                Contacts.Phones.PERSON_ID,
 
85
                                Contacts.Organizations.COMPANY,
 
86
                        }, null, null, Contacts.Organizations.ISPRIMARY + " DESC" );
 
87
                while( cur.moveToNext() ) {
 
88
                        Long id = cur.getLong( cur.getColumnIndex(
 
89
                                Contacts.Organizations.PERSON_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 );
 
101
                                        unadded_ids.remove( id );
 
102
                                }
 
103
 
 
104
                                // add associated data
 
105
                                cache.addAssociatedOrganisation( id, organisation );
 
106
                        }
 
107
                }
 
108
 
 
109
                // get all phone numbers, primary ones first
 
110
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
 
111
                        new String[] {
 
112
                                Contacts.Phones.PERSON_ID,
 
113
                                Contacts.Phones.NUMBER,
 
114
                        }, null, null, Contacts.Phones.ISPRIMARY + " DESC" );
 
115
                while( cur.moveToNext() ) {
 
116
                        Long id = cur.getLong(
 
117
                                cur.getColumnIndex( Contacts.Phones.PERSON_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 );
 
129
                                        unadded_ids.remove( id );
 
130
                                }
 
131
 
 
132
                                // add associated data
 
133
                                cache.addAssociatedNumber( id, number );
 
134
                        }
 
135
                }
 
136
 
 
137
                // now get all email addresses, primary ones first, and postal addresses
 
138
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
139
                        new String[] {
 
140
                                Contacts.ContactMethods.PERSON_ID,
 
141
                                Contacts.ContactMethods.DATA,
 
142
                                Contacts.ContactMethods.KIND,
 
143
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
 
144
                                "" + Contacts.KIND_EMAIL,
 
145
                                "" + Contacts.KIND_POSTAL,
 
146
                        }, Contacts.ContactMethods.ISPRIMARY + " DESC" );
 
147
                while( cur.moveToNext() ) {
 
148
                        Long id = cur.getLong(
 
149
                                cur.getColumnIndex( Contacts.ContactMethods.PERSON_ID ) );
 
150
                        int kind = cur.getInt(
 
151
                                cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
 
152
                        if( kind == Contacts.KIND_EMAIL )
 
153
                        {
 
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 );
 
165
                                                unadded_ids.remove( id );
 
166
                                        }
 
167
 
 
168
                                        // add associated data
 
169
                                        cache.addAssociatedEmail( id, email );
 
170
                                }
 
171
                        }
 
172
                        else if( kind == Contacts.KIND_POSTAL )
 
173
                        {
 
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
                                }
 
182
                        }
 
183
                }
 
184
        }
 
185
 
 
186
        @Override
 
187
        public void deleteContact( Long id )
 
188
        {
 
189
                Uri contact_uri =
 
190
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id );
 
191
                _activity.getContentResolver().delete( contact_uri, null, null );
 
192
        }
 
193
 
 
194
        @Override
 
195
        public Long addContact( String name )
 
196
        {
 
197
                ContentValues values = new ContentValues();
 
198
                values.put( Contacts.People.NAME, name );
 
199
                Uri contact_uri = _activity.getContentResolver().insert(
 
200
                        Contacts.People.CONTENT_URI, values );
 
201
                Long id = ContentUris.parseId( contact_uri );
 
202
                if( id == 0 ) id = null;
 
203
 
 
204
                // 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
                        }
 
213
                }
 
214
 
 
215
                return id;
 
216
        }
 
217
 
 
218
        @Override
 
219
        public void addContactPhone( Long id, String number,
 
220
                ContactData.PreferredDetail data )
 
221
        {
 
222
                Uri contact_phones_uri = Uri.withAppendedPath(
 
223
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
 
224
                        Contacts.People.Phones.CONTENT_DIRECTORY );
 
225
 
 
226
                ContentValues values = new ContentValues();
 
227
                values.put( Contacts.Phones.TYPE, data.getType() );
 
228
                values.put( Contacts.Phones.NUMBER, number );
 
229
                if( data.isPreferred() )
 
230
                        values.put( Contacts.Phones.ISPRIMARY, 1 );
 
231
 
 
232
                _activity.getContentResolver().insert( contact_phones_uri, values );
 
233
        }
 
234
 
 
235
        @Override
 
236
        public void addContactEmail( Long id, String email,
 
237
                ContactData.PreferredDetail data )
 
238
        {
 
239
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
 
240
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
 
241
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
242
 
 
243
                ContentValues values = new ContentValues();
 
244
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
 
245
                values.put( Contacts.ContactMethods.DATA, email );
 
246
                values.put( Contacts.ContactMethods.TYPE, data.getType() );
 
247
                if( data.isPreferred() )
 
248
                        values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
 
249
 
 
250
                _activity.getContentResolver().insert( contact_contact_methods_uri,
 
251
                        values );
 
252
        }
 
253
 
 
254
        @Override
 
255
        public void addContactAddresses( Long id, String address,
 
256
                ContactData.TypeDetail data )
 
257
        {
 
258
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
 
259
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
 
260
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
261
 
 
262
                ContentValues values = new ContentValues();
 
263
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
 
264
                values.put( Contacts.ContactMethods.DATA, address );
 
265
                values.put( Contacts.ContactMethods.TYPE, data.getType() );
 
266
 
 
267
                _activity.getContentResolver().insert( contact_contact_methods_uri,
 
268
                        values );
 
269
        }
 
270
 
 
271
        @Override
 
272
        public void addContactOrganisation( Long id, String organisation,
 
273
                ContactData.ExtraDetail data )
 
274
        {
 
275
                ContentValues values = new ContentValues();
 
276
                values.put( Contacts.Organizations.PERSON_ID, id );
 
277
                values.put( Contacts.Organizations.COMPANY, organisation );
 
278
                values.put( Contacts.ContactMethods.TYPE,
 
279
                        Contacts.OrganizationColumns.TYPE_WORK );
 
280
                if( data.getExtra() != null )
 
281
                        values.put( Contacts.Organizations.TITLE, data.getExtra() );
 
282
 
 
283
                _activity.getContentResolver().insert(
 
284
                        Contacts.Organizations.CONTENT_URI, values );
 
285
        }
 
286
 
 
287
 
 
288
}