2014-08-15 11:18:15 +00:00
|
|
|
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;
|
2015-01-12 15:09:39 +00:00
|
|
|
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;
|
2014-08-15 11:18:15 +00:00
|
|
|
import java.util.List;
|
2015-02-15 17:48:05 +00:00
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
2014-08-15 11:18:15 +00:00
|
|
|
|
|
|
|
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;
|
2014-08-15 11:18:15 +00:00
|
|
|
import eu.siacs.conversations.ui.ConversationActivity;
|
2014-09-02 13:51:20 +00:00
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
2017-03-02 21:24:58 +00:00
|
|
|
import eu.siacs.conversations.ui.widget.UnreadCountCustomView;
|
2014-08-15 11:18:15 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
|
|
|
|
|
|
|
public class ConversationAdapter extends ArrayAdapter<Conversation> {
|
|
|
|
|
2014-09-02 13:51:20 +00:00
|
|
|
private XmppActivity activity;
|
2014-08-15 11:18:15 +00:00
|
|
|
|
2017-11-13 17:37:12 +00:00
|
|
|
public ConversationAdapter(XmppActivity activity, List<Conversation> conversations) {
|
2014-08-15 11:18:15 +00:00
|
|
|
super(activity, 0, conversations);
|
|
|
|
this.activity = activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
|
|
|
if (view == null) {
|
2015-01-12 15:09:39 +00:00
|
|
|
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
view = inflater.inflate(R.layout.conversation_list_row,parent, false);
|
2014-08-15 11:18:15 +00:00
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
Conversation conversation = getItem(position);
|
2014-09-02 13:51:20 +00:00
|
|
|
if (this.activity instanceof ConversationActivity) {
|
2015-04-13 13:59:18 +00:00
|
|
|
View swipeableItem = view.findViewById(R.id.swipeable_item);
|
|
|
|
ConversationActivity a = (ConversationActivity) this.activity;
|
2015-09-07 13:46:22 +00:00
|
|
|
int c = a.highlightSelectedConversations() && conversation == a.getSelectedConversation() ? a.getSecondaryBackgroundColor() : a.getPrimaryBackgroundColor();
|
2015-04-13 13:59:18 +00:00
|
|
|
swipeableItem.setBackgroundColor(c);
|
2014-08-15 11:18:15 +00:00
|
|
|
}
|
2015-01-12 15:09:39 +00:00
|
|
|
TextView convName = (TextView) view.findViewById(R.id.conversation_name);
|
|
|
|
if (conversation.getMode() == Conversation.MODE_SINGLE || activity.useSubjectToIdentifyConference()) {
|
2014-10-15 17:32:12 +00:00
|
|
|
convName.setText(conversation.getName());
|
2014-09-22 11:23:35 +00:00
|
|
|
} else {
|
2014-12-21 20:43:58 +00:00
|
|
|
convName.setText(conversation.getJid().toBareJid().toString());
|
2014-09-22 11:23:35 +00:00
|
|
|
}
|
2015-01-12 15:09:39 +00:00
|
|
|
TextView mLastMessage = (TextView) view.findViewById(R.id.conversation_lastmsg);
|
2017-11-02 09:10:20 +00:00
|
|
|
ImageView mLastMessageImage = (ImageView) view.findViewById(R.id.conversation_lastmsg_img);
|
2015-01-12 15:09:39 +00:00
|
|
|
TextView mTimestamp = (TextView) view.findViewById(R.id.conversation_lastupdate);
|
2017-03-06 08:02:43 +00:00
|
|
|
TextView mSenderName = (TextView) view.findViewById(R.id.sender_name);
|
2015-01-12 15:09:39 +00:00
|
|
|
ImageView imagePreview = (ImageView) view.findViewById(R.id.conversation_lastimage);
|
2016-01-08 13:41:55 +00:00
|
|
|
ImageView notificationStatus = (ImageView) view.findViewById(R.id.notification_status);
|
2017-03-06 08:02:43 +00:00
|
|
|
UnreadCountCustomView unreadCountCustomView = (UnreadCountCustomView) view.findViewById(R.id.unread_count);
|
2014-08-15 11:18:15 +00:00
|
|
|
|
2014-10-15 17:32:12 +00:00
|
|
|
Message message = conversation.getLatestMessage();
|
2017-03-02 21:24:58 +00:00
|
|
|
int unreadCount = conversation.unreadCount();
|
2017-03-17 11:55:33 +00:00
|
|
|
if (unreadCount > 0) {
|
|
|
|
unreadCountCustomView.setVisibility(View.VISIBLE);
|
|
|
|
unreadCountCustomView.setUnreadCount(unreadCount);
|
|
|
|
} else {
|
|
|
|
unreadCountCustomView.setVisibility(View.GONE);
|
|
|
|
}
|
2014-08-15 11:18:15 +00:00
|
|
|
|
2014-10-15 17:32:12 +00:00
|
|
|
if (!conversation.isRead()) {
|
|
|
|
convName.setTypeface(null, Typeface.BOLD);
|
|
|
|
} else {
|
|
|
|
convName.setTypeface(null, Typeface.NORMAL);
|
|
|
|
}
|
|
|
|
|
2017-11-21 11:40:42 +00:00
|
|
|
final boolean fileAvailable = message.getTransferable() == null || message.getTransferable().getStatus() != Transferable.STATUS_DELETED;
|
|
|
|
if (message.getFileParams().width > 0 && fileAvailable) {
|
2017-03-06 18:42:27 +00:00
|
|
|
mSenderName.setVisibility(View.GONE);
|
2015-01-12 15:09:39 +00:00
|
|
|
mLastMessage.setVisibility(View.GONE);
|
2017-11-20 10:12:11 +00:00
|
|
|
mLastMessageImage.setVisibility(View.GONE);
|
2015-01-12 15:09:39 +00:00
|
|
|
imagePreview.setVisibility(View.VISIBLE);
|
|
|
|
activity.loadBitmap(message, imagePreview);
|
|
|
|
} else {
|
2017-11-13 17:37:12 +00:00
|
|
|
final boolean showPreviewText;
|
2017-11-21 11:40:42 +00:00
|
|
|
if (message.getType() == Message.TYPE_FILE && fileAvailable) {
|
2017-11-20 10:12:11 +00:00
|
|
|
if (message.getFileParams().runtime > 0) {
|
|
|
|
showPreviewText = false;
|
|
|
|
mLastMessageImage.setImageResource(activity.getThemeResource(R.attr.ic_attach_record, R.drawable.ic_attach_record));
|
|
|
|
} else {
|
|
|
|
showPreviewText = true;
|
|
|
|
mLastMessageImage.setImageResource(activity.getThemeResource(R.attr.ic_attach_document, R.drawable.ic_attach_document));
|
|
|
|
}
|
2017-11-13 17:37:12 +00:00
|
|
|
mLastMessageImage.setVisibility(View.VISIBLE);
|
|
|
|
} else if (message.isGeoUri()) {
|
|
|
|
showPreviewText = false;
|
|
|
|
mLastMessageImage.setImageResource(activity.getThemeResource(R.attr.ic_attach_location, R.drawable.ic_attach_location));
|
|
|
|
mLastMessageImage.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
showPreviewText = true;
|
|
|
|
mLastMessageImage.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
final Pair<String,Boolean> preview = UIHelper.getMessagePreview(activity,message);
|
|
|
|
if (showPreviewText) {
|
|
|
|
mLastMessage.setText(preview.first);
|
|
|
|
} else {
|
|
|
|
mLastMessageImage.setContentDescription(preview.first);
|
|
|
|
}
|
|
|
|
mLastMessage.setVisibility(showPreviewText ? View.VISIBLE : View.GONE);
|
2015-01-12 15:09:39 +00:00
|
|
|
imagePreview.setVisibility(View.GONE);
|
|
|
|
if (preview.second) {
|
|
|
|
if (conversation.isRead()) {
|
|
|
|
mLastMessage.setTypeface(null, Typeface.ITALIC);
|
2017-03-06 08:02:43 +00:00
|
|
|
mSenderName.setTypeface(null, Typeface.NORMAL);
|
2014-08-15 11:18:15 +00:00
|
|
|
} else {
|
2015-01-12 15:09:39 +00:00
|
|
|
mLastMessage.setTypeface(null,Typeface.BOLD_ITALIC);
|
2017-03-06 08:02:43 +00:00
|
|
|
mSenderName.setTypeface(null,Typeface.BOLD);
|
2014-08-15 11:18:15 +00:00
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
} else {
|
2015-01-12 15:09:39 +00:00
|
|
|
if (conversation.isRead()) {
|
|
|
|
mLastMessage.setTypeface(null,Typeface.NORMAL);
|
2017-03-06 08:02:43 +00:00
|
|
|
mSenderName.setTypeface(null,Typeface.NORMAL);
|
2015-01-12 15:09:39 +00:00
|
|
|
} else {
|
|
|
|
mLastMessage.setTypeface(null,Typeface.BOLD);
|
2017-03-06 08:02:43 +00:00
|
|
|
mSenderName.setTypeface(null,Typeface.BOLD);
|
2015-01-12 15:09:39 +00:00
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
}
|
2017-03-06 08:02:43 +00:00
|
|
|
if (message.getStatus() == Message.STATUS_RECEIVED) {
|
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI) {
|
|
|
|
mSenderName.setVisibility(View.VISIBLE);
|
2017-03-06 18:42:27 +00:00
|
|
|
mSenderName.setText(UIHelper.getMessageDisplayName(message).split("\\s+")[0]+':');
|
2017-03-06 08:02:43 +00:00
|
|
|
} else {
|
|
|
|
mSenderName.setVisibility(View.GONE);
|
|
|
|
}
|
2017-03-07 08:22:42 +00:00
|
|
|
} else if (message.getType() != Message.TYPE_STATUS) {
|
2017-03-06 08:02:43 +00:00
|
|
|
mSenderName.setVisibility(View.VISIBLE);
|
2017-03-06 18:42:27 +00:00
|
|
|
mSenderName.setText(activity.getString(R.string.me)+':');
|
2017-03-07 08:22:42 +00:00
|
|
|
} else {
|
|
|
|
mSenderName.setVisibility(View.GONE);
|
2017-03-06 08:02:43 +00:00
|
|
|
}
|
2014-08-15 11:18:15 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 13:41:55 +00:00
|
|
|
long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL,0);
|
|
|
|
if (muted_till == Long.MAX_VALUE) {
|
|
|
|
notificationStatus.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);
|
Dark theme, theme switch, icons, style, strings
added some white icons,
changed hardcoded icons to theme attributes,
changed icon_edit_dark to icon_edit_body to reflect icons position,
grey message bubbles in dark theme,
misc
purged ic_action_chat as it wasn't used
preference use_white_background changed to use_green_background, default true
grey chat bubbles darker, text white
replaced all grey600 with black icons and 0.54 alpha attribute
highlightColor in dark grey chat bubble now darker than background
2016-05-12 18:00:18 +00:00
|
|
|
notificationStatus.setImageResource(ic_notifications_off);
|
2016-01-08 13:41:55 +00:00
|
|
|
} else if (muted_till >= System.currentTimeMillis()) {
|
|
|
|
notificationStatus.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);
|
Dark theme, theme switch, icons, style, strings
added some white icons,
changed hardcoded icons to theme attributes,
changed icon_edit_dark to icon_edit_body to reflect icons position,
grey message bubbles in dark theme,
misc
purged ic_action_chat as it wasn't used
preference use_white_background changed to use_green_background, default true
grey chat bubbles darker, text white
replaced all grey600 with black icons and 0.54 alpha attribute
highlightColor in dark grey chat bubble now darker than background
2016-05-12 18:00:18 +00:00
|
|
|
notificationStatus.setImageResource(ic_notifications_paused);
|
2016-01-08 20:30:46 +00:00
|
|
|
} else if (conversation.alwaysNotify()) {
|
2016-01-08 13:41:55 +00:00
|
|
|
notificationStatus.setVisibility(View.GONE);
|
2016-01-08 20:30:46 +00:00
|
|
|
} else {
|
|
|
|
notificationStatus.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);
|
Dark theme, theme switch, icons, style, strings
added some white icons,
changed hardcoded icons to theme attributes,
changed icon_edit_dark to icon_edit_body to reflect icons position,
grey message bubbles in dark theme,
misc
purged ic_action_chat as it wasn't used
preference use_white_background changed to use_green_background, default true
grey chat bubbles darker, text white
replaced all grey600 with black icons and 0.54 alpha attribute
highlightColor in dark grey chat bubble now darker than background
2016-05-12 18:00:18 +00:00
|
|
|
notificationStatus.setImageResource(ic_notifications_none);
|
2016-01-08 13:41:55 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 15:09:39 +00:00
|
|
|
mTimestamp.setText(UIHelper.readableTimeDifference(activity,conversation.getLatestMessage().getTimeSent()));
|
|
|
|
ImageView profilePicture = (ImageView) view.findViewById(R.id.conversation_image);
|
2015-02-15 17:48:05 +00:00
|
|
|
loadAvatar(conversation,profilePicture);
|
2014-08-15 11:18:15 +00:00
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
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) {
|
2015-02-18 17:12:43 +00:00
|
|
|
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);
|
2015-02-18 17:12:43 +00:00
|
|
|
imageView.setImageBitmap(bm);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
} else {
|
|
|
|
imageView.setBackgroundColor(UIHelper.getColorForName(conversation.getName()));
|
2015-02-22 12:15:27 +00:00
|
|
|
imageView.setImageDrawable(null);
|
2015-02-18 17:12:43 +00:00
|
|
|
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
|
|
|
}
|