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
;
31 import android
.accounts
.Account
;
32 import android
.accounts
.AccountManager
;
33 import android
.accounts
.AuthenticatorException
;
34 import android
.accounts
.OperationCanceledException
;
35 import android
.content
.ContentProviderClient
;
36 import android
.content
.Context
;
37 import android
.content
.SyncResult
;
38 import android
.content
.res
.AssetFileDescriptor
;
39 import android
.database
.Cursor
;
40 import android
.net
.Uri
;
41 import android
.os
.Bundle
;
42 import android
.provider
.ContactsContract
;
44 public class ContactSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
45 private String mAddrBookUri
;
47 public ContactSyncAdapter(Context context
, boolean autoInitialize
) {
48 super(context
, autoInitialize
);
53 public void onPerformSync(Account account
, Bundle extras
, String authority
,
54 ContentProviderClient provider
, SyncResult syncResult
) {
56 setContentProvider(provider
);
57 Cursor c
= getLocalContacts(false
);
58 if (c
.moveToFirst()) {
60 String lookup
= c
.getString(c
61 .getColumnIndex(ContactsContract
.Contacts
.LOOKUP_KEY
));
62 String a
= getAddressBookUri();
63 String uri
= a
+ lookup
+ ".vcf";
66 f
= getContactVcard(lookup
);
67 HttpPut query
= new HttpPut(uri
);
68 byte[] b
= new byte[f
.available()];
70 query
.setEntity(new ByteArrayEntity(b
));
71 fireRawRequest(query
);
72 } catch (IOException e
) {
75 } catch (OperationCanceledException e
) {
76 // TODO Auto-generated catch block
78 } catch (AuthenticatorException e
) {
79 // TODO Auto-generated catch block
82 } while (c
.moveToNext());
83 // } while (c.moveToNext());
88 private String
getAddressBookUri() {
89 if (mAddrBookUri
!= null
)
92 AccountManager am
= getAccountManager();
93 @SuppressWarnings("deprecation")
94 String uri
= am
.getUserData(getAccount(),
95 OwnCloudAccount
.Constants
.KEY_OC_URL
).replace(
96 AccountUtils
.WEBDAV_PATH_2_0
, AccountUtils
.CARDDAV_PATH_2_0
);
97 uri
+= "/addressbooks/"
98 + getAccount().name
.substring(0,
99 getAccount().name
.lastIndexOf('@')) + "/default/";
104 private FileInputStream
getContactVcard(String lookupKey
)
106 Uri uri
= Uri
.withAppendedPath(
107 ContactsContract
.Contacts
.CONTENT_VCARD_URI
, lookupKey
);
108 AssetFileDescriptor fd
= getContext().getContentResolver()
109 .openAssetFileDescriptor(uri
, "r");
110 return fd
.createInputStream();
113 private Cursor
getLocalContacts(boolean include_hidden_contacts
) {
114 return getContext().getContentResolver().query(
115 ContactsContract
.Contacts
.CONTENT_URI
,
116 new String
[] { ContactsContract
.Contacts
._ID
,
117 ContactsContract
.Contacts
.LOOKUP_KEY
},
118 ContactsContract
.Contacts
.IN_VISIBLE_GROUP
+ " = ?",
119 new String
[] { (include_hidden_contacts ?
"0" : "1") },
120 ContactsContract
.Contacts
._ID
+ " DESC");