start sync adapter
author"Vincent Veronis"
Mon, 21 Mar 2011 00:14:20 +0100
changeset 880 085240d5be71
parent 879 4ab7484af7cb
child 881 31eec457a6e9
start sync adapter
res/xml/contacts.xml
res/xml/sync_contacts.xml
src/com/beem/project/beem/account/SyncAdapterService.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/xml/contacts.xml	Mon Mar 21 00:14:20 2011 +0100
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<ContactsSource xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <ContactsDataKind
+        android:mimeType="vnd.android.cursor.item/vnd.beem.android.profile"
+        android:icon="@drawable/icon"
+        android:summaryColumn="data2"
+        android:detailColumn="data3"
+        android:detailSocialSummary="true" />
+
+</ContactsSource>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/xml/sync_contacts.xml	Mon Mar 21 00:14:20 2011 +0100
@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="utf-8"?>
+<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
+	android:contentAuthority="com.android.contacts" android:accountType="com.beem.project.com" />
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/account/SyncAdapterService.java	Mon Mar 21 00:14:20 2011 +0100
@@ -0,0 +1,127 @@
+/**
+ * 
+ */
+package com.beem.project.beem.account;
+
+import java.util.ArrayList;
+
+import org.jivesoftware.smack.Roster;
+import org.jivesoftware.smack.RosterEntry;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.util.StringUtils;
+
+import android.accounts.Account;
+import android.accounts.OperationCanceledException;
+import android.app.Service;
+import android.content.ContentProviderClient;
+import android.content.ContentProviderOperation;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SyncResult;
+import android.os.Bundle;
+import android.os.IBinder;
+import android.provider.ContactsContract;
+import android.provider.ContactsContract.Data;
+import android.provider.ContactsContract.RawContacts;
+import android.provider.ContactsContract.CommonDataKinds.Im;
+import android.provider.ContactsContract.CommonDataKinds.StructuredName;
+import android.util.Log;
+
+import com.beem.project.beem.BeemConnection;
+
+/**
+ * @author marseille
+ */
+public class SyncAdapterService extends Service {
+
+    private static final String TAG = "SynAcapterService";
+    private static SyncAdapter mSyncAdapter = null;
+    private static ContentResolver mContentResolver = null;
+    private static Context mContext;
+
+    public SyncAdapterService() {
+	super();
+    }
+
+    @Override
+    public void onCreate() {
+	mContext = SyncAdapterService.this;
+    }
+
+    @Override
+    public IBinder onBind(Intent intent) {
+	IBinder ret = null;
+	ret = getSyncAdapter().getSyncAdapterBinder();
+	return ret;
+
+    }
+
+    private SyncAdapter getSyncAdapter() {
+	if (mSyncAdapter == null)
+	    mSyncAdapter = new SyncAdapter(this, true);
+	return mSyncAdapter;
+    }
+
+    public static void performSync(Context context, Account account, Bundle extras, String authority,
+	ContentProviderClient provider, SyncResult syncResult) throws OperationCanceledException {
+	mContentResolver = context.getContentResolver();
+	Log.i(TAG, "performSync: " + account.toString());
+
+	BeemConnection beemco = new BeemConnection(mContext.getSharedPreferences(account.name, MODE_PRIVATE), null);
+	String host = StringUtils.parseServer(account.name);
+	String user = StringUtils.parseName(account.name);
+	XMPPConnection con = new XMPPConnection(host);
+	try {
+	    con.connect();
+	    con.login(user, beemco.getPassword());
+	} catch (XMPPException e) {
+	    Log.d(TAG, "Error during sync connection", e);
+	}
+	Roster roster = con.getRoster();
+	if (roster != null) {
+	    for (RosterEntry entry : roster.getEntries()) {
+		if (entry != null) {
+		    addEntry(account, entry);
+		}
+	    }
+	}
+
+    }
+
+    private static void addEntry(Account account, RosterEntry entry) {
+	ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
+
+	int rawContactInsertIndex = ops.size();
+
+	ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
+	builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
+	builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
+	builder.withValue(RawContacts.SYNC1, entry.getUser());
+	ops.add(builder.build());
+
+	builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
+	builder.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex);
+	builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
+	builder.withValue(StructuredName.DISPLAY_NAME, entry.getUser());
+	ops.add(builder.build());
+	
+	//Add row contact information
+	builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
+	builder.withValueBackReference(Data.RAW_CONTACT_ID, 0);
+	builder.withValue(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
+	builder.withValue(Im.DATA, entry.getUser());
+	builder.withValue(Im.PROTOCOL, Im.PROTOCOL_JABBER);
+	ops.add(builder.build());
+
+	try {
+	    mContentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
+	    Log.i(TAG, "added contact " + entry.getUser());
+	} catch (Exception e) {
+	    Log.d(TAG, "Error during add of contact", e);
+	}
+
+    }
+
+}