make bitmap worker task static; migrate conversation list row to binder

This commit is contained in:
Daniel Gultsch 2019-01-24 12:27:57 +01:00
parent 11e0ff4006
commit 04d1b92a79
8 changed files with 425 additions and 415 deletions

View File

@ -893,7 +893,7 @@ public abstract class XmppActivity extends ActionBarActivity {
if (cancelPotentialWork(message, imageView)) { if (cancelPotentialWork(message, imageView)) {
imageView.setBackgroundColor(0xff333333); imageView.setBackgroundColor(0xff333333);
imageView.setImageDrawable(null); imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(this, imageView); final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable( final AsyncDrawable asyncDrawable = new AsyncDrawable(
getResources(), null, task); getResources(), null, task);
imageView.setImageDrawable(asyncDrawable); imageView.setImageDrawable(asyncDrawable);
@ -944,11 +944,9 @@ public abstract class XmppActivity extends ActionBarActivity {
static class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> { static class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
private final WeakReference<XmppActivity> activity;
private Message message = null; private Message message = null;
private BitmapWorkerTask(XmppActivity activity, ImageView imageView) { private BitmapWorkerTask(ImageView imageView) {
this.activity = new WeakReference<>(activity);
this.imageViewReference = new WeakReference<>(imageView); this.imageViewReference = new WeakReference<>(imageView);
} }
@ -959,7 +957,7 @@ public abstract class XmppActivity extends ActionBarActivity {
} }
message = params[0]; message = params[0];
try { try {
XmppActivity activity = this.activity.get(); final XmppActivity activity = find(imageViewReference);
if (activity != null && activity.xmppConnectionService != null) { if (activity != null && activity.xmppConnectionService != null) {
return activity.xmppConnectionService.getFileBackend().getThumbnail(message, (int) (activity.metrics.density * 288), false); return activity.xmppConnectionService.getFileBackend().getThumbnail(message, (int) (activity.metrics.density * 288), false);
} else { } else {
@ -994,4 +992,16 @@ public abstract class XmppActivity extends ActionBarActivity {
return bitmapWorkerTaskReference.get(); return bitmapWorkerTaskReference.get();
} }
} }
public static XmppActivity find(WeakReference<ImageView> viewWeakReference) {
final View view = viewWeakReference.get();
if (view == null) {
return null;
}
final Context context = view.getContext();
if (context instanceof XmppActivity) {
return (XmppActivity) context;
}
return null;
}
} }

View File

@ -97,17 +97,21 @@ public class AccountAdapter extends ArrayAdapter<Account> {
} }
} }
class BitmapWorkerTask extends AsyncTask<Account, Void, Bitmap> { private static class BitmapWorkerTask extends AsyncTask<Account, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
private Account account = null; private Account account = null;
public BitmapWorkerTask(ImageView imageView) { BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView); imageViewReference = new WeakReference<>(imageView);
} }
@Override @Override
protected Bitmap doInBackground(Account... params) { protected Bitmap doInBackground(Account... params) {
this.account = params[0]; this.account = params[0];
final XmppActivity activity = XmppActivity.find(imageViewReference);
if (activity == null) {
return null;
}
return activity.avatarService().get(this.account, activity.getPixel(48), isCancelled()); return activity.avatarService().get(this.account, activity.getPixel(48), isCancelled());
} }
@ -123,7 +127,7 @@ public class AccountAdapter extends ArrayAdapter<Account> {
} }
} }
public void loadAvatar(Account account, ImageView imageView) { private void loadAvatar(Account account, ImageView imageView) {
if (cancelPotentialWork(account, imageView)) { if (cancelPotentialWork(account, imageView)) {
final Bitmap bm = activity.avatarService().get(account, activity.getPixel(48), true); final Bitmap bm = activity.avatarService().get(account, activity.getPixel(48), true);
if (bm != null) { if (bm != null) {
@ -149,7 +153,7 @@ public class AccountAdapter extends ArrayAdapter<Account> {
void onClickTglAccountState(Account account, boolean state); void onClickTglAccountState(Account account, boolean state);
} }
public static boolean cancelPotentialWork(Account account, ImageView imageView) { private static boolean cancelPotentialWork(Account account, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) { if (bitmapWorkerTask != null) {
@ -177,12 +181,12 @@ public class AccountAdapter extends ArrayAdapter<Account> {
static class AsyncDrawable extends BitmapDrawable { static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap); super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
} }
public BitmapWorkerTask getBitmapWorkerTask() { BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get(); return bitmapWorkerTaskReference.get();
} }
} }

View File

@ -2,6 +2,7 @@ package eu.siacs.conversations.ui.adapter;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
@ -22,6 +23,7 @@ import java.util.List;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.ConversationListRowBinding;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.entities.Transferable;
@ -73,9 +75,7 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte
@NonNull @NonNull
@Override @Override
public ConversationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public ConversationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); return new ConversationViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.conversation_list_row, parent, false));
View view = inflater.inflate(R.layout.conversation_list_row, parent, false);
return ConversationViewHolder.get(view);
} }
@Override @Override
@ -86,15 +86,15 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte
} }
CharSequence name = conversation.getName(); CharSequence name = conversation.getName();
if (name instanceof Jid) { if (name instanceof Jid) {
viewHolder.name.setText(IrregularUnicodeDetector.style(activity, (Jid) name)); viewHolder.binding.conversationName.setText(IrregularUnicodeDetector.style(activity, (Jid) name));
} else { } else {
viewHolder.name.setText(EmojiWrapper.transform(name)); viewHolder.binding.conversationName.setText(EmojiWrapper.transform(name));
} }
if (conversation == ConversationFragment.getConversation(activity)) { if (conversation == ConversationFragment.getConversation(activity)) {
viewHolder.frame.setBackgroundColor(StyledAttributes.getColor(activity,R.attr.color_background_tertiary)); viewHolder.binding.frame.setBackgroundColor(StyledAttributes.getColor(activity, R.attr.color_background_tertiary));
} else { } else {
viewHolder.frame.setBackgroundColor(StyledAttributes.getColor(activity,R.attr.color_background_primary)); viewHolder.binding.frame.setBackgroundColor(StyledAttributes.getColor(activity, R.attr.color_background_primary));
} }
Message message = conversation.getLatestMessage(); Message message = conversation.getLatestMessage();
@ -102,25 +102,25 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte
final boolean isRead = conversation.isRead(); final boolean isRead = conversation.isRead();
final Conversation.Draft draft = isRead ? conversation.getDraft() : null; final Conversation.Draft draft = isRead ? conversation.getDraft() : null;
if (unreadCount > 0) { if (unreadCount > 0) {
viewHolder.unreadCount.setVisibility(View.VISIBLE); viewHolder.binding.unreadCount.setVisibility(View.VISIBLE);
viewHolder.unreadCount.setUnreadCount(unreadCount); viewHolder.binding.unreadCount.setUnreadCount(unreadCount);
} else { } else {
viewHolder.unreadCount.setVisibility(View.GONE); viewHolder.binding.unreadCount.setVisibility(View.GONE);
} }
if (isRead) { if (isRead) {
viewHolder.name.setTypeface(null, Typeface.NORMAL); viewHolder.binding.conversationName.setTypeface(null, Typeface.NORMAL);
} else { } else {
viewHolder.name.setTypeface(null, Typeface.BOLD); viewHolder.binding.conversationName.setTypeface(null, Typeface.BOLD);
} }
if (draft != null) { if (draft != null) {
viewHolder.lastMessageIcon.setVisibility(View.GONE); viewHolder.binding.conversationLastmsgImg.setVisibility(View.GONE);
viewHolder.lastMessage.setText(EmojiWrapper.transform(draft.getMessage())); viewHolder.binding.conversationLastmsg.setText(EmojiWrapper.transform(draft.getMessage()));
viewHolder.sender.setText(R.string.draft); viewHolder.binding.senderName.setText(R.string.draft);
viewHolder.sender.setVisibility(View.VISIBLE); viewHolder.binding.senderName.setVisibility(View.VISIBLE);
viewHolder.lastMessage.setTypeface(null, Typeface.NORMAL); viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.NORMAL);
viewHolder.sender.setTypeface(null, Typeface.ITALIC); viewHolder.binding.senderName.setTypeface(null, Typeface.ITALIC);
} else { } else {
final boolean fileAvailable = !message.isDeleted(); final boolean fileAvailable = !message.isDeleted();
final boolean showPreviewText; final boolean showPreviewText;
@ -150,66 +150,66 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte
break; break;
} }
} }
viewHolder.lastMessageIcon.setImageResource(imageResource); viewHolder.binding.conversationLastmsgImg.setImageResource(imageResource);
viewHolder.lastMessageIcon.setVisibility(View.VISIBLE); viewHolder.binding.conversationLastmsgImg.setVisibility(View.VISIBLE);
} else { } else {
viewHolder.lastMessageIcon.setVisibility(View.GONE); viewHolder.binding.conversationLastmsgImg.setVisibility(View.GONE);
showPreviewText = true; showPreviewText = true;
} }
final Pair<CharSequence, Boolean> preview = UIHelper.getMessagePreview(activity, message, viewHolder.lastMessage.getCurrentTextColor()); final Pair<CharSequence, Boolean> preview = UIHelper.getMessagePreview(activity, message, viewHolder.binding.conversationLastmsg.getCurrentTextColor());
if (showPreviewText) { if (showPreviewText) {
viewHolder.lastMessage.setText(EmojiWrapper.transform(UIHelper.shorten(preview.first))); viewHolder.binding.conversationLastmsg.setText(EmojiWrapper.transform(UIHelper.shorten(preview.first)));
} else { } else {
viewHolder.lastMessageIcon.setContentDescription(preview.first); viewHolder.binding.conversationLastmsgImg.setContentDescription(preview.first);
} }
viewHolder.lastMessage.setVisibility(showPreviewText ? View.VISIBLE : View.GONE); viewHolder.binding.conversationLastmsg.setVisibility(showPreviewText ? View.VISIBLE : View.GONE);
if (preview.second) { if (preview.second) {
if (isRead) { if (isRead) {
viewHolder.lastMessage.setTypeface(null, Typeface.ITALIC); viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.ITALIC);
viewHolder.sender.setTypeface(null, Typeface.NORMAL); viewHolder.binding.senderName.setTypeface(null, Typeface.NORMAL);
} else { } else {
viewHolder.lastMessage.setTypeface(null, Typeface.BOLD_ITALIC); viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.BOLD_ITALIC);
viewHolder.sender.setTypeface(null, Typeface.BOLD); viewHolder.binding.senderName.setTypeface(null, Typeface.BOLD);
} }
} else { } else {
if (isRead) { if (isRead) {
viewHolder.lastMessage.setTypeface(null, Typeface.NORMAL); viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.NORMAL);
viewHolder.sender.setTypeface(null, Typeface.NORMAL); viewHolder.binding.senderName.setTypeface(null, Typeface.NORMAL);
} else { } else {
viewHolder.lastMessage.setTypeface(null, Typeface.BOLD); viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.BOLD);
viewHolder.sender.setTypeface(null, Typeface.BOLD); viewHolder.binding.senderName.setTypeface(null, Typeface.BOLD);
} }
} }
if (message.getStatus() == Message.STATUS_RECEIVED) { if (message.getStatus() == Message.STATUS_RECEIVED) {
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
viewHolder.sender.setVisibility(View.VISIBLE); viewHolder.binding.senderName.setVisibility(View.VISIBLE);
viewHolder.sender.setText(UIHelper.getMessageDisplayName(message).split("\\s+")[0] + ':'); viewHolder.binding.senderName.setText(UIHelper.getMessageDisplayName(message).split("\\s+")[0] + ':');
} else { } else {
viewHolder.sender.setVisibility(View.GONE); viewHolder.binding.senderName.setVisibility(View.GONE);
} }
} else if (message.getType() != Message.TYPE_STATUS) { } else if (message.getType() != Message.TYPE_STATUS) {
viewHolder.sender.setVisibility(View.VISIBLE); viewHolder.binding.senderName.setVisibility(View.VISIBLE);
viewHolder.sender.setText(activity.getString(R.string.me) + ':'); viewHolder.binding.senderName.setText(activity.getString(R.string.me) + ':');
} else { } else {
viewHolder.sender.setVisibility(View.GONE); viewHolder.binding.senderName.setVisibility(View.GONE);
} }
} }
long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL, 0); long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL, 0);
if (muted_till == Long.MAX_VALUE) { if (muted_till == Long.MAX_VALUE) {
viewHolder.notificationIcon.setVisibility(View.VISIBLE); viewHolder.binding.notificationStatus.setVisibility(View.VISIBLE);
int ic_notifications_off = activity.getThemeResource(R.attr.icon_notifications_off, R.drawable.ic_notifications_off_black_24dp); int ic_notifications_off = activity.getThemeResource(R.attr.icon_notifications_off, R.drawable.ic_notifications_off_black_24dp);
viewHolder.notificationIcon.setImageResource(ic_notifications_off); viewHolder.binding.notificationStatus.setImageResource(ic_notifications_off);
} else if (muted_till >= System.currentTimeMillis()) { } else if (muted_till >= System.currentTimeMillis()) {
viewHolder.notificationIcon.setVisibility(View.VISIBLE); viewHolder.binding.notificationStatus.setVisibility(View.VISIBLE);
int ic_notifications_paused = activity.getThemeResource(R.attr.icon_notifications_paused, R.drawable.ic_notifications_paused_black_24dp); int ic_notifications_paused = activity.getThemeResource(R.attr.icon_notifications_paused, R.drawable.ic_notifications_paused_black_24dp);
viewHolder.notificationIcon.setImageResource(ic_notifications_paused); viewHolder.binding.notificationStatus.setImageResource(ic_notifications_paused);
} else if (conversation.alwaysNotify()) { } else if (conversation.alwaysNotify()) {
viewHolder.notificationIcon.setVisibility(View.GONE); viewHolder.binding.notificationStatus.setVisibility(View.GONE);
} else { } else {
viewHolder.notificationIcon.setVisibility(View.VISIBLE); viewHolder.binding.notificationStatus.setVisibility(View.VISIBLE);
int ic_notifications_none = activity.getThemeResource(R.attr.icon_notifications_none, R.drawable.ic_notifications_none_black_24dp); int ic_notifications_none = activity.getThemeResource(R.attr.icon_notifications_none, R.drawable.ic_notifications_none_black_24dp);
viewHolder.notificationIcon.setImageResource(ic_notifications_none); viewHolder.binding.notificationStatus.setImageResource(ic_notifications_none);
} }
long timestamp; long timestamp;
@ -218,9 +218,9 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte
} else { } else {
timestamp = conversation.getLatestMessage().getTimeSent(); timestamp = conversation.getLatestMessage().getTimeSent();
} }
viewHolder.timestamp.setText(UIHelper.readableTimeDifference(activity, timestamp)); viewHolder.binding.conversationLastupdate.setText(UIHelper.readableTimeDifference(activity, timestamp));
loadAvatar(conversation, viewHolder.avatar); loadAvatar(conversation, viewHolder.binding.conversationImage);
viewHolder.itemView.setOnClickListener(v -> listener.onConversationClick(v,conversation)); viewHolder.itemView.setOnClickListener(v -> listener.onConversationClick(v, conversation));
} }
@Override @Override
@ -254,73 +254,58 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte
} }
public void insert(Conversation c, int position) { public void insert(Conversation c, int position) {
conversations.add(position,c); conversations.add(position, c);
notifyDataSetChanged(); notifyDataSetChanged();
} }
public void remove(Conversation conversation,int position) { public void remove(Conversation conversation, int position) {
conversations.remove(conversation); conversations.remove(conversation);
notifyItemRemoved(position); notifyItemRemoved(position);
} }
public static class ConversationViewHolder extends RecyclerView.ViewHolder { public interface OnConversationClickListener {
private TextView name; void onConversationClick(View view, Conversation conversation);
private TextView lastMessage;
private ImageView lastMessageIcon;
private TextView sender;
private TextView timestamp;
private ImageView notificationIcon;
private UnreadCountCustomView unreadCount;
private ImageView avatar;
private FrameLayout frame;
private ConversationViewHolder(View view) {
super(view);
} }
public static ConversationViewHolder get(View layout) { static class ConversationViewHolder extends RecyclerView.ViewHolder {
ConversationViewHolder conversationViewHolder = (ConversationViewHolder) layout.getTag(); private final ConversationListRowBinding binding;
if (conversationViewHolder == null) {
conversationViewHolder = new ConversationViewHolder(layout); private ConversationViewHolder(ConversationListRowBinding binding) {
conversationViewHolder.frame = layout.findViewById(R.id.frame); super(binding.getRoot());
conversationViewHolder.name = layout.findViewById(R.id.conversation_name); this.binding = binding;
conversationViewHolder.lastMessage = layout.findViewById(R.id.conversation_lastmsg);
conversationViewHolder.lastMessageIcon = layout.findViewById(R.id.conversation_lastmsg_img);
conversationViewHolder.timestamp = layout.findViewById(R.id.conversation_lastupdate);
conversationViewHolder.sender = layout.findViewById(R.id.sender_name);
conversationViewHolder.notificationIcon = layout.findViewById(R.id.notification_status);
conversationViewHolder.unreadCount = layout.findViewById(R.id.unread_count);
conversationViewHolder.avatar = layout.findViewById(R.id.conversation_image);
layout.setTag(conversationViewHolder);
}
return conversationViewHolder;
} }
} }
static class AsyncDrawable extends BitmapDrawable { static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap); super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
} }
public BitmapWorkerTask getBitmapWorkerTask() { BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get(); return bitmapWorkerTaskReference.get();
} }
} }
class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> { static class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
private Conversation conversation = null; private Conversation conversation = null;
public BitmapWorkerTask(ImageView imageView) { BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView); imageViewReference = new WeakReference<>(imageView);
} }
@Override @Override
protected Bitmap doInBackground(Conversation... params) { protected Bitmap doInBackground(Conversation... params) {
this.conversation = params[0]; this.conversation = params[0];
final XmppActivity activity = XmppActivity.find(imageViewReference);
if (activity == null) {
return null;
}
return activity.avatarService().get(this.conversation, activity.getPixel(56), isCancelled()); return activity.avatarService().get(this.conversation, activity.getPixel(56), isCancelled());
} }
@ -335,8 +320,4 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte
} }
} }
} }
public interface OnConversationClickListener {
void onConversationClick(View view, Conversation conversation);
}
} }

