Improve Contact syncAdapter
author"Vincent Veronis"
Sun, 08 May 2011 11:35:51 +0200
changeset 887 d9a0565ccacc
parent 886 5d3163053c42
child 888 9ffd19d0249c
Improve Contact syncAdapter
src/com/beem/project/beem/account/SyncAdapterService.java
src/com/beem/project/beem/ui/Chat.java
--- a/src/com/beem/project/beem/account/SyncAdapterService.java	Sat May 07 12:26:45 2011 +0200
+++ b/src/com/beem/project/beem/account/SyncAdapterService.java	Sun May 08 11:35:51 2011 +0200
@@ -48,10 +48,8 @@
 
 import org.jivesoftware.smack.Roster;
 import org.jivesoftware.smack.RosterEntry;
-import org.jivesoftware.smack.RosterGroup;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
-import org.jivesoftware.smack.packet.Presence;
 
 import android.accounts.Account;
 import android.accounts.OperationCanceledException;
@@ -70,18 +68,10 @@
 import android.os.Bundle;
 import android.os.IBinder;
 import android.os.RemoteException;
-import android.provider.BaseColumns;
 import android.provider.ContactsContract;
-import android.provider.ContactsContract.RawContacts;
-import android.provider.ContactsContract.RawContacts.Entity;
 import android.util.Log;
-import android.widget.ImageButton;
-import android.widget.ImageView;
 
 import com.beem.project.beem.BeemConnection;
