Add raw contact informations
author"Vincent Veronis"
Sun, 08 May 2011 20:56:45 +0200
changeset 889 45bbdf20aa9d
parent 888 9ffd19d0249c
child 890 7a2978972b49
Add raw contact informations
AndroidManifest.xml
src/com/beem/project/beem/account/SyncAdapterService.java
src/com/beem/project/beem/ui/Chat.java
src/com/beem/project/beem/utils/Status.java
--- a/AndroidManifest.xml	Sun May 08 12:04:31 2011 +0200
+++ b/AndroidManifest.xml	Sun May 08 20:56:45 2011 +0200
@@ -55,7 +55,26 @@
 				android:label="Beem Connection">
 				<action
 					android:name="com.beem.project.beem.service.XmppConnectionAdapter.CONNECTION_CLOSED" />
-			</intent-filter>			
+			</intent-filter>
+			<intent-filter>
+				<action
+					android:name="android.intent.action.SENDTO" />
+				<category
+					android:name="android.intent.category.DEFAULT" />
+				<category
+					android:name="com.android.im.category.JABBER" />
+				<data
+					android:scheme="im" />
+			</intent-filter>
+			<intent-filter>
+				<action
+					android:name="android.intent.action.SENDTO" />
+				<category
+					android:name="android.intent.category.DEFAULT" />
+				<data
+					android:scheme="imto"
+					android:host="jabber" />
+			</intent-filter>
 		</activity>
 		<activity
 			android:name=".ui.ChangeStatus"
--- a/src/com/beem/project/beem/account/SyncAdapterService.java	Sun May 08 12:04:31 2011 +0200
+++ b/src/com/beem/project/beem/account/SyncAdapterService.java	Sun May 08 20:56:45 2011 +0200
@@ -50,6 +50,7 @@
 import org.jivesoftware.smack.RosterEntry;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.packet.Presence;
 
 import android.accounts.Account;
 import android.accounts.OperationCanceledException;
@@ -72,6 +73,8 @@
 import android.util.Log;
 
 import com.beem.project.beem.BeemConnection;
+import com.beem.project.beem.R;
+import com.beem.project.beem.utils.Status;
 
 /**
  * Class to integrate beem in android's account.
@@ -135,12 +138,14 @@
 	mContentResolver = context.getContentResolver();
 	Log.i(TAG, "performSync: " + account.toString());
 
+	//TODO: Get BeemService connectino support
 	BeemConnection beemco = new BeemConnection(mContext.getSharedPreferences(account.name, MODE_PRIVATE), null);
-	beemco.setNoPresence();
+	//beemco.setNoPresence();
 	XMPPConnection con = new XMPPConnection(beemco.getConnectionConfiguration());
 	Roster roster = null;
 	try {
 	    con.connect();
+	    //TODO: BEEM_SYNC_ADAPTER -> GetSharedPreferences()
 	    con.login(beemco.getLogin(), beemco.getPassword(), "BEEM_SYNC_ADAPTER");
 	    roster = con.getRoster();
 	} catch (XMPPException e) {
@@ -151,7 +156,8 @@
 	if (roster != null) {
 	    manageRoster(roster, account);
 	}
-	con.disconnect();
+	//TODO: Dont disco ?!
+	//con.disconnect();
     }
 
     /**
@@ -177,7 +183,8 @@
 	ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
 	for (RosterEntry entry : r.getEntries()) {
 	    if (entry != null) {
-		manageEntry(ops, a, entry);
+		long rawContactID = manageEntry(ops, a, entry);
+		addUpdateStatus(ops, entry, r.getPresence(entry.getUser()), rawContactID);
 	    }
 	    if (ops.size() > 100)
 		executeOperation(ops);
@@ -191,8 +198,9 @@
      * @param ops The content provider operation
      * @param account The account related
      * @param entry The roster entry to sync
+     * @return The raw contact ID
      */
