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;
@ -36,307 +38,286 @@ import rocks.xmpp.addr.Jid;
public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ConversationViewHolder> { public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ConversationViewHolder> {
private XmppActivity activity; private XmppActivity activity;
private List<Conversation> conversations; private List<Conversation> conversations;
private OnConversationClickListener listener; private OnConversationClickListener listener;
public ConversationAdapter(XmppActivity activity, List<Conversation> conversations) { public ConversationAdapter(XmppActivity activity, List<Conversation> conversations) {
this.activity = activity; this.activity = activity;
this.conversations = conversations; this.conversations = conversations;
} }
private static boolean cancelPotentialWork(Conversation conversation, ImageView imageView) { private static boolean cancelPotentialWork(Conversation conversation, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) { if (bitmapWorkerTask != null) {
final Conversation oldConversation = bitmapWorkerTask.conversation; final Conversation oldConversation = bitmapWorkerTask.conversation;
if (oldConversation == null || conversation != oldConversation) { if (oldConversation == null || conversation != oldConversation) {
bitmapWorkerTask.cancel(true); bitmapWorkerTask.cancel(true);
} else { } else {
return false; return false;
} }
} }
return true; return true;
} }
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) { private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) { if (imageView != null) {
final Drawable drawable = imageView.getDrawable(); final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) { if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable; final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask(); return asyncDrawable.getBitmapWorkerTask();
} }
} }
return null; return null;
} }
@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
public void onBindViewHolder(@NonNull ConversationViewHolder viewHolder, int position) { public void onBindViewHolder(@NonNull ConversationViewHolder viewHolder, int position) {
Conversation conversation = conversations.get(position); Conversation conversation = conversations.get(position);
if (conversation == null) { if (conversation == null) {
return; return;
} }
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();
final int unreadCount = conversation.unreadCount(); final int unreadCount = conversation.unreadCount();
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;
if (fileAvailable && (message.isFileOrImage() || message.treatAsDownloadable() || message.isGeoUri())) { if (fileAvailable && (message.isFileOrImage() || message.treatAsDownloadable() || message.isGeoUri())) {
final int imageResource; final int imageResource;
if (message.isGeoUri()) { if (message.isGeoUri()) {
imageResource = activity.getThemeResource(R.attr.ic_attach_location, R.drawable.ic_attach_location); imageResource = activity.getThemeResource(R.attr.ic_attach_location, R.drawable.ic_attach_location);
showPreviewText = false; showPreviewText = false;
} else { } else {
final String mime = message.getMimeType(); final String mime = message.getMimeType();
switch (mime == null ? "" : mime.split("/")[0]) { switch (mime == null ? "" : mime.split("/")[0]) {
case "image": case "image":
imageResource = activity.getThemeResource(R.attr.ic_attach_photo, R.drawable.ic_attach_photo); imageResource = activity.getThemeResource(R.attr.ic_attach_photo, R.drawable.ic_attach_photo);
showPreviewText = false; showPreviewText = false;
break; break;
case "video": case "video":
imageResource = activity.getThemeResource(R.attr.ic_attach_videocam, R.drawable.ic_attach_videocam); imageResource = activity.getThemeResource(R.attr.ic_attach_videocam, R.drawable.ic_attach_videocam);
showPreviewText = false; showPreviewText = false;
break; break;
case "audio": case "audio":
imageResource = activity.getThemeResource(R.attr.ic_attach_record, R.drawable.ic_attach_record); imageResource = activity.getThemeResource(R.attr.ic_attach_record, R.drawable.ic_attach_record);
showPreviewText = false; showPreviewText = false;
break; break;
default: default:
imageResource = activity.getThemeResource(R.attr.ic_attach_document, R.drawable.ic_attach_document); imageResource = activity.getThemeResource(R.attr.ic_attach_document, R.drawable.ic_attach_document);
showPreviewText = true; showPreviewText = true;
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;
if (draft != null) { if (draft != null) {
timestamp = draft.getTimestamp(); timestamp = draft.getTimestamp();
} 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
public int getItemCount() { public int getItemCount() {
return conversations.size(); return conversations.size();
} }
public void setConversationClickListener(OnConversationClickListener listener) { public void setConversationClickListener(OnConversationClickListener listener) {
this.listener = listener; this.listener = listener;
} }
private void loadAvatar(Conversation conversation, ImageView imageView) { private void loadAvatar(Conversation conversation, ImageView imageView) {
if (cancelPotentialWork(conversation, imageView)) { if (cancelPotentialWork(conversation, imageView)) {
final Bitmap bm = activity.avatarService().get(conversation, activity.getPixel(56), true); final Bitmap bm = activity.avatarService().get(conversation, activity.getPixel(56), true);
if (bm != null) { if (bm != null) {
cancelPotentialWork(conversation, imageView); cancelPotentialWork(conversation, imageView);
imageView.setImageBitmap(bm); imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000); imageView.setBackgroundColor(0x00000000);
} else { } else {
imageView.setBackgroundColor(UIHelper.getColorForName(conversation.getName().toString())); imageView.setBackgroundColor(UIHelper.getColorForName(conversation.getName().toString()));
imageView.setImageDrawable(null); imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final BitmapWorkerTask task = new BitmapWorkerTask(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 {
task.execute(conversation); task.execute(conversation);
} catch (final RejectedExecutionException ignored) { } catch (final RejectedExecutionException ignored) {
} }
} }
} }
} }
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) { static class ConversationViewHolder extends RecyclerView.ViewHolder {
super(view); private final ConversationListRowBinding binding;
}
public static ConversationViewHolder get(View layout) { private ConversationViewHolder(ConversationListRowBinding binding) {
ConversationViewHolder conversationViewHolder = (ConversationViewHolder) layout.getTag(); super(binding.getRoot());
if (conversationViewHolder == null) { this.binding = binding;
conversationViewHolder = new ConversationViewHolder(layout); }
conversationViewHolder.frame = layout.findViewById(R.id.frame);
conversationViewHolder.name = layout.findViewById(R.id.conversation_name);
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 { }
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { static class AsyncDrawable extends BitmapDrawable {
super(res, bitmap); private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask() { AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
return bitmapWorkerTaskReference.get(); super(res, bitmap);
} bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
} }
class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> { BitmapWorkerTask getBitmapWorkerTask() {
private final WeakReference<ImageView> imageViewReference; return bitmapWorkerTaskReference.get();
private Conversation conversation = null; }
}
public BitmapWorkerTask(ImageView imageView) { static class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> {
imageViewReference = new WeakReference<>(imageView); private final WeakReference<ImageView> imageViewReference;
} private Conversation conversation = null;
@Override BitmapWorkerTask(ImageView imageView) {
protected Bitmap doInBackground(Conversation... params) { imageViewReference = new WeakReference<>(imageView);
this.conversation = params[0]; }
return activity.avatarService().get(this.conversation, activity.getPixel(56), isCancelled());
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && !isCancelled()) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setBackgroundColor(0x00000000);
}
}
}
}
public interface OnConversationClickListener { @Override
void onConversationClick(View view, Conversation conversation); protected Bitmap doInBackground(Conversation... params) {
} 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());
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && !isCancelled()) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setBackgroundColor(0x00000000);
}
}
}
}
} }

