2014-08-15 11:18:15 +00:00
|
|
|
package eu.siacs.conversations.ui.adapter;
|
|
|
|
|
2019-01-24 11:27:57 +00:00
|
|
|
import android.databinding.DataBindingUtil;
|
2015-01-11 15:22:29 +00:00
|
|
|
import android.graphics.Typeface;
|
2018-03-11 11:13:56 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2018-04-06 11:49:50 +00:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
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;
|
|
|
|
|
2020-04-27 09:53:31 +00:00
|
|
|
import com.google.common.base.Optional;
|
|
|
|
|
2014-08-15 11:18:15 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
2019-01-24 11:27:57 +00:00
|
|
|
import eu.siacs.conversations.databinding.ConversationListRowBinding;
|
2014-08-15 11:18:15 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2020-04-27 09:53:31 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversational;
|
2014-08-15 11:18:15 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2018-05-03 16:30:44 +00:00
|
|
|
import eu.siacs.conversations.ui.ConversationFragment;
|
2014-09-02 13:51:20 +00:00
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
2019-01-25 09:07:02 +00:00
|
|
|
import eu.siacs.conversations.ui.util.AvatarWorkerTask;
|
2018-09-12 12:37:41 +00:00
|
|
|
import eu.siacs.conversations.ui.util.StyledAttributes;
|
2017-11-23 08:36:51 +00:00
|
|
|
import eu.siacs.conversations.utils.EmojiWrapper;
|
2018-03-11 11:13:56 +00:00
|
|
|
import eu.siacs.conversations.utils.IrregularUnicodeDetector;
|
2014-08-15 11:18:15 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2020-04-27 09:53:31 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection;
|
2020-05-02 07:50:17 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.OngoingRtpSession;
|
2018-03-11 11:13:56 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2014-08-15 11:18:15 +00:00
|
|
|
|
2018-04-06 11:49:50 +00:00
|
|
|
public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ConversationViewHolder> {
|
2014-08-15 11:18:15 +00:00
|
|
|
|
2019-01-24 11:27:57 +00:00
|
|
|
private XmppActivity activity;
|
|
|
|
private List<Conversation> conversations;
|
|
|
|
private OnConversationClickListener listener;
|
|
|
|
|
|
|
|
public ConversationAdapter(XmppActivity activity, List<Conversation> conversations) {
|
|
|
|
this.activity = activity;
|
|
|
|
this.conversations = conversations;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public ConversationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
return new ConversationViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.conversation_list_row, parent, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull ConversationViewHolder viewHolder, int position) {
|
|
|
|
Conversation conversation = conversations.get(position);
|
|
|
|
if (conversation == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CharSequence name = conversation.getName();
|
|
|
|
if (name instanceof Jid) {
|
|
|
|
viewHolder.binding.conversationName.setText(IrregularUnicodeDetector.style(activity, (Jid) name));
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.conversationName.setText(EmojiWrapper.transform(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conversation == ConversationFragment.getConversation(activity)) {
|
|
|
|
viewHolder.binding.frame.setBackgroundColor(StyledAttributes.getColor(activity, R.attr.color_background_tertiary));
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.frame.setBackgroundColor(StyledAttributes.getColor(activity, R.attr.color_background_primary));
|
|
|
|
}
|
|
|
|
|
|
|
|
Message message = conversation.getLatestMessage();
|
|
|
|
final int unreadCount = conversation.unreadCount();
|
|
|
|
final boolean isRead = conversation.isRead();
|
|
|
|
final Conversation.Draft draft = isRead ? conversation.getDraft() : null;
|
|
|
|
if (unreadCount > 0) {
|
|
|
|
viewHolder.binding.unreadCount.setVisibility(View.VISIBLE);
|
|
|
|
viewHolder.binding.unreadCount.setUnreadCount(unreadCount);
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.unreadCount.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isRead) {
|
|
|
|
viewHolder.binding.conversationName.setTypeface(null, Typeface.NORMAL);
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.conversationName.setTypeface(null, Typeface.BOLD);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (draft != null) {
|
|
|
|
viewHolder.binding.conversationLastmsgImg.setVisibility(View.GONE);
|
|
|
|
viewHolder.binding.conversationLastmsg.setText(EmojiWrapper.transform(draft.getMessage()));
|
|
|
|
viewHolder.binding.senderName.setText(R.string.draft);
|
|
|
|
viewHolder.binding.senderName.setVisibility(View.VISIBLE);
|
|
|
|
viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.NORMAL);
|
|
|
|
viewHolder.binding.senderName.setTypeface(null, Typeface.ITALIC);
|
|
|
|
} else {
|
|
|
|
final boolean fileAvailable = !message.isDeleted();
|
|
|
|
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 {
|
2019-07-15 17:11:00 +00:00
|
|
|
//TODO move this into static MediaPreview method and use same icons as in MediaAdapter
|
2019-01-24 11:27:57 +00:00
|
|
|
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.binding.conversationLastmsgImg.setImageResource(imageResource);
|
|
|
|
viewHolder.binding.conversationLastmsgImg.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.conversationLastmsgImg.setVisibility(View.GONE);
|
|
|
|
showPreviewText = true;
|
|
|
|
}
|
|
|
|
final Pair<CharSequence, Boolean> preview = UIHelper.getMessagePreview(activity, message, viewHolder.binding.conversationLastmsg.getCurrentTextColor());
|
|
|
|
if (showPreviewText) {
|
|
|
|
viewHolder.binding.conversationLastmsg.setText(EmojiWrapper.transform(UIHelper.shorten(preview.first)));
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.conversationLastmsgImg.setContentDescription(preview.first);
|
|
|
|
}
|
|
|
|
viewHolder.binding.conversationLastmsg.setVisibility(showPreviewText ? View.VISIBLE : View.GONE);
|
|
|
|
if (preview.second) {
|
|
|
|
if (isRead) {
|
|
|
|
viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.ITALIC);
|
|
|
|
viewHolder.binding.senderName.setTypeface(null, Typeface.NORMAL);
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.BOLD_ITALIC);
|
|
|
|
viewHolder.binding.senderName.setTypeface(null, Typeface.BOLD);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isRead) {
|
|
|
|
viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.NORMAL);
|
|
|
|
viewHolder.binding.senderName.setTypeface(null, Typeface.NORMAL);
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.conversationLastmsg.setTypeface(null, Typeface.BOLD);
|
|
|
|
viewHolder.binding.senderName.setTypeface(null, Typeface.BOLD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (message.getStatus() == Message.STATUS_RECEIVED) {
|
|
|
|
if (conversation.getMode() == Conversation.MODE_MULTI) {
|
|
|
|
viewHolder.binding.senderName.setVisibility(View.VISIBLE);
|
|
|
|
viewHolder.binding.senderName.setText(UIHelper.getMessageDisplayName(message).split("\\s+")[0] + ':');
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.senderName.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
} else if (message.getType() != Message.TYPE_STATUS) {
|
|
|
|
viewHolder.binding.senderName.setVisibility(View.VISIBLE);
|
|
|
|
viewHolder.binding.senderName.setText(activity.getString(R.string.me) + ':');
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.senderName.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:53:31 +00:00
|
|
|
|
2020-05-02 07:50:17 +00:00
|
|
|
final Optional<OngoingRtpSession> ongoingCall;
|
2020-04-27 09:53:31 +00:00
|
|
|
if (conversation.getMode() == Conversational.MODE_MULTI) {
|
|
|
|
ongoingCall = Optional.absent();
|
2019-01-24 11:27:57 +00:00
|
|
|
} else {
|
2020-04-27 09:53:31 +00:00
|
|
|
ongoingCall = activity.xmppConnectionService.getJingleConnectionManager().getOngoingRtpConnection(conversation.getContact());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ongoingCall.isPresent()) {
|
2019-01-24 11:27:57 +00:00
|
|
|
viewHolder.binding.notificationStatus.setVisibility(View.VISIBLE);
|
2020-04-27 09:53:31 +00:00
|
|
|
final int ic_ongoing_call = activity.getThemeResource(R.attr.ic_ongoing_call_hint, R.drawable.ic_phone_in_talk_black_18dp);
|
|
|
|
viewHolder.binding.notificationStatus.setImageResource(ic_ongoing_call);
|
|
|
|
} else {
|
|
|
|
final long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL, 0);
|
|
|
|
if (muted_till == Long.MAX_VALUE) {
|
|
|
|
viewHolder.binding.notificationStatus.setVisibility(View.VISIBLE);
|
|
|
|
int ic_notifications_off = activity.getThemeResource(R.attr.icon_notifications_off, R.drawable.ic_notifications_off_black_24dp);
|
|
|
|
viewHolder.binding.notificationStatus.setImageResource(ic_notifications_off);
|
|
|
|
} else if (muted_till >= System.currentTimeMillis()) {
|
|
|
|
viewHolder.binding.notificationStatus.setVisibility(View.VISIBLE);
|
|
|
|
int ic_notifications_paused = activity.getThemeResource(R.attr.icon_notifications_paused, R.drawable.ic_notifications_paused_black_24dp);
|
|
|
|
viewHolder.binding.notificationStatus.setImageResource(ic_notifications_paused);
|
|
|
|
} else if (conversation.alwaysNotify()) {
|
|
|
|
viewHolder.binding.notificationStatus.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.notificationStatus.setVisibility(View.VISIBLE);
|
|
|
|
int ic_notifications_none = activity.getThemeResource(R.attr.icon_notifications_none, R.drawable.ic_notifications_none_black_24dp);
|
|
|
|
viewHolder.binding.notificationStatus.setImageResource(ic_notifications_none);
|
|
|
|
}
|
2019-01-24 11:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long timestamp;
|
|
|
|
if (draft != null) {
|
|
|
|
timestamp = draft.getTimestamp();
|
|
|
|
} else {
|
|
|
|
timestamp = conversation.getLatestMessage().getTimeSent();
|
|
|
|
}
|
|
|
|
viewHolder.binding.conversationLastupdate.setText(UIHelper.readableTimeDifference(activity, timestamp));
|
2019-01-25 09:07:02 +00:00
|
|
|
AvatarWorkerTask.loadAvatar(conversation, viewHolder.binding.conversationImage, R.dimen.avatar_on_conversation_overview);
|
2019-01-24 11:27:57 +00:00
|
|
|
viewHolder.itemView.setOnClickListener(v -> listener.onConversationClick(v, conversation));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return conversations.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setConversationClickListener(OnConversationClickListener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void insert(Conversation c, int position) {
|
|
|
|
conversations.add(position, c);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void remove(Conversation conversation, int position) {
|
|
|
|
conversations.remove(conversation);
|
|
|
|
notifyItemRemoved(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnConversationClickListener {
|
|
|
|
void onConversationClick(View view, Conversation conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
static class ConversationViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
private final ConversationListRowBinding binding;
|
|
|
|
|
|
|
|
private ConversationViewHolder(ConversationListRowBinding binding) {
|
|
|
|
super(binding.getRoot());
|
|
|
|
this.binding = binding;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-02 09:10:20 +00:00
|
|
|
}
|