Use a common ImageView for avatar and status
authorDa Risk <darisk972@gmail.com>
Tue, 21 Dec 2010 23:26:51 +0100
changeset 829 4b36a3c452e6
parent 809 2655ee22df9f
child 830 e6f793612724
Use a common ImageView for avatar and status
res/drawable/avatar_status.xml
res/layout/chat.xml
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/ui/Chat.java
src/com/beem/project/beem/ui/ContactList.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/drawable/avatar_status.xml	Tue Dec 21 23:26:51 2010 +0100
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Layer Level list drawable for Avatar and status icon
+    See src/com/beem/project/beem/utils/Status.java
+    for level values to change the status.
+    The status icon must be resized using method
+    LayerDrawable.setLayerInset();
+    The drawable with id @id/avatar must be replace by the real
+    avatar using the method LayerDrawable.setDrawableByLayerId()
+-->
+<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:id="@+id/avatar">
+	<shape/>
+    </item>
+    <item android:drawable="@drawable/status_icon" />
+</layer-list>
--- a/res/layout/chat.xml	Tue Nov 02 00:46:12 2010 +0100
+++ b/res/layout/chat.xml	Tue Dec 21 23:26:51 2010 +0100
@@ -7,9 +7,10 @@
 		android:orientation="horizontal" android:gravity="center_vertical"
 		android:background="#222222" android:padding="4px">
 		<ImageView android:id="@+id/chat_contact_status_icon"
-			android:src="@drawable/status_icon"
-			android:adjustViewBounds="true" android:layout_width="wrap_content"
-			android:layout_height="wrap_content" android:gravity="center_vertical" />
+			android:src="@drawable/avatar_status"
+			android:layout_width="48dip"
+			android:layout_height="48dip"
+			/>
 		<LinearLayout android:orientation="vertical"
 			android:layout_width="fill_parent" android:layout_height="wrap_content"
 			android:paddingLeft="15sp">
--- a/src/com/beem/project/beem/BeemService.java	Tue Nov 02 00:46:12 2010 +0100
+++ b/src/com/beem/project/beem/BeemService.java	Tue Dec 21 23:26:51 2010 +0100
@@ -154,7 +154,7 @@
 	    || mSettings.getBoolean("settings_key_gmail", false)) {
 	    mConnectionConfiguration.setSecurityMode(SecurityMode.required);
 	}
-	mConnectionConfiguration.setDebuggerEnabled(true);
+	mConnectionConfiguration.setDebuggerEnabled(false);
 	mConnectionConfiguration.setSendPresence(true);
 	// maybe not the universal path, but it works on most devices (Samsung Galaxy, Google Nexus One)
 	mConnectionConfiguration.setTruststoreType("BKS");
--- a/src/com/beem/project/beem/ui/Chat.java	Tue Nov 02 00:46:12 2010 +0100
+++ b/src/com/beem/project/beem/ui/Chat.java	Tue Dec 21 23:26:51 2010 +0100
@@ -61,6 +61,8 @@
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Color;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.LayerDrawable;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -83,6 +85,8 @@
 import android.widget.ListView;
 import android.widget.TextView;
 
+import java.io.ByteArrayInputStream;
+
 import com.beem.project.beem.R;
 import com.beem.project.beem.service.Contact;
 import com.beem.project.beem.service.Message;
@@ -121,6 +125,7 @@
     private ListView mMessagesListView;
     private EditText mInputField;
     private Button mSendButton;
+    private LayerDrawable mAvatarStatusDrawable;
     private final Map<Integer, Bitmap> mStatusIconsMap = new HashMap<Integer, Bitmap>();
 
     private final List<MessageText> mListMessages = new ArrayList<MessageText>();
@@ -158,6 +163,8 @@
 	mContactStatusMsgTextView = (TextView) findViewById(R.id.chat_contact_status_msg);
 	mContactChatState = (TextView) findViewById(R.id.chat_contact_chat_state);
 	mContactStatusIcon = (ImageView) findViewById(R.id.chat_contact_status_icon);
+	mAvatarStatusDrawable = (LayerDrawable) mContactStatusIcon.getDrawable();
+	mAvatarStatusDrawable.setLayerInset(1, 36, 36, 0, 0);
 	mMessagesListView = (ListView) findViewById(R.id.chat_messages);
 	mMessagesListView.setAdapter(mMessagesListAdapter);
 	mInputField = (EditText) findViewById(R.id.chat_input);
