SyncContact OK
author"Vincent Veronis"
Sat, 07 May 2011 12:26:45 +0200
changeset 886 5d3163053c42
parent 885 193a934390ba
child 887 d9a0565ccacc
SyncContact OK BuildRaw Contact OK
AndroidManifest.xml
res/drawable/beem_icon_launcher_color.png
res/xml/contacts.xml
res/xml/preferences.xml
res/xml/sync_contacts.xml
src/com/beem/project/beem/account/SyncAdapterService.java
src/com/beem/project/beem/ui/Chat.java
src/com/beem/project/beem/ui/Login.java
--- a/AndroidManifest.xml	Thu Apr 21 21:05:25 2011 +0200
+++ b/AndroidManifest.xml	Sat May 07 12:26:45 2011 +0200
@@ -56,6 +56,15 @@
 				<action
 					android:name="com.beem.project.beem.service.XmppConnectionAdapter.CONNECTION_CLOSED" />
 			</intent-filter>
+			<intent-filter
+				android:label="Beem Account">
+				<action
+					android:name="android.intent.action.VIEW" />
+				<category
+					android:name="android.intent.category.DEFAULT" />
+				<data
+					android:mimeType="vnd.android.cursor.item/vnd.com.beem.project.beem.android.profile" />
+			</intent-filter>
 		</activity>
 		<activity
 			android:name=".ui.ChangeStatus"
@@ -137,8 +146,10 @@
 		-->
 		<provider
 			android:name=".providers.AvatarProvider"
+			android:label="Avatar Provider"
 			android:authorities="com.beem.project.beem.providers.avatarprovider"
-			android:exported="false" />
+			android:syncable="true"
+			android:exported="true" />
 		<service
 			android:name=".account.AuthenticatorService"
 			android:exported="true"
@@ -162,6 +173,9 @@
 			<meta-data
 				android:name="android.content.SyncAdapter"
 				android:resource="@xml/sync_contacts" />
+			<meta-data
+				android:name="android.provider.CONTACTS_STRUCTURE"
+				android:resource="@xml/contacts" />
 		</service>
 		<service
 			android:name="BeemService"
Binary file res/drawable/beem_icon_launcher_color.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/xml/contacts.xml	Sat May 07 12:26:45 2011 +0200
@@ -0,0 +1,8 @@
+<ContactsSource
+	xmlns:android="http://schemas.android.com/apk/res/android">
+	<ContactsDataKind
+		android:icon="@drawable/beem_icon_launcher_color"
+		android:mimeType="vnd.android.cursor.item/vnd.com.beem.project.beem.android.profile"
+		android:summaryColumn="data2"
+		android:detailSocialSummary="true" />
+</ContactsSource>
--- a/res/xml/preferences.xml	Thu Apr 21 21:05:25 2011 +0200
+++ b/res/xml/preferences.xml	Sat May 07 12:26:45 2011 +0200
@@ -23,14 +23,9 @@
 	</PreferenceScreen>
 	<CheckBoxPreference
 		android:key="prefer wifi"
-		android:title="Prefer WiFi"
-		android:layout="?android:attr/preferenceLayoutChild" />
-	<PreferenceCategory
-		android:key="custom"
-		android:title="Custom">
-		<com.beem.project.beem.account.CheckBoxPreference_Custom
-			android:key="custom_checkbox"
-			android:title="Title"
-			android:summary="Summary" />
-	</PreferenceCategory>
+		android:title="Prefer WiFi">
+		<intent
+			android:action="com.beem.project.beem.SETTINGS"
+			android:targetClass="ui.Settings.class"/>
+			</CheckBoxPreference>
 </PreferenceScreen>
\ No newline at end of file
--- a/res/xml/sync_contacts.xml	Thu Apr 21 21:05:25 2011 +0200
+++ b/res/xml/sync_contacts.xml	Sat May 07 12:26:45 2011 +0200
@@ -1,3 +1,5 @@
 <?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
+<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
--- a/src/com/beem/project/beem/account/SyncAdapterService.java	Thu Apr 21 21:05:25 2011 +0200
+++ b/src/com/beem/project/beem/account/SyncAdapterService.java	Sat May 07 12:26:45 2011 +0200
@@ -51,6 +51,7 @@
 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;
@@ -69,11 +70,18 @@
 import android.os.Bundle;
 import android.os.IBinder;
 import android.os.RemoteException;
+import android.provider.BaseColumns;
 import android.provider.ContactsContract;
-import android.provider.ContactsContract.CommonDataKinds.StructuredName;
+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
@@ -120,7 +128,7 @@
      * @param authority authority
      * @param provider provider
      * @param syncResult syncResult
