217d3a37cbe99075ca10bd4d91379244c4280f66
[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 } catch (IOException e) {
53 // TODO Auto-generated catch block
54 e.printStackTrace();
55 }
56 }
57 }
58
59 }
60
61 /**
62 * Returns the vCard based on the LookupKey for Contact as Stream
63 *
64 * @param lookupKey
65 * @return
66 * @throws IOException
67 */
68 private FileInputStream getContactVcard(String lookupKey) throws IOException {
69 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
70 AssetFileDescriptor fd = getContext().getContentResolver().openAssetFileDescriptor(uri, "r");
71 return fd.createInputStream();
72 }
73
74 /**
75 * Obtains the contact list.
76 *
77 * @return A cursor for for accessing the contact list.
78 */
79 private Cursor getContacts()
80 {
81 // Run query
82 Uri uri = ContactsContract.Contacts.CONTENT_URI;
83 String[] projection = new String[] {
84 ContactsContract.Contacts._ID,
85 ContactsContract.Contacts.LOOKUP_KEY
86 };
87
88 boolean showInvisible = false;
89 String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
90 (showInvisible ? "0" : "1") + "'";
91 String[] selectionArgs = null;
92 String sortOrder = ContactsContract.Contacts._ID + " DESC";
93
94 return getContext().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
95 }
96
97 }