/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: 2009-01-13 06:35:26 UTC
  • Revision ID: edam@waxworlds.org-20090113063526-l9t1s9git4bav60a
- new contact's phone numebrs and email addresses are added to the caches after those contacts are updated to account for the situation where the same contact is imported again from another file (or the contact exists twice in the same file!?)

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
 
@SuppressWarnings( "deprecation" )
38
 
public class ContactsBackend implements Backend
39
 
{
40
 
        private Activity _activity = null;
41
 
 
42
 
        ContactsBackend( Activity activity )
43
 
        {
44
 
                _activity = activity;
45
 
        }
46
 
 
47
 
        @Override
48
 
        public void populateCache( ContactsCache cache )
49
 
        {
50
 
                Cursor cur;
51
 
 
52
 
                // set of contact ids that we have not yet added
53
 
                HashSet< Long > unadded_ids = new HashSet< Long >();
54
 
 
55
 
                // get all contacts
56
 
                cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
57
 
                        new String[] {
58
 
                                Contacts.People._ID,
59
 
                                Contacts.People.NAME,
60
 
                        }, null, null, null );
61
 
                while( cur.moveToNext() ) {
62
 
                        Long id = cur.getLong(
63
 
                                cur.getColumnIndex( Contacts.People._ID ) );
64
 
                        String name =
65
 
                                ContactsCache.normaliseName( cur.getString(
66
 
                                        cur.getColumnIndex( Contacts.People.NAME ) ) );
67
 
                        if( name != null )
68
 
                        {
69
 
                                // if we can, add a lookup for the contact id by name
70
 
                                if( name.length() > 0 ) {
71
 
                                        cache.addLookup( new CacheIdentifier(
72
 
                                                CacheIdentifier.Type.NAME, name ), id );
73
 
                                        continue;
74
 
                                }
75
 
                        }
76
 
 
77
 
                        // record that a lookup for this contact's id still needs to be
78
 
                        // added by some other means
79
 
                        unadded_ids.add( id );
80
 
                }
81
 
 
82
 
                // get contact organisations, primary ones first
83
 
                cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
84
 
                        new String[] {
85
 
                                Contacts.Phones.PERSON_ID,
86
 
                                Contacts.Organizations.COMPANY,
87
 
                        }, null, null, Contacts.Organizations.ISPRIMARY + " DESC" );