-    private static void manageEntry(ArrayList<ContentProviderOperation> ops, Account account, RosterEntry entry) {
+    private static long manageEntry(ArrayList<ContentProviderOperation> ops, Account account, RosterEntry entry) {
 	long rawContactID = getRawContactID(account.name, entry.getUser());
 	Log.i(TAG, "Sync Contact : " + entry.getUser() + " RawContactID : " + rawContactID);
 	if (rawContactID == -1) { // Not found in database, add new
@@ -205,12 +213,13 @@
 	    values.clear();
 	    ContentProviderOperation.Builder builder = addUpdateStructuredName(entry, rawContactID, true);
 	    ops.add(builder.build());
-	    builder = addUpdateIm(entry, rawContactID, true);
+	    builder = addUpdateIm(entry, rawContactID, account, true);
 	    ops.add(builder.build());
 	} else { // Found, update
 	    ContentProviderOperation.Builder builder = addUpdateStructuredName(entry, rawContactID, false);
 	    ops.add(builder.build());
 	}
+	return rawContactID;
     }
 
     /**
@@ -250,7 +259,8 @@
      * @param isInsert Insert boolean
      * @return
      */
-    private static ContentProviderOperation.Builder addUpdateIm(RosterEntry entry, long rawContactID, boolean isInsert) {
+    private static ContentProviderOperation.Builder addUpdateIm(RosterEntry entry, long rawContactID, Account account,
+	boolean isInsert) {
 	String displayName = entry.getName() != null ? entry.getName() : entry.getUser();
 	ContentProviderOperation.Builder builder;
 	if (isInsert) {
@@ -268,6 +278,32 @@
     }
 
     /**
+     * Method to insert or update IM informations.
+     * @param entry The roster entry to sync
+     * @param rawContactID The contact ID in the android database
+     * @param isInsert Insert boolean
+     * @return
+     */
+    private static void addUpdateStatus(ArrayList<ContentProviderOperation> ops, RosterEntry entry, Presence p,
+	long rawContactID) {
+	String displayName = entry.getName() != null ? entry.getName() : entry.getUser();
+	Log.i(TAG + "UPDATESTATUS", "Contact : " + displayName + " Presence status : " + p.getStatus() + " Presence status state : " + Status.getStatusFromPresence(p));
+	ContentProviderOperation.Builder builder;
+	builder = ContentProviderOperation.newInsert(ContactsContract.StatusUpdates.CONTENT_URI);	
+	builder.withValue(ContactsContract.StatusUpdates.PROTOCOL, ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER);
+	builder.withValue(ContactsContract.StatusUpdates.IM_HANDLE, displayName);
+	builder.withValue(ContactsContract.StatusUpdates.IM_ACCOUNT, "beem@elyzion.net");
+	builder.withValue(ContactsContract.StatusUpdates.STATUS, p.getStatus());
+	builder.withValue(ContactsContract.StatusUpdates.STATUS_RES_PACKAGE, "com.beem.project.beem");
+	builder.withValue(ContactsContract.StatusUpdates.STATUS_LABEL, R.string.app_name);
+	//TODO: Get status icon
+	builder.withValue(ContactsContract.StatusUpdates.STATUS_ICON, R.drawable.beem_status_icon);
+	//TODO: Pb presence ... 2 appear on 3 raw .... random appear
+	builder.withValue(ContactsContract.StatusUpdates.PRESENCE, Status.getStatusFromPresence(p));
+	ops.add(builder.build());
+    }
+
+    /**
      * Get contact ID from android database.
      * @param account The account related
      * @param jid The jid related
--- a/src/com/beem/project/beem/ui/Chat.java	Sun May 08 12:04:31 2011 +0200
+++ b/src/com/beem/project/beem/ui/Chat.java	Sun May 08 20:56:45 2011 +0200
@@ -201,26 +201,14 @@
     @Override
     protected void onResume() {
 	super.onResume();
-	Uri tmpuri = getIntent().getData();
-	Log.e(TAG, "URI : " + tmpuri.getLastPathSegment());
-	Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
-	    new String[] { ContactsContract.Data.CONTACT_ID },
-	    ContactsContract.Data.CONTACT_ID + " = " + tmpuri.getLastPathSegment(), null, null);
-	if (c.getCount() > 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());
-	//	if (!mBinded) {
-	//	    bindService(SERVICE_INTENT, mConn, BIND_AUTO_CREATE);
-	//	    mBinded = true;
-	//	}
+	// When coming from account contact
+	Uri contactURI = getIntent().getData();
+	String jid = contactURI.getPathSegments().get(0);
+	mContact = new Contact(jid);
+//	if (!mBinded) {
+//	    bindService(SERVICE_INTENT, mConn, BIND_AUTO_CREATE);
+//	    mBinded = true;
+//	}
     }
 
     /**
--- a/src/com/beem/project/beem/utils/Status.java	Sun May 08 12:04:31 2011 +0200
+++ b/src/com/beem/project/beem/utils/Status.java	Sun May 08 20:56:45 2011 +0200
@@ -46,6 +46,8 @@
 import org.jivesoftware.smack.packet.Presence;
 import org.jivesoftware.smack.packet.Presence.Mode;
 
+import android.provider.ContactsContract;
+
 /**
  * Utility class to deal with status and presence value.
  * @author marseille
@@ -111,32 +113,32 @@
      * @return an int representing the status
      */
     public static int getStatusFromPresence(final Presence presence) {
-	int res = Status.CONTACT_STATUS_DISCONNECT;
+	int res = ContactsContract.StatusUpdates.OFFLINE;
 	if (presence.getType().equals(Presence.Type.unavailable)) {
-	    res = Status.CONTACT_STATUS_DISCONNECT;
+	    res = ContactsContract.StatusUpdates.OFFLINE;
 	} else {
 	    Mode mode = presence.getMode();
 	    if (mode == null) {
-		res = Status.CONTACT_STATUS_AVAILABLE;
+		res = ContactsContract.StatusUpdates.AVAILABLE;
 	    } else {
 		switch (mode) {
 		    case available:
-			res = Status.CONTACT_STATUS_AVAILABLE;
+			res = ContactsContract.StatusUpdates.AVAILABLE;
 			break;
 		    case away:
-			res = Status.CONTACT_STATUS_AWAY;
+			res = ContactsContract.StatusUpdates.AWAY;
 			break;
 		    case chat:
-			res = Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT;
+			res = ContactsContract.StatusUpdates.AVAILABLE;	
 			break;
 		    case dnd:
-			res = Status.CONTACT_STATUS_BUSY;
+			res = ContactsContract.StatusUpdates.DO_NOT_DISTURB;	
 			break;
 		    case xa:
-			res = Status.CONTACT_STATUS_UNAVAILABLE;
+			res = ContactsContract.StatusUpdates.INVISIBLE;	
 			break;
 		    default:
-			res = Status.CONTACT_STATUS_DISCONNECT;
+			res = ContactsContract.StatusUpdates.OFFLINE;	
 			break;
 		}
 	    }