/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts
62 by edam
added preliminary (buggy) ContactsContract backend
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.HashMap;
27
import java.util.HashSet;
28
29
import am.ed.importcontacts.ContactsCache.CacheIdentifier;
30
import am.ed.importcontacts.Importer.ContactData;
31
import android.annotation.TargetApi;
32
import android.app.Activity;
33
import android.content.ContentUris;
34
import android.content.ContentValues;
35
import android.database.Cursor;
36
import android.net.Uri;
37
import android.provider.ContactsContract;
38
import android.provider.ContactsContract.CommonDataKinds;
39
40
@TargetApi(5)
41
public class ContactsContractBackend implements Backend
42
{
43
	private Activity _activity = null;
44
	private HashMap< Long, Long > _aggregate_to_raw_ids = null;
45
46
	ContactsContractBackend( Activity activity )
47
	{
48
		_activity = activity;
49
		_aggregate_to_raw_ids = new HashMap< Long, Long >();
50
	}
51
52
	@Override
53
	public void populateCache( ContactsCache cache )
54
	{
55
		Cursor cur;
56
57
		// build a set of aggregate contact ids that haven't been added to the
58
		// cache yet
59
		HashSet< Long > unadded_ids = new HashSet< Long >();
60
		cur = _activity.managedQuery( ContactsContract.Contacts.CONTENT_URI,
61
			new String[] {
62
				ContactsContract.Contacts._ID,
63
			}, null, null, null );
64
		while( cur.moveToNext() ) {
65
			Long id = cur.getLong(
66
				cur.getColumnIndex( ContactsContract.Contacts._ID ) );
67
			unadded_ids.add( id );
68
		}
69
70
		// build a mapping of the ids of raw contacts to the ids of their
71
		// aggregate contacts
72
		HashMap< Long, Long > raw_to_aggregate_ids =
73
			new HashMap< Long, Long >();
74
		cur = _activity.managedQuery( ContactsContract.RawContacts.CONTENT_URI,
75
			new String[] {
76
				ContactsContract.RawContacts._ID,
77
				ContactsContract.RawContacts.CONTACT_ID,
63 by edam
fixed selection of raw contacts in ContactsContract backend
78
			}, ContactsContract.RawContacts.DELETED + " = 0", null, null );
62 by edam
added preliminary (buggy) ContactsContract backend
79
		while( cur.moveToNext() ) {
80
			Long raw_id = cur.getLong(
81
				cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
82
			Long id = cur.getLong(
83
				cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) );
84
			raw_to_aggregate_ids.put( raw_id, id );
85
		}
86
87
		// get structured names, primary ones first
88
		cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
89
			new String[] {
90
				ContactsContract.Data.RAW_CONTACT_ID,
91
				CommonDataKinds.StructuredName.DISPLAY_NAME,
92
			},
93
			ContactsContract.Data.MIMETYPE + " = '" +
94
				CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'",
95
			null, ContactsContract.Data.IS_PRIMARY + " DESC" );
96
		while( cur.moveToNext() ) {
97
			Long raw_id = cur.getLong( cur.getColumnIndex(
98
				ContactsContract.Data.RAW_CONTACT_ID ) );
99
			Long id = raw_to_aggregate_ids.get( raw_id );
100
			String name = ContactsCache.normaliseName(
101
				cur.getString( cur.getColumnIndex(
102
					CommonDataKinds.StructuredName.DISPLAY_NAME ) ) );
103
			if( name != null && id != null )
104
			{
105
				// if this is a name for a contact for whom we have not added a
106
				// lookup, add a lookup for the contact id by name
107
				if( unadded_ids.contains( id ) ) {
108
					cache.addLookup( new CacheIdentifier(
109
						CacheIdentifier.Type.NAME, name ), id );
110
					unadded_ids.remove( id );
111
				}
112
			}
113
		}
114
115
		// get contact organisations, primary ones first
116
		cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
117
			new String[] {
118
				ContactsContract.Data.RAW_CONTACT_ID,
119
				CommonDataKinds.Organization.COMPANY,
120
			},
121
			ContactsContract.Data.MIMETYPE + " = '" +
122
				CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'",
123
			null, ContactsContract.Data.IS_PRIMARY + " DESC" );
124
		while( cur.moveToNext() ) {
125
			Long raw_id = cur.getLong( cur.getColumnIndex(
126
				ContactsContract.Data.RAW_CONTACT_ID ) );
127
			Long id = raw_to_aggregate_ids.get( raw_id );
128
			String organisation = ContactsCache.normaliseOrganisation(
129
				cur.getString( cur.getColumnIndex(
130
					CommonDataKinds.Organization.COMPANY ) ) );
131
			if( organisation != null && id != null )
132
			{
133
				// if this is an organisation name for a contact for whom we
134
				// have not added a lookup, add a lookup for the contact id
135
				// by organisation
136
				if( unadded_ids.contains( id ) ) {
137
					cache.addLookup( new CacheIdentifier(
138
						CacheIdentifier.Type.ORGANISATION, organisation ), id );
139
					unadded_ids.remove( id );
140
				}
141
142
				// add associated data
143
				cache.addAssociatedOrganisation( id, organisation );
144
			}
145
		}