View file

@ -34,16 +34,13 @@ 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 if (view instanceof TextView && mOnTagClickedListener != null) {
public void onClick(View view) { TextView tv = (TextView) view;
if (view instanceof TextView && mOnTagClickedListener != null) { final String tag = tv.getText().toString();
TextView tv = (TextView) view; mOnTagClickedListener.onTagClicked(tag);
final String tag = tv.getText().toString();
mOnTagClickedListener.onTagClicked(tag);
}
} }
}; };
@ -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(); this.attachment = params[0];
if (activity instanceof XmppActivity) { final XmppActivity activity = XmppActivity.find(imageViewReference);
final XmppActivity xmppActivity = (XmppActivity) activity; if (activity == null) {
this.attachment = params[0];
return xmppActivity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, Math.round(xmppActivity.getResources().getDimension(R.dimen.media_preview_size)), false);
} else {
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,128 +1,134 @@
<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">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<FrameLayout <FrameLayout
android:id="@+id/frame"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?attr/color_background_primary"> android:descendantFocusability="blocksDescendants">
<RelativeLayout <FrameLayout
android:id="@+id/frame"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?android:selectableItemBackground" android:background="?attr/color_background_primary">
android:orientation="horizontal"
android:padding="8dp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/conversation_image"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentLeft="true"
android:scaleType="centerCrop"
app:riv_corner_radius="2dp"/>
<RelativeLayout <RelativeLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" android:background="?android:selectableItemBackground"
android:layout_toRightOf="@+id/conversation_image" android:orientation="horizontal"
android:paddingLeft="@dimen/avatar_item_distance"> android:padding="8dp">
<TextView <com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/conversation_name" android:id="@+id/conversation_image"
android:layout_width="wrap_content" android:layout_width="56dp"
android:layout_height="wrap_content" android:layout_height="56dp"
android:layout_alignLeft="@+id/conversation_lastwrapper" android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/conversation_lastupdate" android:scaleType="centerCrop"
android:paddingRight="4dp" app:riv_corner_radius="2dp" />
android:maxLines="1"
android:ellipsize="end"
android:textAppearance="@style/TextAppearance.Conversations.Subhead" />
<RelativeLayout <RelativeLayout
android:id="@+id/conversation_lastwrapper" android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/conversation_name" android:layout_centerVertical="true"
android:layout_marginTop="4dp"> android:layout_toRightOf="@+id/conversation_image"
android:paddingLeft="@dimen/avatar_item_distance">
<TextView <TextView
android:id="@+id/sender_name" android:id="@+id/conversation_name"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:minHeight="?attr/IconSize" android:layout_alignLeft="@+id/conversation_lastwrapper"
android:layout_centerVertical="true" android:layout_toLeftOf="@+id/conversation_lastupdate"
android:gravity="center_vertical" android:paddingRight="4dp"
android:layout_alignParentLeft="true" android:maxLines="1"
android:text="@string/me" android:ellipsize="end"
android:layout_marginRight="?attr/TextSeparation" android:textAppearance="@style/TextAppearance.Conversations.Subhead" />
android:textAppearance="@style/TextAppearance.Conversations.Body1.Secondary"
android:visibility="visible"/>
<LinearLayout android:layout_width="match_parent" <RelativeLayout
android:layout_height="wrap_content" android:id="@+id/conversation_lastwrapper"
android:layout_centerVertical="true" android:layout_width="match_parent"
android:orientation="horizontal" android:layout_height="wrap_content"
android:layout_toRightOf="@id/sender_name" android:layout_below="@id/conversation_name"
android:layout_alignWithParentIfMissing="true" android:layout_marginTop="4dp">
android:layout_toLeftOf="@+id/notification_status"
android:id="@+id/txt_img_wrapper">
<ImageView
android:id="@+id/conversation_lastmsg_img"
android:layout_width="?attr/IconSize"
android:layout_height="?attr/IconSize"
android:layout_marginRight="?attr/TextSeparation"/>
<TextView <TextView
android:id="@+id/conversation_lastmsg" android:id="@+id/sender_name"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:minHeight="?attr/IconSize" android:minHeight="?attr/IconSize"
android:layout_centerVertical="true"
android:gravity="center_vertical" android:gravity="center_vertical"
android:scrollHorizontally="false" android:layout_alignParentLeft="true"
android:maxLines="1" android:text="@string/me"
android:ellipsize="end" android:layout_marginRight="?attr/TextSeparation"
android:textAppearance="@style/TextAppearance.Conversations.Body1"/> android:textAppearance="@style/TextAppearance.Conversations.Body1.Secondary"
</LinearLayout> android:visibility="visible" />
<ImageView
android:visibility="visible" <LinearLayout
android:id="@+id/notification_status" android:layout_width="match_parent"
android:layout_width="?attr/IconSize" android:layout_height="wrap_content"
android:layout_height="?attr/IconSize" android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/unread_count" android:orientation="horizontal"
android:layout_alignWithParentIfMissing="true" android:layout_toRightOf="@id/sender_name"
android:layout_centerVertical="true" android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="4dp" android:layout_toLeftOf="@+id/notification_status"
android:alpha="?attr/icon_alpha" android:id="@+id/txt_img_wrapper">
android:src="?attr/icon_notifications"
/> <ImageView
<eu.siacs.conversations.ui.widget.UnreadCountCustomView android:id="@+id/conversation_lastmsg_img"
android:id="@+id/unread_count" 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_centerVertical="true"
android:layout_marginLeft="3dp" <TextView
android:layout_marginTop="2dp" android:id="@+id/conversation_lastmsg"
android:layout_marginBottom="1dp" android:layout_width="match_parent"
android:visibility="gone" android:layout_height="wrap_content"
android:minHeight="?attr/IconSize"
android:gravity="center_vertical"
android:scrollHorizontally="false"
android:maxLines="1"
android:ellipsize="end"
android:textAppearance="@style/TextAppearance.Conversations.Body1" />
</LinearLayout>
<ImageView
android:visibility="visible"
android:id="@+id/notification_status"
android:layout_width="?attr/IconSize"
android:layout_height="?attr/IconSize"
android:layout_toLeftOf="@+id/unread_count"
android:layout_alignWithParentIfMissing="true"
android:layout_centerVertical="true"
android:layout_marginLeft="4dp"
android:alpha="?attr/icon_alpha"
android:src="?attr/icon_notifications" />
<eu.siacs.conversations.ui.widget.UnreadCountCustomView
android:id="@+id/unread_count"
android:layout_width="?attr/IconSize"
android:layout_height="?attr/IconSize"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="1dp"
android:visibility="gone"
android:layout_alignParentRight="true"
app:backgroundColor="?attr/unread_count" />
</RelativeLayout>
<TextView
android:id="@+id/conversation_lastupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/conversation_name"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
app:backgroundColor="?attr/unread_count" /> android:gravity="right"
android:textAppearance="@style/TextAppearance.Conversations.Caption" />
</RelativeLayout> </RelativeLayout>
<TextView
android:id="@+id/conversation_lastupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/conversation_name"
android:layout_alignParentRight="true"
android:gravity="right"
android:textAppearance="@style/TextAppearance.Conversations.Caption"/>
</RelativeLayout> </RelativeLayout>
</RelativeLayout> </FrameLayout>
</FrameLayout> </FrameLayout>
</FrameLayout> </layout>