1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.syncadapter
;
21 import java
.io
.FileInputStream
;
22 import java
.io
.IOException
;
24 import org
.apache
.http
.client
.methods
.HttpPut
;
25 import org
.apache
.http
.entity
.ByteArrayEntity
;
27 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
28 import com
.owncloud
.android
.authentication
.AccountUtils
;
29 import com
.owncloud
.android
.oc_framework
.accounts
.OwnCloudAccount
;
32 import android
.accounts
.Account
;
33 import android
.accounts
.AccountManager
;
34 import android
.accounts
.AuthenticatorException
;
35 import android
.accounts
.OperationCanceledException
;
36 import android
.content
.ContentProviderClient
;
37 import android
.content
.Context
;
38 import android
.content
.SyncResult
;
39 import android
.content
.res
.AssetFileDescriptor
;
40 import android
.database
.Cursor
;
41 import android
.net
.Uri
;
42 import android
.os
.Bundle
;
43 import android
.provider
.ContactsContract
;
45 public class ContactSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
46 private String mAddrBookUri
;
48 public ContactSyncAdapter(Context context
, boolean autoInitialize
) {
49 super(context
, autoInitialize
);
54 public void onPerformSync(Account account
, Bundle extras
, String authority
,
55 ContentProviderClient provider
, SyncResult syncResult
) {
57 setContentProviderClient(provider
);
58 Cursor c
= getLocalContacts(false
);
59 if (c
.moveToFirst()) {
61 String lookup
= c
.getString(c
62 .getColumnIndex(ContactsContract
.Contacts
.LOOKUP_KEY
));
63 String a
= getAddressBookUri();
64 String uri
= a
+ lookup
+ ".vcf";
67 f
= getContactVcard(lookup
);
68 HttpPut query
= new HttpPut(uri
);
69 byte[] b
= new byte[f
.available()];
71 query
.setEntity(new ByteArrayEntity(b
));
72 fireRawRequest(query
);
73 } catch (IOException e
) {
76 } catch (OperationCanceledException e
) {
77 // TODO Auto-generated catch block
79 } catch (AuthenticatorException e
) {
80 // TODO Auto-generated catch block
83 } while (c
.moveToNext());
84 // } while (c.moveToNext());
89 private String
getAddressBookUri() {
90 if (mAddrBookUri
!= null
)
93 AccountManager am
= getAccountManager();
94 @SuppressWarnings("deprecation")
95 String uri
= am
.getUserData(getAccount(),
96 OwnCloudAccount
.Constants
.KEY_OC_URL
).replace(
97 AccountUtils
.WEBDAV_PATH_2_0
, AccountUtils
.CARDDAV_PATH_2_0
);
98 uri
+= "/addressbooks/"
99 + getAccount().name
.substring(0,
100 getAccount().name
.lastIndexOf('@')) + "/default/";
105 private FileInputStream
getContactVcard(String lookupKey
)
107 Uri uri
= Uri
.withAppendedPath(
108 ContactsContract
.Contacts
.CONTENT_VCARD_URI
, lookupKey
);
109 AssetFileDescriptor fd
= getContext().getContentResolver()
110 .openAssetFileDescriptor(uri
, "r");
111 return fd
.createInputStream();
114 private Cursor
getLocalContacts(boolean include_hidden_contacts
) {
115 return getContext().getContentResolver().query(
116 ContactsContract
.Contacts
.CONTENT_URI
,
117 new String
[] { ContactsContract
.Contacts
._ID
,
118 ContactsContract
.Contacts
.LOOKUP_KEY
},
119 ContactsContract
.Contacts
.IN_VISIBLE_GROUP
+ " = ?",
120 new String
[] { (include_hidden_contacts ?
"0" : "1") },
121 ContactsContract
.Contacts
._ID
+ " DESC");