View File

@ -34,17 +34,14 @@ import rocks.xmpp.addr.Jid;
public class ListItemAdapter extends ArrayAdapter<ListItem> { public class ListItemAdapter extends ArrayAdapter<ListItem> {
protected XmppActivity activity; protected XmppActivity activity;
protected boolean showDynamicTags = false; private boolean showDynamicTags = false;
private OnTagClickedListener mOnTagClickedListener = null; private OnTagClickedListener mOnTagClickedListener = null;
private View.OnClickListener onTagTvClick = new View.OnClickListener() { private View.OnClickListener onTagTvClick = view -> {
@Override
public void onClick(View view) {
if (view instanceof TextView && mOnTagClickedListener != null) { if (view instanceof TextView && mOnTagClickedListener != null) {
TextView tv = (TextView) view; TextView tv = (TextView) view;
final String tag = tv.getText().toString(); final String tag = tv.getText().toString();
mOnTagClickedListener.onTagClicked(tag); mOnTagClickedListener.onTagClicked(tag);
} }
}
}; };
public ListItemAdapter(XmppActivity activity, List<ListItem> objects) { public ListItemAdapter(XmppActivity activity, List<ListItem> objects) {
@ -52,7 +49,7 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
this.activity = activity; this.activity = activity;
} }
public static boolean cancelPotentialWork(ListItem item, ImageView imageView) { private static boolean cancelPotentialWork(ListItem item, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) { if (bitmapWorkerTask != null) {
@ -125,7 +122,7 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
this.mOnTagClickedListener = listener; this.mOnTagClickedListener = listener;
} }
public void loadAvatar(ListItem item, ImageView imageView) { private void loadAvatar(ListItem item, ImageView imageView) {
if (cancelPotentialWork(item, imageView)) { if (cancelPotentialWork(item, imageView)) {
final Bitmap bm = activity.avatarService().get(item, activity.getPixel(48), true); final Bitmap bm = activity.avatarService().get(item, activity.getPixel(48), true);
if (bm != null) { if (bm != null) {
@ -175,27 +172,31 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
static class AsyncDrawable extends BitmapDrawable { static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap); super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
} }
public BitmapWorkerTask getBitmapWorkerTask() { BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get(); return bitmapWorkerTaskReference.get();
} }
} }
class BitmapWorkerTask extends AsyncTask<ListItem, Void, Bitmap> { private static class BitmapWorkerTask extends AsyncTask<ListItem, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
private ListItem item = null; private ListItem item = null;
public BitmapWorkerTask(ImageView imageView) { BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView); imageViewReference = new WeakReference<>(imageView);
} }
@Override @Override
protected Bitmap doInBackground(ListItem... params) { protected Bitmap doInBackground(ListItem... params) {
this.item = params[0]; this.item = params[0];
final XmppActivity activity = XmppActivity.find(imageViewReference);
if (activity == null) {
return null;
}
return activity.avatarService().get(this.item, activity.getPixel(48), isCancelled()); return activity.avatarService().get(this.item, activity.getPixel(48), isCancelled());
} }

View File

@ -88,7 +88,7 @@ public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHol
return attr; return attr;
} }
public static void renderPreview(Context context, Attachment attachment, ImageView imageView) { static void renderPreview(Context context, Attachment attachment, ImageView imageView) {
imageView.setBackgroundColor(StyledAttributes.getColor(context, R.attr.color_background_tertiary)); imageView.setBackgroundColor(StyledAttributes.getColor(context, R.attr.color_background_tertiary));
imageView.setImageAlpha(Math.round(StyledAttributes.getFloat(context, R.attr.icon_alpha) * 255)); imageView.setImageAlpha(Math.round(StyledAttributes.getFloat(context, R.attr.icon_alpha) * 255));
imageView.setImageDrawable(StyledAttributes.getDrawable(context, getImageAttr(attachment))); imageView.setImageDrawable(StyledAttributes.getDrawable(context, getImageAttr(attachment)));
@ -160,7 +160,7 @@ public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHol
} else { } else {
imageView.setBackgroundColor(0xff333333); imageView.setBackgroundColor(0xff333333);
imageView.setImageDrawable(null); imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final BitmapWorkerTask task = new BitmapWorkerTask(mediaSize, imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task); final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
imageView.setImageDrawable(asyncDrawable); imageView.setImageDrawable(asyncDrawable);
try { try {
@ -199,17 +199,23 @@ public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHol
} }
} }
class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> { private static class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
private Attachment attachment = null; private Attachment attachment = null;
private final int mediaSize;
BitmapWorkerTask(ImageView imageView) { BitmapWorkerTask(int mediaSize, ImageView imageView) {
this.mediaSize = mediaSize;
imageViewReference = new WeakReference<>(imageView); imageViewReference = new WeakReference<>(imageView);
} }
@Override @Override
protected Bitmap doInBackground(Attachment... params) { protected Bitmap doInBackground(Attachment... params) {
this.attachment = params[0]; this.attachment = params[0];
final XmppActivity activity = XmppActivity.find(imageViewReference);
if (activity == null) {
return null;
}
return activity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, mediaSize, false); return activity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, mediaSize, false);
} }

