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