Use a content resolver to get the avatar in UI.
authorDa Risk <darisk972@gmail.com>
Sat, 19 Feb 2011 22:07:02 +0100
changeset 866 0173963643d1
parent 865 70ca3ab6e459
child 867 11136daa9e81
Use a content resolver to get the avatar in UI.
src/com/beem/project/beem/ui/Chat.java
src/com/beem/project/beem/ui/ContactList.java
--- a/src/com/beem/project/beem/ui/Chat.java	Sat Feb 19 22:06:26 2011 +0100
+++ b/src/com/beem/project/beem/ui/Chat.java	Sat Feb 19 22:07:02 2011 +0100
@@ -49,6 +49,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.io.InputStream;
+import java.io.IOException;
 
 import org.jivesoftware.smack.packet.Presence.Mode;
 import org.jivesoftware.smack.util.StringUtils;
@@ -65,6 +67,7 @@
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Color;
+import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -88,9 +91,9 @@
 import android.widget.ListView;
 import android.widget.TextView;
 
-import java.io.ByteArrayInputStream;
 
 import com.beem.project.beem.R;
+import com.beem.project.beem.providers.AvatarProvider;
 import com.beem.project.beem.service.Contact;
 import com.beem.project.beem.service.Message;
 import com.beem.project.beem.service.PresenceAdapter;
@@ -143,6 +146,7 @@
     private final BeemBroadcastReceiver mBroadcastReceiver = new BeemBroadcastReceiver();
     private final BeemRosterListener mBeemRosterListener = new BeemRosterListener();
     private IXmppFacade mXmppFacade;
+    private String mCurrentAvatarId;
     private boolean mBinded;
     private boolean mCompact;
 
@@ -588,8 +592,15 @@
     private void updateContactStatusIcon() {
 	if (mCompact)
 	    return;
-	Drawable avatar = getAvatarDrawable(mContact.getAvatarId());
-	mAvatarStatusDrawable.setDrawableByLayerId(R.id.avatar, avatar);
+	String id = mContact.getAvatarId();
+	if (id == null)
+	    id = "";
+	Log.d(TAG, "update contact icon  : " + id);
+	if (!id.equals(mCurrentAvatarId)) {
+	    Drawable avatar = getAvatarDrawable(mContact.getAvatarId());
+	    mAvatarStatusDrawable.setDrawableByLayerId(R.id.avatar, avatar);
+	    mCurrentAvatarId = id;
+	}
 	mContactStatusIcon.setImageLevel(mContact.getStatus());
     }
 
@@ -601,19 +612,25 @@
      */
     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);
+	if (avatarId != null) {
+	    Uri uri = AvatarProvider.CONTENT_URI.buildUpon().appendPath(avatarId).build();
+	    InputStream in = null;
+	    try {
+		try {
+		    in = getContentResolver().openInputStream(uri);
+	    avatarDrawable = Drawable.createFromStream(in, avatarId);
+		} finally {
+		    if (in != null)
+			in.close();
+		}
+	    } catch (IOException e) {
+		Log.w(TAG, "Error while setting the avatar", e);
 	    }
-	} 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	Sat Feb 19 22:06:26 2011 +0100
+++ b/src/com/beem/project/beem/ui/ContactList.java	Sat Feb 19 22:07:02 2011 +0100
@@ -49,7 +49,8 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.IOException;
 
 import org.jivesoftware.smack.util.StringUtils;
 
@@ -60,6 +61,7 @@
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
+import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -89,6 +91,7 @@
 import android.graphics.drawable.LayerDrawable;
 
 import com.beem.project.beem.R;
+import com.beem.project.beem.providers.AvatarProvider;
 import com.beem.project.beem.service.Contact;
 import com.beem.project.beem.service.PresenceAdapter;
 import com.beem.project.beem.service.aidl.IBeemRosterListener;
@@ -676,16 +679,20 @@
 	 */
 	private Drawable getAvatarStatusDrawable(String avatarId) {
 	    Drawable avatarDrawable = null;
-	    try {
-		if (mXmppFacade != null) {
-		    byte[] avatar = mXmppFacade.getAvatar(avatarId);
-		    if (avatar != null) {
-			ByteArrayInputStream in = new ByteArrayInputStream(avatar);
+	    if (avatarId != null) {
+		Uri uri = AvatarProvider.CONTENT_URI.buildUpon().appendPath(avatarId).build();
+		InputStream in = null;
+		try {
+		    try {
+			in = getContentResolver().openInputStream(uri);
 			avatarDrawable = Drawable.createFromStream(in, avatarId);
+		    } finally {
+			if (in != null)
+			    in.close();
 		    }
+		} catch (IOException e) {
+		    Log.w(TAG, "Error while setting the avatar", e);
 		}
-	    } catch (RemoteException e) {
-		Log.e(TAG, "Error while setting the avatar", e);
 	    }
 	    if (avatarDrawable == null)
 		avatarDrawable = getResources().getDrawable(R.drawable.beem_launcher_icon_silver);