/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts
39 by edam
- pulled contacts cache out in to seperate class
1
/*
2
 * ContactsCache.java
3
 *
57 by edam
cleanup; fixed some typos; updated TODO
4
 * Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
39 by edam
- pulled contacts cache out in to seperate class
5
 *
6
 * This file is part of the Import Contacts program (hereafter referred
7
 * to as "this program"). For more information, see
50 by edam
updated all URLs, email addresses and package names to ed.am
8
 * http://ed.am/dev/android/import-contacts
39 by edam
- pulled contacts cache out in to seperate class
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
50 by edam
updated all URLs, email addresses and package names to ed.am
24
package am.ed.importcontacts;
39 by edam
- pulled contacts cache out in to seperate class
25
26
import java.util.HashMap;
27
import java.util.HashSet;
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
28
import java.util.Locale;
39 by edam
- pulled contacts cache out in to seperate class
29
30
public class ContactsCache
31
{
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
32
	/**
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
33
	 * A thing that can be used to identify (or lookup) a contact within the
34
	 * contacts cache.  It is not a reference to a cache entry and may not
35
	 * identify an existing contact in the cache.
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
36
	 */
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
37
	public static class CacheIdentifier
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
38
	{
66 by edam
removed redundant CacheIdentifier constructor and NONE type
39
		public enum Type { NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
40
41
		private Type _type;
42
		private String _detail;
43
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
44
		/**
45
		 * Obtain a cache identifier.  This routine is designed to be as robust
46
		 * as possible (in terms of bad or null detail values), and to return
47
		 * null when a cache identifier can not be created.
48
		 * @param type the detail type
49
		 * @param detail the detail
50
		 * @return the cache identifier, or null
51
		 */
52
		public static CacheIdentifier factory( Type type, String detail )
53
		{
54
			switch( type )
55
			{
56
			case NAME: detail = normaliseName( detail ); break;
57
			case ORGANISATION: detail = normaliseOrganisation( detail ); break;
58
			case PRIMARY_NUMBER: detail = normalisePhoneNumber( detail ); break;
59
			case PRIMARY_EMAIL: detail = normaliseEmailAddress( detail ); break;
60
			default: return null;
61
			}
62
			if( detail == null ) return null;
63
			return new CacheIdentifier( type, detail );
64
		}
65
66
		/**
67
		 * Obtain a cache identifier from contact data.  This routine is
68
		 * designed to be as robust as possible and may return null when a cache
69
		 * identifier can not be created.
70
		 * @param contact the contact data
71
		 * @return the cache identifier, or null
72
		 */
73
		public static CacheIdentifier factory( Importer.ContactData contact )
74
		{
75
			CacheIdentifier ret = null;
76
77
			if( contact.hasName() )
78
				ret = factory( CacheIdentifier.Type.NAME,
79
					contact.getName() );
80
			if( ret == null && contact.hasPrimaryOrganisation() )
81
				ret = factory( CacheIdentifier.Type.ORGANISATION,
82
					contact.getPrimaryOrganisation() );
83
			if( ret == null && contact.hasPrimaryNumber() )
84
				ret = factory( CacheIdentifier.Type.PRIMARY_NUMBER,
85
					contact.getPrimaryNumber() );
86
			if( ret == null && contact.hasPrimaryEmail() )
87
				ret = factory( CacheIdentifier.Type.PRIMARY_EMAIL,
88
					contact.getPrimaryEmail() );
89
90
			return ret;
91
		}
92
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
93
		protected CacheIdentifier( Type type, String detail )
94
		{
95
			_type = type;
96
			_detail = detail;
97
		}
98
99
		public Type getType()
100
		{
101
			return _type;
102
		}
103
104
		public String getDetail()
105
		{
106
			return _detail;
107
		}
108
	}
109
110
	// mappings of contact names, organisations and primary numbers to ids
53 by edam
abstracted the android contacts API in to an interface, ready to be switched to
111
	private HashMap< String, Long > _contactsByName
112
		= new HashMap< String, Long >();
113
	private HashMap< String, Long > _contactsByOrg
114
		= new HashMap< String, Long >();
115
	private HashMap< String, Long > _contactsByNumber
116
		= new HashMap< String, Long >();
117
	private HashMap< String, Long > _contactsByEmail
118
		= new HashMap< String, Long >();
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
119
120
	// mapping of contact ids to sets of associated data
53 by edam
abstracted the android contacts API in to an interface, ready to be switched to
121
	private HashMap< Long, HashSet< String > > _contactNumbers
122
		= new HashMap< Long, HashSet< String > >();
123
	private HashMap< Long, HashSet< String > > _contactEmails
124
		= new HashMap< Long, HashSet< String > >();
125
	private HashMap< Long, HashSet< String > > _contactAddresses
126
		= new HashMap< Long, HashSet< String > >();
127
	private HashMap< Long, HashSet< String > > _contactOrganisations
128
		= new HashMap< Long, HashSet< String > >();
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
129
	private HashMap< Long, HashSet< String > > _contactNotes
130
		= new HashMap< Long, HashSet< String > >();
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
131
132
	public boolean canLookup( CacheIdentifier identifier )
133
	{
134
		return lookup( identifier ) != null;
135
	}
136
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
137
	/**
138
	 * Retrieve the contact id of a contact identified by the specified cache
139
	 * identifier, if it exists.
140
	 * @param identifier the cache identifier
141
	 * @return a contact id, or null
142
	 */
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
143
	public Long lookup( CacheIdentifier identifier )
144
	{
145
		switch( identifier.getType() )
146
		{
147
		case NAME:
148
			return _contactsByName.get( identifier.getDetail() );
149
		case ORGANISATION:
150
			return _contactsByOrg.get( identifier.getDetail() );
151
		case PRIMARY_NUMBER:
152
			return _contactsByNumber.get( identifier.getDetail() );
153
		case PRIMARY_EMAIL:
154
			return _contactsByEmail.get( identifier.getDetail() );
155
		}
156
		return null;
157
	}
158
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
159
	/**
160
	 * Remove any cache entry that is identified by the cache identifier.
161
	 * @param identifier the cache identifier
162
	 * @return the contact id of the contact that was removed, or null
163
	 */
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
164
	public Long removeLookup( CacheIdentifier identifier )
165
	{
166
		switch( identifier.getType() )
167
		{
168
		case NAME:
169
			return _contactsByName.remove( identifier.getDetail() );
170
		case ORGANISATION:
171
			return _contactsByOrg.remove( identifier.getDetail() );
172
		case PRIMARY_NUMBER:
173
			return _contactsByNumber.remove( identifier.getDetail() );
174
		case PRIMARY_EMAIL:
175
			return _contactsByEmail.remove( identifier.getDetail() );
176
		}
177
		return null;
178
	}
179
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
180
	/**
181
	 * Add a lookup from a contact identifier to a contact id to the cache.
182
	 * @param identifier the cache identifier
183
	 * @param id teh contact id
184
	 */
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
185
	public void addLookup( CacheIdentifier identifier, Long id )
186
	{
187
		switch( identifier.getType() )
188
		{
189
		case NAME:
190
			_contactsByName.put( identifier.getDetail(), id );
191
			break;
192
		case ORGANISATION:
193
			_contactsByOrg.put( identifier.getDetail(), id );
194
			break;
195
		case PRIMARY_NUMBER:
196
			_contactsByNumber.put( identifier.getDetail(), id );
197
			break;
198
		case PRIMARY_EMAIL:
199
			_contactsByEmail.put( identifier.getDetail(), id );
200
			break;
201
		}
202
	}
203
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
204
	/**
205
	 * Remove any data that is associated with an contact id.
206
	 * @param id
207
	 */
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
208
	public void removeAssociatedData( Long id )
209
	{
210
		_contactNumbers.remove( id );
211
		_contactEmails.remove( id );
212
		_contactAddresses.remove( id );
213
		_contactOrganisations.remove( id );
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
214
		_contactNotes.remove( id );
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
215
	}
216
217
	public boolean hasAssociatedNumber( Long id, String number )
218
	{
219
		number = normalisePhoneNumber( number );
220
		if( number == null ) return false;
221
222
		HashSet< String > set = _contactNumbers.get( id );
223
		return set != null && set.contains( number );
224
	}
225
226
	public void addAssociatedNumber( Long id, String number )
227
	{
228
		number = normalisePhoneNumber( number );
229
		if( number == null ) return;
230
231
		HashSet< String > set = _contactNumbers.get( id );
232
		if( set == null ) {
233
			set = new HashSet< String >();
234
			_contactNumbers.put( id, set );
235
		}
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
236
		set.add( number );
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
237
	}
238
239
	public boolean hasAssociatedEmail( Long id, String email )
240
	{
241
		email = normaliseEmailAddress( email );
242
		if( email == null ) return false;
243
244
		HashSet< String > set = _contactEmails.get( id );
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
245
		return set != null && set.contains( email );
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
246
	}
247
248
	public void addAssociatedEmail( Long id, String email )
249
	{
250
		email = normaliseEmailAddress( email );
251
		if( email == null ) return;
252
253
		HashSet< String > set = _contactEmails.get( id );
254
		if( set == null ) {
255
			set = new HashSet< String >();
256
			_contactEmails.put( id, set );
257
		}
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
258
		set.add( email );
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
259
	}
260
261
	public boolean hasAssociatedAddress( Long id, String address )
262
	{
263
		address = normaliseAddress( address );
264
		if( address == null ) return false;
265
266
		HashSet< String > set = _contactAddresses.get( id );
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
267
		return set != null && set.contains( address );
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
268
	}
269
270
	public void addAssociatedAddress( Long id, String address )
271
	{
272
		address = normaliseAddress( address );
273
		if( address == null ) return;
274
275
		HashSet< String > set = _contactAddresses.get( id );
276
		if( set == null ) {
277
			set = new HashSet< String >();
278
			_contactAddresses.put( id, set );
279
		}
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
280
		set.add( address );
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
281
	}
282
283
	public boolean hasAssociatedOrganisation( Long id, String organisation )
284
	{
285
		organisation = normaliseOrganisation( organisation );
286
		if( organisation == null ) return false;
287
288
		HashSet< String > set = _contactOrganisations.get( id );
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
289
		return set != null && set.contains( organisation );
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
290
	}
291
292
	public void addAssociatedOrganisation( Long id, String organisation )
293
	{
294
		organisation = normaliseOrganisation( organisation );
295
		if( organisation == null ) return;
296
297
		HashSet< String > set = _contactOrganisations.get( id );
298
		if( set == null ) {
299
			set = new HashSet< String >();
300
			_contactOrganisations.put( id, set );
301
		}
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
302
		set.add( organisation );
303
	}
304
305
	public boolean hasAssociatedNote( Long id, String note )
306
	{
307
		note = normaliseNote( note );
308
		if( note == null ) return false;
309
310
		HashSet< String > set = _contactNotes.get( id );
311
		return set != null && set.contains( note );
312
	}
313
314
	public void addAssociatedNote( Long id, String note )
315
	{
316
		note = normaliseNote( note );
317
		if( note == null ) return;
318
319
		HashSet< String > set = _contactNotes.get( id );
320
		if( set == null ) {
321
			set = new HashSet< String >();
322
			_contactNotes.put( id, set );
323
		}
324
		set.add( note );
39 by edam
- pulled contacts cache out in to seperate class
325
	}
326
53 by edam
abstracted the android contacts API in to an interface, ready to be switched to
327
	static public String normaliseName( String name )
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
328
	{
329
		if( name == null ) return null;
330
		name = name.trim();
331
		return name.length() > 0? name : null;
332
	}
333
53 by edam
abstracted the android contacts API in to an interface, ready to be switched to
334
	static public String normalisePhoneNumber( String number )
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
335
	{
336
		if( number == null ) return null;
337
		number = number.trim().replaceAll( "[-\\(\\) ]", "" );
338
		return number.length() > 0? number : null;
339
	}
340
53 by edam
abstracted the android contacts API in to an interface, ready to be switched to
341
	static public String normaliseEmailAddress( String email )
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
342
	{
343
		if( email == null ) return null;
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
344
		email = email.trim().toLowerCase( Locale.US );
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
345
		return email.length() > 0? email : null;
346
	}
347
53 by edam
abstracted the android contacts API in to an interface, ready to be switched to
348
	static public String normaliseOrganisation( String organisation )
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
349
	{
350
		if( organisation == null ) return null;
351
		organisation = organisation.trim();
352
		return organisation.length() > 0? organisation : null;
353
	}
354
53 by edam
abstracted the android contacts API in to an interface, ready to be switched to
355
	static public String normaliseAddress( String address )
40 by edam
- fixed logic for vcard field types (home, work, cell, etc) so it works
356
	{
357
		if( address == null ) return null;
358
		address = address.trim();
359
		return address.length() > 0? address : null;
39 by edam
- pulled contacts cache out in to seperate class
360
	}
65 by edam
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters
361
362
	static public String normaliseNote( String note )
363
	{
364
		if( note == null ) return null;
365
		note = note.trim();
366
		return note.length() > 0? note : null;
367
	}
39 by edam
- pulled contacts cache out in to seperate class
368
}