/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * ContactsCache.java
 *
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
 *
 * This file is part of the Import Contacts program (hereafter referred
 * to as "this program"). For more information, see
 * http://ed.am/dev/android/import-contacts
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package am.ed.importcontacts;

import java.util.HashMap;
import java.util.HashSet;

import am.ed.importcontacts.Importer.AbortImportException;

import android.app.Activity;
import android.database.Cursor;
import android.provider.Contacts;


public class ContactsCache
{
	/**
	 * Information that can be used to identify a contact within the cache
	 */
	static public class CacheIdentifier
	{
		public enum Type {
			NONE, NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }

		private Type _type;
		private String _detail;

		protected CacheIdentifier()
		{
			_type = Type.NONE;
		}

		protected CacheIdentifier( Type type, String detail )
		{
			_type = type;
			_detail = detail;
		}

		public Type getType()
		{
			return _type;
		}

		public String getDetail()
		{
			return _detail;
		}
	}

	// mappings of contact names, organisations and primary numbers to ids
	private HashMap< String, Long > _contactsByName;
	private HashMap< String, Long > _contactsByOrg;
	private HashMap< String, Long > _contactsByNumber;
	private HashMap< String, Long > _contactsByEmail;

	// mapping of contact ids to sets of associated data
	private HashMap< Long, HashSet< String > > _contactNumbers;
	private HashMap< Long, HashSet< String > > _contactEmails;
	private HashMap< Long, HashSet< String > > _contactAddresses;
	private HashMap< Long, HashSet< String > > _contactOrganisations;

	public static CacheIdentifier createIdentifier(
		Importer.ContactData contact )
	{
		if( contact.hasName() ) {
			String name = normaliseName( contact.getName() );
			if( name != null )
				return new CacheIdentifier(
					CacheIdentifier.Type.NAME, name );
		}

		if( contact.hasPrimaryOrganisation() ) {
			String organisation = normaliseOrganisation(
				contact.getPrimaryOrganisation() );
			if( organisation != null )
				return new CacheIdentifier(
					CacheIdentifier.Type.ORGANISATION, organisation );
		}

		if( contact.hasPrimaryNumber() ) {
			String number = normalisePhoneNumber( contact.getPrimaryNumber() );
			if( number != null )
			return new CacheIdentifier(
				CacheIdentifier.Type.PRIMARY_NUMBER, number );
		}

		if( contact.hasPrimaryEmail() ) {
			String email = normaliseEmailAddress( contact.getPrimaryEmail() );
			if( email != null )
			return new CacheIdentifier(
				CacheIdentifier.Type.PRIMARY_EMAIL, email );
		}

		return null;
	}

	public boolean canLookup( CacheIdentifier identifier )
	{
		return lookup( identifier ) != null;
	}

	public Long lookup( CacheIdentifier identifier )
	{
		switch( identifier.getType() )
		{
		case NAME:
			return _contactsByName.get( identifier.getDetail() );
		case ORGANISATION:
			return _contactsByOrg.get( identifier.getDetail() );
		case PRIMARY_NUMBER:
			return _contactsByNumber.get( identifier.getDetail() );
		case PRIMARY_EMAIL:
			return _contactsByEmail.get( identifier.getDetail() );
		}
		return null;
	}

	public Long removeLookup( CacheIdentifier identifier )
	{
		switch( identifier.getType() )
		{
		case NAME:
			return _contactsByName.remove( identifier.getDetail() );
		case ORGANISATION:
			return _contactsByOrg.remove( identifier.getDetail() );
		case PRIMARY_NUMBER:
			return _contactsByNumber.remove( identifier.getDetail() );
		case PRIMARY_EMAIL:
			return _contactsByEmail.remove( identifier.getDetail() );
		}
		return null;
	}

	public void addLookup( CacheIdentifier identifier, Long id )
	{
		switch( identifier.getType() )
		{
		case NAME:
			_contactsByName.put( identifier.getDetail(), id );
			break;
		case ORGANISATION:
			_contactsByOrg.put( identifier.getDetail(), id );
			break;
		case PRIMARY_NUMBER:
			_contactsByNumber.put( identifier.getDetail(), id );
			break;
		case PRIMARY_EMAIL:
			_contactsByEmail.put( identifier.getDetail(), id );
			break;
		}
	}

