/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
/*
 * ContactsCache.java
 *
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 *
 * This file is part of the Import Contacts program (hereafter referred
 * to as "this program"). For more information, see
 * http://www.waxworlds.org/edam/software/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 org.waxworlds.edam.importcontacts;

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

import org.waxworlds.edam.importcontacts.Importer.AbortImportException;

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


public class ContactsCache
{
	private HashMap< String, Long > _contacts;

	private HashMap< Long, HashSet< String > > _contactNumbers;
	private HashMap< Long, HashSet< String > > _contactEmails;
	private HashMap< Long, HashSet< String > > _contactAddresses;

	public boolean exists( String name )
	{
		return _contacts.containsKey( name );
	}

	public Long getId( String name )
	{
		return _contacts.get( name );
	}

	public void remove( String name )
	{
		_contacts.remove( name );
	}

	public void put( Long id, String name )
	{
		_contacts.put( name, id );
	}

	public boolean hasNumber( Long id, String number )
	{
		HashSet< String > cache = _contactNumbers.get( id );
		return cache != null && cache.contains( number );
	}

	public void addNumber( Long id, String number )
	{
		HashSet< String > cache = _contactNumbers.get( id );
		if( cache == null ) {
			cache = new HashSet< String >();
			_contactNumbers.put( id, cache );
		}
		cache.add( number );
	}

	public boolean hasEmail( Long id, String email )
	{
		HashSet< String > cache = _contactEmails.get( id );
		return cache != null && cache.contains( email );
	}

	public void addEmail( Long id, String email )
	{
		HashSet< String > cache = _contactEmails.get( id );
		if( cache == null ) {
			cache = new HashSet< String >();
			_contactEmails.put( id, cache );
		}
		cache.add( email );
	}

	public boolean hasAddress( Long id, String address )
	{
		HashSet< String > cache = _contactAddresses.get( id );
		return cache != null && cache.contains( address );
	}

	public void addAddress( Long id, String address )
	{
		HashSet< String > cache = _contactAddresses.get( id );
		if( cache == null ) {
			cache = new HashSet< String >();
			_contactAddresses.put( id, cache );
		}
		cache.add( address );
	}

	public void buildCache( Activity activity )
		throws AbortImportException
	{
		String[] cols;
		Cursor cur;

		// init contacts caches
		_contacts = new HashMap< String, Long >();
		_contactNumbers = new HashMap< Long, HashSet< String > >();
		_contactEmails = new HashMap< Long, HashSet< String > >();
		_contactAddresses = new HashMap< Long, HashSet< String > >();

		// query and store map of contact names to ids
		cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
		cur = activity.managedQuery( Contacts.People.CONTENT_URI,
			cols, null, null, null);
		if( cur.moveToFirst() ) {
			int idCol = cur.getColumnIndex( Contacts.People._ID );
			int nameCol = cur.getColumnIndex( Contacts.People.NAME );
			do {
				_contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
			} while( cur.moveToNext() );
		}

		// query and store map of contact ids to sets of phone numbers
		cols = new String[] { Contacts.Phones.PERSON_ID,
				Contacts.Phones.NUMBER };
		cur = activity.managedQuery( Contacts.Phones.CONTENT_URI,
			cols, null, null, null);
		if( cur.moveToFirst() ) {
			int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
			int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
			do {
				Long id = cur.getLong( personIdCol );
				String number = Importer.sanitisePhoneNumber(
						cur.getString( numberCol ) );
				if( number != null ) {
					HashSet< String > numbers = _contactNumbers.get( id );
					if( numbers == null ) {
						numbers = new HashSet< String >();
						_contactNumbers.put( id, numbers );
					}
					numbers.add( number );
				}
			} while( cur.moveToNext() );
		}

		// query and store map of contact ids to sets of email addresses
		cols = new String[] { Contacts.ContactMethods.PERSON_ID,
				Contacts.ContactMethods.DATA };
		cur = activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
				cols, Contacts.ContactMethods.KIND + " = ?",
				new String[] { "" + Contacts.KIND_EMAIL }, null );
		if( cur.moveToFirst() ) {
			int personIdCol = cur.getColumnIndex(
				Contacts.ContactMethods.PERSON_ID );
			int addressCol = cur.getColumnIndex(
				Contacts.ContactMethods.DATA );
			do {
				Long id = cur.getLong( personIdCol );
				String address = Importer.sanitiseEmailAddress(
					cur.getString( addressCol ) );
				if( address != null ) {
					HashSet< String > addresses = _contactEmails.get( id );
					if( addresses == null ) {
						addresses = new HashSet< String >();
						_contactEmails.put( id, addresses );
					}
					addresses.add( address );
				}
			} while( cur.moveToNext() );
		}

		// query and store map of contact ids to sets of postal addresses
		cols = new String[] { Contacts.ContactMethods.PERSON_ID,
			Contacts.ContactMethods.DATA };
		cur = activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
			cols, Contacts.ContactMethods.KIND + " = ?",
			new String[] { "" + Contacts.KIND_POSTAL }, null );
		if( cur.moveToFirst() ) {
			int personIdCol = cur.getColumnIndex(
				Contacts.ContactMethods.PERSON_ID );
			int addressCol = cur.getColumnIndex(
				Contacts.ContactMethods.DATA );
			do {
				Long id = cur.getLong( personIdCol );
				String address = cur.getString( addressCol );
				if( address != null ) {
					HashSet< String > addresses = _contactAddresses.get( id );
					if( addresses == null ) {
						addresses = new HashSet< String >();
						_contactAddresses.put( id, addresses );
					}
					addresses.add( address );
				}
			} while( cur.moveToNext() );
		}
	}
}