54
51
// set of contact ids that we have not yet added
55
52
HashSet< Long > unadded_ids = new HashSet< Long >();
58
HashMap< Long, String > notes = new HashMap< Long, String >();
60
54
// get all contacts
61
55
cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
63
57
Contacts.People._ID,
64
58
Contacts.People.NAME,
65
Contacts.People.NOTES,
66
59
}, null, null, null );
67
60
while( cur.moveToNext() ) {
68
61
Long id = cur.getLong(
69
62
cur.getColumnIndex( Contacts.People._ID ) );
70
String name = cur.getString(
71
cur.getColumnIndex( Contacts.People.NAME ) );
72
String note = cur.getString(
73
cur.getColumnIndex( Contacts.People.NOTES ) );
75
// if we can, add a lookup for the contact id by name
76
CacheIdentifier cache_identifier = CacheIdentifier.factory(
77
CacheIdentifier.Type.NAME, name );
78
if( cache_identifier != null ) {
79
cache.addLookup( cache_identifier, id );
81
// add any associated notes (this would get done at the end but,
82
// since it is most common that contacts are identified by name,
83
// it is worth doing a special case here
84
cache.addAssociatedNote( id, note );
64
ContactsCache.normaliseName( cur.getString(
65
cur.getColumnIndex( Contacts.People.NAME ) ) );
88
// record that a lookup for this contact's id still needs to be
89
// added by some other means
90
unadded_ids.add( id );
68
// if we can, add a lookup for the contact id by name
69
if( name.length() > 0 ) {
70
cache.addLookup( new CacheIdentifier(
71
CacheIdentifier.Type.NAME, name ), id );
92
// store this contact's notes, so that they can be added to the
93
// cache at the end, after this contact has been added (by
94
// whatever identifying means)
95
if( note != null && note.length() > 0 )
96
notes.put( id, note );
76
// record that a lookup for this contact's id still needs to be
77
// added by some other means
78
unadded_ids.add( id );
100
81
// get contact organisations, primary ones first
106
87
while( cur.moveToNext() ) {
107
88
Long id = cur.getLong( cur.getColumnIndex(
108
89
Contacts.Organizations.PERSON_ID ) );
109
String organisation = cur.getString(
110
cur.getColumnIndex( Contacts.Organizations.COMPANY ) );
112
// if this is an organisation name for a contact for whom we have
113
// not added a lookup, add a lookup for the contact id by
115
if( unadded_ids.contains( id ) ) {
116
CacheIdentifier cache_identifier = CacheIdentifier.factory(
117
CacheIdentifier.Type.ORGANISATION, organisation );
118
if( cache_identifier != null ) {
119
cache.addLookup( cache_identifier, id );
91
ContactsCache.normaliseOrganisation( cur.getString(
92
cur.getColumnIndex( Contacts.Organizations.COMPANY ) ) );
93
if( organisation != null )
95
// if this is an organisation name for a contact for whom we
96
// have not added a lookup, add a lookup for the contact id
98
if( unadded_ids.contains( id ) ) {
99
cache.addLookup( new CacheIdentifier(
100
CacheIdentifier.Type.ORGANISATION, organisation ), id );
120
101
unadded_ids.remove( id );
104
// add associated data
105
cache.addAssociatedOrganisation( id, organisation );
124
// add associated data
125
cache.addAssociatedOrganisation( id, organisation );
128
109
// get all phone numbers, primary ones first
134
115
while( cur.moveToNext() ) {
135
116
Long id = cur.getLong(
136
117
cur.getColumnIndex( Contacts.Phones.PERSON_ID ) );
137
String number = cur.getString(
138
cur.getColumnIndex( Contacts.Phones.NUMBER ) );
140
// if this is a number for a contact for whom we have not
141
// added a lookup, add a lookup for the contact id by phone
143
if( unadded_ids.contains( id ) ) {
144
CacheIdentifier cache_identifier = CacheIdentifier.factory(
145
CacheIdentifier.Type.PRIMARY_NUMBER, number );
146
if( cache_identifier != null ) {
147
cache.addLookup( cache_identifier, id );
119
ContactsCache.normalisePhoneNumber( cur.getString(
120
cur.getColumnIndex( Contacts.Phones.NUMBER ) ) );
123
// if this is a number for a contact for whom we have not
124
// added a lookup, add a lookup for the contact id by phone
126
if( unadded_ids.contains( id ) ) {
127
cache.addLookup( new CacheIdentifier(
128
CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
148
129
unadded_ids.remove( id );
132
// add associated data
133
cache.addAssociatedNumber( id, number );
152
// add associated data
153
cache.addAssociatedNumber( id, number );
156
137
// now get all email addresses, primary ones first, and postal addresses
159
140
Contacts.ContactMethods.PERSON_ID,
160
141
Contacts.ContactMethods.DATA,
161
142
Contacts.ContactMethods.KIND,
162
}, Contacts.ContactMethods.KIND + " IN( ?, ? )",
143
}, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
164
144
"" + Contacts.KIND_EMAIL,
165
145
"" + Contacts.KIND_POSTAL,
166
146
}, Contacts.ContactMethods.ISPRIMARY + " DESC" );
171
151
cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
172
152
if( kind == Contacts.KIND_EMAIL )
174
String email = cur.getString(
175
cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
177
// if this is an email address for a contact for whom we have
178
// not added a lookup, add a lookup for the contact id by email
180
if( unadded_ids.contains( id ) ) {
181
CacheIdentifier cache_identifier = CacheIdentifier.factory(
182
CacheIdentifier.Type.PRIMARY_EMAIL, email );
183
if( cache_identifier != null ) {
184
cache.addLookup( cache_identifier, id );
155
ContactsCache.normaliseEmailAddress( cur.getString(
156
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
159
// if this is an email address for a contact for whom we
160
// have not added a lookup, add a lookup for the contact
161
// id by email address
162
if( unadded_ids.contains( id ) ) {
163
cache.addLookup( new CacheIdentifier(
164
CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
185
165
unadded_ids.remove( id );
168
// add associated data
169
cache.addAssociatedEmail( id, email );
189
// add associated data
190
cache.addAssociatedEmail( id, email );
192
172
else if( kind == Contacts.KIND_POSTAL )
194
String address = cur.getString(
195
cur.getColumnIndex( Contacts.ContactMethods.DATA ) );
197
// add associated data
198
cache.addAssociatedAddress( id, address );
175
ContactsCache.normaliseAddress( cur.getString(
176
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
177
if( address != null )
179
// add associated data
180
cache.addAssociatedAddress( id, address );
202
// finally, add the notes that we stored earlier (we have to add these
203
// at the end because we can't be sure which piece of contact data will
204
// cause the contact to be added to the cache
205
Iterator< Long > i = notes.keySet().iterator();
206
while( i.hasNext() ) {
208
cache.addAssociatedNote( id, notes.get( id ) );
221
public Long addContact( String name ) throws ContactCreationException
195
public Long addContact( String name )
223
197
ContentValues values = new ContentValues();
225
values.put( Contacts.People.NAME, name );
198
values.put( Contacts.People.NAME, name );
226
199
Uri contact_uri = _activity.getContentResolver().insert(
227
200
Contacts.People.CONTENT_URI, values );
228
201
Long id = ContentUris.parseId( contact_uri );
230
throw new ContactCreationException();
202
if( id == 0 ) id = null;
232
204
// try to add them to the "My Contacts" group
234
Contacts.People.addToMyContactsGroup(
235
_activity.getContentResolver(), id );
237
catch( IllegalStateException e ) {
238
// ignore any failure
207
Contacts.People.addToMyContactsGroup(
208
_activity.getContentResolver(), id );
210
catch( IllegalStateException e ) {
211
// ignore any failure
244
private int convertTypeToBackendType( Class< ? > cls, int type )
245
throws ContactCreationException
247
if( cls == Contacts.Phones.class )
251
case ContactData.TYPE_HOME:
252
return Contacts.PhonesColumns.TYPE_HOME;
253
case ContactData.TYPE_WORK:
254
return Contacts.PhonesColumns.TYPE_WORK;
255
case ContactData.TYPE_MOBILE:
256
return Contacts.PhonesColumns.TYPE_MOBILE;
257
case ContactData.TYPE_FAX_HOME:
258
return Contacts.PhonesColumns.TYPE_FAX_HOME;
259
case ContactData.TYPE_FAX_WORK:
260
return Contacts.PhonesColumns.TYPE_FAX_WORK;
261
case ContactData.TYPE_PAGER:
262
return Contacts.PhonesColumns.TYPE_PAGER;
265
else if( cls == Contacts.ContactMethods.class )
269
case ContactData.TYPE_HOME:
270
return Contacts.ContactMethodsColumns.TYPE_HOME;
271
case ContactData.TYPE_WORK:
272
return Contacts.ContactMethodsColumns.TYPE_WORK;
277
throw new ContactCreationException();
281
219
public void addContactPhone( Long id, String number,
282
ContactData.PreferredDetail data ) throws ContactCreationException
220
ContactData.PreferredDetail data )
284
222
Uri contact_phones_uri = Uri.withAppendedPath(
285
223
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
286
224
Contacts.People.Phones.CONTENT_DIRECTORY );
288
226
ContentValues values = new ContentValues();
289
values.put( Contacts.Phones.TYPE,
290
convertTypeToBackendType( Contacts.Phones.class,
227
values.put( Contacts.Phones.TYPE, data.getType() );
292
228
values.put( Contacts.Phones.NUMBER, number );
293
229
if( data.isPreferred() )
294
230
values.put( Contacts.Phones.ISPRIMARY, 1 );
307
243
ContentValues values = new ContentValues();
308
244
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
309
245
values.put( Contacts.ContactMethods.DATA, email );
310
values.put( Contacts.ContactMethods.TYPE,
311
convertTypeToBackendType( Contacts.ContactMethods.class,
246
values.put( Contacts.ContactMethods.TYPE, data.getType() );
313
247
if( data.isPreferred() )
314
248
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
328
262
ContentValues values = new ContentValues();
329
263
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
330
264
values.put( Contacts.ContactMethods.DATA, address );
331
values.put( Contacts.ContactMethods.TYPE,
332
convertTypeToBackendType( Contacts.ContactMethods.class,
265
values.put( Contacts.ContactMethods.TYPE, data.getType() );
335
267
_activity.getContentResolver().insert( contact_contact_methods_uri,
352
284
Contacts.Organizations.CONTENT_URI, values );
356
public void addContactNote( Long id, String note )
357
throws ContactCreationException
359
ContentValues values = new ContentValues();
360
values.put( Contacts.People.NOTES, note );
361
_activity.getContentResolver().update(
362
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
363
values, null, null );