1 package com
.owncloud
.android
.syncadapter
;
3 import java
.io
.FileInputStream
;
4 import java
.io
.IOException
;
6 import org
.apache
.http
.client
.methods
.HttpPut
;
7 import org
.apache
.http
.entity
.ByteArrayEntity
;
9 import com
.owncloud
.android
.AccountUtils
;
10 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
12 import android
.accounts
.Account
;
13 import android
.accounts
.AccountManager
;
14 import android
.accounts
.AuthenticatorException
;
15 import android
.accounts
.OperationCanceledException
;
16 import android
.content
.ContentProviderClient
;
17 import android
.content
.Context
;
18 import android
.content
.SyncResult
;
19 import android
.content
.res
.AssetFileDescriptor
;
20 import android
.database
.Cursor
;
21 import android
.net
.Uri
;
22 import android
.os
.Bundle
;
23 import android
.provider
.ContactsContract
;
25 public class ContactSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
26 private String mAddrBookUri
;
28 public ContactSyncAdapter(Context context
, boolean autoInitialize
) {
29 super(context
, autoInitialize
);
34 public void onPerformSync(Account account
, Bundle extras
, String authority
,
35 ContentProviderClient provider
, SyncResult syncResult
) {
37 setContentProvider(provider
);
38 Cursor c
= getLocalContacts(false
);
39 if (c
.moveToFirst()) {
41 String lookup
= c
.getString(c
42 .getColumnIndex(ContactsContract
.Contacts
.LOOKUP_KEY
));
43 String a
= getAddressBookUri();
44 String uri
= a
+ lookup
+ ".vcf";
47 f
= getContactVcard(lookup
);
48 HttpPut query
= new HttpPut(uri
);
49 byte[] b
= new byte[f
.available()];
51 query
.setEntity(new ByteArrayEntity(b
));
52 fireRawRequest(query
);
53 } catch (IOException e
) {
56 } catch (OperationCanceledException e
) {
57 // TODO Auto-generated catch block
59 } catch (AuthenticatorException e
) {
60 // TODO Auto-generated catch block
63 } while (c
.moveToNext());
64 // } while (c.moveToNext());
69 private String
getAddressBookUri() {
70 if (mAddrBookUri
!= null
)
73 AccountManager am
= getAccountManager();
74 @SuppressWarnings("deprecation")
75 String uri
= am
.getUserData(getAccount(),
76 AccountAuthenticator
.KEY_OC_URL
).replace(
77 AccountUtils
.WEBDAV_PATH_2_0
, AccountUtils
.CARDDAV_PATH_2_0
);
78 uri
+= "/addressbooks/"
79 + getAccount().name
.substring(0,
80 getAccount().name
.lastIndexOf('@')) + "/default/";
85 private FileInputStream
getContactVcard(String lookupKey
)
87 Uri uri
= Uri
.withAppendedPath(
88 ContactsContract
.Contacts
.CONTENT_VCARD_URI
, lookupKey
);
89 AssetFileDescriptor fd
= getContext().getContentResolver()
90 .openAssetFileDescriptor(uri
, "r");
91 return fd
.createInputStream();
94 private Cursor
getLocalContacts(boolean include_hidden_contacts
) {
95 return getContext().getContentResolver().query(
96 ContactsContract
.Contacts
.CONTENT_URI
,
97 new String
[] { ContactsContract
.Contacts
._ID
,
98 ContactsContract
.Contacts
.LOOKUP_KEY
},
99 ContactsContract
.Contacts
.IN_VISIBLE_GROUP
+ " = ?",
100 new String
[] { (include_hidden_contacts ?
"0" : "1") },
101 ContactsContract
.Contacts
._ID
+ " DESC");