Fixed local path NULL when making favourite a file not down ; fixed change of local...
[pub/Android/ownCloud.git] / src / com / owncloud / android / syncadapter / ContactSyncAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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.AccountUtils;
28 import com.owncloud.android.authenticator.AccountAuthenticator;
29
30 import android.accounts.Account;
31 import android.accounts.AccountManager;
32 import android.accounts.AuthenticatorException;
33 import android.accounts.OperationCanceledException;
34 import android.content.ContentProviderClient;
35 import android.content.Context;
36 import android.content.SyncResult;
37 import android.content.res.AssetFileDescriptor;
38 import android.database.Cursor;
39 import android.net.Uri;
40 import android.os.Bundle;
41 import android.provider.ContactsContract;
42
43 public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
44 private String mAddrBookUri;
45
46 public ContactSyncAdapter(Context context, boolean autoInitialize) {
47 super(context, autoInitialize);
48 mAddrBookUri = null;
49 }
50
51 @Override
52 public void onPerformSync(Account account, Bundle extras, String authority,
53 ContentProviderClient provider, SyncResult syncResult) {
54 setAccount(account);
55 setContentProvider(provider);
56 Cursor c = getLocalContacts(false);
57 if (c.moveToFirst()) {
58 do {
59 String lookup = c.getString(c
60 .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
61 String a = getAddressBookUri();
62 String uri = a + lookup + ".vcf";
63 FileInputStream f;
64 try {
65 f = getContactVcard(lookup);
66 HttpPut query = new HttpPut(uri);
67 byte[] b = new byte[f.available()];
68 f.read(b);
69 query.setEntity(new ByteArrayEntity(b));
70 fireRawRequest(query);
71 } catch (IOException e) {
72 e.printStackTrace();
73 return;
74 } catch (OperationCanceledException e) {
75 // TODO Auto-generated catch block
76 e.printStackTrace();
77 } catch (AuthenticatorException e) {
78 // TODO Auto-generated catch block
79 e.printStackTrace();
80 }
81 } while (c.moveToNext());
82 // } while (c.moveToNext());
83 }
84
85 }
86
87 private String getAddressBookUri() {
88 if (mAddrBookUri != null)
89 return mAddrBookUri;
90
91 AccountManager am = getAccountManager();
92 @SuppressWarnings("deprecation")
93 String uri = am.getUserData(getAccount(),
94 AccountAuthenticator.KEY_OC_URL).replace(
95 AccountUtils.WEBDAV_PATH_2_0, AccountUtils.CARDDAV_PATH_2_0);
96 uri += "/addressbooks/"
97 + getAccount().name.substring(0,
98 getAccount().name.lastIndexOf('@')) + "/default/";
99 mAddrBookUri = uri;
100 return uri;
101 }
102
103 private FileInputStream getContactVcard(String lookupKey)
104 throws IOException {
105 Uri uri = Uri.withAppendedPath(
106 ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
107 AssetFileDescriptor fd = getContext().getContentResolver()
108 .openAssetFileDescriptor(uri, "r");
109 return fd.createInputStream();
110 }
111
112 private Cursor getLocalContacts(boolean include_hidden_contacts) {
113 return getContext().getContentResolver().query(
114 ContactsContract.Contacts.CONTENT_URI,
115 new String[] { ContactsContract.Contacts._ID,
116 ContactsContract.Contacts.LOOKUP_KEY },
117 ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?",
118 new String[] { (include_hidden_contacts ? "0" : "1") },
119 ContactsContract.Contacts._ID + " DESC");
120 }
121
122 }