88
 
                while( cur.moveToNext() ) {
89
 
                        Long id = cur.getLong( cur.getColumnIndex(
90
 
                                Contacts.Organizations.PERSON_ID ) );
91
 
                        String organisation =
92
 
                                ContactsCache.normaliseOrganisation( cur.getString(
93
 
                                        cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
94
 
                        if( organisation != null )
95
 
                        {
96
 
                                // if this is an organisation name for a contact for whom we
97
 
                                // have not added a lookup, add a lookup for the contact id
98
 
                                // by organisation
99
 
                                if( unadded_ids.contains( id ) ) {
100
 
                                        cache.addLookup( new CacheIdentifier(
101
 
                                                CacheIdentifier.Type.ORGANISATION, organisation ), id );
102
 
                                        unadded_ids.remove( id );
103
 
                                }
104
 
 
105
 
                                // add associated data
106
 
                                cache.addAssociatedOrganisation( id, organisation );
107
 
                        }
108
 
                }
109
 
 
110
 
                // get all phone numbers, primary ones first
111
 
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
112
 
                        new String[] {
113
 
                                Contacts.Phones.PERSON_ID,
114
 
                                Contacts.Phones.NUMBER,
115
 
                        }, null, null, Contacts.Phones.ISPRIMARY + " DESC" );
116
 
                while( cur.moveToNext() ) {
117
 
                        Long id = cur.getLong(
118
 
                                cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
119
 
                        String number =
120
 
                                ContactsCache.normalisePhoneNumber( cur.getString(
121
 
                                        cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
122
 
                        if( number != null )
123
 
                        {
124
 
                                // if this is a number for a contact for whom we have not
125
 
                                // added a lookup, add a lookup for the contact id by phone
126
 
                                // number
127
 
                                if( unadded_ids.contains( id ) ) {
128
 
                                        cache.addLookup( new CacheIdentifier(
129
 
                                                CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
130
 
                                        unadded_ids.remove( id );
131
 
                                }
132
 
 
133
 
                                // add associated data
134
 
                                cache.addAssociatedNumber( id, number );
135
 
                        }
136
 
                }
137
 
 
138
 
                // now get all email addresses, primary ones first, and postal addresses
139
 
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
140
 
                        new String[] {
141
 
                                Contacts.ContactMethods.PERSON_ID,
142
 
                                Contacts.ContactMethods.DATA,
143
 
                                Contacts.ContactMethods.KIND,
144
 
                        }, Contacts.ContactMethods.KIND + " IN( ?, ? )",
145
 
                        new String[] {
146
 
                                "" + Contacts.KIND_EMAIL,
147
 
                                "" + Contacts.KIND_POSTAL,
148
 
                        }, Contacts.ContactMethods.ISPRIMARY + " DESC" );
149
 
                while( cur.moveToNext() ) {
150
 
                        Long id = cur.getLong(
151
 
                                cur.getColumnIndex( Contacts.ContactMethods.PERSON_ID ) );
152
 
                        int kind = cur.getInt(
153
 
                                cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
154
 
                        if( kind == Contacts.KIND_EMAIL )
155
 
                        {
156
 
                                String email =
157
 
                                        ContactsCache.normaliseEmailAddress( cur.getString(
158
 
                                                cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
159
 
                                if( email != null )
160
 
                                {
161
 
                                        // if this is an email address for a contact for whom we
162
 
                                        // have not added a lookup, add a lookup for the contact
163
 
                                        // id by email address
164
 
                                        if( unadded_ids.contains( id ) ) {
165
 
                                                cache.addLookup( new CacheIdentifier(
166
 
                                                        CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
167
 
                                                unadded_ids.remove( id );
168
 
                                        }
169
 
 
170
 
                                        // add associated data
171
 
                                        cache.addAssociatedEmail( id, email );
172
 
                                }
173
 
                        }
174
 
                        else if( kind == Contacts.KIND_POSTAL )
175
 
                        {
176
 
                                String address =
177
 
                                        ContactsCache.normaliseAddress( cur.getString(
178
 
                                                cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
179
 
                                if( address != null )
180
 
                                {
181
 
                                        // add associated data
182
 
                                        cache.addAssociatedAddress( id, address );
183
 
                                }
184
 
                        }
185
 
                }
186
 
        }
187
 
 
188
 
        @Override
189
 
        public void deleteContact( Long id )
190
 
        {
191
 
                Uri contact_uri =
192
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id );
193
 
                _activity.getContentResolver().delete( contact_uri, null, null );
194
 
        }
195
 
 
196
 
        @Override
197
 
        public Long addContact( String name ) throws ContactCreationException
198
 
        {
199
 
                ContentValues values = new ContentValues();
200
 
                if( name != null )
201
 
                        values.put( Contacts.People.NAME, name );
202
 
                Uri contact_uri = _activity.getContentResolver().insert(
203
 
                        Contacts.People.CONTENT_URI, values );
204
 
                Long id = ContentUris.parseId( contact_uri );
205
 
                if( id == 0 )
206
 
                        throw new ContactCreationException();
207
 
 
208
 
                // try to add them to the "My Contacts" group
209
 
                try {
210
 
                        Contacts.People.addToMyContactsGroup(
211
 
                                _activity.getContentResolver(), id );
212
 
                }
213
 
                catch( IllegalStateException e ) {
214
 
                        // ignore any failure
215
 
                }
216
 
 
217
 
                return id;
218
 
        }
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
 
 
256
 
        @Override
257
 
        public void addContactPhone( Long id, String number,
258
 
                ContactData.PreferredDetail data ) throws ContactCreationException
259
 
        {
260
 
                Uri contact_phones_uri = Uri.withAppendedPath(
261
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
262
 
                        Contacts.People.Phones.CONTENT_DIRECTORY );
263
 
 
264
 
                ContentValues values = new ContentValues();
265
 
                values.put( Contacts.Phones.TYPE,
266
 
                        convertTypeToBackendType( Contacts.Phones.class,
267
 
                                data.getType() ) );
268
 
                values.put( Contacts.Phones.NUMBER, number );
269
 
                if( data.isPreferred() )
270
 
                        values.put( Contacts.Phones.ISPRIMARY, 1 );
271
 
 
272
 
                _activity.getContentResolver().insert( contact_phones_uri, values );
273
 
        }
274
 
 
275
 
        @Override
276
 
        public void addContactEmail( Long id, String email,
277
 
                ContactData.PreferredDetail data ) throws ContactCreationException
278
 
        {
279
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
280
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
281
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
282
 
 
283
 
                ContentValues values = new ContentValues();
284
 
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
285
 
                values.put( Contacts.ContactMethods.DATA, email );
286
 
                values.put( Contacts.ContactMethods.TYPE,
287
 
                        convertTypeToBackendType( Contacts.ContactMethods.class,
288
 
                                data.getType() ) );
289
 
                if( data.isPreferred() )
290
 
                        values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
291
 
 
292
 
                _activity.getContentResolver().insert( contact_contact_methods_uri,
293
 
                        values );
294
 
        }
295
 
 
296
 
        @Override
297
 
        public void addContactAddresses( Long id, String address,
298
 
                ContactData.TypeDetail data ) throws ContactCreationException
299
 
        {
300
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
301
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
302
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
303
 
 
304
 
                ContentValues values = new ContentValues();
305
 
                values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
306
 
                values.put( Contacts.ContactMethods.DATA, address );
307
 
                values.put( Contacts.ContactMethods.TYPE,
308
 
                        convertTypeToBackendType( Contacts.ContactMethods.class,
309
 
                                data.getType() ) );
310
 
 
311
 
                _activity.getContentResolver().insert( contact_contact_methods_uri,
312
 
                        values );
313
 
        }
314
 
 
315
 
        @Override
316
 
        public void addContactOrganisation( Long id, String organisation,
317
 
                ContactData.ExtraDetail data ) throws ContactCreationException
318
 
        {
319
 
                ContentValues values = new ContentValues();
320
 
                values.put( Contacts.Organizations.PERSON_ID, id );
321
 
                values.put( Contacts.Organizations.COMPANY, organisation );
322
 
                values.put( Contacts.ContactMethods.TYPE,
323
 
                        Contacts.OrganizationColumns.TYPE_WORK );
324
 
                if( data.getExtra() != null )
325
 
                        values.put( Contacts.Organizations.TITLE, data.getExtra() );
326
 
 
327
 
                _activity.getContentResolver().insert(
328
 
                        Contacts.Organizations.CONTENT_URI, values );
329
 
        }
330
 
 
331
 
 
332
 
}