4
* Copyright (C) 2012 Tim Marston <tim@ed.am>
6
* This file is part of the Import Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/android/import-contacts
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.
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.
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/>.
24
package am.ed.importcontacts;
26
import java.util.HashSet;
28
import am.ed.importcontacts.ContactsCache.CacheIdentifier;
29
import am.ed.importcontacts.Importer.ContactData;
30
import android.app.Activity;
31
import android.content.ContentUris;
32
import android.content.ContentValues;
33
import android.database.Cursor;
34
import android.net.Uri;
35
import android.provider.Contacts;
38
public class ContactsBackend implements Backend
40
private Activity _activity = null;
42
ContactsBackend( Activity activity )
48
public void populateCache( ContactsCache cache )
52
// set of contact ids that we have not yet added
53
HashSet< Long > unadded = new HashSet< Long >();
56
cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
60
}, null, null, null );
61
while( cur.moveToNext() ) {
62
Long id = cur.getLong(
63
cur.getColumnIndex( Contacts.People._ID ) );
65
ContactsCache.normaliseName( cur.getString(
66
cur.getColumnIndex( Contacts.People.NAME ) ) );
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 );
77
// record that a lookup for this contact's id still needs to be
78
// added by some other means
82
// get contact organisations, primary ones first
83
cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
85
Contacts.Phones.PERSON_ID,
86
Contacts.Organizations.COMPANY,
87
}, null, null, Contacts.Organizations.ISPRIMARY + " DESC" );
88
while( cur.moveToNext() ) {
89
Long id = cur.getLong( cur.getColumnIndex(
90
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.contains( id ) ) {
100
cache.addLookup( new CacheIdentifier(
101
CacheIdentifier.Type.ORGANISATION, organisation ), id );
102
unadded.remove( id );
105
// add associated data
106
cache.addAssociatedOrganisation( id, organisation );
110
// get all phone numbers, primary ones first
111
cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
113
Contacts.Phones.PERSON_ID,
114
Contacts.Phones.NUMBER,
115
}, null, null, Contacts.Phones.ISPRIMARY + " DESC" );
116
while( cur.moveToNext() ) {
117
Long id = cur.getLong(
118
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.contains( id ) ) {
128
cache.addLookup( new CacheIdentifier(
129
CacheIdentifier.Type.PRIMARY_NUMBER, number ), id );
130
unadded.remove( id );
133
// add associated data
134
cache.addAssociatedNumber( id, number );
138
// now get all email addresses, primary ones first, and postal addresses
139
cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
141
Contacts.ContactMethods.PERSON_ID,
142
Contacts.ContactMethods.DATA,
143
Contacts.ContactMethods.KIND,
144
}, Contacts.ContactMethods.KIND + " IN( ?, ? )", new String[] {
145
"" + Contacts.KIND_EMAIL,
146
"" + Contacts.KIND_POSTAL,
147
}, Contacts.ContactMethods.ISPRIMARY + " DESC" );
148
while( cur.moveToNext() ) {
149
Long id = cur.getLong(
150
cur.getColumnIndex( Contacts.ContactMethods.PERSON_ID ) );
151
int kind = cur.getInt(
152
cur.getColumnIndex( Contacts.ContactMethods.KIND ) );
153
if( kind == Contacts.KIND_EMAIL )
156
ContactsCache.normaliseEmailAddress( cur.getString(
157
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
160
// if this is an email address for a contact for whom we
161
// have not added a lookup, add a lookup for the contact
162
// id by email address
163
if( unadded.contains( id ) ) {
164
cache.addLookup( new CacheIdentifier(
165
CacheIdentifier.Type.PRIMARY_EMAIL, email ), id );
166
unadded.remove( id );
169
// add associated data
170
cache.addAssociatedEmail( id, email );
173
else if( kind == Contacts.KIND_POSTAL )
176
ContactsCache.normaliseAddress( cur.getString(
177
cur.getColumnIndex( Contacts.ContactMethods.DATA ) ) );
178
if( address != null )
180
// add associated data
181
cache.addAssociatedAddress( id, address );
188
public void deleteContact( Long id )
191
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id );
192
_activity.getContentResolver().delete( contact_uri, null, null );
196
public Long addContact( String name )
198
ContentValues values = new ContentValues();
199
values.put( Contacts.People.NAME, name );
200
Uri contact_uri = _activity.getContentResolver().insert(
201
Contacts.People.CONTENT_URI, values );
202
Long id = ContentUris.parseId( contact_uri );
204
// try to add them to the "My Contacts" group
205
if( id != null && id > 0 ) {
207
Contacts.People.addToMyContactsGroup(
208
_activity.getContentResolver(), id );
210
catch( IllegalStateException e ) {
211
// ignore any failure
215
return id == null? 0 : id;
219
public void addContactPhone( Long id, String number,
220
ContactData.PreferredDetail data )
222
Uri contact_phones_uri = Uri.withAppendedPath(
223
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
224
Contacts.People.Phones.CONTENT_DIRECTORY );
226
ContentValues values = new ContentValues();
227
values.put( Contacts.Phones.TYPE, data.getType() );
228
values.put( Contacts.Phones.NUMBER, number );
229
if( data.isPreferred() )
230
values.put( Contacts.Phones.ISPRIMARY, 1 );
232
_activity.getContentResolver().insert( contact_phones_uri, values );
236
public void addContactEmail( Long id, String email,
237
ContactData.PreferredDetail data )
239
Uri contact_contact_methods_uri = Uri.withAppendedPath(
240
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
241
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
243
ContentValues values = new ContentValues();
244
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
245
values.put( Contacts.ContactMethods.DATA, email );
246
values.put( Contacts.ContactMethods.TYPE, data.getType() );
247
if( data.isPreferred() )
248
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
250
_activity.getContentResolver().insert( contact_contact_methods_uri,
255
public void addContactAddresses( Long id, String address,
256
ContactData.TypeDetail data )
258
Uri contact_contact_methods_uri = Uri.withAppendedPath(
259
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
260
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
262
ContentValues values = new ContentValues();
263
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
264
values.put( Contacts.ContactMethods.DATA, address );
265
values.put( Contacts.ContactMethods.TYPE, data.getType() );
267
_activity.getContentResolver().insert( contact_contact_methods_uri,
272
public void addContactOrganisation( Long id, String organisation,
273
ContactData.ExtraDetail data )
275
ContentValues values = new ContentValues();
276
values.put( Contacts.Organizations.PERSON_ID, id );
277
values.put( Contacts.Organizations.COMPANY, organisation );
278
values.put( Contacts.ContactMethods.TYPE,
279
Contacts.OrganizationColumns.TYPE_WORK );
280
if( data.getExtra() != null )
281
values.put( Contacts.Organizations.TITLE, data.getExtra() );
283
_activity.getContentResolver().insert(
284
Contacts.Organizations.CONTENT_URI, values );