filesync fixed
[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.Account;
8 import android.content.ContentProviderClient;
9 import android.content.Context;
10 import android.content.SyncResult;
11 import android.content.res.AssetFileDescriptor;
12 import android.database.Cursor;
13 import android.net.Uri;
14 import android.os.Bundle;
15 import android.provider.ContactsContract;
16 import android.util.Log;
17
18 public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
19
20 private static final String TAG = "ContactSyncAdapter";
21
22 public ContactSyncAdapter(Context context, boolean autoInitialize) {
23 super(context, autoInitialize);
24 }
25
26 @Override
27 public synchronized void onPerformSync(
28 Account account,
29 Bundle extras,
30 String authority,
31 ContentProviderClient provider,
32 SyncResult syncResult) {
33
34 this.setAccount(account);
35 this.setContentProvider(provider);
36
37 // TODO find all contacts on ownCloud that not synced or the sync date is behind than the last sync date
38 Cursor cursor = getContacts();
39 if (cursor != null && cursor.getCount() > 0) {
40 while (cursor.moveToNext()) {
41 String id = cursor.getString(
42 cursor.getColumnIndex(ContactsContract.Contacts._ID));
43 String lookup = cursor.getString(
44 cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
45 Log.d(TAG, "Found Contact id: " + id + " with lookupkey: "+lookup);
46
47 try {
48 FileInputStream fis = getContactVcard(lookup);
49 // TODO make a webdav request based on the stream
50 // TODO send request to the ownCloud server
51 // TODO mark the current contact as synced - where to store?
52 fis.close();
53 } catch (IOException e) {
54 // TODO Auto-generated catch block
55 e.printStackTrace();
56 }
57 }
58 }
59
60 }
61
62 /**
63 * Returns the vCard based on the LookupKey for Contact as Stream
64 *
65 * @param lookupKey
66 * @return
67 * @throws IOException
68 */
69 private FileInputStream getContactVcard(String lookupKey) throws IOException {
70 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
71 AssetFileDescriptor fd = getContext().getContentResolver().openAssetFileDescriptor(uri, "r");
72 return fd.createInputStream();
73 }
74
75 /**
76 * Obtains the contact list.
77 *
78 * @return A cursor for for accessing the contact list.
79 */
80 private Cursor getContacts()
81 {
82 // Run query
83 Uri uri = ContactsContract.Contacts.CONTENT_URI;
84 String[] projection = new String[] {
85 ContactsContract.Contacts._ID,
86 ContactsContract.Contacts.LOOKUP_KEY
87 };
88
89 boolean showInvisible = false;
90 String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
91 (showInvisible ? "0" : "1") + "'";
92 String[] selectionArgs = null;
93 String sortOrder = ContactsContract.Contacts._ID + " DESC";
94
95 return getContext().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
96 }
97
98 }