	public void removeAssociatedData( Long id )
	{
		_contactNumbers.remove( id );
		_contactEmails.remove( id );
		_contactAddresses.remove( id );
		_contactOrganisations.remove( id );
	}

	public boolean hasAssociatedNumber( Long id, String number )
	{
		number = normalisePhoneNumber( number );
		if( number == null ) return false;

		HashSet< String > set = _contactNumbers.get( id );
		return set != null && set.contains( number );
	}

	public void addAssociatedNumber( Long id, String number )
	{
		number = normalisePhoneNumber( number );
		if( number == null ) return;

		HashSet< String > set = _contactNumbers.get( id );
		if( set == null ) {
			set = new HashSet< String >();
			_contactNumbers.put( id, set );
		}
		set.add( normalisePhoneNumber( number ) );
	}

	public boolean hasAssociatedEmail( Long id, String email )
	{
		email = normaliseEmailAddress( email );
		if( email == null ) return false;

		HashSet< String > set = _contactEmails.get( id );
		return set != null && set.contains( normaliseEmailAddress( email ) );
	}

	public void addAssociatedEmail( Long id, String email )
	{
		email = normaliseEmailAddress( email );
		if( email == null ) return;

		HashSet< String > set = _contactEmails.get( id );
		if( set == null ) {
			set = new HashSet< String >();
			_contactEmails.put( id, set );
		}
		set.add( normaliseEmailAddress( email ) );
	}

	public boolean hasAssociatedAddress( Long id, String address )
	{
		address = normaliseAddress( address );
		if( address == null ) return false;

		HashSet< String > set = _contactAddresses.get( id );
		return set != null && set.contains( normaliseAddress( address ) );
	}

	public void addAssociatedAddress( Long id, String address )
	{
		address = normaliseAddress( address );
		if( address == null ) return;

		HashSet< String > set = _contactAddresses.get( id );
		if( set == null ) {
			set = new HashSet< String >();
			_contactAddresses.put( id, set );
		}
		set.add( normaliseAddress( address ) );
	}

	public boolean hasAssociatedOrganisation( Long id, String organisation )
	{
		organisation = normaliseOrganisation( organisation );
		if( organisation == null ) return false;

		HashSet< String > set = _contactOrganisations.get( id );
		return set != null && set.contains(
			normaliseOrganisation( organisation ) );
	}

	public void addAssociatedOrganisation( Long id, String organisation )
	{
		organisation = normaliseOrganisation( organisation );
		if( organisation == null ) return;

		HashSet< String > set = _contactOrganisations.get( id );
		if( set == null ) {
			set = new HashSet< String >();
			_contactOrganisations.put( id, set );
		}
		set.add( normaliseOrganisation( organisation ) );
	}