View File

@ -157,7 +157,7 @@ public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapte
} }
} }
class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> { private static class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
private Attachment attachment = null; private Attachment attachment = null;
@ -167,14 +167,12 @@ public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapte
@Override @Override
protected Bitmap doInBackground(Attachment... params) { protected Bitmap doInBackground(Attachment... params) {
Activity activity = conversationFragment.getActivity();
if (activity instanceof XmppActivity) {
final XmppActivity xmppActivity = (XmppActivity) activity;
this.attachment = params[0]; this.attachment = params[0];
return xmppActivity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, Math.round(xmppActivity.getResources().getDimension(R.dimen.media_preview_size)), false); final XmppActivity activity = XmppActivity.find(imageViewReference);
} else { if (activity == null) {
return null; return null;
} }
return activity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, Math.round(activity.getResources().getDimension(R.dimen.media_preview_size)), false);
} }
@Override @Override

View File

@ -902,7 +902,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
ViewUtil.view(activity, file); ViewUtil.view(activity, file);
} }
public void showLocation(Message message) { private void showLocation(Message message) {
for (Intent intent : GeoHelper.createGeoIntentsFromMessage(activity, message)) { for (Intent intent : GeoHelper.createGeoIntentsFromMessage(activity, message)) {
if (intent.resolveActivity(getContext().getPackageManager()) != null) { if (intent.resolveActivity(getContext().getPackageManager()) != null) {
getContext().startActivity(intent); getContext().startActivity(intent);
@ -918,7 +918,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
this.mUseGreenBackground = p.getBoolean("use_green_background", activity.getResources().getBoolean(R.bool.use_green_background)); this.mUseGreenBackground = p.getBoolean("use_green_background", activity.getResources().getBoolean(R.bool.use_green_background));
} }
public void loadAvatar(Message message, ImageView imageView, int size) { private void loadAvatar(Message message, ImageView imageView, int size) {
if (cancelPotentialWork(message, imageView)) { if (cancelPotentialWork(message, imageView)) {
final Bitmap bm = activity.avatarService().get(message, size, true); final Bitmap bm = activity.avatarService().get(message, size, true);
if (bm != null) { if (bm != null) {
@ -1037,12 +1037,12 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
} }
} }
class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> { private static class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
private final int size; private final int size;
private Message message = null; private Message message = null;
public BitmapWorkerTask(ImageView imageView, int size) { BitmapWorkerTask(ImageView imageView, int size) {
imageViewReference = new WeakReference<>(imageView); imageViewReference = new WeakReference<>(imageView);
this.size = size; this.size = size;
} }
@ -1050,6 +1050,10 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
@Override @Override
protected Bitmap doInBackground(Message... params) { protected Bitmap doInBackground(Message... params) {
this.message = params[0]; this.message = params[0];
final XmppActivity activity = XmppActivity.find(imageViewReference);
if (activity == null) {
return null;
}
return activity.avatarService().get(this.message, size, isCancelled()); return activity.avatarService().get(this.message, size, isCancelled());
} }

View File

@ -1,5 +1,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"> android:descendantFocusability="blocksDescendants">
@ -23,7 +25,7 @@
android:layout_height="56dp" android:layout_height="56dp"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:scaleType="centerCrop" android:scaleType="centerCrop"
app:riv_corner_radius="2dp"/> app:riv_corner_radius="2dp" />
<RelativeLayout <RelativeLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
@ -61,9 +63,10 @@
android:text="@string/me" android:text="@string/me"
android:layout_marginRight="?attr/TextSeparation" android:layout_marginRight="?attr/TextSeparation"
android:textAppearance="@style/TextAppearance.Conversations.Body1.Secondary" android:textAppearance="@style/TextAppearance.Conversations.Body1.Secondary"
android:visibility="visible"/> android:visibility="visible" />
<LinearLayout android:layout_width="match_parent" <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:orientation="horizontal" android:orientation="horizontal"
@ -71,11 +74,12 @@
android:layout_alignWithParentIfMissing="true" android:layout_alignWithParentIfMissing="true"
android:layout_toLeftOf="@+id/notification_status" android:layout_toLeftOf="@+id/notification_status"
android:id="@+id/txt_img_wrapper"> android:id="@+id/txt_img_wrapper">
<ImageView <ImageView
android:id="@+id/conversation_lastmsg_img" android:id="@+id/conversation_lastmsg_img"
android:layout_width="?attr/IconSize" android:layout_width="?attr/IconSize"
android:layout_height="?attr/IconSize" android:layout_height="?attr/IconSize"
android:layout_marginRight="?attr/TextSeparation"/> android:layout_marginRight="?attr/TextSeparation" />
<TextView <TextView
android:id="@+id/conversation_lastmsg" android:id="@+id/conversation_lastmsg"
@ -86,8 +90,9 @@
android:scrollHorizontally="false" android:scrollHorizontally="false"
android:maxLines="1" android:maxLines="1"
android:ellipsize="end" android:ellipsize="end"
android:textAppearance="@style/TextAppearance.Conversations.Body1"/> android:textAppearance="@style/TextAppearance.Conversations.Body1" />
</LinearLayout> </LinearLayout>
<ImageView <ImageView
android:visibility="visible" android:visibility="visible"
android:id="@+id/notification_status" android:id="@+id/notification_status"
@ -98,8 +103,8 @@
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_marginLeft="4dp" android:layout_marginLeft="4dp"
android:alpha="?attr/icon_alpha" android:alpha="?attr/icon_alpha"
android:src="?attr/icon_notifications" android:src="?attr/icon_notifications" />
/>
<eu.siacs.conversations.ui.widget.UnreadCountCustomView <eu.siacs.conversations.ui.widget.UnreadCountCustomView
android:id="@+id/unread_count" android:id="@+id/unread_count"
android:layout_width="?attr/IconSize" android:layout_width="?attr/IconSize"
@ -121,8 +126,9 @@
android:layout_alignBaseline="@+id/conversation_name" android:layout_alignBaseline="@+id/conversation_name"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:gravity="right" android:gravity="right"
android:textAppearance="@style/TextAppearance.Conversations.Caption"/> android:textAppearance="@style/TextAppearance.Conversations.Caption" />
</RelativeLayout> </RelativeLayout>
</RelativeLayout> </RelativeLayout>
</FrameLayout> </FrameLayout>
</FrameLayout> </FrameLayout>
</layout>