@@ -562,9 +569,27 @@
      * Update the contact status icon.
      */
     private void updateContactStatusIcon() {
+	Drawable avatar = getAvatarDrawable(mContact.getAvatarId());
+	mAvatarStatusDrawable.setDrawableByLayerId(R.id.avatar, avatar);
 	mContactStatusIcon.setImageLevel(mContact.getStatus());
     }
 
+    private Drawable getAvatarDrawable(String avatarId) {
+	Drawable avatarDrawable = null;
+	try {
+	    byte[] avatar = mXmppFacade.getAvatar(avatarId);
+	    if (avatar != null) {
+		ByteArrayInputStream in = new ByteArrayInputStream(avatar);
+		avatarDrawable = Drawable.createFromStream(in, avatarId);
+	    }
+	} catch (RemoteException e) {
+	    Log.e(TAG, "Error while setting the avatar", e);
+	}
+	if (avatarDrawable == null)
+	    avatarDrawable = getResources().getDrawable(R.drawable.beem_launcher_icon_silver);
+	return avatarDrawable;
+    }
+
     /**
      * Prepare the status icons map.
      */
--- a/src/com/beem/project/beem/ui/ContactList.java	Tue Nov 02 00:46:12 2010 +0100
+++ b/src/com/beem/project/beem/ui/ContactList.java	Tue Dec 21 23:26:51 2010 +0100
@@ -60,7 +60,6 @@
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
-import android.graphics.drawable.LevelListDrawable;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -87,6 +86,7 @@
 import android.widget.ListView;
 import android.widget.TextView;
 import android.graphics.drawable.Drawable;
+import android.graphics.drawable.LayerDrawable;
 
 import com.beem.project.beem.R;
 import com.beem.project.beem.service.Contact;
@@ -656,30 +656,36 @@
 	private void bindView(View view, Contact curContact) {
 	    if (curContact != null) {
 		TextView v = (TextView) view.findViewById(R.id.contactlistpseudo);
-		LevelListDrawable mStatusDrawable = (LevelListDrawable) getResources()
-		.getDrawable(R.drawable.status_icon);
-		mStatusDrawable.setLevel(curContact.getStatus());
-		v.setCompoundDrawablesWithIntrinsicBounds(mStatusDrawable, null, null, null);
 		v.setText(curContact.getName());
 		v = (TextView) view.findViewById(R.id.contactlistmsgperso);
 		v.setText(curContact.getMsgState());
-		Drawable d = null;
-		try {
-		    String avatarId = curContact.getAvatarId();
-		    byte[] avatar = mXmppFacade.getAvatar(avatarId);
-		    if (avatar != null) {
-			ByteArrayInputStream in = new ByteArrayInputStream(avatar);
-			d = Drawable.createFromStream(in, avatarId);
-		    }
-		} catch (RemoteException e) {
-		    Log.e(TAG, "Error while setting the avatar", e);
+		ImageView img = (ImageView) view.findViewById(R.id.avatar);
+		String avatarId = curContact.getAvatarId();
+		int contactStatus = curContact.getStatus();
+		Drawable avatar = getAvatarStatusDrawable(curContact.getAvatarId(),
+			curContact.getStatus());
+		img.setImageDrawable(avatar);
+		img.setImageLevel(contactStatus);
+	    }
+	}
+
+	private Drawable getAvatarStatusDrawable(String avatarId, int contactStatus) {
+	    Drawable avatarDrawable = null;
+	    try {
+		byte[] avatar = mXmppFacade.getAvatar(avatarId);
+		if (avatar != null) {
+		    ByteArrayInputStream in = new ByteArrayInputStream(avatar);
+		    avatarDrawable = Drawable.createFromStream(in, avatarId);
 		}
-		ImageView img = (ImageView) view.findViewById(R.id.avatar);
-		if (d != null)
-		    img.setImageDrawable(d);
-		else
-		    img.setImageResource(R.drawable.beem_launcher_icon_silver);
+	    } catch (RemoteException e) {
+		Log.e(TAG, "Error while setting the avatar", e);
 	    }
+	    if (avatarDrawable == null)
+		avatarDrawable = getResources().getDrawable(R.drawable.beem_launcher_icon_silver);
+	    LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.avatar_status);
+	    ld.setLayerInset(1, 36, 36, 0, 0);
+	    ld.setDrawableByLayerId(R.id.avatar, avatarDrawable);
+	    return ld;
 	}
 
 	/**