52
54
// set of contact ids that we have not yet added
53
55
HashSet< Long > unadded_ids = new HashSet< Long >();
58
HashMap< Long, String > notes = new HashMap< Long, String >();
55
60
// get all contacts
56
cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
61
cur = _activity.getContentResolver().query(
62
Contacts.People.CONTENT_URI,
58
64
Contacts.People._ID,
59
65
Contacts.People.NAME,
66
Contacts.People.NOTES,
60
67
}, null, null, null );
61
68
while( cur.moveToNext() ) {
62
69
Long id = cur.getLong(
63
70
cur.getColumnIndex( Contacts.People._ID ) );
65
ContactsCache.normaliseName( cur.getString(
66
cur.getColumnIndex( Contacts.People.NAME ) ) );
71
String name = cur.getString(
72
cur.getColumnIndex( Contacts.People.NAME ) );
73
String note = cur.getString(
74
cur.getColumnIndex( Contacts.People.NOTES ) );
76
// if we can, add a lookup for the contact id by name
77
CacheIdentifier cache_identifier = CacheIdentifier.factory(
78
CacheIdentifier.Type.NAME, name );
79
if( cache_identifier != null ) {
80
cache.addLookup( cache_identifier, id );
82
// add any associated notes (this would get done at the end but,
83
// since it is most common that contacts are identified by name,
84
// it is worth doing a special case here
85
cache.addAssociatedNote( id, note );
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 );
89
// record that a lookup for this contact's id still needs to be
90
// added by some other means
91
unadded_ids.add( id );
93
// store this contact's notes, so that they can be added to the
94
// cache at the end, after this contact has been added (by
95
// whatever identifying means)
96
if( note != null && note.length() > 0 )
97
notes.put( id, note );
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 );
82
102
// get contact organisations, primary ones first
83
cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
103
cur = _activity.getContentResolver().query(
104
Contacts.Organizations.CONTENT_URI,
85
106
Contacts.Phones.PERSON_ID,
86
107
Contacts.Organizations.COMPANY,
88
109
while( cur.moveToNext() ) {
89
110
Long id = cur.getLong( cur.getColumnIndex(
90
111
Contacts.Organizations.PERSON_ID ) );
92
ContactsCache.normaliseOrganisation( cur.getString(
93
cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
94
if( organisation != null )
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
99
if( unadded_ids.contains( id ) ) {
100
cache.addLookup( new CacheIdentifier(
101
CacheIdentifier.Type.ORGANISATION, organisation ), id );
112
String organisation = cur.getString(
113
cur.getColumnIndex( Contacts.Organizations.COMPANY ) );
115
// if this is an organisation name for a contact for whom we have
116
// not added a lookup, add a lookup for the contact id by
118
if( unadded_ids.contains( id ) ) {
119
CacheIdentifier cache_identifier = CacheIdentifier.factory(
120
CacheIdentifier.Type.ORGANISATION, organisation );
121
if( cache_identifier != null ) {
122
cache.addLookup( cache_identifier, id );
102
123
unadded_ids.remove( id );
105
// add associated data
106
cache.addAssociatedOrganisation( id, organisation );
127
// add associated data
128
cache.addAssociatedOrganisation( id, organisation );
110
132
// get all phone numbers, primary ones first
111
cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
133
cur = _activity.getContentResolver().query(
134
Contacts.Phones.CONTENT_URI,
113
136
Contacts.Phones.PERSON_ID,
114
137
Contacts.Phones.NUMBER,
116
139
while( cur.moveToNext() ) {
117
140
Long id = cur.getLong(
118
141
cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
120
ContactsCache.normalisePhoneNumber( cur.getString(
121
cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
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
127
if( unadded_ids.contains( id ) ) {
128
cache.addLookup( new CacheIdentifier(
129
CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
142
String number = cur.getString(
143
cur.getColumnIndex( Contacts.Phones.NUMBER ) );
145
// if this is a number for a contact for whom we have not
146
// added a lookup, add a lookup for the contact id by phone
148
if( unadded_ids.contains( id ) ) {
149
CacheIdentifier cache_identifier = CacheIdentifier.factory(
150
CacheIdentifier.Type.PRIMARY_NUMBER, number );
151
if( cache_identifier != null ) {
152
cache.addLookup( cache_identifier, id );
130
153
unadded_ids.remove( id );
133
// add associated data
134
cache.addAssociatedNumber( id, number );
157
// add associated data
158
cache.addAssociatedNumber( id, number );
138
162
// now get all email addresses, primary ones first, and postal addresses
139
cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
163
cur = _activity.getContentResolver().query(
164
Contacts.ContactMethods.CONTENT_URI,
141
166
Contacts.ContactMethods.PERSON_ID,
142
167
Contacts.ContactMethods.DATA,
153
178
cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
154
179
if( kind == Contacts.KIND_EMAIL )
157
ContactsCache.normaliseEmailAddress( cur.getString(
158
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
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 );
181
String email = cur.getString(
182
cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
184
// if this is an email address for a contact for whom we have
185
// not added a lookup, add a lookup for the contact id by email
187
if( unadded_ids.contains( id ) ) {
188
CacheIdentifier cache_identifier = CacheIdentifier.factory(
189
CacheIdentifier.Type.PRIMARY_EMAIL, email );
190
if( cache_identifier != null ) {
191
cache.addLookup( cache_identifier, id );
167
192
unadded_ids.remove( id );
170
// add associated data
171
cache.addAssociatedEmail( id, email );
196
// add associated data
197
cache.addAssociatedEmail( id, email );
174
199
else if( kind == Contacts.KIND_POSTAL )
177
ContactsCache.normaliseAddress( cur.getString(
178
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
179
if( address != null )
181
// add associated data
182
cache.addAssociatedAddress( id, address );
201
String address = cur.getString(
202
cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
204
// add associated data
205
cache.addAssociatedAddress( id, address );
210
// finally, add the notes that we stored earlier (we have to add these
211
// at the end because we can't be sure which piece of contact data will
212
// cause the contact to be added to the cache
213
Iterator< Long > i = notes.keySet().iterator();
214
while( i.hasNext() ) {
216
cache.addAssociatedNote( id, notes.get( id ) );
227
259
case ContactData.TYPE_HOME:
228
return Contacts.Phones.TYPE_HOME;
260
return Contacts.PhonesColumns.TYPE_HOME;
229
261
case ContactData.TYPE_WORK:
230
return Contacts.Phones.TYPE_WORK;
262
return Contacts.PhonesColumns.TYPE_WORK;
231
263
case ContactData.TYPE_MOBILE:
232
return Contacts.Phones.TYPE_MOBILE;
264
return Contacts.PhonesColumns.TYPE_MOBILE;
233
265
case ContactData.TYPE_FAX_HOME:
234
return Contacts.Phones.TYPE_FAX_HOME;
266
return Contacts.PhonesColumns.TYPE_FAX_HOME;
235
267
case ContactData.TYPE_FAX_WORK:
236
return Contacts.Phones.TYPE_FAX_WORK;
268
return Contacts.PhonesColumns.TYPE_FAX_WORK;
237
269
case ContactData.TYPE_PAGER:
238
return Contacts.Phones.TYPE_PAGER;
270
return Contacts.PhonesColumns.TYPE_PAGER;
241
273
else if( cls == Contacts.ContactMethods.class )
328
360
Contacts.Organizations.CONTENT_URI, values );
364
public void addContactNote( Long id, String note )
365
throws ContactCreationException
367
ContentValues values = new ContentValues();
368
values.put( Contacts.People.NOTES, note );
369
_activity.getContentResolver().update(
370
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
371
values, null, null );