	public void buildCache( Activity activity )
		throws AbortImportException
	{
		Cursor cur;

		// init id lookups
		_contactsByName = new HashMap< String, Long >();
		_contactsByOrg = new HashMap< String, Long >();
		_contactsByNumber = new HashMap< String, Long >();
		_contactsByEmail = new HashMap< String, Long >();

		// init associated data cache
		_contactNumbers = new HashMap< Long, HashSet< String > >();
		_contactEmails = new HashMap< Long, HashSet< String > >();
		_contactAddresses = new HashMap< Long, HashSet< String > >();
		_contactOrganisations = new HashMap< Long, HashSet< String > >();

		// set of contact ids that we have not yet added
		HashSet< Long > unadded = new HashSet< Long >();

		// get all contacts
		cur = activity.managedQuery( Contacts.People.CONTENT_URI,
			new String[] {
				Contacts.People._ID,
				Contacts.People.NAME,
			}, null, null, null );
		while( cur.moveToNext() ) {
			Long id = cur.getLong(
				cur.getColumnIndex( Contacts.People._ID ) );
			String name = normaliseName( cur.getString(
				cur.getColumnIndex( Contacts.People.NAME ) ) );
			if( name != null )
			{
				// if we can, add a lookup for the contact id by name
				if( name.length() > 0 ) {
					addLookup( new CacheIdentifier(
						CacheIdentifier.Type.NAME, name ), id );
					continue;
				}
			}

			// record that a lookup for this contact's id still needs to be
			// added by some other means
			unadded.add( id );
		}

		// get contact organisations, primary ones first
		cur = activity.managedQuery( Contacts.Organizations.CONTENT_URI,
			new String[] {
				Contacts.Phones.PERSON_ID,
				Contacts.Organizations.COMPANY,
			}, null, null, Contacts.Organizations.ISPRIMARY + " DESC" );
		while( cur.moveToNext() ) {
			Long id = cur.getLong( cur.getColumnIndex(
				Contacts.Organizations.PERSON_ID ) );
			String organisation = normaliseOrganisation( cur.getString(
				cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
			if( organisation != null )
			{
				// if this is an organisation name for a contact for whom we
				// have not added a lookup, add a lookup for the contact id
				// by organisation
				if( unadded.contains( id ) ) {
					addLookup( new CacheIdentifier(
						CacheIdentifier.Type.ORGANISATION, organisation ), id );
					unadded.remove( id );
				}

				// add associated data
				addAssociatedOrganisation( id, organisation );
			}
		}

		// get all phone numbers, primary ones first
		cur = activity.managedQuery( Contacts.Phones.CONTENT_URI,
			new String[] {
				Contacts.Phones.PERSON_ID,
				Contacts.Phones.NUMBER,
			}, null, null, Contacts.Phones.ISPRIMARY + " DESC" );
		while( cur.moveToNext() ) {
			Long id = cur.getLong(
				cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
			String number = normalisePhoneNumber( cur.getString(
				cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
			if( number != null )
			{
				// if this is a number for a contact for whom we have not
				// added a lookup, add a lookup for the contact id by phone
				// number
				if( unadded.contains( id ) ) {
					addLookup( new CacheIdentifier(
						CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
					unadded.remove( id );
				}

				// add associated data
				addAssociatedNumber( id, number );
			}
		}

		// now get all email addresses, primary ones first, and postal addresses
		cur = activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
			new String[] {
				Contacts.ContactMethods.PERSON_ID,
				Contacts.ContactMethods.DATA,
				Contacts.ContactMethods.KIND,
			}, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
				"" + Contacts.KIND_EMAIL,
				"" + Contacts.KIND_POSTAL,
			}, Contacts.ContactMethods.ISPRIMARY + " DESC" );
		while( cur.moveToNext() ) {
			Long id = cur.getLong(
				cur.getColumnIndex( Contacts.ContactMethods.PERSON_ID ) );
			int kind = cur.getInt(
				cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
			if( kind == Contacts.KIND_EMAIL )
			{
				String email = normaliseEmailAddress( cur.getString(
					cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
				if( email != null )
				{
					// if this is an email address for a contact for whom we
					// have not added a lookup, add a lookup for the contact
					// id by email address
					if( unadded.contains( id ) ) {
						addLookup( new CacheIdentifier(
							CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
						unadded.remove( id );
					}

					// add associated data
					addAssociatedEmail( id, email );
				}
			}
			else if( kind == Contacts.KIND_POSTAL )
			{
				String address = normaliseAddress( cur.getString(
					cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
				if( address != null )
				{
					// add associated data
					addAssociatedAddress( id, address );
				}
			}
		}
	}

	static private String normaliseName( String name )
	{
		if( name == null ) return null;
		name = name.trim();
		return name.length() > 0? name : null;
	}

	static private String normalisePhoneNumber( String number )
	{
		if( number == null ) return null;
		number = number.trim().replaceAll( "[-\\(\\) ]", "" );
		return number.length() > 0? number : null;
	}

	static private String normaliseEmailAddress( String email )
	{
		if( email == null ) return null;
		email = email.trim().toLowerCase();
		return email.length() > 0? email : null;
	}

	static private String normaliseOrganisation( String organisation )
	{
		if( organisation == null ) return null;
		organisation = organisation.trim();
		return organisation.length() > 0? organisation : null;
	}

	static private String normaliseAddress( String address )
	{
		if( address == null ) return null;
		address = address.trim();
		return address.length() > 0? address : null;
	}
}