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