1 package eu
.alefzero
.owncloud
.syncadapter
;
3 import java
.io
.FileInputStream
;
4 import java
.io
.IOException
;
6 import org
.apache
.http
.HttpResponse
;
7 import org
.apache
.http
.client
.methods
.HttpPut
;
8 import org
.apache
.http
.entity
.ByteArrayEntity
;
10 import eu
.alefzero
.owncloud
.AccountUtils
;
11 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
12 import eu
.alefzero
.owncloud
.db
.ProviderMeta
;
13 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
14 import android
.accounts
.Account
;
15 import android
.accounts
.AccountManager
;
16 import android
.accounts
.AuthenticatorException
;
17 import android
.accounts
.OperationCanceledException
;
18 import android
.content
.ContentProviderClient
;
19 import android
.content
.Context
;
20 import android
.content
.SyncResult
;
21 import android
.content
.res
.AssetFileDescriptor
;
22 import android
.database
.Cursor
;
23 import android
.net
.Uri
;
24 import android
.os
.Bundle
;
25 import android
.provider
.ContactsContract
;
26 import android
.util
.Log
;
28 public class ContactSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
29 private String mAddrBookUri
;
31 public ContactSyncAdapter(Context context
, boolean autoInitialize
) {
32 super(context
, autoInitialize
);
37 public void onPerformSync(Account account
, Bundle extras
, String authority
,
38 ContentProviderClient provider
, SyncResult syncResult
) {
40 setContentProvider(provider
);
41 Cursor c
= getLocalContacts(false
);
42 if (c
.moveToFirst()) {
44 String lookup
= c
.getString(c
45 .getColumnIndex(ContactsContract
.Contacts
.LOOKUP_KEY
));
46 String a
= getAddressBookUri();
47 String uri
= a
+ lookup
+ ".vcf";
50 f
= getContactVcard(lookup
);
51 HttpPut query
= new HttpPut(uri
);
52 byte[] b
= new byte[f
.available()];
54 query
.setEntity(new ByteArrayEntity(b
));
55 HttpResponse response
= fireRawRequest(query
);
56 } catch (IOException e
) {
59 } catch (OperationCanceledException e
) {
60 // TODO Auto-generated catch block
62 } catch (AuthenticatorException e
) {
63 // TODO Auto-generated catch block
66 } while (c
.moveToNext());
67 // } while (c.moveToNext());
72 private String
getAddressBookUri() {
73 if (mAddrBookUri
!= null
)
76 AccountManager am
= getAccountManager();
77 String uri
= am
.getUserData(getAccount(),
78 AccountAuthenticator
.KEY_OC_URL
).replace(
79 AccountUtils
.WEBDAV_PATH_2_0
, AccountUtils
.CARDDAV_PATH_2_0
);
80 uri
+= "/addressbooks/"
81 + getAccount().name
.substring(0,
82 getAccount().name
.lastIndexOf('@')) + "/default/";
87 private FileInputStream
getContactVcard(String lookupKey
)
89 Uri uri
= Uri
.withAppendedPath(
90 ContactsContract
.Contacts
.CONTENT_VCARD_URI
, lookupKey
);
91 AssetFileDescriptor fd
= getContext().getContentResolver()
92 .openAssetFileDescriptor(uri
, "r");
93 return fd
.createInputStream();
96 private Cursor
getLocalContacts(boolean include_hidden_contacts
) {
97 return getContext().getContentResolver().query(
98 ContactsContract
.Contacts
.CONTENT_URI
,
99 new String
[] { ContactsContract
.Contacts
._ID
,
100 ContactsContract
.Contacts
.LOOKUP_KEY
},
101 ContactsContract
.Contacts
.IN_VISIBLE_GROUP
+ " = ?",
102 new String
[] { (include_hidden_contacts ?
"0" : "1") },
103 ContactsContract
.Contacts
._ID
+ " DESC");