-import com.beem.project.beem.R;
-import com.beem.project.beem.providers.AvatarProvider;
-import com.beem.project.beem.utils.Status;
 
 /**
  * Class to integrate beem in android's account
@@ -168,9 +158,7 @@
 	ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
 	for (RosterEntry entry : r.getEntries()) {
 	    if (entry != null) {
-		long rawContactID = manageEntry(ops, a, entry);
-		if (rawContactID != -1)
-		    updateContactStatus(ops, entry, rawContactID, r.getPresence(entry.getUser()));
+		manageEntry(ops, a, entry);
 	    }
 	    if (ops.size() > 100)
 		executeOperation(ops);
@@ -186,10 +174,11 @@
 	    executeOperation(ops);
     }
 
-    private static long manageEntry(ArrayList<ContentProviderOperation> ops, Account account, RosterEntry entry) {
+    private static void manageEntry(ArrayList<ContentProviderOperation> ops, Account account, RosterEntry entry) {
 	long rawContactID = getRawContactID(account.name, entry.getUser());
+	String displayName = entry.getName() != null ? entry.getName() : entry.getUser();
 	Log.i(TAG, "Sync Contact : " + entry.getUser() + " RawContactID : " + rawContactID);
-	if (rawContactID == -1) { // Not found in database, add new 	    
+	if (rawContactID == -1) { // Not found in database, add new
 	    ContentValues values = new ContentValues();
 	    values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type);
 	    values.put(ContactsContract.RawContacts.ACCOUNT_NAME, account.name);
@@ -197,76 +186,40 @@
 	    Uri rawContactUri = mContentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values);
 	    rawContactID = ContentUris.parseId(rawContactUri);
 	    values.clear();
-	    ContentProviderOperation.Builder builder = BuildProfile(entry, rawContactID);
-	    ops.add(builder.build());	    
-	    builder = buildStructuredName(entry, rawContactID);
+	    //Insert Name values
+	    ContentProviderOperation.Builder builder = ContentProviderOperation
+		.newInsert(ContactsContract.Data.CONTENT_URI);
+	    builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactID);
+	    builder.withValue(ContactsContract.Data.MIMETYPE,
+		ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
+	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, rawContactID);
+	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
+	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, displayName);
+	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, displayName);
+	    ops.add(builder.build());
+	    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
+	    builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactID);
+	    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
+	    builder.withValue(ContactsContract.CommonDataKinds.Im.RAW_CONTACT_ID, rawContactID);
+	    builder.withValue(ContactsContract.CommonDataKinds.Im.DATA1, displayName);
+	    builder.withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL,
+		ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER);
 	    ops.add(builder.build());
 	} else { // Found, update
-	    ContentProviderOperation.Builder builder = buildStructuredName(entry, rawContactID);
+	    // Update Name values
+	    ContentProviderOperation.Builder builder = ContentProviderOperation
+		.newUpdate(ContactsContract.Data.CONTENT_URI);
+	    builder.withSelection(
+		ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID + " =? AND "
+		    + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
+		    + "'", new String[] { String.valueOf(rawContactID)});
+	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
+	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, displayName);
+	    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, displayName);
 	    ops.add(builder.build());
 	}
-	return rawContactID;
     }
-
-    private static void manageGroup(ArrayList<ContentProviderOperation> ops, Account account, RosterGroup group) {
-	Log.i(TAG, "Sync Group : " + group.getName());
-	ContentProviderOperation.Builder builder = ContentProviderOperation
-	    .newInsert(ContactsContract.Groups.CONTENT_URI);
-	builder.withValue(ContactsContract.Groups.TITLE, group.getName());
-	builder.withValue(ContactsContract.Settings.UNGROUPED_VISIBLE, true);
-    }
-
-    private static ContentProviderOperation.Builder buildStructuredName(RosterEntry entry, long rawContactID) {
-	ContentProviderOperation.Builder builder = ContentProviderOperation
-	    .newInsert(ContactsContract.Data.CONTENT_URI);
-	String displayName = entry.getName() != null ? entry.getName() : entry.getUser();
-	builder.withValue(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, rawContactID);
-	builder.withValue(ContactsContract.Data.MIMETYPE,
-	    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
-	builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
-	return builder;
-    }
-
-    private static ContentProviderOperation.Builder BuildProfile(RosterEntry entry, long rawContactID) {
-	ContentProviderOperation.Builder builder = ContentProviderOperation
-	    .newInsert(ContactsContract.Data.CONTENT_URI);
-	String displayName = entry.getName() != null ? entry.getName() : entry.getUser();
-	builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactID);
-	builder.withValue(ContactsContract.Data.MIMETYPE,
-	    "vnd.android.cursor.item/vnd.com.beem.project.beem.android.profile");
-	builder.withValue(ContactsContract.Data.DATA2, displayName);
-	//builder.withValue(ContactsContract.Data.DATA2, R.string.chat_name);
-	return builder;
-    }
-
-    private static void updateContactStatus(ArrayList<ContentProviderOperation> operationList, RosterEntry entry,
-	long rawContactId, Presence p) {
-	Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
-	Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
-	Cursor c = mContentResolver.query(entityUri, new String[] { Entity.DATA_ID }, Entity.MIMETYPE
-	    + " = 'vnd.android.cursor.item/vnd.com.beem.project.beem.android.profile'", null, null);
-	try {
-	    if (c.moveToNext()) {
-		if (!c.isNull(0)) {
-		    Log.e(TAG + "UPDATECONTACTSTATUS", "Entity.DATA_ID : " + c.getLong(0));
-		    // Build raw Contact after click on it
-		    ContentProviderOperation.Builder builder = ContentProviderOperation
-			.newInsert(ContactsContract.StatusUpdates.CONTENT_URI);
-		    builder.withValue(ContactsContract.StatusUpdates.DATA_ID, c.getLong(0));
-		    //TODO: Get status message
-		    builder.withValue(ContactsContract.StatusUpdates.STATUS, "BEEM STATUS");
-		    builder.withValue(ContactsContract.StatusUpdates.STATUS_RES_PACKAGE, "com.beem.project.beem");
-		    builder.withValue(ContactsContract.StatusUpdates.STATUS_LABEL, R.string.app_name);
-		    //TODO: GET Status Logo
-		    // Actually getting default logo in the xml
-//		    builder.withValue(ContactsContract.StatusUpdates.STATUS_ICON, img);
-		    operationList.add(builder.build());
-		}
-	    }
-	} finally {
-	    c.close();
-	}
-    }
+   
 
     private static long getRawContactID(String account, String jid) {
 	long authorId = -1;
--- a/src/com/beem/project/beem/ui/Chat.java	Sat May 07 12:26:45 2011 +0200
+++ b/src/com/beem/project/beem/ui/Chat.java	Sun May 08 11:35:51 2011 +0200
@@ -76,6 +76,7 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.preference.PreferenceManager;
+import android.provider.Contacts;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.RawContacts.Entity;
 import android.text.util.Linkify;
@@ -203,10 +204,16 @@
 	Uri tmpuri = getIntent().getData();
 	Log.e(TAG, "URI : " + tmpuri.getLastPathSegment());
 	Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
-	    new String[] { ContactsContract.Data.DISPLAY_NAME },
-	    ContactsContract.Data._ID + " = " + tmpuri.getLastPathSegment(), null, null);
+	    new String[] { ContactsContract.Data.CONTACT_ID },
+	    ContactsContract.Data.CONTACT_ID + " = " + tmpuri.getLastPathSegment(), null, null);
 	if (c.getCount() > 0) {
-	    Log.e(TAG, "JID : " + c.getString(0));
+	    Log.e(TAG, "ID : " + c.getInt(0));
+	    c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
+		new String[] { ContactsContract.RawContacts.SOURCE_ID },
+		ContactsContract.RawContacts.CONTACT_ID + " = " + c.getInt(0), null, null);
+	    if (c.getCount() > 0) {
+		Log.e(TAG, "JID : " + c.getString(0));
+	    }
 	}
 	//makeXmppUri
 	//mContact = new Contact(getIntent().getData());