d3ab06c1a4c0861a063c7166add7cc8d76e25d64
[pub/Android/ownCloud.git] / src / com / owncloud / android / syncadapter / ContactSyncAdapter.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 package com.owncloud.android.syncadapter;
22
23 import java.io.FileInputStream;
24 import java.io.IOException;
25
26 import org.apache.http.client.methods.HttpPut;
27 import org.apache.http.entity.ByteArrayEntity;
28
29 import com.owncloud.android.authentication.AccountUtils;
30 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
31
32
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;
45
46 public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
47 private String mAddrBookUri;
48
49 public ContactSyncAdapter(Context context, boolean autoInitialize) {
50 super(context, autoInitialize);
51 mAddrBookUri = null;
52 }
53
54 @Override
55 public void onPerformSync(Account account, Bundle extras, String authority,
56 ContentProviderClient provider, SyncResult syncResult) {
57 setAccount(account);
58 setContentProviderClient(provider);
59 Cursor c = getLocalContacts(false);
60 if (c.moveToFirst()) {
61 do {
62 String lookup = c.getString(c
63 .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
64 String a = getAddressBookUri();
65 String uri = a + lookup + ".vcf";
66 FileInputStream f;
67 try {
68 f = getContactVcard(lookup);
69 HttpPut query = new HttpPut(uri);
70 byte[] b = new byte[f.available()];
71 f.read(b);
72 query.setEntity(new ByteArrayEntity(b));
73 fireRawRequest(query);
74 } catch (IOException e) {
75 e.printStackTrace();
76 return;
77 } catch (OperationCanceledException e) {
78 // TODO Auto-generated catch block
79 e.printStackTrace();
80 } catch (AuthenticatorException e) {
81 // TODO Auto-generated catch block
82 e.printStackTrace();
83 }
84 } while (c.moveToNext());
85 // } while (c.moveToNext());
86 }
87
88 }
89
90 private String getAddressBookUri() {
91 if (mAddrBookUri != null)
92 return mAddrBookUri;
93
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/";
102 mAddrBookUri = uri;
103 return uri;
104 }
105
106 private FileInputStream getContactVcard(String lookupKey)
107 throws IOException {
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();
113 }
114
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");
123 }
124
125 }