1 package eu
.alefzero
.owncloud
.syncadapter
;
3 import java
.io
.FileInputStream
;
4 import java
.io
.FileNotFoundException
;
5 import java
.io
.IOException
;
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
;
18 public class ContactSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
20 private static final String TAG
= "ContactSyncAdapter";
22 public ContactSyncAdapter(Context context
, boolean autoInitialize
) {
23 super(context
, autoInitialize
);
27 public synchronized void onPerformSync(
31 ContentProviderClient provider
,
32 SyncResult syncResult
) {
34 this.setAccount(account
);
35 this.setContentProvider(provider
);
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
);
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
62 * Returns the vCard based on the LookupKey for Contact as Stream
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();
75 * Obtains the contact list.
77 * @return A cursor for for accessing the contact list.
79 private Cursor
getContacts()
82 Uri uri
= ContactsContract
.Contacts
.CONTENT_URI
;
83 String
[] projection
= new String
[] {
84 ContactsContract
.Contacts
._ID
,
85 ContactsContract
.Contacts
.LOOKUP_KEY
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";
94 return getContext().getContentResolver().query(uri
, projection
, selection
, selectionArgs
, sortOrder
);