-     * @throws OperationCanceledException OperationCanceledException 
+     * @throws OperationCanceledException OperationCanceledException
      */
     public static void performSync(Context context, final Account account, Bundle extras, String authority,
 	ContentProviderClient provider, SyncResult syncResult) throws OperationCanceledException {
@@ -135,7 +143,6 @@
 	    con.connect();
 	    con.login(beemco.getLogin(), beemco.getPassword(), "BEEM_SYNC_ADAPTER");
 	    roster = con.getRoster();
-
 	} catch (XMPPException e) {
 	    Log.e(TAG, "Error while connecting with syncAdapter", e);
 	} catch (IllegalStateException e) {
@@ -144,9 +151,7 @@
 	if (roster != null) {
 	    manageRoster(roster, account);
 	}
-
 	con.disconnect();
-
     }
 
     private static void executeOperation(ArrayList<ContentProviderOperation> ops) {
@@ -161,26 +166,29 @@
 
     private static void manageRoster(final Roster r, final Account a) {
 	ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
-
 	for (RosterEntry entry : r.getEntries()) {
 	    if (entry != null) {
-		manageEntry(ops, a, entry);
+		long rawContactID = manageEntry(ops, a, entry);
+		if (rawContactID != -1)
+		    updateContactStatus(ops, entry, rawContactID, r.getPresence(entry.getUser()));
 	    }
 	    if (ops.size() > 100)
 		executeOperation(ops);
 	}
-	for (RosterGroup group : r.getGroups()) {
-	    if (group != null) {
-		manageGroup(ops, a, group);
-	    }
-	}
+	// TODO: dont know how to ttest
+	//	for (RosterGroup group : r.getGroups()) {
+	//	    if (group != null)
+	//		manageGroup(ops, a, group);
+	//	    if (ops.size() > 0)
+	//		    executeOperation(ops);
+	//	}
 	if (ops.size() > 0)
 	    executeOperation(ops);
     }
 
-    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());
+	Log.i(TAG, "Sync Contact : " + entry.getUser() + " RawContactID : " + rawContactID);
 	if (rawContactID == -1) { // Not found in database, add new 	    
 	    ContentValues values = new ContentValues();
 	    values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type);
@@ -189,43 +197,77 @@
 	    Uri rawContactUri = mContentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values);
 	    rawContactID = ContentUris.parseId(rawContactUri);
 	    values.clear();
-	    ContentProviderOperation.Builder builder = buildStructuredName(ContentProviderOperation
-		.newInsert(ContactsContract.Data.CONTENT_URI), entry, rawContactID);
+	    ContentProviderOperation.Builder builder = BuildProfile(entry, rawContactID);
+	    ops.add(builder.build());	    
+	    builder = buildStructuredName(entry, rawContactID);
 	    ops.add(builder.build());
 	} else { // Found, update
-	    // if newUpdate instead of newInster ... fail update : all rows = 1 row
-	    ContentProviderOperation.Builder builder = buildStructuredName(ContentProviderOperation
-		.newInsert(ContactsContract.Data.CONTENT_URI), entry, rawContactID);
+	    ContentProviderOperation.Builder builder = buildStructuredName(entry, rawContactID);
 	    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 );
+	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(ContentProviderOperation.Builder builder,
-	RosterEntry entry, long rawContactID) {
+    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.Data.RAW_CONTACT_ID, rawContactID);
-	builder.withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
-	builder.withValue(StructuredName.DISPLAY_NAME, displayName);
+	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 BuildImProtocol(RosterEntry entry, long rawContactID) {
+    private static ContentProviderOperation.Builder BuildProfile(RosterEntry entry, long rawContactID) {
 	ContentProviderOperation.Builder builder = ContentProviderOperation
-	    .newInsert(ContactsContract.StatusUpdates.CONTENT_URI);
-	builder.withValue(ContactsContract.StatusUpdates.DATA_ID, rawContactID);
-	builder.withValue(ContactsContract.StatusUpdates.PROTOCOL, ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER);
-	//TODO: ajouter le protocol jabber
+	    .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;
 	final Cursor c = mContentResolver.query(ContactsContract.RawContacts.CONTENT_URI, new String[] {
--- a/src/com/beem/project/beem/ui/Chat.java	Thu Apr 21 21:05:25 2011 +0200
+++ b/src/com/beem/project/beem/ui/Chat.java	Sat May 07 12:26:45 2011 +0200
@@ -64,6 +64,7 @@
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
+import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Color;
@@ -75,6 +76,8 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.preference.PreferenceManager;
+import android.provider.ContactsContract;
+import android.provider.ContactsContract.RawContacts.Entity;
 import android.text.util.Linkify;
 import android.util.Log;
 import android.view.KeyEvent;
@@ -197,11 +200,20 @@
     @Override
     protected void onResume() {
 	super.onResume();
-	mContact = new Contact(getIntent().getData());
-	if (!mBinded) {
-	    bindService(SERVICE_INTENT, mConn, BIND_AUTO_CREATE);
-	    mBinded = true;
+	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);
+	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;
+	//	}
     }
 
     /**
@@ -950,14 +962,14 @@
     public class MyReceiver extends BroadcastReceiver {
 
 	@Override
-	 public void onReceive(Context context, Intent intent) {
-	        Context c = getApplicationContext();
-	  Intent sendIntent = new Intent();
-	  if ( intent.getAction().equals(Intent.ACTION_SENDTO) ) {
-	   sendIntent.setClass(c, Chat.class);
-	   context.startActivity(sendIntent);
-	  }
-	 }
+	public void onReceive(Context context, Intent intent) {
+	    Context c = getApplicationContext();
+	    Intent sendIntent = new Intent();
+	    if (intent.getAction().equals(Intent.ACTION_SENDTO)) {
+		sendIntent.setClass(c, Chat.class);
+		context.startActivity(sendIntent);
+	    }
+	}
     }
 
 }
--- a/src/com/beem/project/beem/ui/Login.java	Thu Apr 21 21:05:25 2011 +0200
+++ b/src/com/beem/project/beem/ui/Login.java	Sat May 07 12:26:45 2011 +0200
@@ -211,8 +211,6 @@
 	    Account allAccount[] = am.getAccountsByType("com.beem.project.com");
 	    for (Account account : allAccount) {
 		mListAccount.add(account.name);
-		mListAccount.add(account.name);
-
 	    }
 	}