146
147
		// get all phone numbers, primary ones first
148
		cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
149
			new String[] {
150
				ContactsContract.Data.RAW_CONTACT_ID,
151
				CommonDataKinds.Phone.NUMBER,
152
			},
153
			ContactsContract.Data.MIMETYPE + " = '" +
154
				CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'",
155
			null, ContactsContract.Data.IS_PRIMARY + " DESC" );
156
		while( cur.moveToNext() ) {
157
			Long raw_id = cur.getLong( cur.getColumnIndex(
158
				ContactsContract.Data.RAW_CONTACT_ID ) );
159
			Long id = raw_to_aggregate_ids.get( raw_id );
160
			String number = ContactsCache.normalisePhoneNumber(
161
				cur.getString( cur.getColumnIndex(
162
					CommonDataKinds.Phone.NUMBER ) ) );
163
			if( number != null && id != null )
164
			{
165
				// if this is a number for a contact for whom we have not
166
				// added a lookup, add a lookup for the contact id by phone
167
				// number
168
				if( unadded_ids.contains( id ) ) {
169
					cache.addLookup( new CacheIdentifier(
170
						CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
171
					unadded_ids.remove( id );
172
				}
173
174
				// add associated data
175
				cache.addAssociatedNumber( id, number );
176
			}
177
		}
178
179
		// get all email addresses, primary ones first
180
		cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
181
			new String[] {
182
				ContactsContract.Data.RAW_CONTACT_ID,
183
				CommonDataKinds.Email.DATA,
184
			},
185
			ContactsContract.Data.MIMETYPE + " = '" +
186
				CommonDataKinds.Email.CONTENT_ITEM_TYPE + "'",
187
			null, ContactsContract.Data.IS_PRIMARY + " DESC" );
188
		while( cur.moveToNext() ) {
189
			Long raw_id = cur.getLong( cur.getColumnIndex(
190
				ContactsContract.Data.RAW_CONTACT_ID ) );
191
			Long id = raw_to_aggregate_ids.get( raw_id );
192
			String email = ContactsCache.normaliseEmailAddress(
193
				cur.getString( cur.getColumnIndex(
194
					CommonDataKinds.Email.DATA ) ) );
195
			if( email != null && id != null )
196
			{
197
				// if this is an email address for a contact for whom we have
198
				// not added a lookup, add a lookup for the contact id by email
199
				// address
200
				if( unadded_ids.contains( id ) ) {
201
					cache.addLookup( new CacheIdentifier(
202
						CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
203
					unadded_ids.remove( id );
204
				}
205
206
				// add associated data
207
				cache.addAssociatedEmail( id, email );
208
			}
209
		}
210
211
		// get all postal addresses, primary ones first
212
		cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI,
213
			new String[] {
214
				ContactsContract.Data.RAW_CONTACT_ID,
215
				CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
216
			},
217
			ContactsContract.Data.MIMETYPE + " = '" +
218
				CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'",
219
			null, ContactsContract.Data.IS_PRIMARY + " DESC" );
220
		while( cur.moveToNext() ) {
221
			Long raw_id = cur.getLong( cur.getColumnIndex(
222
				ContactsContract.Data.RAW_CONTACT_ID ) );
223
			Long id = raw_to_aggregate_ids.get( raw_id );
224
			String address = ContactsCache.normaliseAddress(
225
				cur.getString( cur.getColumnIndex(
226
					CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) );
227
			if( address != null && id != null )
228
			{
229
				// add associated data
230
				cache.addAssociatedAddress( id, address );
231
			}
232
		}
233
	}
234
235
	@Override
236
	public void deleteContact( Long id )
237
	{
238
		Uri contact_uri = ContentUris.withAppendedId(
239
			ContactsContract.Contacts.CONTENT_URI, id );
240
		_activity.getContentResolver().delete( contact_uri, null, null );
241
	}
242
243
	@Override
244
	public Long addContact( String name ) throws ContactCreationException
245
	{
246
		// create raw contact
247
		ContentValues values = new ContentValues();
248
		Uri contact_uri = _activity.getContentResolver().insert(
249
			ContactsContract.RawContacts.CONTENT_URI, values);
250
		Long raw_id = ContentUris.parseId( contact_uri );
251
		if( raw_id == 0 ) throw new ContactCreationException();
252
253
		// add name data for this raw contact
254
		if( name != null ) {
255
			values.put( ContactsContract.Data.RAW_CONTACT_ID, raw_id );
256
			values.put( ContactsContract.Data.MIMETYPE,
257
				CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE );
258
			values.put( CommonDataKinds.StructuredName.DISPLAY_NAME, name );
259
			_activity.getContentResolver().insert(
260
				ContactsContract.Data.CONTENT_URI, values );
261
		}
262
263
		// find corresponding aggregate contact
264
		contact_uri = Uri.withAppendedPath(
265
			ContentUris.withAppendedId(
266
				ContactsContract.RawContacts.CONTENT_URI, raw_id ),
267
			ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY );
268
		Cursor cur = _activity.managedQuery( contact_uri,
269
			new String[] {
270
				ContactsContract.RawContacts.CONTACT_ID,
271
			}, null, null, null );
272
		Long id = null;
273
		if( cur.moveToNext() )
274
			id = cur.getLong(
275
				cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) );
276
		if( id == null || id == 0 )
277
		{
278
			// we didn't find an aggregate contact id, so try to clean up (by
279
			// deleting the raw contact we just created) before bailing
280
			contact_uri = ContentUris.withAppendedId(
281
				ContactsContract.RawContacts.CONTENT_URI, id );
282
			_activity.getContentResolver().delete( contact_uri, null, null );
283
284
			throw new ContactCreationException();
285
		}
286
287
		return id;
288
	}
289
290
	/**
291
	 * Obtain the raw contact id for the phone-only raw contact that is
292
	 * associated with the aggregate contact id.  One will be created if
293
	 * necessary.
294
	 * @param id the aggregate contact id
295
	 * @return the raw contact id
296
	 * @throws ContactCreationException
297
	 */
298
	Long obtainRawContact( Long id ) throws ContactCreationException
299
	{
300
		// attempt to lookup cached value
301
		Long raw_id = _aggregate_to_raw_ids.get( id );
302
		if( raw_id != null ) return raw_id;
303
304
		// find a corresponding raw contact that has no account name/type
305
		Cursor cur = _activity.managedQuery(
306
			ContactsContract.RawContacts.CONTENT_URI,
307
			new String[] {
308
				ContactsContract.RawContacts._ID,
63 by edam
fixed selection of raw contacts in ContactsContract backend
309
				ContactsContract.RawContacts.ACCOUNT_NAME,
62 by edam
added preliminary (buggy) ContactsContract backend
310
			},
63 by edam
fixed selection of raw contacts in ContactsContract backend
311
			ContactsContract.RawContacts.DELETED + " = 0 AND " +
312
				ContactsContract.RawContacts.CONTACT_ID + " = ? AND " +
62 by edam
added preliminary (buggy) ContactsContract backend
313
				"IFNULL( " + ContactsContract.RawContacts.ACCOUNT_NAME +
314
					", '' ) = '' AND " +
315
				"IFNULL( " + ContactsContract.RawContacts.ACCOUNT_TYPE +
316
					", '' ) = ''",
63 by edam
fixed selection of raw contacts in ContactsContract backend
317
			new String[] {
318
				String.valueOf( id ),
319
			}, null );
62 by edam
added preliminary (buggy) ContactsContract backend
320
		if( cur.moveToNext() )
321
			raw_id = cur.getLong(
322
				cur.getColumnIndex( ContactsContract.RawContacts._ID ) );
323
324
		// if one wasn't found, we'll need to create one
325
		if( raw_id == null ) {
326
			ContentValues values = new ContentValues();
327
			Uri contact_uri = _activity.getContentResolver().insert(
328
				ContactsContract.RawContacts.CONTENT_URI, values);
329
			raw_id = ContentUris.parseId( contact_uri );
330
			if( raw_id == 0 ) throw new ContactCreationException();
331
		}
332
333
		// save value in our cache
334
		_aggregate_to_raw_ids.put( id, raw_id );
335
		return raw_id;
336
	}
337
338
	private int convertTypeToBackendType( Class< ? > cls, int type )
339
		throws ContactCreationException
340
	{
341
		if( cls == CommonDataKinds.Phone.class )
342
		{
343
			switch( type )
344
			{
345
			case ContactData.TYPE_HOME:
346
				return CommonDataKinds.Phone.TYPE_HOME;
347
			case ContactData.TYPE_WORK:
348
				return CommonDataKinds.Phone.TYPE_WORK;
349
			case ContactData.TYPE_MOBILE:
350
				return CommonDataKinds.Phone.TYPE_MOBILE;
351
			case ContactData.TYPE_FAX_HOME:
352
				return CommonDataKinds.Phone.TYPE_FAX_HOME;
353
			case ContactData.TYPE_FAX_WORK:
354
				return CommonDataKinds.Phone.TYPE_FAX_WORK;
355
			case ContactData.TYPE_PAGER:
356
				return CommonDataKinds.Phone.TYPE_PAGER;
357
			}
358
		}
359
		else if( cls == CommonDataKinds.Email.class )
360
		{
361
			switch( type )
362
			{
363
			case ContactData.TYPE_HOME:
364
				return CommonDataKinds.Email.TYPE_HOME;
365
			case ContactData.TYPE_WORK:
366
				return CommonDataKinds.Email.TYPE_WORK;
367
			}
368
		}
369
		else if( cls == CommonDataKinds.StructuredPostal.class )
370
		{
371
			switch( type )
372
			{
373
			case ContactData.TYPE_HOME:
374
				return CommonDataKinds.StructuredPostal.TYPE_HOME;
375
			case ContactData.TYPE_WORK:
376
				return CommonDataKinds.StructuredPostal.TYPE_WORK;
377
			}
378
		}
379
380
		// still here?
381
		throw new ContactCreationException();
382
	}
383
384
	@Override
385
	public void addContactPhone( Long id, String number,
386
		ContactData.PreferredDetail data ) throws ContactCreationException
387
	{
388
		ContentValues values = new ContentValues();
389
		values.put( ContactsContract.Data.RAW_CONTACT_ID,
390
			obtainRawContact( id ) );
391
		values.put( ContactsContract.Data.MIMETYPE,
392
			CommonDataKinds.Phone.CONTENT_ITEM_TYPE );
393
		values.put( CommonDataKinds.Phone.TYPE,
394
			convertTypeToBackendType( CommonDataKinds.Phone.class,
395
				data.getType() ) );
396
		values.put( CommonDataKinds.Phone.NUMBER, number );
397
		if( data.isPreferred() )
398
			values.put( CommonDataKinds.Phone.IS_PRIMARY, 1 );
399
400
		_activity.getContentResolver().insert(
401
			ContactsContract.Data.CONTENT_URI, values );
402
	}
403
404
	@Override
405
	public void addContactEmail( Long id, String email,
406
		ContactData.PreferredDetail data ) throws ContactCreationException
407
	{
408
		ContentValues values = new ContentValues();
409
		values.put( ContactsContract.Data.RAW_CONTACT_ID,
410
			obtainRawContact( id ) );
411
		values.put( ContactsContract.Data.MIMETYPE,
412
			CommonDataKinds.Email.CONTENT_ITEM_TYPE );
413
		values.put( CommonDataKinds.Email.TYPE,
414
			convertTypeToBackendType( CommonDataKinds.Email.class,
415
				data.getType() ) );
416
		values.put( CommonDataKinds.Email.DATA, email );
417
		if( data.isPreferred() )
418
			values.put( CommonDataKinds.Email.IS_PRIMARY, 1 );
419
420
		_activity.getContentResolver().insert(
421
			ContactsContract.Data.CONTENT_URI, values );
422
	}
423
424
	@Override
425
	public void addContactAddresses( Long id, String address,
426
		ContactData.TypeDetail data ) throws ContactCreationException
427
	{
428
		ContentValues values = new ContentValues();
429
		values.put( ContactsContract.Data.RAW_CONTACT_ID,
430
			obtainRawContact( id ) );
431
		values.put( ContactsContract.Data.MIMETYPE,
432
			CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE );
433
		values.put( CommonDataKinds.StructuredPostal.TYPE,
434
			convertTypeToBackendType( CommonDataKinds.StructuredPostal.class,
435
				data.getType() ) );
436
		values.put(
437
			CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, address );
438
439
		_activity.getContentResolver().insert(
440
			ContactsContract.Data.CONTENT_URI, values );
441
	}
442
443
	@Override
444
	public void addContactOrganisation( Long id, String organisation,
445
		ContactData.ExtraDetail data ) throws ContactCreationException
446
	{
447
		ContentValues values = new ContentValues();
448
		values.put( ContactsContract.Data.RAW_CONTACT_ID,
449
			obtainRawContact( id ) );
450
		values.put( ContactsContract.Data.MIMETYPE,
451
			CommonDataKinds.Organization.CONTENT_ITEM_TYPE );
452
		values.put( CommonDataKinds.Organization.TYPE,
453
			CommonDataKinds.Organization.TYPE_WORK );
454
		values.put(
455
			CommonDataKinds.Organization.COMPANY, organisation );
456
		if( data.getExtra() != null )
457
			values.put( CommonDataKinds.Organization.TITLE, data.getExtra() );
458
459
		_activity.getContentResolver().insert(
460
			ContactsContract.Data.CONTENT_URI, values );
461
	}
462
}