refactor the request creation to more general creation that can easy be overritten...
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / ContactSyncAdapter.java
1 package eu.alefzero.owncloud.syncadapter;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6
7 import android.accounts.AccountManager;
8 import org.apache.http.HttpResponse;
9 import org.apache.http.client.methods.HttpPut;
10 import org.apache.http.entity.ByteArrayEntity;
11
12 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
13 import eu.alefzero.webdav.HttpPropFind;
14
15 import android.accounts.Account;
16 import android.accounts.AuthenticatorException;
17 import android.accounts.OperationCanceledException;
18 import android.content.ContentProviderClient;
19 import android.content.Context;
20 import android.content.SyncResult;
21 import android.content.res.AssetFileDescriptor;
22 import android.database.Cursor;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.provider.ContactsContract;
26 import android.util.Log;
27
28 public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
29
30 private static final String TAG = "ContactSyncAdapter";
31
32 public ContactSyncAdapter(Context context, boolean autoInitialize) {
33 super(context, autoInitialize);
34 }
35
36 @Override
37 public synchronized void onPerformSync(
38 Account account,
39 Bundle extras,
40 String authority,
41 ContentProviderClient provider,
42 SyncResult syncResult) {
43
44 this.setAccount(account);
45 this.setContentProvider(provider);
46
47 // TODO find all contacts on ownCloud that not synced or the sync date is behind than the last sync date
48 Cursor cursor = getContacts();
49 if (cursor != null && cursor.getCount() > 0) {
50 while (cursor.moveToNext()) {
51 String id = cursor.getString(
52 cursor.getColumnIndex(ContactsContract.Contacts._ID));
53 String lookup = cursor.getString(
54 cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
55 Log.d(TAG, "Found Contact id: " + id + " with lookupkey: "+lookup);
56
57 try {
58 FileInputStream fis = getContactVcard(lookup);
59
60 HttpPut query = new HttpPut(
61 getUri() +
62 "/addressbooks/"+
63 getAccount().name.split("@")[0]+
64 "/default/"+
65 lookup+
66 ".vcf"
67 );
68
69 byte[] b = new byte[fis.available()];
70 fis.read(b);
71 query.setEntity(new ByteArrayEntity(b));
72 HttpResponse response = fireRawRequest(query);
73
74 if(201 != response.getStatusLine().getStatusCode()) {
75 syncResult.stats.numIoExceptions++;
76 }
77 // TODO make a webdav request based on the stream
78 // TODO send request to the ownCloud server
79 // TODO mark the current contact as synced - where to store?
80 fis.close();
81 } catch (IOException e) {
82 syncResult.stats.numIoExceptions++;
83 } catch (OperationCanceledException e) {
84 //TODO maybe to a better break here
85 return;
86 } catch (AuthenticatorException e) {
87 syncResult.stats.numAuthExceptions++;
88 }
89 }
90 }
91
92 }
93
94 protected Uri getUri() {
95 Uri uri = Uri.parse(this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_CONTACT_URL));
96 return uri;
97 }
98
99 /**
100 * Returns the vCard based on the LookupKey for Contact as Stream
101 *
102 * @param lookupKey
103 * @return
104 * @throws IOException
105 */
106 private FileInputStream getContactVcard(String lookupKey) throws IOException {
107 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
108 AssetFileDescriptor fd = getContext().getContentResolver().openAssetFileDescriptor(uri, "r");
109 return fd.createInputStream();
110 }
111
112 /**
113 * Obtains the contact list.
114 *
115 * @return A cursor for for accessing the contact list.
116 */
117 private Cursor getContacts()
118 {
119 // Run query
120 Uri uri = ContactsContract.Contacts.CONTENT_URI;
121 String[] projection = new String[] {
122 ContactsContract.Contacts._ID,
123 ContactsContract.Contacts.LOOKUP_KEY
124 };
125
126 boolean showInvisible = false;
127 String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
128 (showInvisible ? "0" : "1") + "'";
129 String[] selectionArgs = null;
130 String sortOrder = ContactsContract.Contacts._ID + " DESC";
131
132 return getContext().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
133 }
134
135
136
137 }