load avatars in message adapter in background task

This commit is contained in:
Daniel Gultsch 2015-10-29 12:08:15 +01:00
parent a7fd629c05
commit 1221cff561
4 changed files with 115 additions and 17 deletions

View File

@ -16,6 +16,7 @@ import eu.siacs.conversations.entities.Bookmark;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
@ -179,9 +180,13 @@ public class AvatarService {
} }
public Bitmap get(Account account, int size) { public Bitmap get(Account account, int size) {
return get(account, size, false);
}
public Bitmap get(Account account, int size, boolean cachedOnly) {
final String KEY = key(account, size); final String KEY = key(account, size);
Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY); Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
if (avatar != null) { if (avatar != null || cachedOnly) {
return avatar; return avatar;
} }
avatar = mXmppConnectionService.getFileBackend().getAvatar( avatar = mXmppConnectionService.getFileBackend().getAvatar(
@ -193,6 +198,19 @@ public class AvatarService {
return avatar; return avatar;
} }
public Bitmap get(Message message, int size, boolean cachedOnly) {
if (message.getStatus() == Message.STATUS_RECEIVED) {
Contact contact = message.getContact();
if (contact != null) {
return get(contact, size, cachedOnly);
} else {
return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
}
} else {
return get(message.getConversation().getAccount(), size, cachedOnly);
}
}
public void clear(Account account) { public void clear(Account account) {
synchronized (this.sizes) { synchronized (this.sizes) {
for (Integer size : sizes) { for (Integer size : sizes) {
@ -291,5 +309,4 @@ public class AvatarService {
Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom); Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
canvas.drawBitmap(bm, null, dst, null); canvas.drawBitmap(bm, null, dst, null);
} }
} }

View File

@ -4,8 +4,13 @@ import android.content.ActivityNotFoundException;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.Spanned; import android.text.Spanned;
@ -24,7 +29,9 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import java.lang.ref.WeakReference;
import java.util.List; import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession; import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
@ -493,17 +500,8 @@ public class MessageAdapter extends ArrayAdapter<Message> {
viewHolder.status_message.setText(message.getBody()); viewHolder.status_message.setText(message.getBody());
} }
return view; return view;
} else if (type == RECEIVED) { } else {
Contact contact = message.getContact(); loadAvatar(message,viewHolder.contact_picture);
if (contact != null) {
viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(contact, activity.getPixel(48)));
} else if (conversation.getMode() == Conversation.MODE_MULTI) {
viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(
UIHelper.getMessageDisplayName(message),
activity.getPixel(48)));
}
} else if (type == SENT) {
viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(account, activity.getPixel(48)));
} }
viewHolder.contact_picture viewHolder.contact_picture
@ -674,4 +672,87 @@ public class MessageAdapter extends ArrayAdapter<Message> {
protected TextView status_message; protected TextView status_message;
protected TextView encryption; protected TextView encryption;
} }
class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Message message = null;
public BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(Message... params) {
return activity.avatarService().get(params[0], activity.getPixel(48), isCancelled());
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setBackgroundColor(0x00000000);
}
}
}
}
public void loadAvatar(Message message, ImageView imageView) {
if (cancelPotentialWork(message, imageView)) {
final Bitmap bm = activity.avatarService().get(message, activity.getPixel(48), true);
if (bm != null) {
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setBackgroundColor(UIHelper.getColorForName(UIHelper.getMessageDisplayName(message)));
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
imageView.setImageDrawable(asyncDrawable);
try {
task.execute(message);
} catch (final RejectedExecutionException ignored) {
}
}
}
}
public static boolean cancelPotentialWork(Message message, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final Message oldMessage = bitmapWorkerTask.message;
if (oldMessage == null || message != oldMessage) {
bitmapWorkerTask.cancel(true);
} else {
return false;
}
}
return true;
}
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
} }

View File

@ -11,8 +11,8 @@
<com.makeramen.roundedimageview.RoundedImageView <com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/message_photo" android:id="@+id/message_photo"
android:layout_width="wrap_content" android:layout_width="48dp"
android:layout_height="wrap_content" android:layout_height="48dp"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:scaleType="fitXY" android:scaleType="fitXY"

View File

@ -11,8 +11,8 @@
<com.makeramen.roundedimageview.RoundedImageView <com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/message_photo" android:id="@+id/message_photo"
android:layout_width="wrap_content" android:layout_width="48dp"
android:layout_height="wrap_content" android:layout_height="48dp"
android:scaleType="fitXY" android:scaleType="fitXY"
android:paddingBottom="3dp" android:paddingBottom="3dp"
android:src="@drawable/ic_profile" android:src="@drawable/ic_profile"