Fix, creation of subdirectories
[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
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;
44
45 public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
46 private String mAddrBookUri;
47
48 public ContactSyncAdapter(Context context, boolean autoInitialize) {
49 super(context, autoInitialize);
50 mAddrBookUri = null;
51 }
52
53 @Override
54 public void onPerformSync(Account account, Bundle extras, String authority,
55 ContentProviderClient provider, SyncResult syncResult) {
56 setAccount(account);
57 setContentProviderClient(provider);
58 Cursor c = getLocalContacts(false);
59 if (c.moveToFirst()) {
60 do {
61 String lookup = c.getString(c
62 .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
63 String a = getAddressBookUri();
64 String uri = a + lookup + ".vcf";
65 FileInputStream f;
66 try {
67 f = getContactVcard(lookup);
68 HttpPut query = new HttpPut(uri);
69 byte[] b = new byte[f.available()];
70 f.read(b);
71 query.setEntity(new ByteArrayEntity(b));
72 fireRawRequest(query);
73 } catch (IOException e) {
74 e.printStackTrace();
75 return;
76 } catch (OperationCanceledException e) {
77 // TODO Auto-generated catch block
78 e.printStackTrace();
79 } catch (AuthenticatorException e) {
80 // TODO Auto-generated catch block
81 e.printStackTrace();
82 }
83 } while (c.moveToNext());
84 // } while (c.moveToNext());
85 }
86
87 }
88
89 private String getAddressBookUri() {
90 if (mAddrBookUri != null)
91 return mAddrBookUri;
92
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/";
101 mAddrBookUri = uri;
102 return uri;
103 }
104
105 private FileInputStream getContactVcard(String lookupKey)
106 throws IOException {
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();
112 }
113
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");
122 }
123
124 }