version bump
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / ContactSyncAdapter.java
index 87174bc..b0ab956 100644 (file)
@@ -1,10 +1,20 @@
 package eu.alefzero.owncloud.syncadapter;
 
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.ByteArrayEntity;
+
+import eu.alefzero.owncloud.AccountUtils;
+import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
+import eu.alefzero.owncloud.db.ProviderMeta;
+import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
 import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.accounts.AuthenticatorException;
+import android.accounts.OperationCanceledException;
 import android.content.ContentProviderClient;
 import android.content.Context;
 import android.content.SyncResult;
@@ -16,83 +26,81 @@ import android.provider.ContactsContract;
 import android.util.Log;
 
 public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
+    private String mAddrBookUri;
 
-       private static final String TAG = "ContactSyncAdapter";
-
-       public ContactSyncAdapter(Context context, boolean autoInitialize) {
-               super(context, autoInitialize);
-       }
+    public ContactSyncAdapter(Context context, boolean autoInitialize) {
+        super(context, autoInitialize);
+        mAddrBookUri = null;
+    }
 
-       @Override
-       public synchronized void onPerformSync(
-                       Account account, 
-                       Bundle extras, 
-                       String authority, 
-                       ContentProviderClient provider, 
-                       SyncResult syncResult) {
-               
-               this.setAccount(account);
-               this.setContentProvider(provider);
+    @Override
+    public void onPerformSync(Account account, Bundle extras, String authority,
+            ContentProviderClient provider, SyncResult syncResult) {
+        setAccount(account);
+        setContentProvider(provider);
+        Cursor c = getLocalContacts(false);
+        if (c.moveToFirst()) {
+            do {
+                String lookup = c.getString(c
+                        .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
+                String a = getAddressBookUri();
+                String uri = a + lookup + ".vcf";
+                FileInputStream f;
+                try {
+                    f = getContactVcard(lookup);
+                    HttpPut query = new HttpPut(uri);
+                    byte[] b = new byte[f.available()];
+                    f.read(b);
+                    query.setEntity(new ByteArrayEntity(b));
+                    HttpResponse response = fireRawRequest(query);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                    return;
+                } catch (OperationCanceledException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                } catch (AuthenticatorException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+            } while (c.moveToNext());
+            // } while (c.moveToNext());
+        }
 
-               // TODO find all contacts on ownCloud that not synced or the sync date is behind than the last sync date
-               Cursor cursor = getContacts();
-               if (cursor != null && cursor.getCount() > 0) {
-                       while (cursor.moveToNext()) {
-                               String id = cursor.getString(
-                                               cursor.getColumnIndex(ContactsContract.Contacts._ID));
-                               String lookup = cursor.getString(
-                                               cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
-                               Log.d(TAG, "Found Contact id: " + id + " with lookupkey: "+lookup);
-                               
-                               try {
-                                       FileInputStream fis = getContactVcard(lookup);
-                                       // TODO make a webdav request based on the stream
-                                       // TODO send request to the ownCloud server
-                                       // TODO mark the current contact as synced - where to store?
-                                       fis.close();
-                               } catch (IOException e) {
-                                       // TODO Auto-generated catch block
-                                       e.printStackTrace();
-                               }
-                       }
-               }
+    }
 
-       }
-       
-       /**
-        * Returns the vCard based on the LookupKey for Contact as Stream 
-        * 
-        * @param lookupKey
-        * @return
-        * @throws IOException
-        */
-       private FileInputStream getContactVcard(String lookupKey) throws IOException {
-               Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
-               AssetFileDescriptor fd = getContext().getContentResolver().openAssetFileDescriptor(uri, "r");
-               return fd.createInputStream();
-       }
+    private String getAddressBookUri() {
+        if (mAddrBookUri != null)
+            return mAddrBookUri;
 
-       /**
-        * Obtains the contact list.
-        *
-        * @return A cursor for for accessing the contact list.
-        */
-       private Cursor getContacts()
-       {
-               // Run query
-               Uri uri = ContactsContract.Contacts.CONTENT_URI;
-               String[] projection = new String[] {
-                               ContactsContract.Contacts._ID,
-                               ContactsContract.Contacts.LOOKUP_KEY
-               };
+        AccountManager am = getAccountManager();
+        String uri = am.getUserData(getAccount(),
+                AccountAuthenticator.KEY_OC_URL).replace(
+                AccountUtils.WEBDAV_PATH_2_0, AccountUtils.CARDDAV_PATH_2_0);
+        uri += "/addressbooks/"
+                + getAccount().name.substring(0,
+                        getAccount().name.lastIndexOf('@')) + "/default/";
+        mAddrBookUri = uri;
+        return uri;
+    }
 
-               boolean showInvisible = false;
-               String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + 
-                               (showInvisible ? "0" : "1") + "'";
-               String[] selectionArgs = null;
-               String sortOrder = ContactsContract.Contacts._ID + " DESC";
+    private FileInputStream getContactVcard(String lookupKey)
+            throws IOException {
+        Uri uri = Uri.withAppendedPath(
+                ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
+        AssetFileDescriptor fd = getContext().getContentResolver()
+                .openAssetFileDescriptor(uri, "r");
+        return fd.createInputStream();
+    }
 
-               return getContext().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
-       }
+    private Cursor getLocalContacts(boolean include_hidden_contacts) {
+        return getContext().getContentResolver().query(
+                ContactsContract.Contacts.CONTENT_URI,
+                new String[] { ContactsContract.Contacts._ID,
+                        ContactsContract.Contacts.LOOKUP_KEY },
+                ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?",
+                new String[] { (include_hidden_contacts ? "0" : "1") },
+                ContactsContract.Contacts._ID + " DESC");
+    }
 
 }