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
.authenticator
.AccountAuthenticator
;
11 import eu
.alefzero
.owncloud
.authenticator
.AuthUtils
;
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
.getColumnIndex(ContactsContract
.Contacts
.LOOKUP_KEY
));
45 String a
= getAddressBookUri();
46 String uri
= a
+ lookup
+ ".vcf";
49 f
= getContactVcard(lookup
);
50 HttpPut query
= new HttpPut(uri
);
51 byte[] b
= new byte[f
.available()];
53 query
.setEntity(new ByteArrayEntity(b
));
54 HttpResponse response
= fireRawRequest(query
);
55 } catch (IOException e
) {
58 } catch (OperationCanceledException e
) {
59 // TODO Auto-generated catch block
61 } catch (AuthenticatorException e
) {
62 // TODO Auto-generated catch block
65 }while (c
.moveToNext());
66 //} while (c.moveToNext());
71 private String
getAddressBookUri() {
72 if (mAddrBookUri
!= null
) return mAddrBookUri
;
74 AccountManager am
= getAccountManager();
75 String uri
= am
.getUserData(getAccount(), AccountAuthenticator
.KEY_OC_URL
)
76 .replace(AuthUtils
.WEBDAV_PATH_2_0
, AuthUtils
.CARDDAV_PATH_2_0
);
77 uri
+= "/addressbooks/" + getAccount().name
.substring(0, getAccount().name
.lastIndexOf('@'))
83 private FileInputStream
getContactVcard(String lookupKey
) throws IOException
{
84 Uri uri
= Uri
.withAppendedPath(ContactsContract
.Contacts
.CONTENT_VCARD_URI
, lookupKey
);
85 AssetFileDescriptor fd
= getContext().getContentResolver().openAssetFileDescriptor(uri
, "r");
86 return fd
.createInputStream();
89 private Cursor
getLocalContacts(boolean include_hidden_contacts
) {
90 return getContext().getContentResolver().query(
91 ContactsContract
.Contacts
.CONTENT_URI
,
92 new String
[] {ContactsContract
.Contacts
._ID
, ContactsContract
.Contacts
.LOOKUP_KEY
},
93 ContactsContract
.Contacts
.IN_VISIBLE_GROUP
+ " = ?",
94 new String
[]{ (include_hidden_contacts?
"0":"1")},
95 ContactsContract
.Contacts
._ID
+ " DESC");