package de.thedevstack.conversationsplus.ui.adapter; import android.content.Context; import android.graphics.Typeface; import android.util.Pair; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; import de.thedevstack.conversationsplus.ConversationsPlusColors; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; import de.thedevstack.conversationsplus.ui.listeners.ShowResourcesListDialogListener; import de.thedevstack.conversationsplus.utils.ImageUtil; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.tzur.conversations.Settings; import de.thedevstack.conversationsplus.R; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.entities.Transferable; import de.thedevstack.conversationsplus.services.AvatarService; import de.thedevstack.conversationsplus.ui.ConversationActivity; import de.thedevstack.conversationsplus.ui.XmppActivity; import de.thedevstack.conversationsplus.utils.UIHelper; public class ConversationAdapter extends ArrayAdapter { private XmppActivity activity; public ConversationAdapter(XmppActivity activity, List 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); // Highlight the currently selected conversation if (this.activity instanceof ConversationActivity) { ConversationActivity a = (ConversationActivity) this.activity; int c = conversation == a.getSelectedConversation() ? ConversationsPlusColors.secondaryBackground() : ConversationsPlusColors.primaryBackground(); view.findViewById(R.id.conversationListRowContent).setBackgroundColor(c); view.findViewById(R.id.conversationListRowFrame).setBackgroundColor(c); } TextView convName = (TextView) view.findViewById(R.id.conversation_name); if (conversation.getMode() == Conversation.MODE_SINGLE || ConversationsPlusPreferences.useSubject()) { convName.setText(conversation.getName()); } else { convName.setText(conversation.getJid().toBareJid().toString()); } TextView mLastMessage = (TextView) view.findViewById(R.id.conversation_lastmsg); TextView mTimestamp = (TextView) view.findViewById(R.id.conversation_lastupdate); ImageView imagePreview = (ImageView) view.findViewById(R.id.conversation_lastimage); ImageView notificationStatus = (ImageView) view.findViewById(R.id.notification_status); TextView txtUnreadCount = (TextView) view.findViewById(R.id.conversation_unreadcount); if (Settings.SHOW_ONLINE_STATUS) { int color = ConversationsPlusColors.offline(); if (conversation.getAccount().getStatus() == Account.State.ONLINE) { if (conversation.getMode() == Conversation.MODE_SINGLE) { color = UIHelper.getStatusColor(conversation.getContact().getMostAvailableStatus()); } else if (conversation.getMode() == Conversation.MODE_MULTI && conversation.getMucOptions().online()) { color = ConversationsPlusColors.online(); } } TextView status = (TextView) view.findViewById(R.id.status); status.setBackgroundColor(color); } Message message = conversation.getLatestMessage(); int txtUnreadCountVisibility = View.INVISIBLE; if (!conversation.isRead()) { convName.setTypeface(null, Typeface.BOLD); txtUnreadCountVisibility = View.VISIBLE; txtUnreadCount.setText(String.valueOf(conversation.unreadCount())); } else { convName.setTypeface(null, Typeface.NORMAL); } txtUnreadCount.setVisibility(txtUnreadCountVisibility); if ((null != message.getFileParams() && message.getFileParams().getWidth() > 0) // TODO: Use FileParams.getMimeType() && (message.getTransferable() == null || message.getTransferable().getStatus() != Transferable.STATUS_DELETED)) { mLastMessage.setVisibility(View.GONE); imagePreview.setVisibility(View.VISIBLE); ImageUtil.loadBitmap(message, imagePreview, mLastMessage, false); } else { Pair preview = UIHelper.getMessagePreview(activity,message); mLastMessage.setVisibility(View.VISIBLE); imagePreview.setVisibility(View.GONE); CharSequence msgText = preview.first; String msgPrefix = null; if (MessageUtil.isMessageSent(message)) { msgPrefix = activity.getString(R.string.cplus_me); } else if (conversation.getMode() == Conversation.MODE_MULTI) { msgPrefix = UIHelper.getMessageDisplayName(message); } String lastMessagePreview = ((null == msgPrefix || msgPrefix.isEmpty()) ? "" : (msgPrefix + ": ")) + msgText; mLastMessage.setText(lastMessagePreview); int lastMessageTypeface = Typeface.NORMAL; if (conversation.isRead()) { if (preview.second) { lastMessageTypeface = Typeface.ITALIC; } } else { if (preview.second) { lastMessageTypeface = Typeface.BOLD_ITALIC; } else { lastMessageTypeface = Typeface.BOLD; } } mLastMessage.setTypeface(null, lastMessageTypeface); } long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL,0); if (muted_till == Long.MAX_VALUE) { notificationStatus.setVisibility(View.VISIBLE); notificationStatus.setImageResource(R.drawable.ic_notifications_off_grey600_24dp); } else if (muted_till >= System.currentTimeMillis()) { notificationStatus.setVisibility(View.VISIBLE); notificationStatus.setImageResource(R.drawable.ic_notifications_paused_grey600_24dp); } else if (conversation.alwaysNotify()) { notificationStatus.setImageResource(R.drawable.ic_notifications_grey600_24dp); if (conversation.getMode() == Conversation.MODE_SINGLE) { notificationStatus.setVisibility(View.GONE); } else { notificationStatus.setVisibility(View.VISIBLE); } } else { notificationStatus.setVisibility(View.VISIBLE); notificationStatus.setImageResource(R.drawable.ic_notifications_none_grey600_24dp); } mTimestamp.setText(UIHelper.readableTimeDifference(activity, message.getTimeSent())); ImageView profilePicture = (ImageView) view.findViewById(R.id.conversation_image); if (conversation.getMode() == Conversation.MODE_SINGLE) { profilePicture.setOnLongClickListener(new ShowResourcesListDialogListener(activity, conversation.getContact())); } AvatarService.getInstance().loadAvatar(conversation, profilePicture); return view; } }