bzr branch
http://bzr.ed.am/android/import-contacts
62
by edam
added preliminary (buggy) ContactsContract backend |
1 |
/* |
2 |
* ContactsBackend.java |
|
3 |
* |
|
4 |
* Copyright (C) 2012 Tim Marston <tim@ed.am> |
|
5 |
* |
|
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 |
|
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 |
||
24 |
package am.ed.importcontacts; |
|
25 |
||
26 |
import java.util.HashMap; |
|
27 |
import java.util.HashSet; |
|
28 |
||
29 |
import am.ed.importcontacts.ContactsCache.CacheIdentifier; |
|
30 |
import am.ed.importcontacts.Importer.ContactData; |
|
31 |
import android.annotation.TargetApi; |
|
32 |
import android.app.Activity; |
|
33 |
import android.content.ContentUris; |
|
34 |
import android.content.ContentValues; |
|
35 |
import android.database.Cursor; |
|
36 |
import android.net.Uri; |
|
37 |
import android.provider.ContactsContract; |
|
38 |
import android.provider.ContactsContract.CommonDataKinds; |
|
39 |
||
40 |
@TargetApi(5) |
|
41 |
public class ContactsContractBackend implements Backend |
|
42 |
{ |
|
43 |
private Activity _activity = null; |
|
44 |
private HashMap< Long, Long > _aggregate_to_raw_ids = null; |
|
45 |
||
46 |
ContactsContractBackend( Activity activity ) |
|
47 |
{ |
|
48 |
_activity = activity; |
|
49 |
_aggregate_to_raw_ids = new HashMap< Long, Long >(); |
|
50 |
} |
|
51 |
||
52 |
@Override |
|
53 |
public void populateCache( ContactsCache cache ) |
|
54 |
{ |
|
55 |
Cursor cur; |
|
56 |
||
57 |
// build a set of aggregate contact ids that haven't been added to the |
|
58 |
// cache yet |
|
59 |
HashSet< Long > unadded_ids = new HashSet< Long >(); |
|
60 |
cur = _activity.managedQuery( ContactsContract.Contacts.CONTENT_URI, |
|
61 |
new String[] { |
|
62 |
ContactsContract.Contacts._ID, |
|
63 |
}, null, null, null ); |
|
64 |
while( cur.moveToNext() ) { |
|
65 |
Long id = cur.getLong( |
|
66 |
cur.getColumnIndex( ContactsContract.Contacts._ID ) ); |
|
67 |
unadded_ids.add( id ); |
|
68 |
} |
|
69 |
||
70 |
// build a mapping of the ids of raw contacts to the ids of their |
|
71 |
// aggregate contacts |
|
72 |
HashMap< Long, Long > raw_to_aggregate_ids = |
|
73 |
new HashMap< Long, Long >(); |
|
74 |
cur = _activity.managedQuery( ContactsContract.RawContacts.CONTENT_URI, |
|
75 |
new String[] { |
|
76 |
ContactsContract.RawContacts._ID, |
|
77 |
ContactsContract.RawContacts.CONTACT_ID, |
|
63
by edam
fixed selection of raw contacts in ContactsContract backend |
78 |
}, ContactsContract.RawContacts.DELETED + " = 0", null, null ); |
62
by edam
added preliminary (buggy) ContactsContract backend |
79 |
while( cur.moveToNext() ) { |
80 |
Long raw_id = cur.getLong( |
|
81 |
cur.getColumnIndex( ContactsContract.RawContacts._ID ) ); |
|
82 |
Long id = cur.getLong( |
|
83 |
cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) ); |
|
84 |
raw_to_aggregate_ids.put( raw_id, id ); |
|
85 |
} |
|
86 |
||
87 |
// get structured names, primary ones first |
|
88 |
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI, |
|
89 |
new String[] { |
|
90 |
ContactsContract.Data.RAW_CONTACT_ID, |
|
91 |
CommonDataKinds.StructuredName.DISPLAY_NAME, |
|
92 |
}, |
|
93 |
ContactsContract.Data.MIMETYPE + " = '" + |
|
94 |
CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'", |
|
95 |
null, ContactsContract.Data.IS_PRIMARY + " DESC" ); |
|
96 |
while( cur.moveToNext() ) { |
|
97 |
Long raw_id = cur.getLong( cur.getColumnIndex( |
|
98 |
ContactsContract.Data.RAW_CONTACT_ID ) ); |
|
99 |
Long id = raw_to_aggregate_ids.get( raw_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 |
100 |
if( id != null ) |
62
by edam
added preliminary (buggy) ContactsContract backend |
101 |
{ |
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 |
102 |
String name = cur.getString( cur.getColumnIndex( |
103 |
CommonDataKinds.StructuredName.DISPLAY_NAME ) ); |
|
104 |
||
62
by edam
added preliminary (buggy) ContactsContract backend |
105 |
// if this is a name for a contact for whom we have not added a |
106 |
// lookup, add a lookup for the contact id by name |
|
107 |
if( unadded_ids.contains( 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 |
108 |
CacheIdentifier cache_identifier = CacheIdentifier.factory( |
109 |
CacheIdentifier.Type.NAME, name ); |
|
110 |
if( cache_identifier != null ) { |
|
111 |
cache.addLookup( cache_identifier, id ); |
|
112 |
unadded_ids.remove( id ); |
|
113 |
} |
|
62
by edam
added preliminary (buggy) ContactsContract backend |
114 |
} |
115 |
} |
|
116 |
} |
|
117 |
||
118 |
// get contact organisations, primary ones first |
|
119 |
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI, |
|
120 |
new String[] { |
|
121 |
ContactsContract.Data.RAW_CONTACT_ID, |
|
122 |
CommonDataKinds.Organization.COMPANY, |
|
123 |
}, |
|
124 |
ContactsContract.Data.MIMETYPE + " = '" + |
|
125 |
CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'", |
|
126 |
null, ContactsContract.Data.IS_PRIMARY + " DESC" ); |
|
127 |
while( cur.moveToNext() ) { |
|
128 |
Long raw_id = cur.getLong( cur.getColumnIndex( |
|
129 |
ContactsContract.Data.RAW_CONTACT_ID ) ); |
|
130 |
Long id = raw_to_aggregate_ids.get( raw_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 |
131 |
if( id != null ) |
62
by edam
added preliminary (buggy) ContactsContract backend |
132 |
{ |
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 |
133 |
String organisation = cur.getString( cur.getColumnIndex( |
134 |
CommonDataKinds.Organization.COMPANY ) ); |
|
135 |
||
62
by edam
added preliminary (buggy) ContactsContract backend |
136 |
// if this is an organisation name for a contact for whom we |
137 |
// have not added a lookup, add a lookup for the contact id |
|
138 |
// by organisation |
|
139 |
if( unadded_ids.contains( 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 |
140 |
CacheIdentifier cache_identifier = CacheIdentifier.factory( |
141 |
CacheIdentifier.Type.ORGANISATION, organisation ); |
|
142 |
if( cache_identifier != null ) { |
|
143 |
cache.addLookup( cache_identifier, id ); |
|
144 |
unadded_ids.remove( id ); |
|
145 |
} |
|
62
by edam
added preliminary (buggy) ContactsContract backend |
146 |
} |
147 |
||
148 |
// add associated data |
|
149 |
cache.addAssociatedOrganisation( id, organisation ); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
// get all phone numbers, primary ones first |
|
154 |
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI, |
|
155 |
new String[] { |
|
156 |
ContactsContract.Data.RAW_CONTACT_ID, |
|
157 |
CommonDataKinds.Phone.NUMBER, |
|
158 |
}, |
|
159 |
ContactsContract.Data.MIMETYPE + " = '" + |
|
160 |
CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", |
|
161 |
null, ContactsContract.Data.IS_PRIMARY + " DESC" ); |
|
162 |
while( cur.moveToNext() ) { |
|
163 |
Long raw_id = cur.getLong( cur.getColumnIndex( |
|
164 |
ContactsContract.Data.RAW_CONTACT_ID ) ); |
|
165 |
Long id = raw_to_aggregate_ids.get( raw_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 |
166 |
if( id != null ) |
62
by edam
added preliminary (buggy) ContactsContract backend |
167 |
{ |
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 |
168 |
String number = cur.getString( cur.getColumnIndex( |
169 |
CommonDataKinds.Phone.NUMBER ) ); |
|
170 |
||
62
by edam
added preliminary (buggy) ContactsContract backend |
171 |
// if this is a number for a contact for whom we have not |
172 |
// added a lookup, add a lookup for the contact id by phone |
|
173 |
// number |
|
174 |
if( unadded_ids.contains( 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 |
175 |
CacheIdentifier cache_identifier = CacheIdentifier.factory( |
176 |
CacheIdentifier.Type.PRIMARY_NUMBER, number ); |
|
177 |
if( cache_identifier != null ) { |
|
178 |
cache.addLookup( cache_identifier, id ); |
|
179 |
unadded_ids.remove( id ); |
|
180 |
} |
|
62
by edam
added preliminary (buggy) ContactsContract backend |
181 |
} |
182 |
||
183 |
// add associated data |
|
184 |
cache.addAssociatedNumber( id, number ); |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
// get all email addresses, primary ones first |
|
189 |
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI, |
|
190 |
new String[] { |
|
191 |
ContactsContract.Data.RAW_CONTACT_ID, |
|
192 |
CommonDataKinds.Email.DATA, |
|
193 |
}, |
|
194 |
ContactsContract.Data.MIMETYPE + " = '" + |
|
195 |
CommonDataKinds.Email.CONTENT_ITEM_TYPE + "'", |
|
196 |
null, ContactsContract.Data.IS_PRIMARY + " DESC" ); |
|
197 |
while( cur.moveToNext() ) { |
|
198 |
Long raw_id = cur.getLong( cur.getColumnIndex( |
|
199 |
ContactsContract.Data.RAW_CONTACT_ID ) ); |
|
200 |
Long id = raw_to_aggregate_ids.get( raw_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 |
201 |
if( id != null ) |
62
by edam
added preliminary (buggy) ContactsContract backend |
202 |
{ |
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 |
203 |
String email = cur.getString( cur.getColumnIndex( |
204 |
CommonDataKinds.Email.DATA ) ); |
|
205 |
||
62
by edam
added preliminary (buggy) ContactsContract backend |
206 |
// if this is an email address for a contact for whom we have |
207 |
// not added a lookup, add a lookup for the contact id by email |
|
208 |
// address |
|
209 |
if( unadded_ids.contains( 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 |
210 |
CacheIdentifier cache_identifier = CacheIdentifier.factory( |
211 |
CacheIdentifier.Type.PRIMARY_EMAIL, email ); |
|
212 |
if( cache_identifier != null ) { |
|
213 |
cache.addLookup( cache_identifier, id ); |
|
214 |
unadded_ids.remove( id ); |
|
215 |
} |
|
62
by edam
added preliminary (buggy) ContactsContract backend |
216 |
} |
217 |
||
218 |
// add associated data |
|
219 |
cache.addAssociatedEmail( id, email ); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
// get all postal addresses, primary ones first |
|
224 |
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI, |
|
225 |
new String[] { |
|
226 |
ContactsContract.Data.RAW_CONTACT_ID, |
|
227 |
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, |
|
228 |
}, |
|
229 |
ContactsContract.Data.MIMETYPE + " = '" + |
|
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 |
230 |
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'", |
62
by edam
added preliminary (buggy) ContactsContract backend |
231 |
null, ContactsContract.Data.IS_PRIMARY + " DESC" ); |
232 |
while( cur.moveToNext() ) { |
|
233 |
Long raw_id = cur.getLong( cur.getColumnIndex( |
|
234 |
ContactsContract.Data.RAW_CONTACT_ID ) ); |
|
235 |
Long id = raw_to_aggregate_ids.get( raw_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 |
236 |
if( id != null ) |
62
by edam
added preliminary (buggy) ContactsContract backend |
237 |
{ |
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 |
238 |
String address = cur.getString( cur.getColumnIndex( |
239 |
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ); |
|
240 |
||
62
by edam
added preliminary (buggy) ContactsContract backend |
241 |
// add associated data |
242 |
cache.addAssociatedAddress( id, address ); |
|
243 |
} |
|
244 |
} |
|
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 |
|
246 |
// get all notes |
|
247 |
cur = _activity.managedQuery( ContactsContract.Data.CONTENT_URI, |
|
248 |
new String[] { |
|
249 |
ContactsContract.Data.RAW_CONTACT_ID, |
|
250 |
CommonDataKinds.Note.NOTE, |
|
251 |
}, |
|
252 |
ContactsContract.Data.MIMETYPE + " = '" + |
|
253 |
CommonDataKinds.Note.CONTENT_ITEM_TYPE + "'", |
|
254 |
null, null ); |
|
255 |
while( cur.moveToNext() ) { |
|
256 |
Long raw_id = cur.getLong( cur.getColumnIndex( |
|
257 |
ContactsContract.Data.RAW_CONTACT_ID ) ); |
|
258 |
Long id = raw_to_aggregate_ids.get( raw_id ); |
|
259 |
if( id != null ) |
|
260 |
{ |
|
261 |
String note = cur.getString( cur.getColumnIndex( |
|
262 |
CommonDataKinds.Note.NOTE ) ); |
|
263 |
||
264 |
// add associated data |
|
265 |
cache.addAssociatedNote( id, note ); |
|
266 |
} |
|
267 |
} |
|
62
by edam
added preliminary (buggy) ContactsContract backend |
268 |
} |
269 |
||
270 |
@Override |
|
271 |
public void deleteContact( Long id ) |
|
272 |
{ |
|
273 |
Uri contact_uri = ContentUris.withAppendedId( |
|
274 |
ContactsContract.Contacts.CONTENT_URI, id ); |
|
275 |
_activity.getContentResolver().delete( contact_uri, null, null ); |
|
276 |
} |
|
277 |
||
278 |
@Override |
|
279 |
public Long addContact( String name ) throws ContactCreationException |
|
280 |
{ |
|
281 |
// create raw contact |
|
282 |
ContentValues values = new ContentValues(); |
|
283 |
Uri contact_uri = _activity.getContentResolver().insert( |
|
284 |
ContactsContract.RawContacts.CONTENT_URI, values); |
|
285 |
Long raw_id = ContentUris.parseId( contact_uri ); |
|
286 |
if( raw_id == 0 ) throw new ContactCreationException(); |
|
287 |
||
288 |
// add name data for this raw contact |
|
289 |
if( name != null ) { |
|
290 |
values.put( ContactsContract.Data.RAW_CONTACT_ID, raw_id ); |
|
291 |
values.put( ContactsContract.Data.MIMETYPE, |
|
292 |
CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE ); |
|
293 |
values.put( CommonDataKinds.StructuredName.DISPLAY_NAME, name ); |
|
294 |
_activity.getContentResolver().insert( |
|
295 |
ContactsContract.Data.CONTENT_URI, values ); |
|
296 |
} |
|
297 |
||
298 |
// find corresponding aggregate contact |
|
299 |
contact_uri = Uri.withAppendedPath( |
|
300 |
ContentUris.withAppendedId( |
|
301 |
ContactsContract.RawContacts.CONTENT_URI, raw_id ), |
|
302 |
ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY ); |
|
303 |
Cursor cur = _activity.managedQuery( contact_uri, |
|
304 |
new String[] { |
|
305 |
ContactsContract.RawContacts.CONTACT_ID, |
|
306 |
}, null, null, null ); |
|
307 |
Long id = null; |
|
308 |
if( cur.moveToNext() ) |
|
309 |
id = cur.getLong( |
|
310 |
cur.getColumnIndex( ContactsContract.RawContacts.CONTACT_ID ) ); |
|
311 |
if( id == null || id == 0 ) |
|
312 |
{ |
|
313 |
// we didn't find an aggregate contact id, so try to clean up (by |
|
314 |
// deleting the raw contact we just created) before bailing |
|
315 |
contact_uri = ContentUris.withAppendedId( |
|
316 |
ContactsContract.RawContacts.CONTENT_URI, id ); |
|
317 |
_activity.getContentResolver().delete( contact_uri, null, null ); |
|
318 |
||
319 |
throw new ContactCreationException(); |
|
320 |
} |
|
321 |
||
322 |
return id; |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* Obtain the raw contact id for the phone-only raw contact that is |
|
327 |
* associated with the aggregate contact id. One will be created if |
|
328 |
* necessary. |
|
329 |
* @param id the aggregate contact id |
|
330 |
* @return the raw contact id |
|
331 |
* @throws ContactCreationException |
|
332 |
*/ |
|
333 |
Long obtainRawContact( Long id ) throws ContactCreationException |
|
334 |
{ |
|
335 |
// attempt to lookup cached value |
|
336 |
Long raw_id = _aggregate_to_raw_ids.get( id ); |
|
337 |
if( raw_id != null ) return raw_id; |
|
338 |
||
339 |
// find a corresponding raw contact that has no account name/type |
|
340 |
Cursor cur = _activity.managedQuery( |
|
341 |
ContactsContract.RawContacts.CONTENT_URI, |
|
342 |
new String[] { |
|
343 |
ContactsContract.RawContacts._ID, |
|
63
by edam
fixed selection of raw contacts in ContactsContract backend |
344 |
ContactsContract.RawContacts.ACCOUNT_NAME, |
62
by edam
added preliminary (buggy) ContactsContract backend |
345 |
}, |
63
by edam
fixed selection of raw contacts in ContactsContract backend |
346 |
ContactsContract.RawContacts.DELETED + " = 0 AND " + |
347 |
ContactsContract.RawContacts.CONTACT_ID + " = ? AND " + |
|
62
by edam
added preliminary (buggy) ContactsContract backend |
348 |
"IFNULL( " + ContactsContract.RawContacts.ACCOUNT_NAME + |
349 |
", '' ) = '' AND " + |
|
350 |
"IFNULL( " + ContactsContract.RawContacts.ACCOUNT_TYPE + |
|
351 |
", '' ) = ''", |
|
63
by edam
fixed selection of raw contacts in ContactsContract backend |
352 |
new String[] { |
353 |
String.valueOf( id ), |
|
354 |
}, null ); |
|
62
by edam
added preliminary (buggy) ContactsContract backend |
355 |
if( cur.moveToNext() ) |
356 |
raw_id = cur.getLong( |
|
357 |
cur.getColumnIndex( ContactsContract.RawContacts._ID ) ); |
|
358 |
||
359 |
// if one wasn't found, we'll need to create one |
|
360 |
if( raw_id == null ) { |
|
361 |
ContentValues values = new ContentValues(); |
|
362 |
Uri contact_uri = _activity.getContentResolver().insert( |
|
363 |
ContactsContract.RawContacts.CONTENT_URI, values); |
|
364 |
raw_id = ContentUris.parseId( contact_uri ); |
|
365 |
if( raw_id == 0 ) throw new ContactCreationException(); |
|
366 |
} |
|
367 |
||
368 |
// save value in our cache |
|
369 |
_aggregate_to_raw_ids.put( id, raw_id ); |
|
370 |
return raw_id; |
|
371 |
} |
|
372 |
||
373 |
private int convertTypeToBackendType( Class< ? > cls, int type ) |
|
374 |
throws ContactCreationException |
|
375 |
{ |
|
376 |
if( cls == CommonDataKinds.Phone.class ) |
|
377 |
{ |
|
378 |
switch( type ) |
|
379 |
{ |
|
380 |
case ContactData.TYPE_HOME: |
|
381 |
return CommonDataKinds.Phone.TYPE_HOME; |
|
382 |
case ContactData.TYPE_WORK: |
|
383 |
return CommonDataKinds.Phone.TYPE_WORK; |
|
384 |
case ContactData.TYPE_MOBILE: |
|
385 |
return CommonDataKinds.Phone.TYPE_MOBILE; |
|
386 |
case ContactData.TYPE_FAX_HOME: |
|
387 |
return CommonDataKinds.Phone.TYPE_FAX_HOME; |
|
388 |
case ContactData.TYPE_FAX_WORK: |
|
389 |
return CommonDataKinds.Phone.TYPE_FAX_WORK; |
|
390 |
case ContactData.TYPE_PAGER: |
|
391 |
return CommonDataKinds.Phone.TYPE_PAGER; |
|
392 |
} |
|
393 |
} |
|
394 |
else if( cls == CommonDataKinds.Email.class ) |
|
395 |
{ |
|
396 |
switch( type ) |
|
397 |
{ |
|
398 |
case ContactData.TYPE_HOME: |
|
399 |
return CommonDataKinds.Email.TYPE_HOME; |
|
400 |
case ContactData.TYPE_WORK: |
|
401 |
return CommonDataKinds.Email.TYPE_WORK; |
|
402 |
} |
|
403 |
} |
|
404 |
else if( cls == CommonDataKinds.StructuredPostal.class ) |
|
405 |
{ |
|
406 |
switch( type ) |
|
407 |
{ |
|
408 |
case ContactData.TYPE_HOME: |
|
409 |
return CommonDataKinds.StructuredPostal.TYPE_HOME; |
|
410 |
case ContactData.TYPE_WORK: |
|
411 |
return CommonDataKinds.StructuredPostal.TYPE_WORK; |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
// still here? |
|
416 |
throw new ContactCreationException(); |
|
417 |
} |
|
418 |
||
419 |
@Override |
|
420 |
public void addContactPhone( Long id, String number, |
|
421 |
ContactData.PreferredDetail data ) throws ContactCreationException |
|
422 |
{ |
|
423 |
ContentValues values = new ContentValues(); |
|
424 |
values.put( ContactsContract.Data.RAW_CONTACT_ID, |
|
425 |
obtainRawContact( id ) ); |
|
426 |
values.put( ContactsContract.Data.MIMETYPE, |
|
427 |
CommonDataKinds.Phone.CONTENT_ITEM_TYPE ); |
|
428 |
values.put( CommonDataKinds.Phone.TYPE, |
|
429 |
convertTypeToBackendType( CommonDataKinds.Phone.class, |
|
430 |
data.getType() ) ); |
|
431 |
values.put( CommonDataKinds.Phone.NUMBER, number ); |
|
432 |
if( data.isPreferred() ) |
|
433 |
values.put( CommonDataKinds.Phone.IS_PRIMARY, 1 ); |
|
434 |
||
435 |
_activity.getContentResolver().insert( |
|
436 |
ContactsContract.Data.CONTENT_URI, values ); |
|
437 |
} |
|
438 |
||
439 |
@Override |
|
440 |
public void addContactEmail( Long id, String email, |
|
441 |
ContactData.PreferredDetail data ) throws ContactCreationException |
|
442 |
{ |
|
443 |
ContentValues values = new ContentValues(); |
|
444 |
values.put( ContactsContract.Data.RAW_CONTACT_ID, |
|
445 |
obtainRawContact( id ) ); |
|
446 |
values.put( ContactsContract.Data.MIMETYPE, |
|
447 |
CommonDataKinds.Email.CONTENT_ITEM_TYPE ); |
|
448 |
values.put( CommonDataKinds.Email.TYPE, |
|
449 |
convertTypeToBackendType( CommonDataKinds.Email.class, |
|
450 |
data.getType() ) ); |
|
451 |
values.put( CommonDataKinds.Email.DATA, email ); |
|
452 |
if( data.isPreferred() ) |
|
453 |
values.put( CommonDataKinds.Email.IS_PRIMARY, 1 ); |
|
454 |
||
455 |
_activity.getContentResolver().insert( |
|
456 |
ContactsContract.Data.CONTENT_URI, values ); |
|
457 |
} |
|
458 |
||
459 |
@Override |
|
460 |
public void addContactAddresses( Long id, String address, |
|
461 |
ContactData.TypeDetail data ) throws ContactCreationException |
|
462 |
{ |
|
463 |
ContentValues values = new ContentValues(); |
|
464 |
values.put( ContactsContract.Data.RAW_CONTACT_ID, |
|
465 |
obtainRawContact( id ) ); |
|
466 |
values.put( ContactsContract.Data.MIMETYPE, |
|
467 |
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ); |
|
468 |
values.put( CommonDataKinds.StructuredPostal.TYPE, |
|
469 |
convertTypeToBackendType( CommonDataKinds.StructuredPostal.class, |
|
470 |
data.getType() ) ); |
|
471 |
values.put( |
|
472 |
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, address ); |
|
473 |
||
474 |
_activity.getContentResolver().insert( |
|
475 |
ContactsContract.Data.CONTENT_URI, values ); |
|
476 |
} |
|
477 |
||
478 |
@Override |
|
479 |
public void addContactOrganisation( Long id, String organisation, |
|
480 |
ContactData.ExtraDetail data ) throws ContactCreationException |
|
481 |
{ |
|
482 |
ContentValues values = new ContentValues(); |
|
483 |
values.put( ContactsContract.Data.RAW_CONTACT_ID, |
|
484 |
obtainRawContact( id ) ); |
|
485 |
values.put( ContactsContract.Data.MIMETYPE, |
|
486 |
CommonDataKinds.Organization.CONTENT_ITEM_TYPE ); |
|
487 |
values.put( CommonDataKinds.Organization.TYPE, |
|
488 |
CommonDataKinds.Organization.TYPE_WORK ); |
|
489 |
values.put( |
|
490 |
CommonDataKinds.Organization.COMPANY, organisation ); |
|
491 |
if( data.getExtra() != null ) |
|
492 |
values.put( CommonDataKinds.Organization.TITLE, data.getExtra() ); |
|
493 |
||
494 |
_activity.getContentResolver().insert( |
|
495 |
ContactsContract.Data.CONTENT_URI, values ); |
|
496 |
} |
|
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 |
497 |
|
498 |
@Override |
|
499 |
public void addContactNote( Long id, String note ) |
|
500 |
throws ContactCreationException |
|
501 |
{ |
|
502 |
ContentValues values = new ContentValues(); |
|
503 |
values.put( ContactsContract.Data.RAW_CONTACT_ID, |
|
504 |
obtainRawContact( id ) ); |
|
505 |
values.put( ContactsContract.Data.MIMETYPE, |
|
506 |
CommonDataKinds.Note.CONTENT_ITEM_TYPE ); |
|
507 |
values.put( |
|
508 |
CommonDataKinds.Note.NOTE, note ); |
|
509 |
||
510 |
_activity.getContentResolver().insert( |
|
511 |
ContactsContract.Data.CONTENT_URI, values ); |
|
512 |
} |
|
513 |
||
62
by edam
added preliminary (buggy) ContactsContract backend |
514 |
} |