anotherim/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java

287 lines
11 KiB
Java
Raw Normal View History

package eu.siacs.conversations.ui.adapter;
2015-01-11 15:22:29 +00:00
import android.content.Context;
2015-02-15 17:48:05 +00:00
import android.content.res.Resources;
import android.graphics.Bitmap;
2015-01-11 15:22:29 +00:00
import android.graphics.Typeface;
2015-02-15 17:48:05 +00:00
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Pair;
2015-01-11 15:22:29 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
2015-02-15 17:48:05 +00:00
import java.lang.ref.WeakReference;
import java.util.List;
2015-02-15 17:48:05 +00:00
import java.util.concurrent.RejectedExecutionException;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
2015-07-20 12:26:29 +00:00
import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.ui.ConversationActivity;
2014-09-02 13:51:20 +00:00
import eu.siacs.conversations.ui.XmppActivity;
import eu.siacs.conversations.ui.widget.UnreadCountCustomView;
import eu.siacs.conversations.utils.EmojiWrapper;
import eu.siacs.conversations.utils.UIHelper;
public class ConversationAdapter extends ArrayAdapter<Conversation> {
2014-09-02 13:51:20 +00:00
private XmppActivity activity;
public ConversationAdapter(XmppActivity activity, List<Conversation> conversations) {
super(activity, 0, conversations);
this.activity = activity;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.conversation_list_row,parent, false);
}
Conversation conversation = getItem(position);
2014-09-02 13:51:20 +00:00
if (this.activity instanceof ConversationActivity) {
View swipeableItem = view.findViewById(R.id.swipeable_item);
ConversationActivity a = (ConversationActivity) this.activity;
int c = a.highlightSelectedConversations() && conversation == a.getSelectedConversation() ? a.getSecondaryBackgroundColor() : a.getPrimaryBackgroundColor();
swipeableItem.setBackgroundColor(c);
}
2017-11-23 08:59:49 +00:00
ViewHolder viewHolder = ViewHolder.get(view);
if (conversation.getMode() == Conversation.MODE_SINGLE || activity.useSubjectToIdentifyConference()) {
2017-11-23 08:59:49 +00:00
viewHolder.name.setText(EmojiWrapper.transform(conversation.getName()));
2014-09-22 11:23:35 +00:00
} else {
2017-11-23 08:59:49 +00:00
viewHolder.name.setText(conversation.getJid().toBareJid().toString());
2014-09-22 11:23:35 +00:00
}
Message message = conversation.getLatestMessage();
int unreadCount = conversation.unreadCount();
if (unreadCount > 0) {
2017-11-23 08:59:49 +00:00
viewHolder.unreadCount.setVisibility(View.VISIBLE);
viewHolder.unreadCount.setUnreadCount(unreadCount);
} else {
2017-11-23 08:59:49 +00:00
viewHolder.unreadCount.setVisibility(View.GONE);
}
if (!conversation.isRead()) {
2017-11-23 08:59:49 +00:00
viewHolder.name.setTypeface(null, Typeface.BOLD);
} else {
2017-11-23 08:59:49 +00:00
viewHolder.name.setTypeface(null, Typeface.NORMAL);
}
final boolean fileAvailable = message.getTransferable() == null || message.getTransferable().getStatus() != Transferable.STATUS_DELETED;
final boolean showPreviewText;
if (fileAvailable && (message.isFileOrImage() || message.treatAsDownloadable() || message.isGeoUri())) {
final int imageResource;
if (message.isGeoUri()) {
imageResource = activity.getThemeResource(R.attr.ic_attach_location, R.drawable.ic_attach_location);
showPreviewText = false;
} else {
final String mime = message.getMimeType();
switch (mime == null ? "" : mime.split("/")[0]) {
case "image":
imageResource = activity.getThemeResource(R.attr.ic_attach_photo, R.drawable.ic_attach_photo);
showPreviewText = false;
break;
case "video":
imageResource = activity.getThemeResource(R.attr.ic_attach_videocam, R.drawable.ic_attach_videocam);
showPreviewText = false;
break;
case "audio":
imageResource = activity.getThemeResource(R.attr.ic_attach_record, R.drawable.ic_attach_record);
showPreviewText = false;
break;
default:
imageResource = activity.getThemeResource(R.attr.ic_attach_document, R.drawable.ic_attach_document);
showPreviewText = true;
break;
}
}
viewHolder.lastMessageIcon.setImageResource(imageResource);
viewHolder.lastMessageIcon.setVisibility(View.VISIBLE);
} else {
viewHolder.lastMessageIcon.setVisibility(View.GONE);
showPreviewText = true;
}
final Pair<String,Boolean> preview = UIHelper.getMessagePreview(activity,message);
if (showPreviewText) {
viewHolder.lastMessage.setText(EmojiWrapper.transform(preview.first));
} else {
viewHolder.lastMessageIcon.setContentDescription(preview.first);
}
viewHolder.lastMessage.setVisibility(showPreviewText ? View.VISIBLE : View.GONE);
if (preview.second) {
if (conversation.isRead()) {
viewHolder.lastMessage.setTypeface(null, Typeface.ITALIC);
viewHolder.sender.setTypeface(null, Typeface.NORMAL);
} else {
viewHolder.lastMessage.setTypeface(null,Typeface.BOLD_ITALIC);
viewHolder.sender.setTypeface(null,Typeface.BOLD);
}
} else {
if (conversation.isRead()) {
viewHolder.lastMessage.setTypeface(null,Typeface.NORMAL);
viewHolder.sender.setTypeface(null,Typeface.NORMAL);
} else {
viewHolder.lastMessage.setTypeface(null,Typeface.BOLD);
viewHolder.sender.setTypeface(null,Typeface.BOLD);
}
}
if (message.getStatus() == Message.STATUS_RECEIVED) {
if (conversation.getMode() == Conversation.MODE_MULTI) {
2017-11-23 08:59:49 +00:00
viewHolder.sender.setVisibility(View.VISIBLE);
viewHolder.sender.setText(UIHelper.getMessageDisplayName(message).split("\\s+")[0]+':');
} else {
2017-11-23 08:59:49 +00:00
viewHolder.sender.setVisibility(View.GONE);
}
} else if (message.getType() != Message.TYPE_STATUS) {
viewHolder.sender.setVisibility(View.VISIBLE);
viewHolder.sender.setText(activity.getString(R.string.me)+':');
} else {
viewHolder.sender.setVisibility(View.GONE);
}
long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL,0);
if (muted_till == Long.MAX_VALUE) {
2017-11-23 08:59:49 +00:00
viewHolder.notificationIcon.setVisibility(View.VISIBLE);
2017-06-03 22:05:02 +00:00
int ic_notifications_off = activity.getThemeResource(R.attr.icon_notifications_off, R.drawable.ic_notifications_off_black_24dp);
2017-11-23 08:59:49 +00:00
viewHolder.notificationIcon.setImageResource(ic_notifications_off);
} else if (muted_till >= System.currentTimeMillis()) {
2017-11-23 08:59:49 +00:00
viewHolder.notificationIcon.setVisibility(View.VISIBLE);
2017-06-03 22:05:02 +00:00
int ic_notifications_paused = activity.getThemeResource(R.attr.icon_notifications_paused, R.drawable.ic_notifications_paused_black_24dp);
2017-11-23 08:59:49 +00:00
viewHolder.notificationIcon.setImageResource(ic_notifications_paused);
} else if (conversation.alwaysNotify()) {
2017-11-23 08:59:49 +00:00
viewHolder.notificationIcon.setVisibility(View.GONE);
} else {
2017-11-23 08:59:49 +00:00
viewHolder.notificationIcon.setVisibility(View.VISIBLE);
2017-06-03 22:05:02 +00:00
int ic_notifications_none = activity.getThemeResource(R.attr.icon_notifications_none, R.drawable.ic_notifications_none_black_24dp);
2017-11-23 08:59:49 +00:00
viewHolder.notificationIcon.setImageResource(ic_notifications_none);
}
2017-11-23 08:59:49 +00:00
viewHolder.timestamp.setText(UIHelper.readableTimeDifference(activity,conversation.getLatestMessage().getTimeSent()));
loadAvatar(conversation, viewHolder.avatar);
return view;
}
2015-02-15 17:48:05 +00:00
2017-11-23 08:59:49 +00:00
public static class ViewHolder {
private TextView name;
private TextView lastMessage;
private ImageView lastMessageIcon;
private TextView sender;
private TextView timestamp;
private ImageView notificationIcon;
private UnreadCountCustomView unreadCount;
private ImageView avatar;
private ViewHolder() {
}
public static ViewHolder get(View layout) {
ViewHolder viewHolder = (ViewHolder) layout.getTag();
if (viewHolder == null) {
viewHolder = new ViewHolder();
viewHolder.name = layout.findViewById(R.id.conversation_name);
viewHolder.lastMessage = layout.findViewById(R.id.conversation_lastmsg);
viewHolder.lastMessageIcon = layout.findViewById(R.id.conversation_lastmsg_img);
viewHolder.timestamp = layout.findViewById(R.id.conversation_lastupdate);
viewHolder.sender = layout.findViewById(R.id.sender_name);
viewHolder.notificationIcon = layout.findViewById(R.id.notification_status);
viewHolder.unreadCount = layout.findViewById(R.id.unread_count);
viewHolder.avatar = layout.findViewById(R.id.conversation_image);
layout.setTag(viewHolder);
}
return viewHolder;
}
}
2015-02-15 17:48:05 +00:00
class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Conversation conversation = null;
public BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(Conversation... params) {
2016-04-29 18:38:23 +00:00
return activity.avatarService().get(params[0], activity.getPixel(56), isCancelled());
2015-02-15 17:48:05 +00:00
}
@Override
protected void onPostExecute(Bitmap bitmap) {
2016-04-29 18:38:23 +00:00
if (bitmap != null && !isCancelled()) {
2015-02-15 17:48:05 +00:00
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setBackgroundColor(0x00000000);
}
}
}
}
public void loadAvatar(Conversation conversation, ImageView imageView) {
if (cancelPotentialWork(conversation, imageView)) {
final Bitmap bm = activity.avatarService().get(conversation, activity.getPixel(56), true);
if (bm != null) {
2016-04-29 18:38:23 +00:00
cancelPotentialWork(conversation, imageView);
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setBackgroundColor(UIHelper.getColorForName(conversation.getName()));
2015-02-22 12:15:27 +00:00
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
imageView.setImageDrawable(asyncDrawable);
try {
task.execute(conversation);
} catch (final RejectedExecutionException ignored) {
}
2015-02-15 17:48:05 +00:00
}
}
}
public static boolean cancelPotentialWork(Conversation conversation, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final Conversation oldConversation = bitmapWorkerTask.conversation;
if (oldConversation == null || conversation != oldConversation) {
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();
}
}
2017-11-02 09:10:20 +00:00
}