1 package com
.owncloud
.android
.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 com
.owncloud
.android
.AccountUtils
;
11 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
12 import com
.owncloud
.android
.db
.ProviderMeta
;
13 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
15 import android
.accounts
.Account
;
16 import android
.accounts
.AccountManager
;
17 import android
.accounts
.AuthenticatorException
;
18 import android
.accounts
.OperationCanceledException
;
19 import android
.content
.ContentProviderClient
;
20 import android
.content
.Context
;
21 import android
.content
.SyncResult
;
22 import android
.content
.res
.AssetFileDescriptor
;
23 import android
.database
.Cursor
;
24 import android
.net
.Uri
;
25 import android
.os
.Bundle
;
26 import android
.provider
.ContactsContract
;
27 import android
.util
.Log
;
29 public class ContactSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
30 private String mAddrBookUri
;
32 public ContactSyncAdapter(Context context
, boolean autoInitialize
) {
33 super(context
, autoInitialize
);
38 public void onPerformSync(Account account
, Bundle extras
, String authority
,
39 ContentProviderClient provider
, SyncResult syncResult
) {
41 setContentProvider(provider
);
42 Cursor c
= getLocalContacts(false
);
43 if (c
.moveToFirst()) {
45 String lookup
= c
.getString(c
46 .getColumnIndex(ContactsContract
.Contacts
.LOOKUP_KEY
));
47 String a
= getAddressBookUri();
48 String uri
= a
+ lookup
+ ".vcf";
51 f
= getContactVcard(lookup
);
52 HttpPut query
= new HttpPut(uri
);
53 byte[] b
= new byte[f
.available()];
55 query
.setEntity(new ByteArrayEntity(b
));
56 HttpResponse response
= fireRawRequest(query
);
57 } catch (IOException e
) {
60 } catch (OperationCanceledException e
) {
61 // TODO Auto-generated catch block
63 } catch (AuthenticatorException e
) {
64 // TODO Auto-generated catch block
67 } while (c
.moveToNext());
68 // } while (c.moveToNext());
73 private String
getAddressBookUri() {
74 if (mAddrBookUri
!= null
)
77 AccountManager am
= getAccountManager();
78 String uri
= am
.getUserData(getAccount(),
79 AccountAuthenticator
.KEY_OC_URL
).replace(
80 AccountUtils
.WEBDAV_PATH_2_0
, AccountUtils
.CARDDAV_PATH_2_0
);
81 uri
+= "/addressbooks/"
82 + getAccount().name
.substring(0,
83 getAccount().name
.lastIndexOf('@')) + "/default/";
88 private FileInputStream
getContactVcard(String lookupKey
)
90 Uri uri
= Uri
.withAppendedPath(
91 ContactsContract
.Contacts
.CONTENT_VCARD_URI
, lookupKey
);
92 AssetFileDescriptor fd
= getContext().getContentResolver()
93 .openAssetFileDescriptor(uri
, "r");
94 return fd
.createInputStream();
97 private Cursor
getLocalContacts(boolean include_hidden_contacts
) {
98 return getContext().getContentResolver().query(
99 ContactsContract
.Contacts
.CONTENT_URI
,
100 new String
[] { ContactsContract
.Contacts
._ID
,
101 ContactsContract
.Contacts
.LOOKUP_KEY
},
102 ContactsContract
.Contacts
.IN_VISIBLE_GROUP
+ " = ?",
103 new String
[] { (include_hidden_contacts ?
"0" : "1") },
104 ContactsContract
.Contacts
._ID
+ " DESC");